BigW Consortium Gitlab

labels_spec.rb 5.76 KB
Newer Older
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 19 20 21
      expect(response.status).to eq(200)
      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
22 23
    end
  end
24 25 26 27 28 29

  describe 'POST /projects/:id/labels' do
    it 'should return created label' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAABB'
30 31 32
      expect(response.status).to eq(201)
      expect(json_response['name']).to eq('Foo')
      expect(json_response['color']).to eq('#FFAABB')
33 34 35 36
    end

    it 'should return a 400 bad request if name not given' do
      post api("/projects/#{project.id}/labels", user), color: '#FFAABB'
37
      expect(response.status).to eq(400)
38 39 40 41
    end

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

45
    it 'should return 400 for invalid color' do
46 47 48
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAA'
49
      expect(response.status).to eq(400)
50
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
51 52
    end

53 54 55 56
    it 'should return 400 for too long color code' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
57
      expect(response.status).to eq(400)
58
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
59 60
    end

61
    it 'should return 400 for invalid name' do
62 63 64
      post api("/projects/#{project.id}/labels", user),
           name: '?',
           color: '#FFAABB'
65 66
      expect(response.status).to eq(400)
      expect(json_response['message']['title']).to eq(['is invalid'])
67 68 69 70 71 72
    end

    it 'should return 409 if label already exists' do
      post api("/projects/#{project.id}/labels", user),
           name: 'label1',
           color: '#FFAABB'
73 74
      expect(response.status).to eq(409)
      expect(json_response['message']).to eq('Label already exists')
75 76 77 78 79
    end
  end

  describe 'DELETE /projects/:id/labels' do
    it 'should return 200 for existing label' do
Robert Schilling committed
80
      delete api("/projects/#{project.id}/labels", user), name: 'label1'
81
      expect(response.status).to eq(200)
82 83 84
    end

    it 'should return 404 for non existing label' do
Robert Schilling committed
85
      delete api("/projects/#{project.id}/labels", user), name: 'label2'
86 87
      expect(response.status).to eq(404)
      expect(json_response['message']).to eq('404 Label Not Found')
88 89 90 91
    end

    it 'should return 400 for wrong parameters' do
      delete api("/projects/#{project.id}/labels", user)
92
      expect(response.status).to eq(400)
93 94
    end
  end
Robert Schilling committed
95 96 97 98 99 100 101

  describe 'PUT /projects/:id/labels' do
    it 'should return 200 if name and colors are changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label',
          color: '#FFFFFF'
102 103 104
      expect(response.status).to eq(200)
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq('#FFFFFF')
Robert Schilling committed
105 106 107 108 109 110
    end

    it 'should return 200 if name is changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label'
111 112 113
      expect(response.status).to eq(200)
      expect(json_response['name']).to eq('New Label')
      expect(json_response['color']).to eq(label1.color)
Robert Schilling committed
114 115 116 117 118 119
    end

    it 'should return 200 if colors is changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FFFFFF'
120 121 122
      expect(response.status).to eq(200)
      expect(json_response['name']).to eq(label1.name)
      expect(json_response['color']).to eq('#FFFFFF')
Robert Schilling committed
123 124 125 126 127 128
    end

    it 'should return 404 if label does not exist' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label2',
          new_name: 'label3'
129
      expect(response.status).to eq(404)
Robert Schilling committed
130 131 132 133
    end

    it 'should return 400 if no label name given' do
      put api("/projects/#{project.id}/labels", user), new_name: 'label2'
134 135
      expect(response.status).to eq(400)
      expect(json_response['message']).to eq('400 (Bad request) "name" not given')
Robert Schilling committed
136 137 138 139
    end

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

145
    it 'should return 400 for invalid name' do
Robert Schilling committed
146 147 148 149
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: '?',
          color: '#FFFFFF'
150 151
      expect(response.status).to eq(400)
      expect(json_response['message']['title']).to eq(['is invalid'])
Robert Schilling committed
152 153
    end

154
    it 'should return 400 when color code is too short' do
Robert Schilling committed
155 156 157
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FF'
158
      expect(response.status).to eq(400)
159
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
Robert Schilling committed
160
    end
161 162 163 164 165

    it 'should return 400 for too long color code' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
166
      expect(response.status).to eq(400)
167
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
168
    end
Robert Schilling committed
169
  end
Dmitriy Zaporozhets committed
170
end