BigW Consortium Gitlab

labels_spec.rb 9.42 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers

  let(:user) { create(:user) }
  let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
  let!(:label1) { create(:label, title: 'label1', project: project) }

  before do
    project.team << [user, :master]
  end

  describe 'GET /projects/:id/labels' do
    it 'should return project labels' do
      get api("/projects/#{project.id}/labels", user)
17
      expect(response).to have_http_status(200)
18 19 20
      expect(json_response).to be_an Array
      expect(json_response.size).to eq(1)
      expect(json_response.first['name']).to eq(label1.name)
Dmitriy Zaporozhets committed
21 22
    end
  end
23 24

  describe 'POST /projects/:id/labels' do
25 26 27 28 29
    it 'should return created label when all params' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAABB',
           description: 'test'
30
      expect(response).to have_http_status(201)
31 32 33 34 35 36
      expect(json_response['name']).to eq('Foo')
      expect(json_response['color']).to eq('#FFAABB')
      expect(json_response['description']).to eq('test')
    end

    it 'should return created label when only required params' do
37
      post api("/projects/#{project.id}/labels", user),
38
           name: 'Foo & Bar',
39
           color: '#FFAABB'
40 41
      expect(response.status).to eq(201)
      expect(json_response['name']).to eq('Foo & Bar')
42
      expect(json_response['color']).to eq('#FFAABB')
43
      expect(json_response['description']).to be_nil
44 45 46 47
    end

    it 'should return a 400 bad request if name not given' do
      post api("/projects/#{project.id}/labels", user), color: '#FFAABB'
48
      expect(response).to have_http_status(400)
49 50 51 52
    end

    it 'should return a 400 bad request if color not given' do
      post api("/projects/#{project.id}/labels", user), name: 'Foobar'
53
      expect(response).to have_http_status(400)
54 55
    end

56
    it 'should return 400 for invalid color' do
57 58 59
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAA'
60
      expect(response).to have_http_status(400)
61
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
62 63
    end

64 65 66 67
    it 'should return 400 for too long color code' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
68
      expect(response).to have_http_status(400)
69
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
70 71
    end

72
    it 'should return 400 for invalid name' do
73
      post api("/projects/#{project.id}/labels", user),
74
           name: ',',
75
           color: '#FFAABB'
76
      expect(response).to have_http_status(400)
77
      expect(json_response['message']['title']).to eq(['is invalid'])
78 79 80 81 82 83
    end

    it 'should return 409 if label already exists' do
      post api("/projects/#{project.id}/labels", user),
           name: 'label1',
           color: '#FFAABB'
84
      expect(response).to have_http_status(409)
85
      expect(json_response['message']).to eq('Label already exists')
86 87 88 89 90
    end
  end

  describe 'DELETE /projects/:id/labels' do
    it 'should return 200 for existing label' do
Robert Schilling committed
91
      delete api("/projects/#{project.id}/labels", user), name: 'label1'
92
      expect(response).to have_http_status(200)
93 94 95
    end

    it 'should return 404 for non existing label' do
Robert Schilling committed
96
      delete api("/projects/#{project.id}/labels", user), name: 'label2'
97
      expect(response).to have_http_status(404)
98
      expect(json_response['message']).to eq('404 Label Not Found')
99 100 101 102
    end

    it 'should return 400 for wrong parameters' do
      delete api("/projects/#{project.id}/labels", user)
103
      expect(response).to have_http_status(400)
104 105
    end
  end
Robert Schilling committed
106 107

  describe 'PUT /projects/:id/labels' do
108
    it 'should return 200 if name and colors and description are changed' do
Robert Schilling committed
109 110 111
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label',
112 113
          color: '#FFFFFF',
          description: 'test'
114
      expect(response).to have_http_status(200)
115 116
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq('#FFFFFF')
117
      expect(json_response['description']).to eq('test')
Robert Schilling committed
118 119 120 121 122 123
    end

    it 'should return 200 if name is changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label'
124
      expect(response).to have_http_status(200)
125 126
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq(label1.color)
Robert Schilling committed
127 128 129 130 131 132
    end

    it 'should return 200 if colors is changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FFFFFF'
133
      expect(response).to have_http_status(200)
134 135
      expect(json_response['name']).to eq(label1.name)
      expect(json_response['color']).to eq('#FFFFFF')
Robert Schilling committed
136 137
    end

138 139 140 141
    it 'should return 200 if description is changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          description: 'test'
142
      expect(response).to have_http_status(200)
143 144 145 146
      expect(json_response['name']).to eq(label1.name)
      expect(json_response['description']).to eq('test')
    end

Robert Schilling committed
147 148 149 150
    it 'should return 404 if label does not exist' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label2',
          new_name: 'label3'
151
      expect(response).to have_http_status(404)
Robert Schilling committed
152 153 154 155
    end

    it 'should return 400 if no label name given' do
      put api("/projects/#{project.id}/labels", user), new_name: 'label2'
156
      expect(response).to have_http_status(400)
157
      expect(json_response['message']).to eq('400 (Bad request) "name" not given')
Robert Schilling committed
158 159 160 161
    end

    it 'should return 400 if no new parameters given' do
      put api("/projects/#{project.id}/labels", user), name: 'label1'
162
      expect(response).to have_http_status(400)
163 164
      expect(json_response['message']).to eq('Required parameters '\
                                         '"new_name" or "color" missing')
Robert Schilling committed
165 166
    end

167
    it 'should return 400 for invalid name' do
Robert Schilling committed
168 169
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
170
          new_name: ',',
Robert Schilling committed
171
          color: '#FFFFFF'
172
      expect(response).to have_http_status(400)
173
      expect(json_response['message']['title']).to eq(['is invalid'])
Robert Schilling committed
174 175
    end

176
    it 'should return 400 when color code is too short' do
Robert Schilling committed
177 178 179
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FF'
180
      expect(response).to have_http_status(400)
181
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
Robert Schilling committed
182
    end
183 184 185 186 187

    it 'should return 400 for too long color code' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
188
      expect(response).to have_http_status(400)
189
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
190
    end
Robert Schilling committed
191
  end
192 193 194 195 196 197

  describe "POST /projects/:id/labels/:label_id/subscription" do
    context "when label_id is a label title" do
      it "should subscribe to the label" do
        post api("/projects/#{project.id}/labels/#{label1.title}/subscription", user)

198
        expect(response).to have_http_status(201)
199 200 201 202 203 204 205 206 207
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_truthy
      end
    end

    context "when label_id is a label ID" do
      it "should subscribe to the label" do
        post api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

208
        expect(response).to have_http_status(201)
209 210 211 212 213 214 215 216 217 218 219
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_truthy
      end
    end

    context "when user is already subscribed to label" do
      before { label1.subscribe(user) }

      it "should return 304" do
        post api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

220
        expect(response).to have_http_status(304)
221 222 223 224 225 226 227
      end
    end

    context "when label ID is not found" do
      it "should a return 404 error" do
        post api("/projects/#{project.id}/labels/1234/subscription", user)

228
        expect(response).to have_http_status(404)
229 230 231 232 233 234 235 236 237 238 239
      end
    end
  end

  describe "DELETE /projects/:id/labels/:label_id/subscription" do
    before { label1.subscribe(user) }

    context "when label_id is a label title" do
      it "should unsubscribe from the label" do
        delete api("/projects/#{project.id}/labels/#{label1.title}/subscription", user)

240
        expect(response).to have_http_status(200)
241 242 243 244 245 246 247 248 249
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_falsey
      end
    end

    context "when label_id is a label ID" do
      it "should unsubscribe from the label" do
        delete api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

250
        expect(response).to have_http_status(200)
251 252 253 254 255 256 257 258 259 260 261
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_falsey
      end
    end

    context "when user is already unsubscribed from label" do
      before { label1.unsubscribe(user) }

      it "should return 304" do
        delete api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)

262
        expect(response).to have_http_status(304)
263 264 265 266 267 268 269
      end
    end

    context "when label ID is not found" do
      it "should a return 404 error" do
        delete api("/projects/#{project.id}/labels/1234/subscription", user)

270
        expect(response).to have_http_status(404)
271 272 273
      end
    end
  end
Dmitriy Zaporozhets committed
274
end