award_emoji_spec.rb 8.5 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers
  let(:user)            { create(:user) }
  let!(:project)        { create(:project) }
  let(:issue)           { create(:issue, project: project, author: user) }
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 42 43 44 45 46 47
        expect(json_response).to be_an Array
        expect(json_response.first['name']).to eq(downvote.name)
      end
    end

    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)

48
        expect(response).to have_http_status(404)
49 50 51 52
      end
    end
  end

53 54 55 56 57 58
  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)

59
      expect(response).to have_http_status(200)
60 61 62 63 64
      expect(json_response).to be_an Array
      expect(json_response.first['name']).to eq(rocket.name)
    end
  end

65 66 67 68 69
  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)

70
        expect(response).to have_http_status(200)
71 72 73 74 75 76 77 78
        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)

79
        expect(response).to have_http_status(404)
80 81 82 83 84 85 86
      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)

87
        expect(response).to have_http_status(200)
88
        expect(json_response['name']).to eq(downvote.name)
89
        expect(json_response['awardable_id']).to eq(merge_request.id)
90 91 92 93 94 95 96 97 98 99
        expect(json_response['awardable_type']).to eq("MergeRequest")
      end
    end

    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)

100
        expect(response).to have_http_status(404)
101 102 103 104
      end
    end
  end

105 106 107 108 109 110
  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)

111
      expect(response).to have_http_status(200)
112 113 114 115 116
      expect(json_response).not_to be_an Array
      expect(json_response['name']).to eq(rocket.name)
    end
  end

117 118 119 120 121
  describe "POST /projects/:id/awardable/:awardable_id/award_emoji" do
    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'

122
        expect(response).to have_http_status(201)
123 124 125 126
        expect(json_response['name']).to eq('blowfish')
        expect(json_response['user']['username']).to eq(user.username)
      end

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

130
        expect(response).to have_http_status(400)
131 132
      end

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

136
        expect(response).to have_http_status(401)
137
      end
dixpac's avatar
dixpac committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

      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
154 155 156
    end
  end

157 158 159 160 161 162
  describe "POST /projects/:id/awardable/:awardable_id/notes/:note_id/award_emoji" do
    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)

163
      expect(response).to have_http_status(201)
164 165
      expect(json_response['user']['username']).to eq(user.username)
    end
dixpac's avatar
dixpac committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

    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
182 183 184
  end

  describe 'DELETE /projects/:id/awardable/:awardable_id/award_emoji/:award_id' do
185 186 187 188 189 190
    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)

191
        expect(response).to have_http_status(200)
192 193 194 195 196
      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)

197
        expect(response).to have_http_status(404)
198 199 200 201 202 203 204 205 206
      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)

207
        expect(response).to have_http_status(200)
208 209 210 211 212
      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)

213
        expect(response).to have_http_status(404)
214 215 216
      end
    end
  end
217 218 219 220 221 222 223 224 225

  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)

226
      expect(response).to have_http_status(200)
227 228
    end
  end
229
end