2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::ProjectTemplates do
|
2020-08-11 23:10:17 -04:00
|
|
|
let_it_be(:public_project) { create(:project, :public, :repository, create_templates: :merge_request, path: 'path.with.dot') }
|
|
|
|
let_it_be(:private_project) { create(:project, :private, :repository, create_templates: :issue) }
|
2020-03-30 05:07:58 -04:00
|
|
|
let_it_be(:developer) { create(:user) }
|
2018-10-02 19:00:38 -04:00
|
|
|
|
2020-05-13 11:08:23 -04:00
|
|
|
let(:url_encoded_path) { "#{public_project.namespace.path}%2F#{public_project.path}" }
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
before do
|
|
|
|
private_project.add_developer(developer)
|
|
|
|
end
|
|
|
|
|
2020-05-13 11:08:23 -04:00
|
|
|
shared_examples 'accepts project paths with dots' do
|
|
|
|
it do
|
|
|
|
subject
|
2020-04-28 11:09:29 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
2020-05-13 11:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/templates/:type' do
|
|
|
|
it_behaves_like 'accepts project paths with dots' do
|
|
|
|
subject { get api("/projects/#{url_encoded_path}/templates/dockerfiles") }
|
|
|
|
end
|
2020-04-28 11:09:29 -04:00
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
it 'returns dockerfiles' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/dockerfiles")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response).to satisfy_one { |template| template['key'] == 'Binary' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns gitignores' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitignores")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response).to satisfy_one { |template| template['key'] == 'Actionscript' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns gitlab_ci_ymls' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitlab_ci_ymls")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response).to satisfy_one { |template| template['key'] == 'Android' }
|
|
|
|
end
|
|
|
|
|
2020-12-17 16:09:57 -05:00
|
|
|
it 'returns gitlab_ci_syntax_ymls' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitlab_ci_syntax_ymls")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response).to satisfy_one { |template| template['key'] == 'Artifacts example' }
|
|
|
|
end
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
it 'returns licenses' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/licenses")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response).to satisfy_one { |template| template['key'] == 'mit' }
|
|
|
|
end
|
|
|
|
|
2020-08-03 05:09:42 -04:00
|
|
|
it 'returns metrics_dashboard_ymls' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/metrics_dashboard_ymls")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response).to satisfy_one { |template| template['key'] == 'Default' }
|
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
it 'returns issue templates' do
|
|
|
|
get api("/projects/#{private_project.id}/templates/issues", developer)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response.map {|t| t['key']}).to match_array(%w(bug feature_proposal template_test))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns merge request templates' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/merge_requests")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
expect(json_response.map {|t| t['key']}).to match_array(%w(bug feature_proposal template_test))
|
|
|
|
end
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
it 'returns 400 for an unknown template type' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/unknown")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-10-02 19:00:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'denies access to an anonymous user on a private project' do
|
|
|
|
get api("/projects/#{private_project.id}/templates/licenses")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-10-02 19:00:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits access to a developer on a private project' do
|
|
|
|
get api("/projects/#{private_project.id}/templates/licenses", developer)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/templates/licenses' do
|
|
|
|
it 'returns key and name for the listed licenses' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/licenses")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/template_list')
|
|
|
|
end
|
2020-05-13 11:08:23 -04:00
|
|
|
|
|
|
|
it_behaves_like 'accepts project paths with dots' do
|
|
|
|
subject { get api("/projects/#{url_encoded_path}/templates/licenses") }
|
|
|
|
end
|
2018-10-02 19:00:38 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 13:09:10 -05:00
|
|
|
describe 'GET /projects/:id/templates/:type/:name' do
|
2018-10-02 19:00:38 -04:00
|
|
|
it 'returns a specific dockerfile' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/dockerfiles/Binary")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('Binary')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a specific gitignore' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitignores/Actionscript")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('Actionscript')
|
|
|
|
end
|
|
|
|
|
2019-02-25 05:41:50 -05:00
|
|
|
it 'returns C++ gitignore' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitignores/C++")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-02-25 05:41:50 -05:00
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('C++')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns C++ gitignore for URL-encoded names' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitignores/C%2B%2B")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-02-25 05:41:50 -05:00
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('C++')
|
|
|
|
end
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
it 'returns a specific gitlab_ci_yml' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitlab_ci_ymls/Android")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('Android')
|
|
|
|
end
|
|
|
|
|
2020-12-17 16:09:57 -05:00
|
|
|
it 'returns a specific gitlab_ci_syntax_yml' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/gitlab_ci_syntax_ymls/Artifacts%20example")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('Artifacts example')
|
|
|
|
end
|
|
|
|
|
2020-08-03 05:09:42 -04:00
|
|
|
it 'returns a specific metrics_dashboard_yml' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/metrics_dashboard_ymls/Default")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('Default')
|
|
|
|
end
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
it 'returns a specific license' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/licenses/mit")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/license')
|
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
it 'returns a specific issue template' do
|
|
|
|
get api("/projects/#{private_project.id}/templates/issues/bug", developer)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('bug')
|
|
|
|
expect(json_response['content']).to eq('something valid')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a specific merge request template' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/merge_requests/feature_proposal")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/template')
|
|
|
|
expect(json_response['name']).to eq('feature_proposal')
|
|
|
|
expect(json_response['content']).to eq('feature_proposal') # Content is identical to filename here
|
|
|
|
end
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
it 'returns 404 for an unknown specific template' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/licenses/unknown")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-10-02 19:00:38 -04:00
|
|
|
end
|
2020-08-11 23:10:17 -04:00
|
|
|
|
|
|
|
it 'returns 404 for an unknown issue template' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/issues/unknown")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 for an unknown merge request template' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/merge_requests/unknown")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
2018-10-02 19:00:38 -04:00
|
|
|
|
|
|
|
it 'denies access to an anonymous user on a private project' do
|
|
|
|
get api("/projects/#{private_project.id}/templates/licenses/mit")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-10-02 19:00:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits access to a developer on a private project' do
|
|
|
|
get api("/projects/#{private_project.id}/templates/licenses/mit", developer)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/license')
|
|
|
|
end
|
2019-02-25 05:41:50 -05:00
|
|
|
|
2020-05-13 11:08:23 -04:00
|
|
|
it_behaves_like 'accepts project paths with dots' do
|
|
|
|
subject { get api("/projects/#{url_encoded_path}/templates/gitlab_ci_ymls/Android") }
|
|
|
|
end
|
|
|
|
|
2020-08-03 05:09:42 -04:00
|
|
|
it_behaves_like 'accepts project paths with dots' do
|
|
|
|
subject { get api("/projects/#{url_encoded_path}/templates/metrics_dashboard_ymls/Default") }
|
|
|
|
end
|
|
|
|
|
2019-02-25 05:41:50 -05:00
|
|
|
shared_examples 'path traversal attempt' do |template_type|
|
|
|
|
it 'rejects invalid filenames' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/#{template_type}/%2e%2e%2fPython%2ea")
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:internal_server_error)
|
2019-02-25 05:41:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
TemplateFinder::VENDORED_TEMPLATES.each do |template_type, _|
|
|
|
|
it_behaves_like 'path traversal attempt', template_type
|
|
|
|
end
|
2018-10-02 19:00:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/templates/licenses/:key' do
|
|
|
|
it 'fills placeholders in the license' do
|
|
|
|
get api("/projects/#{public_project.id}/templates/licenses/agpl-3.0"),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
project: 'Project Placeholder',
|
|
|
|
fullname: 'Fullname Placeholder'
|
|
|
|
}
|
2018-10-02 19:00:38 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-10-02 19:00:38 -04:00
|
|
|
expect(response).to match_response_schema('public_api/v4/license')
|
|
|
|
|
|
|
|
content = json_response['content']
|
|
|
|
|
|
|
|
expect(content).to include('Project Placeholder')
|
|
|
|
expect(content).to include("Copyright (C) #{Time.now.year} Fullname Placeholder")
|
|
|
|
end
|
2020-05-13 11:08:23 -04:00
|
|
|
|
|
|
|
it_behaves_like 'accepts project paths with dots' do
|
|
|
|
subject { get api("/projects/#{url_encoded_path}/templates/licenses/mit") }
|
|
|
|
end
|
2018-10-02 19:00:38 -04:00
|
|
|
end
|
|
|
|
end
|