BigW Consortium Gitlab

award_emoji_spec.rb 11.1 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::AwardEmoji do
4
  let(:user)            { create(:user) }
5 6
  let!(:project)        { create(:empty_project) }
  let(:issue)           { create(:issue, project: project) }
7 8 9
  let!(:award_emoji)    { create(:award_emoji, awardable: issue, user: user) }
  let!(:merge_request)  { create(:merge_request, source_project: project, target_project: project) }
  let!(:downvote)       { create(:award_emoji, :downvote, awardable: merge_request, user: user) }
10
  let!(:note)           { create(:note, project: project, noteable: issue) }
11 12 13 14 15 16

  before { project.team << [user, :master] }

  describe "GET /projects/:id/awardable/:awardable_id/award_emoji" do
    context 'on an issue' do
      it "returns an array of award_emoji" do
17
        get api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user)
18

19
        expect(response).to have_http_status(200)
20 21 22 23
        expect(json_response).to be_an Array
        expect(json_response.first['name']).to eq(award_emoji.name)
      end

24
      it "returns a 404 error when issue id not found" do
25 26
        get api("/projects/#{project.id}/issues/12345/award_emoji", user)

27
        expect(response).to have_http_status(404)
28 29 30 31 32
      end
    end

    context 'on a merge request' do
      it "returns an array of award_emoji" do
33
        get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji", user)
34

35
        expect(response).to have_http_status(200)
36
        expect(response).to include_pagination_headers
37 38 39 40 41
        expect(json_response).to be_an Array
        expect(json_response.first['name']).to eq(downvote.name)
      end
    end

42
    context 'on a snippet' do
43 44 45 46 47 48 49 50 51 52
      let(:snippet) { create(:project_snippet, :public, project: project) }
      let!(:award)  { create(:award_emoji, awardable: snippet) }

      it 'returns the awarded emoji' do
        get api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji", user)

        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array
        expect(json_response.first['name']).to eq(award.name)
      end
53 54
    end

55 56 57 58
    context 'when the user has no access' do
      it 'returns a status code 404' do
        user1 = create(:user)

59
        get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji", user1)
60

61
        expect(response).to have_http_status(404)
62 63 64 65
      end
    end
  end

66 67 68 69
  describe 'GET /projects/:id/awardable/:awardable_id/notes/:note_id/award_emoji' do
    let!(:rocket)  { create(:award_emoji, awardable: note, name: 'rocket') }

    it 'returns an array of award emoji' do
70
      get api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user)
71

72
      expect(response).to have_http_status(200)
73 74 75 76 77
      expect(json_response).to be_an Array
      expect(json_response.first['name']).to eq(rocket.name)
    end
  end

78 79 80
  describe "GET /projects/:id/awardable/:awardable_id/award_emoji/:award_id" do
    context 'on an issue' do
      it "returns the award emoji" do
81
        get api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/#{award_emoji.id}", user)
82

83
        expect(response).to have_http_status(200)
84 85 86 87 88 89
        expect(json_response['name']).to eq(award_emoji.name)
        expect(json_response['awardable_id']).to eq(issue.id)
        expect(json_response['awardable_type']).to eq("Issue")
      end

      it "returns a 404 error if the award is not found" do
90
        get api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/12345", user)
91

92
        expect(response).to have_http_status(404)
93 94 95 96 97
      end
    end

    context 'on a merge request' do
      it 'returns the award emoji' do
98
        get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user)
99

100
        expect(response).to have_http_status(200)
101
        expect(json_response['name']).to eq(downvote.name)
102
        expect(json_response['awardable_id']).to eq(merge_request.id)
103 104 105 106
        expect(json_response['awardable_type']).to eq("MergeRequest")
      end
    end

107 108 109 110 111 112 113 114 115 116 117 118 119 120
    context 'on a snippet' do
      let(:snippet) { create(:project_snippet, :public, project: project) }
      let!(:award)  { create(:award_emoji, awardable: snippet) }

      it 'returns the awarded emoji' do
        get api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji/#{award.id}", user)

        expect(response).to have_http_status(200)
        expect(json_response['name']).to eq(award.name)
        expect(json_response['awardable_id']).to eq(snippet.id)
        expect(json_response['awardable_type']).to eq("Snippet")
      end
    end

121 122 123 124
    context 'when the user has no access' do
      it 'returns a status code 404' do
        user1 = create(:user)

125
        get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user1)
126

127
        expect(response).to have_http_status(404)
128 129 130 131
      end
    end
  end

132 133 134 135
  describe 'GET /projects/:id/awardable/:awardable_id/notes/:note_id/award_emoji/:award_id' do
    let!(:rocket)  { create(:award_emoji, awardable: note, name: 'rocket') }

    it 'returns an award emoji' do
136
      get api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
137

138
      expect(response).to have_http_status(200)
139 140 141 142 143
      expect(json_response).not_to be_an Array
      expect(json_response['name']).to eq(rocket.name)
    end
  end

144
  describe "POST /projects/:id/awardable/:awardable_id/award_emoji" do
145 146
    let(:issue2)  { create(:issue, project: project, author: user) }

147 148
    context "on an issue" do
      it "creates a new award emoji" do
149
        post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: 'blowfish'
150

151
        expect(response).to have_http_status(201)
152 153 154 155
        expect(json_response['name']).to eq('blowfish')
        expect(json_response['user']['username']).to eq(user.username)
      end

156
      it "returns a 400 bad request error if the name is not given" do
157
        post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user)
158

159
        expect(response).to have_http_status(400)
160 161
      end

162
      it "returns a 401 unauthorized error if the user is not authenticated" do
163
        post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji"), name: 'thumbsup'
164

165
        expect(response).to have_http_status(401)
166
      end
167

168 169 170 171 172 173
      it "returns a 404 error if the user authored issue" do
        post api("/projects/#{project.id}/issues/#{issue2.id}/award_emoji", user), name: 'thumbsup'

        expect(response).to have_http_status(404)
      end

174
      it "normalizes +1 as thumbsup award" do
175
        post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: '+1'
176 177 178 179 180 181

        expect(issue.award_emoji.last.name).to eq("thumbsup")
      end

      context 'when the emoji already has been awarded' do
        it 'returns a 404 status code' do
182 183
          post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: 'thumbsup'
          post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: 'thumbsup'
184 185 186 187 188

          expect(response).to have_http_status(404)
          expect(json_response["message"]).to match("has already been taken")
        end
      end
189
    end
190 191 192 193 194 195 196 197 198 199 200 201

    context 'on a snippet' do
      it 'creates a new award emoji' do
        snippet = create(:project_snippet, :public, project: project)

        post api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji", user), name: 'blowfish'

        expect(response).to have_http_status(201)
        expect(json_response['name']).to eq('blowfish')
        expect(json_response['user']['username']).to eq(user.username)
      end
    end
202 203
  end

204
  describe "POST /projects/:id/awardable/:awardable_id/notes/:note_id/award_emoji" do
205 206
    let(:note2)  { create(:note, project: project, noteable: issue, author: user) }

207 208
    it 'creates a new award emoji' do
      expect do
209
        post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: 'rocket'
210 211
      end.to change { note.award_emoji.count }.from(0).to(1)

212
      expect(response).to have_http_status(201)
213 214
      expect(json_response['user']['username']).to eq(user.username)
    end
215

216
    it "it returns 404 error when user authored note" do
217
      post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note2.id}/award_emoji", user), name: 'thumbsup'
218 219 220 221

      expect(response).to have_http_status(404)
    end

222
    it "normalizes +1 as thumbsup award" do
223
      post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: '+1'
224 225 226 227 228 229

      expect(note.award_emoji.last.name).to eq("thumbsup")
    end

    context 'when the emoji already has been awarded' do
      it 'returns a 404 status code' do
230 231
        post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: 'rocket'
        post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: 'rocket'
232 233 234 235 236

        expect(response).to have_http_status(404)
        expect(json_response["message"]).to match("has already been taken")
      end
    end
237 238 239
  end

  describe 'DELETE /projects/:id/awardable/:awardable_id/award_emoji/:award_id' do
240 241 242
    context 'when the awardable is an Issue' do
      it 'deletes the award' do
        expect do
243
          delete api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/#{award_emoji.id}", user)
244

245 246
          expect(response).to have_http_status(204)
        end.to change { issue.award_emoji.count }.from(1).to(0)
247 248 249
      end

      it 'returns a 404 error when the award emoji can not be found' do
250
        delete api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/12345", user)
251

252
        expect(response).to have_http_status(404)
253 254 255 256 257 258
      end
    end

    context 'when the awardable is a Merge Request' do
      it 'deletes the award' do
        expect do
259
          delete api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user)
260

261 262
          expect(response).to have_http_status(204)
        end.to change { merge_request.award_emoji.count }.from(1).to(0)
263 264 265
      end

      it 'returns a 404 error when note id not found' do
266
        delete api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/notes/12345", user)
267

268
        expect(response).to have_http_status(404)
269 270
      end
    end
271 272 273 274 275 276 277 278 279

    context 'when the awardable is a Snippet' do
      let(:snippet) { create(:project_snippet, :public, project: project) }
      let!(:award)  { create(:award_emoji, awardable: snippet, user: user) }

      it 'deletes the award' do
        expect do
          delete api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji/#{award.id}", user)

280 281
          expect(response).to have_http_status(204)
        end.to change { snippet.award_emoji.count }.from(1).to(0)
282 283
      end
    end
284
  end
285 286 287 288 289 290

  describe 'DELETE /projects/:id/awardable/:awardable_id/award_emoji/:award_emoji_id' do
    let!(:rocket)  { create(:award_emoji, awardable: note, name: 'rocket', user: user) }

    it 'deletes the award' do
      expect do
291
        delete api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
292

293 294
        expect(response).to have_http_status(204)
      end.to change { note.award_emoji.count }.from(1).to(0)
295 296
    end
  end
297
end