BigW Consortium Gitlab

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

3
describe API::AwardEmoji do
4
  let(:user)            { create(:user) }
5
  let!(:project)        { create(:project) }
6
  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
  before do
    project.team << [user, :master]
  end
15 16 17 18

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

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

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

29
        expect(response).to have_http_status(404)
30 31 32 33 34
      end
    end

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

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

44
    context 'on a snippet' do
45 46 47 48 49 50 51 52 53 54
      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
55 56
    end

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

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

63
        expect(response).to have_http_status(404)
64 65 66 67
      end
    end
  end

68 69 70 71
  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
72
      get api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user)
73

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

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

85
        expect(response).to have_http_status(200)
86 87 88 89 90 91
        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
92
        get api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/12345", user)
93

94
        expect(response).to have_http_status(404)
95 96 97 98 99
      end
    end

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

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

109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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

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

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

129
        expect(response).to have_http_status(404)
130 131 132 133
      end
    end
  end

134 135 136 137
  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
138
      get api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
139

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

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

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

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

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

161
        expect(response).to have_http_status(400)
162 163
      end

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

167
        expect(response).to have_http_status(401)
168
      end
169

170 171 172 173 174 175
      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

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

        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
184 185
          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'
186 187 188 189 190

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

    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
204 205
  end

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

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

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

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

      expect(response).to have_http_status(404)
    end

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

      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
232 233
        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'
234 235 236 237 238

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

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

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

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

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

      it_behaves_like '412 response' do
        let(:request) { api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/#{award_emoji.id}", user) }
      end
260 261 262 263 264
    end

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

267 268
          expect(response).to have_http_status(204)
        end.to change { merge_request.award_emoji.count }.from(1).to(0)
269 270 271
      end

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

274
        expect(response).to have_http_status(404)
275
      end
276 277 278 279

      it_behaves_like '412 response' do
        let(:request) { api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user) }
      end
280
    end
281 282 283 284 285 286 287 288 289

    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)

290 291
          expect(response).to have_http_status(204)
        end.to change { snippet.award_emoji.count }.from(1).to(0)
292
      end
293 294 295 296

      it_behaves_like '412 response' do
        let(:request) { api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji/#{award.id}", user) }
      end
297
    end
298
  end
299 300 301 302 303 304

  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
305
        delete api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
306

307 308
        expect(response).to have_http_status(204)
      end.to change { note.award_emoji.count }.from(1).to(0)
309
    end
310 311 312 313

    it_behaves_like '412 response' do
      let(:request) { api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user) }
    end
314
  end
315
end