2014-07-30 09:05:43 -04:00
|
|
|
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)
|
2015-02-12 13:17:35 -05:00
|
|
|
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)
|
2014-07-30 09:05:43 -04:00
|
|
|
end
|
|
|
|
end
|
2014-08-12 08:16:25 -04:00
|
|
|
|
|
|
|
describe 'POST /projects/:id/labels' do
|
|
|
|
it 'should return created label' do
|
|
|
|
post api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'Foo',
|
|
|
|
color: '#FFAABB'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(json_response['name']).to eq('Foo')
|
|
|
|
expect(json_response['color']).to eq('#FFAABB')
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return a 400 bad request if name not given' do
|
|
|
|
post api("/projects/#{project.id}/labels", user), color: '#FFAABB'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return a 400 bad request if color not given' do
|
|
|
|
post api("/projects/#{project.id}/labels", user), name: 'Foobar'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
|
2014-08-17 18:25:49 -04:00
|
|
|
it 'should return 400 for invalid color' do
|
2014-08-12 08:16:25 -04:00
|
|
|
post api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'Foo',
|
|
|
|
color: '#FFAA'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']['color']).to eq(['is invalid'])
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
|
2014-08-17 16:22:01 -04:00
|
|
|
it 'should return 400 for too long color code' do
|
|
|
|
post api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'Foo',
|
|
|
|
color: '#FFAAFFFF'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']['color']).to eq(['is invalid'])
|
2014-08-17 16:22:01 -04:00
|
|
|
end
|
|
|
|
|
2014-08-17 18:25:49 -04:00
|
|
|
it 'should return 400 for invalid name' do
|
2014-08-12 08:16:25 -04:00
|
|
|
post api("/projects/#{project.id}/labels", user),
|
|
|
|
name: '?',
|
|
|
|
color: '#FFAABB'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']['title']).to eq(['is invalid'])
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 409 if label already exists' do
|
|
|
|
post api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'label1',
|
|
|
|
color: '#FFAABB'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(409)
|
|
|
|
expect(json_response['message']).to eq('Label already exists')
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /projects/:id/labels' do
|
|
|
|
it 'should return 200 for existing label' do
|
2014-08-13 06:23:51 -04:00
|
|
|
delete api("/projects/#{project.id}/labels", user), name: 'label1'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 404 for non existing label' do
|
2014-08-13 06:23:51 -04:00
|
|
|
delete api("/projects/#{project.id}/labels", user), name: 'label2'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(json_response['message']).to eq('404 Label Not Found')
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 400 for wrong parameters' do
|
|
|
|
delete api("/projects/#{project.id}/labels", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
2014-08-12 08:16:25 -04:00
|
|
|
end
|
|
|
|
end
|
2014-08-13 06:23:51 -04:00
|
|
|
|
|
|
|
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'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['name']).to eq('New Label')
|
|
|
|
expect(json_response['color']).to eq('#FFFFFF')
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 200 if name is changed' do
|
|
|
|
put api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'label1',
|
|
|
|
new_name: 'New Label'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['name']).to eq('New Label')
|
|
|
|
expect(json_response['color']).to eq(label1.color)
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 200 if colors is changed' do
|
|
|
|
put api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'label1',
|
|
|
|
color: '#FFFFFF'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['name']).to eq(label1.name)
|
|
|
|
expect(json_response['color']).to eq('#FFFFFF')
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 404 if label does not exist' do
|
|
|
|
put api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'label2',
|
|
|
|
new_name: 'label3'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 400 if no label name given' do
|
|
|
|
put api("/projects/#{project.id}/labels", user), new_name: 'label2'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']).to eq('400 (Bad request) "name" not given')
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 400 if no new parameters given' do
|
|
|
|
put api("/projects/#{project.id}/labels", user), name: 'label1'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']).to eq('Required parameters '\
|
|
|
|
'"new_name" or "color" missing')
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
|
|
|
|
2014-08-17 18:25:49 -04:00
|
|
|
it 'should return 400 for invalid name' do
|
2014-08-13 06:23:51 -04:00
|
|
|
put api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'label1',
|
|
|
|
new_name: '?',
|
|
|
|
color: '#FFFFFF'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']['title']).to eq(['is invalid'])
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
|
|
|
|
2014-08-17 18:25:49 -04:00
|
|
|
it 'should return 400 for invalid name' do
|
2014-08-13 06:23:51 -04:00
|
|
|
put api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'label1',
|
|
|
|
color: '#FF'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']['color']).to eq(['is invalid'])
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
2014-08-17 16:22:01 -04:00
|
|
|
|
|
|
|
it 'should return 400 for too long color code' do
|
|
|
|
post api("/projects/#{project.id}/labels", user),
|
|
|
|
name: 'Foo',
|
|
|
|
color: '#FFAAFFFF'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(json_response['message']['color']).to eq(['is invalid'])
|
2014-08-17 16:22:01 -04:00
|
|
|
end
|
2014-08-13 06:23:51 -04:00
|
|
|
end
|
2014-07-30 09:05:43 -04:00
|
|
|
end
|