debian-mirror-gitlab/spec/controllers/projects/templates_controller_spec.rb

196 lines
8 KiB
Ruby
Raw Permalink Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::TemplatesController do
2019-07-07 11:18:12 +05:30
let(:project) { create(:project, :repository, :private) }
2016-09-13 17:45:13 +05:30
let(:user) { create(:user) }
2021-01-29 00:20:46 +05:30
let(:issue_template_path_1) { '.gitlab/issue_templates/issue_template_1.md' }
let(:issue_template_path_2) { '.gitlab/issue_templates/issue_template_2.md' }
let(:merge_request_template_path_1) { '.gitlab/merge_request_templates/merge_request_template_1.md' }
let(:merge_request_template_path_2) { '.gitlab/merge_request_templates/merge_request_template_2.md' }
let!(:issue_template_file_1) { project.repository.create_file(user, issue_template_path_1, 'issue content 1', message: 'message 1', branch_name: 'master') }
let!(:issue_template_file_2) { project.repository.create_file(user, issue_template_path_2, 'issue content 2', message: 'message 2', branch_name: 'master') }
let!(:merge_request_template_file_1) { project.repository.create_file(user, merge_request_template_path_1, 'merge request content 1', message: 'message 1', branch_name: 'master') }
let!(:merge_request_template_file_2) { project.repository.create_file(user, merge_request_template_path_2, 'merge request content 2', message: 'message 2', branch_name: 'master') }
let(:expected_issue_template_1) { { 'key' => 'issue_template_1', 'name' => 'issue_template_1', 'content' => 'issue content 1' } }
let(:expected_issue_template_2) { { 'key' => 'issue_template_2', 'name' => 'issue_template_2', 'content' => 'issue content 2' } }
let(:expected_merge_request_template_1) { { 'key' => 'merge_request_template_1', 'name' => 'merge_request_template_1', 'content' => 'merge request content 1' } }
let(:expected_merge_request_template_2) { { 'key' => 'merge_request_template_2', 'name' => 'merge_request_template_2', 'content' => 'merge request content 2' } }
describe '#index' do
before do
project.add_developer(user)
sign_in(user)
end
shared_examples 'templates request' do
it 'returns the templates' do
get(:index, params: { namespace_id: project.namespace, template_type: template_type, project_id: project }, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to match(expected_templates)
end
it 'fails for user with no access' do
other_user = create(:user)
sign_in(other_user)
get(:index, params: { namespace_id: project.namespace, template_type: template_type, project_id: project }, format: :json)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when querying for issue templates' do
it_behaves_like 'templates request' do
let(:template_type) { 'issue' }
let(:expected_templates) { [expected_issue_template_1, expected_issue_template_2] }
end
end
context 'when querying for merge_request templates' do
it_behaves_like 'templates request' do
let(:template_type) { 'merge_request' }
let(:expected_templates) { [expected_merge_request_template_1, expected_merge_request_template_2] }
end
end
end
2016-09-13 17:45:13 +05:30
2019-07-07 11:18:12 +05:30
describe '#show' do
shared_examples 'renders issue templates as json' do
2021-01-29 00:20:46 +05:30
let(:expected_issue_template) { expected_issue_template_2 }
2019-07-07 11:18:12 +05:30
it do
2021-01-29 00:20:46 +05:30
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template_2', project_id: project }, format: :json)
2016-09-13 17:45:13 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-01-29 00:20:46 +05:30
expect(json_response).to match(expected_issue_template)
2019-07-07 11:18:12 +05:30
end
end
2016-09-13 17:45:13 +05:30
2019-07-07 11:18:12 +05:30
shared_examples 'renders merge request templates as json' do
2021-01-29 00:20:46 +05:30
let(:expected_merge_request_template) { expected_merge_request_template_2 }
2019-07-07 11:18:12 +05:30
it do
2021-01-29 00:20:46 +05:30
get(:show, params: { namespace_id: project.namespace, template_type: 'merge_request', key: 'merge_request_template_2', project_id: project }, format: :json)
2019-07-07 11:18:12 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2021-01-29 00:20:46 +05:30
expect(json_response).to match(expected_merge_request_template)
2019-07-07 11:18:12 +05:30
end
end
shared_examples 'renders 404 when requesting an issue template' do
it do
2021-01-29 00:20:46 +05:30
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template_1', project_id: project }, format: :json)
2019-07-07 11:18:12 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-07-07 11:18:12 +05:30
end
end
shared_examples 'renders 404 when requesting a merge request template' do
it do
2021-01-29 00:20:46 +05:30
get(:show, params: { namespace_id: project.namespace, template_type: 'merge_request', key: 'merge_request_template_1', project_id: project }, format: :json)
2016-09-13 17:45:13 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-07-07 11:18:12 +05:30
end
2016-09-13 17:45:13 +05:30
end
2021-01-29 00:20:46 +05:30
shared_examples 'raises error when template type is invalid' do
2019-07-07 11:18:12 +05:30
it 'does not route when the template type is invalid' do
expect do
2021-01-29 00:20:46 +05:30
get(:show, params: { namespace_id: project.namespace, template_type: 'invalid_type', key: 'issue_template_1', project_id: project }, format: :json)
2019-07-07 11:18:12 +05:30
end.to raise_error(ActionController::UrlGenerationError)
end
2021-01-29 00:20:46 +05:30
end
2019-07-07 11:18:12 +05:30
2021-01-29 00:20:46 +05:30
shared_examples 'renders 404 when params are invalid' do
2019-07-07 11:18:12 +05:30
it 'renders 404 when the format type is invalid' do
2021-01-29 00:20:46 +05:30
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template_1', project_id: project }, format: :html)
2019-07-07 11:18:12 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-07-07 11:18:12 +05:30
end
it 'renders 404 when the key is unknown' do
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'unknown_template', project_id: project }, format: :json)
2016-09-13 17:45:13 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-07-07 11:18:12 +05:30
end
2016-09-13 17:45:13 +05:30
end
2019-07-07 11:18:12 +05:30
context 'when the user is not a member of the project' do
before do
sign_in(user)
end
2016-09-13 17:45:13 +05:30
2019-07-07 11:18:12 +05:30
include_examples 'renders 404 when requesting an issue template'
include_examples 'renders 404 when requesting a merge request template'
2016-09-13 17:45:13 +05:30
end
2019-07-07 11:18:12 +05:30
context 'when user is a member of the project' do
before do
project.add_developer(user)
sign_in(user)
end
include_examples 'renders issue templates as json'
include_examples 'renders merge request templates as json'
2021-01-29 00:20:46 +05:30
context 'when params are invalid' do
include_examples 'raises error when template type is invalid'
include_examples 'renders 404 when params are invalid'
end
2019-07-07 11:18:12 +05:30
end
context 'when user is a guest of the project' do
before do
project.add_guest(user)
sign_in(user)
end
include_examples 'renders issue templates as json'
include_examples 'renders 404 when requesting a merge request template'
2016-09-13 17:45:13 +05:30
end
end
2019-12-21 20:55:43 +05:30
describe '#names' do
before do
project.add_developer(user)
sign_in(user)
end
shared_examples 'template names request' do
2021-09-04 01:27:46 +05:30
it 'returns the template names', :aggregate_failures do
get(:names, params: { namespace_id: project.namespace, template_type: template_type, project_id: project }, format: :json)
2021-04-17 20:07:23 +05:30
2021-09-04 01:27:46 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['Project Templates'].size).to eq(2)
expect(json_response['Project Templates'].map { |x| x.slice('name') }).to match(expected_template_names)
2019-12-21 20:55:43 +05:30
end
it 'fails for user with no access' do
other_user = create(:user)
sign_in(other_user)
get(:names, params: { namespace_id: project.namespace, template_type: template_type, project_id: project }, format: :json)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-12-21 20:55:43 +05:30
end
end
context 'when querying for issue templates' do
it_behaves_like 'template names request' do
let(:template_type) { 'issue' }
2021-01-29 00:20:46 +05:30
let(:expected_template_names) { [{ 'name' => 'issue_template_1' }, { 'name' => 'issue_template_2' }] }
2019-12-21 20:55:43 +05:30
end
end
context 'when querying for merge_request templates' do
it_behaves_like 'template names request' do
let(:template_type) { 'merge_request' }
2021-01-29 00:20:46 +05:30
let(:expected_template_names) { [{ 'name' => 'merge_request_template_1' }, { 'name' => 'merge_request_template_2' }] }
2019-12-21 20:55:43 +05:30
end
end
end
2016-09-13 17:45:13 +05:30
end