2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-05 12:51:40 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::Badges do
|
2018-07-11 10:36:08 -04:00
|
|
|
let(:maintainer) { create(:user, username: 'maintainer_user') }
|
2018-03-05 12:51:40 -05:00
|
|
|
let(:developer) { create(:user) }
|
|
|
|
let(:access_requester) { create(:user) }
|
|
|
|
let(:stranger) { create(:user) }
|
|
|
|
let(:project_group) { create(:group) }
|
|
|
|
let(:project) { setup_project }
|
|
|
|
let!(:group) { setup_group }
|
|
|
|
|
|
|
|
shared_context 'source helpers' do
|
|
|
|
def get_source(source_type)
|
|
|
|
source_type == 'project' ? project : group
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'GET /:sources/:id/badges' do |source_type|
|
|
|
|
include_context 'source helpers'
|
|
|
|
|
|
|
|
let(:source) { get_source(source_type) }
|
|
|
|
|
|
|
|
context "with :sources == #{source_type.pluralize}" do
|
|
|
|
it_behaves_like 'a 404 response when source is private' do
|
|
|
|
let(:route) { get api("/#{source_type.pluralize}/#{source.id}/badges", stranger) }
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
%i[maintainer developer access_requester stranger].each do |type|
|
2018-03-05 12:51:40 -05:00
|
|
|
context "when authenticated as a #{type}" do
|
|
|
|
it 'returns 200' do
|
|
|
|
user = public_send(type)
|
|
|
|
badges_count = source_type == 'project' ? 3 : 2
|
|
|
|
|
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges", user)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-03-05 12:51:40 -05:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.size).to eq(badges_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N+1 queries' do
|
|
|
|
# Establish baseline
|
2018-07-11 10:36:08 -04:00
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
|
|
|
control = ActiveRecord::QueryRecorder.new do
|
2018-07-11 10:36:08 -04:00
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
project.add_developer(create(:user))
|
|
|
|
|
|
|
|
expect do
|
2018-07-11 10:36:08 -04:00
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
end.not_to exceed_query_limit(control)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'GET /:sources/:id/badges/:badge_id' do |source_type|
|
|
|
|
include_context 'source helpers'
|
|
|
|
|
|
|
|
let(:source) { get_source(source_type) }
|
|
|
|
|
|
|
|
context "with :sources == #{source_type.pluralize}" do
|
|
|
|
it_behaves_like 'a 404 response when source is private' do
|
|
|
|
let(:route) { get api("/#{source_type.pluralize}/#{source.id}/badges/#{developer.id}", stranger) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a non-member' do
|
2018-07-11 10:36:08 -04:00
|
|
|
%i[maintainer developer access_requester stranger].each do |type|
|
2018-03-05 12:51:40 -05:00
|
|
|
let(:badge) { source.badges.first }
|
|
|
|
|
|
|
|
context "as a #{type}" do
|
2019-04-18 07:35:01 -04:00
|
|
|
it 'returns 200', :quarantine do
|
2018-03-05 12:51:40 -05:00
|
|
|
user = public_send(type)
|
|
|
|
|
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", user)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-11-22 04:06:20 -05:00
|
|
|
expect(json_response['name']).to eq(badge.name)
|
2018-03-05 12:51:40 -05:00
|
|
|
expect(json_response['id']).to eq(badge.id)
|
|
|
|
expect(json_response['link_url']).to eq(badge.link_url)
|
|
|
|
expect(json_response['rendered_link_url']).to eq(badge.rendered_link_url)
|
|
|
|
expect(json_response['image_url']).to eq(badge.image_url)
|
|
|
|
expect(json_response['rendered_image_url']).to eq(badge.rendered_image_url)
|
|
|
|
expect(json_response['kind']).to eq source_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'POST /:sources/:id/badges' do |source_type|
|
|
|
|
include_context 'source helpers'
|
|
|
|
|
|
|
|
let(:source) { get_source(source_type) }
|
2019-11-22 04:06:20 -05:00
|
|
|
let(:example_name) { 'BadgeName' }
|
2018-03-05 12:51:40 -05:00
|
|
|
let(:example_url) { 'http://www.example.com' }
|
|
|
|
let(:example_url2) { 'http://www.example1.com' }
|
|
|
|
|
|
|
|
context "with :sources == #{source_type.pluralize}" do
|
|
|
|
it_behaves_like 'a 404 response when source is private' do
|
|
|
|
let(:route) do
|
|
|
|
post api("/#{source_type.pluralize}/#{source.id}/badges", stranger),
|
2019-11-22 04:06:20 -05:00
|
|
|
params: { name: example_name, link_url: example_url, image_url: example_url2 }
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a non-member or member with insufficient rights' do
|
|
|
|
%i[access_requester stranger developer].each do |type|
|
|
|
|
context "as a #{type}" do
|
|
|
|
it 'returns 403' do
|
|
|
|
user = public_send(type)
|
|
|
|
|
|
|
|
post api("/#{source_type.pluralize}/#{source.id}/badges", user),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { link_url: example_url, image_url: example_url2 }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when authenticated as a maintainer/owner' do
|
2018-03-05 12:51:40 -05:00
|
|
|
it 'creates a new badge' do
|
|
|
|
expect do
|
2018-07-11 10:36:08 -04:00
|
|
|
post api("/#{source_type.pluralize}/#{source.id}/badges", maintainer),
|
2019-11-22 04:06:20 -05:00
|
|
|
params: { name: example_name, link_url: example_url, image_url: example_url2 }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2018-03-05 12:51:40 -05:00
|
|
|
end.to change { source.badges.count }.by(1)
|
|
|
|
|
2019-11-22 04:06:20 -05:00
|
|
|
expect(json_response['name']).to eq(example_name)
|
2018-03-05 12:51:40 -05:00
|
|
|
expect(json_response['link_url']).to eq(example_url)
|
|
|
|
expect(json_response['image_url']).to eq(example_url2)
|
|
|
|
expect(json_response['kind']).to eq source_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 when link_url is not given' do
|
2018-07-11 10:36:08 -04:00
|
|
|
post api("/#{source_type.pluralize}/#{source.id}/badges", maintainer),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { link_url: example_url }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 when image_url is not given' do
|
2018-07-11 10:36:08 -04:00
|
|
|
post api("/#{source_type.pluralize}/#{source.id}/badges", maintainer),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { image_url: example_url2 }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 when link_url or image_url is not valid' do
|
2018-07-11 10:36:08 -04:00
|
|
|
post api("/#{source_type.pluralize}/#{source.id}/badges", maintainer),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { link_url: 'whatever', image_url: 'whatever' }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'PUT /:sources/:id/badges/:badge_id' do |source_type|
|
|
|
|
include_context 'source helpers'
|
|
|
|
|
|
|
|
let(:source) { get_source(source_type) }
|
|
|
|
|
|
|
|
context "with :sources == #{source_type.pluralize}" do
|
|
|
|
let(:badge) { source.badges.first }
|
2019-11-22 04:06:20 -05:00
|
|
|
let(:example_name) { 'BadgeName' }
|
2018-03-05 12:51:40 -05:00
|
|
|
let(:example_url) { 'http://www.example.com' }
|
|
|
|
let(:example_url2) { 'http://www.example1.com' }
|
|
|
|
|
|
|
|
it_behaves_like 'a 404 response when source is private' do
|
|
|
|
let(:route) do
|
|
|
|
put api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", stranger),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { link_url: example_url }
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a non-member or member with insufficient rights' do
|
|
|
|
%i[access_requester stranger developer].each do |type|
|
|
|
|
context "as a #{type}" do
|
|
|
|
it 'returns 403' do
|
|
|
|
user = public_send(type)
|
|
|
|
|
|
|
|
put api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", user),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { link_url: example_url }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when authenticated as a maintainer/owner' do
|
2019-04-18 07:35:01 -04:00
|
|
|
it 'updates the member', :quarantine do
|
2018-07-11 10:36:08 -04:00
|
|
|
put api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", maintainer),
|
2019-11-22 04:06:20 -05:00
|
|
|
params: { name: example_name, link_url: example_url, image_url: example_url2 }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-11-22 04:06:20 -05:00
|
|
|
expect(json_response['name']).to eq(example_name)
|
2018-03-05 12:51:40 -05:00
|
|
|
expect(json_response['link_url']).to eq(example_url)
|
|
|
|
expect(json_response['image_url']).to eq(example_url2)
|
|
|
|
expect(json_response['kind']).to eq source_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 when link_url or image_url is not valid' do
|
2018-07-11 10:36:08 -04:00
|
|
|
put api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", maintainer),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { link_url: 'whatever', image_url: 'whatever' }
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'DELETE /:sources/:id/badges/:badge_id' do |source_type|
|
|
|
|
include_context 'source helpers'
|
|
|
|
|
|
|
|
let(:source) { get_source(source_type) }
|
|
|
|
|
|
|
|
context "with :sources == #{source_type.pluralize}" do
|
|
|
|
let(:badge) { source.badges.first }
|
|
|
|
|
|
|
|
it_behaves_like 'a 404 response when source is private' do
|
|
|
|
let(:route) { delete api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", stranger) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a non-member or member with insufficient rights' do
|
|
|
|
%i[access_requester developer stranger].each do |type|
|
|
|
|
context "as a #{type}" do
|
|
|
|
it 'returns 403' do
|
|
|
|
user = public_send(type)
|
|
|
|
|
|
|
|
delete api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", user)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-18 07:35:01 -04:00
|
|
|
context 'when authenticated as a maintainer/owner', :quarantine do
|
2018-03-05 12:51:40 -05:00
|
|
|
it 'deletes the badge' do
|
|
|
|
expect do
|
2018-07-11 10:36:08 -04:00
|
|
|
delete api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2018-03-05 12:51:40 -05:00
|
|
|
end.to change { source.badges.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
2018-07-11 10:36:08 -04:00
|
|
|
let(:request) { api("/#{source_type.pluralize}/#{source.id}/badges/#{badge.id}", maintainer) }
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 if badge does not exist' do
|
2018-07-11 10:36:08 -04:00
|
|
|
delete api("/#{source_type.pluralize}/#{source.id}/badges/123", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'GET /:sources/:id/badges/render' do |source_type|
|
|
|
|
include_context 'source helpers'
|
|
|
|
|
|
|
|
let(:source) { get_source(source_type) }
|
|
|
|
let(:example_url) { 'http://www.example.com' }
|
|
|
|
let(:example_url2) { 'http://www.example1.com' }
|
|
|
|
|
|
|
|
context "with :sources == #{source_type.pluralize}" do
|
|
|
|
it_behaves_like 'a 404 response when source is private' do
|
|
|
|
let(:route) do
|
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges/render?link_url=#{example_url}&image_url=#{example_url2}", stranger)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as a non-member or member with insufficient rights' do
|
|
|
|
%i[access_requester stranger developer].each do |type|
|
|
|
|
context "as a #{type}" do
|
|
|
|
it 'returns 403' do
|
|
|
|
user = public_send(type)
|
|
|
|
|
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges/render?link_url=#{example_url}&image_url=#{example_url2}", user)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when authenticated as a maintainer/owner' do
|
2018-03-05 12:51:40 -05:00
|
|
|
it 'gets the rendered badge values' do
|
2018-07-11 10:36:08 -04:00
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges/render?link_url=#{example_url}&image_url=#{example_url2}", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2019-11-22 04:06:20 -05:00
|
|
|
expect(json_response.keys).to contain_exactly('name', 'link_url', 'rendered_link_url', 'image_url', 'rendered_image_url')
|
2018-03-05 12:51:40 -05:00
|
|
|
expect(json_response['link_url']).to eq(example_url)
|
|
|
|
expect(json_response['image_url']).to eq(example_url2)
|
|
|
|
expect(json_response['rendered_link_url']).to eq(example_url)
|
|
|
|
expect(json_response['rendered_image_url']).to eq(example_url2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 when link_url is not given' do
|
2018-07-11 10:36:08 -04:00
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges/render?link_url=#{example_url}", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 when image_url is not given' do
|
2018-07-11 10:36:08 -04:00
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges/render?image_url=#{example_url}", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 when link_url or image_url is not valid' do
|
2018-07-11 10:36:08 -04:00
|
|
|
get api("/#{source_type.pluralize}/#{source.id}/badges/render?link_url=whatever&image_url=whatever", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deleting a badge' do
|
|
|
|
context 'and the source is a project' do
|
|
|
|
it 'cannot delete badges owned by the project group' do
|
2018-07-11 10:36:08 -04:00
|
|
|
delete api("/projects/#{project.id}/badges/#{project_group.badges.first.id}", maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Endpoints' do
|
|
|
|
%w(project group).each do |source_type|
|
|
|
|
it_behaves_like 'GET /:sources/:id/badges', source_type
|
|
|
|
it_behaves_like 'GET /:sources/:id/badges/:badge_id', source_type
|
|
|
|
it_behaves_like 'GET /:sources/:id/badges/render', source_type
|
|
|
|
it_behaves_like 'POST /:sources/:id/badges', source_type
|
|
|
|
it_behaves_like 'PUT /:sources/:id/badges/:badge_id', source_type
|
|
|
|
it_behaves_like 'DELETE /:sources/:id/badges/:badge_id', source_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_project
|
2019-10-16 08:06:32 -04:00
|
|
|
create(:project, :public, creator_id: maintainer.id, namespace: project_group) do |project|
|
2018-03-05 12:51:40 -05:00
|
|
|
project.add_developer(developer)
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
project.request_access(access_requester)
|
2019-11-22 04:06:20 -05:00
|
|
|
project.project_badges << build(:project_badge, project: project, name: 'ExampleBadge1')
|
|
|
|
project.project_badges << build(:project_badge, project: project, name: 'ExampleBadge2')
|
|
|
|
project_group.badges << build(:group_badge, group: group, name: 'ExampleBadge3')
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_group
|
2019-10-16 08:06:32 -04:00
|
|
|
create(:group, :public) do |group|
|
2018-03-05 12:51:40 -05:00
|
|
|
group.add_developer(developer)
|
2018-07-11 10:36:08 -04:00
|
|
|
group.add_owner(maintainer)
|
2018-03-05 12:51:40 -05:00
|
|
|
group.request_access(access_requester)
|
2019-11-22 04:06:20 -05:00
|
|
|
group.badges << build(:group_badge, group: group, name: 'ExampleBadge4')
|
|
|
|
group.badges << build(:group_badge, group: group, name: 'ExampleBadge5')
|
2018-03-05 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|