debian-mirror-gitlab/spec/requests/api/labels_spec.rb

280 lines
9.4 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
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
2016-11-03 12:29:30 +05:30
it 'returns all available labels to the project' do
group = create(:group)
group_label = create(:group_label, group: group)
project.update(group: group)
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/labels", user)
2016-11-03 12:29:30 +05:30
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2015-04-26 12:48:37 +05:30
expect(json_response).to be_an Array
2016-11-03 12:29:30 +05:30
expect(json_response.size).to eq(2)
expect(json_response.map { |l| l['name'] }).to match_array([group_label.name, label1.name])
2014-09-02 18:07:02 +05:30
end
end
describe 'POST /projects/:id/labels' do
2016-09-13 17:45:13 +05:30
it 'returns created label when all params' do
2016-06-02 11:05:42 +05:30
post api("/projects/#{project.id}/labels", user),
name: 'Foo',
color: '#FFAABB',
description: 'test'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
2016-06-02 11:05:42 +05:30
expect(json_response['name']).to eq('Foo')
expect(json_response['color']).to eq('#FFAABB')
expect(json_response['description']).to eq('test')
end
2016-09-13 17:45:13 +05:30
it 'returns created label when only required params' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user),
2016-08-24 12:49:21 +05:30
name: 'Foo & Bar',
2014-09-02 18:07:02 +05:30
color: '#FFAABB'
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(201)
2016-08-24 12:49:21 +05:30
expect(json_response['name']).to eq('Foo & Bar')
2015-04-26 12:48:37 +05:30
expect(json_response['color']).to eq('#FFAABB')
2016-06-02 11:05:42 +05:30
expect(json_response['description']).to be_nil
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns a 400 bad request if name not given' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user), color: '#FFAABB'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns a 400 bad request if color not given' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user), name: 'Foobar'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 for invalid color' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user),
name: 'Foo',
color: '#FFAA'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-12-23 02:04:40 +05:30
expect(json_response['message']['color']).to eq(['must be a valid color code'])
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 for too long color code' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user),
name: 'Foo',
color: '#FFAAFFFF'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-12-23 02:04:40 +05:30
expect(json_response['message']['color']).to eq(['must be a valid color code'])
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 for invalid name' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user),
2016-08-24 12:49:21 +05:30
name: ',',
2014-09-02 18:07:02 +05:30
color: '#FFAABB'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-04-26 12:48:37 +05:30
expect(json_response['message']['title']).to eq(['is invalid'])
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 409 if label already exists' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user),
name: 'label1',
color: '#FFAABB'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(409)
2015-04-26 12:48:37 +05:30
expect(json_response['message']).to eq('Label already exists')
2014-09-02 18:07:02 +05:30
end
end
describe 'DELETE /projects/:id/labels' do
2016-09-13 17:45:13 +05:30
it 'returns 200 for existing label' do
2014-09-02 18:07:02 +05:30
delete api("/projects/#{project.id}/labels", user), name: 'label1'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 404 for non existing label' do
2014-09-02 18:07:02 +05:30
delete api("/projects/#{project.id}/labels", user), name: 'label2'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2015-04-26 12:48:37 +05:30
expect(json_response['message']).to eq('404 Label Not Found')
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 for wrong parameters' do
2014-09-02 18:07:02 +05:30
delete api("/projects/#{project.id}/labels", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2014-09-02 18:07:02 +05:30
end
end
describe 'PUT /projects/:id/labels' do
2016-09-13 17:45:13 +05:30
it 'returns 200 if name and colors and description are changed' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user),
name: 'label1',
new_name: 'New Label',
2016-06-02 11:05:42 +05:30
color: '#FFFFFF',
description: 'test'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2015-04-26 12:48:37 +05:30
expect(json_response['name']).to eq('New Label')
expect(json_response['color']).to eq('#FFFFFF')
2016-06-02 11:05:42 +05:30
expect(json_response['description']).to eq('test')
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 200 if name is changed' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user),
name: 'label1',
new_name: 'New Label'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2015-04-26 12:48:37 +05:30
expect(json_response['name']).to eq('New Label')
expect(json_response['color']).to eq(label1.color)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 200 if colors is changed' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user),
name: 'label1',
color: '#FFFFFF'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2015-04-26 12:48:37 +05:30
expect(json_response['name']).to eq(label1.name)
expect(json_response['color']).to eq('#FFFFFF')
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 200 if description is changed' do
2016-06-02 11:05:42 +05:30
put api("/projects/#{project.id}/labels", user),
name: 'label1',
description: 'test'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2016-06-02 11:05:42 +05:30
expect(json_response['name']).to eq(label1.name)
expect(json_response['description']).to eq('test')
end
2016-09-13 17:45:13 +05:30
it 'returns 404 if label does not exist' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user),
name: 'label2',
new_name: 'label3'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 if no label name given' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user), new_name: 'label2'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-04-26 12:48:37 +05:30
expect(json_response['message']).to eq('400 (Bad request) "name" not given')
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 if no new parameters given' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user), name: 'label1'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-04-26 12:48:37 +05:30
expect(json_response['message']).to eq('Required parameters '\
'"new_name" or "color" missing')
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 for invalid name' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user),
name: 'label1',
2016-08-24 12:49:21 +05:30
new_name: ',',
2014-09-02 18:07:02 +05:30
color: '#FFFFFF'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-04-26 12:48:37 +05:30
expect(json_response['message']['title']).to eq(['is invalid'])
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 when color code is too short' do
2014-09-02 18:07:02 +05:30
put api("/projects/#{project.id}/labels", user),
name: 'label1',
color: '#FF'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-12-23 02:04:40 +05:30
expect(json_response['message']['color']).to eq(['must be a valid color code'])
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 400 for too long color code' do
2014-09-02 18:07:02 +05:30
post api("/projects/#{project.id}/labels", user),
name: 'Foo',
color: '#FFAAFFFF'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-12-23 02:04:40 +05:30
expect(json_response['message']['color']).to eq(['must be a valid color code'])
2014-09-02 18:07:02 +05:30
end
end
2016-06-02 11:05:42 +05:30
describe "POST /projects/:id/labels/:label_id/subscription" do
context "when label_id is a label title" do
2016-09-13 17:45:13 +05:30
it "subscribes to the label" do
2016-06-02 11:05:42 +05:30
post api("/projects/#{project.id}/labels/#{label1.title}/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
2016-06-02 11:05:42 +05:30
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
2016-09-13 17:45:13 +05:30
it "subscribes to the label" do
2016-06-02 11:05:42 +05:30
post api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
2016-06-02 11:05:42 +05:30
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) }
2016-09-13 17:45:13 +05:30
it "returns 304" do
2016-06-02 11:05:42 +05:30
post api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(304)
2016-06-02 11:05:42 +05:30
end
end
context "when label ID is not found" do
2016-09-13 17:45:13 +05:30
it "returns 404 error" do
2016-06-02 11:05:42 +05:30
post api("/projects/#{project.id}/labels/1234/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2016-06-02 11:05:42 +05:30
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
2016-09-13 17:45:13 +05:30
it "unsubscribes from the label" do
2016-06-02 11:05:42 +05:30
delete api("/projects/#{project.id}/labels/#{label1.title}/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2016-06-02 11:05:42 +05:30
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
2016-09-13 17:45:13 +05:30
it "unsubscribes from the label" do
2016-06-02 11:05:42 +05:30
delete api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2016-06-02 11:05:42 +05:30
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) }
2016-09-13 17:45:13 +05:30
it "returns 304" do
2016-06-02 11:05:42 +05:30
delete api("/projects/#{project.id}/labels/#{label1.id}/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(304)
2016-06-02 11:05:42 +05:30
end
end
context "when label ID is not found" do
2016-09-13 17:45:13 +05:30
it "returns 404 error" do
2016-06-02 11:05:42 +05:30
delete api("/projects/#{project.id}/labels/1234/subscription", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2016-06-02 11:05:42 +05:30
end
end
end
2014-09-02 18:07:02 +05:30
end