BigW Consortium Gitlab

award_emoji_spec.rb 11 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers
  let(:user)            { create(:user) }
6 7
  let!(:project)        { create(:empty_project) }
  let(:issue)           { create(:issue, project: project) }
8 9 10
  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) }
11
  let!(:note)           { create(:note, project: project, noteable: issue) }
12 13 14 15 16 17 18 19

  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
        get api("/projects/#{project.id}/issues/#{issue.id}/award_emoji", user)

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

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

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

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

36
        expect(response).to have_http_status(200)
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 59 60
    context 'when the user has no access' do
      it 'returns a status code 404' do
        user1 = create(:user)

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

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

66 67 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
      get api("/projects/#{project.id}/issues/#{issue.id}/notes/#{note.id}/award_emoji", user)

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 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
        get api("/projects/#{project.id}/issues/#{issue.id}/award_emoji/#{award_emoji.id}", user)

83
        expect(response).to have_http_status(200)
84 85 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
        get api("/projects/#{project.id}/issues/#{issue.id}/award_emoji/12345", user)

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

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

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 125 126
    context 'when the user has no access' do
      it 'returns a status code 404' do
        user1 = create(:user)

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

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

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

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 149 150
    context "on an issue" do
      it "creates a new award emoji" do
        post api("/projects/#{project.id}/issues/#{issue.id}/award_emoji", user), name: 'blowfish'

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 158
        post api("/projects/#{project.id}/issues/#{issue.id}/award_emoji", user)

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 164
        post api("/projects/#{project.id}/issues/#{issue.id}/award_emoji"), name: 'thumbsup'

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 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      it "normalizes +1 as thumbsup award" do
        post api("/projects/#{project.id}/issues/#{issue.id}/award_emoji", user), name: '+1'

        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
          post api("/projects/#{project.id}/issues/#{issue.id}/award_emoji", user), name: 'thumbsup'
          post api("/projects/#{project.id}/issues/#{issue.id}/award_emoji", user), name: 'thumbsup'

          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 209 210 211
    it 'creates a new award emoji' do
      expect do
        post api("/projects/#{project.id}/issues/#{issue.id}/notes/#{note.id}/award_emoji", user), name: 'rocket'
      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 217 218 219 220 221
    it "it returns 404 error when user authored note" do
      post api("/projects/#{project.id}/issues/#{issue.id}/notes/#{note2.id}/award_emoji", user), name: 'thumbsup'

      expect(response).to have_http_status(404)
    end

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

      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
        post api("/projects/#{project.id}/issues/#{issue.id}/notes/#{note.id}/award_emoji", user), name: 'rocket'
        post api("/projects/#{project.id}/issues/#{issue.id}/notes/#{note.id}/award_emoji", user), name: 'rocket'

        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 243 244 245
    context 'when the awardable is an Issue' do
      it 'deletes the award' do
        expect do
          delete api("/projects/#{project.id}/issues/#{issue.id}/award_emoji/#{award_emoji.id}", user)
        end.to change { issue.award_emoji.count }.from(1).to(0)

246
        expect(response).to have_http_status(200)
247 248 249 250 251
      end

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

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

    context 'when the awardable is a Merge Request' do
      it 'deletes the award' do
        expect do
          delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}/award_emoji/#{downvote.id}", user)
        end.to change { merge_request.award_emoji.count }.from(1).to(0)

262
        expect(response).to have_http_status(200)
263 264 265 266 267
      end

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

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

    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)
        end.to change { snippet.award_emoji.count }.from(1).to(0)

        expect(response).to have_http_status(200)
      end
    end
284
  end
285 286 287 288 289 290 291 292 293

  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
        delete api("/projects/#{project.id}/issues/#{issue.id}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
      end.to change { note.award_emoji.count }.from(1).to(0)

294
      expect(response).to have_http_status(200)
295 296
    end
  end
297
end