labels_spec.rb 9.41 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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)
18
      expect(response).to have_http_status(200)
19 20 21
      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's avatar
Dmitriy Zaporozhets committed
22 23
    end
  end
24 25

  describe 'POST /projects/:id/labels' do
26 27 28 29 30
    it 'should return created label when all params' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAABB',
           description: 'test'
31
      expect(response).to have_http_status(201)
32 33 34 35 36 37
      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
38 39 40
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAABB'
41
      expect(response).to have_http_status(201)
42 43
      expect(json_response['name']).to eq('Foo')
      expect(json_response['color']).to eq('#FFAABB')
44
      expect(json_response['description']).to be_nil
45 46 47 48
    end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  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)

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

209
        expect(response).to have_http_status(201)
210 211 212 213 214 215 216 217 218 219 220
        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)

221
        expect(response).to have_http_status(304)
222 223 224 225 226 227 228
      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)

229
        expect(response).to have_http_status(404)
230 231 232 233 234 235 236 237 238 239 240
      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)

241
        expect(response).to have_http_status(200)
242 243 244 245 246 247 248 249 250
        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)

251
        expect(response).to have_http_status(200)
252 253 254 255 256 257 258 259 260 261 262
        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)

263
        expect(response).to have_http_status(304)
264 265 266 267 268 269 270
      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)

271
        expect(response).to have_http_status(404)
272 273 274
      end
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
275
end