Commit e2fb7374 authored by Chad Woolley's avatar Chad Woolley

Fix and improve tests for TemplatesController

- Fix tests for #index
- Add and DRY up fixtures
- Eliminate some redundant runs of shared examples
  to speed things up.
parent a88be4cc
...@@ -112,11 +112,11 @@ module Gitlab ...@@ -112,11 +112,11 @@ module Gitlab
if categories.any? if categories.any?
categories.keys.map do |category| categories.keys.map do |category|
files = self.by_category(category, project) files = self.by_category(category, project)
[category, files.map { |t| { key: t.key, name: t.name, content: t.content} }] [category, files.map { |t| { key: t.key, name: t.name, content: t.content } }]
end.to_h end.to_h
else else
files = self.all(project) files = self.all(project)
files.map { |t| { key: t.key, name: t.name, content: t.content} } files.map { |t| { key: t.key, name: t.name, content: t.content } }
end end
end end
end end
......
...@@ -5,13 +5,22 @@ require 'spec_helper' ...@@ -5,13 +5,22 @@ require 'spec_helper'
RSpec.describe Projects::TemplatesController do RSpec.describe Projects::TemplatesController do
let(:project) { create(:project, :repository, :private) } let(:project) { create(:project, :repository, :private) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:file_path_1) { '.gitlab/issue_templates/issue_template.md' } let(:issue_template_path_1) { '.gitlab/issue_templates/issue_template_1.md' }
let(:file_path_2) { '.gitlab/merge_request_templates/merge_request_template.md' } let(:issue_template_path_2) { '.gitlab/issue_templates/issue_template_2.md' }
let!(:file_1) { project.repository.create_file(user, file_path_1, 'issue content', message: 'message', branch_name: 'master') } let(:merge_request_template_path_1) { '.gitlab/merge_request_templates/merge_request_template_1.md' }
let!(:file_2) { project.repository.create_file(user, file_path_2, 'merge request content', message: 'message', branch_name: 'master') } 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 describe '#index' do
before do before do
project.add_developer(user)
sign_in(user) sign_in(user)
end end
...@@ -20,8 +29,7 @@ RSpec.describe Projects::TemplatesController do ...@@ -20,8 +29,7 @@ RSpec.describe Projects::TemplatesController do
get(:index, params: { namespace_id: project.namespace, template_type: template_type, project_id: project }, format: :json) 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(response).to have_gitlab_http_status(:ok)
expect(json_response.size).to eq(1) expect(json_response).to match(expected_templates)
expect(json_response).to eq(expected_templates)
end end
it 'fails for user with no access' do it 'fails for user with no access' do
...@@ -37,42 +45,44 @@ RSpec.describe Projects::TemplatesController do ...@@ -37,42 +45,44 @@ RSpec.describe Projects::TemplatesController do
context 'when querying for issue templates' do context 'when querying for issue templates' do
it_behaves_like 'templates request' do it_behaves_like 'templates request' do
let(:template_type) { 'issue' } let(:template_type) { 'issue' }
let(:expected_templates) { [ { key: "template1", name: "template_1", content: "This is template 1!" } ] } let(:expected_templates) { [expected_issue_template_1, expected_issue_template_2] }
end end
end end
context 'when querying for merge_request templates' do context 'when querying for merge_request templates' do
it_behaves_like 'templates request' do it_behaves_like 'templates request' do
let(:template_type) { 'merge_request' } let(:template_type) { 'merge_request' }
let(:expected_templates) { [ { key: "template1", name: "template_1", content: "This is template 1!" } ] } let(:expected_templates) { [expected_merge_request_template_1, expected_merge_request_template_2] }
end end
end end
end end
describe '#show' do describe '#show' do
shared_examples 'renders issue templates as json' do shared_examples 'renders issue templates as json' do
let(:expected_issue_template) { expected_issue_template_2 }
it do it do
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template', project_id: project }, format: :json) get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template_2', project_id: project }, format: :json)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq('issue_template') expect(json_response).to match(expected_issue_template)
expect(json_response['content']).to eq('issue content')
end end
end end
shared_examples 'renders merge request templates as json' do shared_examples 'renders merge request templates as json' do
let(:expected_merge_request_template) { expected_merge_request_template_2 }
it do it do
get(:show, params: { namespace_id: project.namespace, template_type: 'merge_request', key: 'merge_request_template', project_id: project }, format: :json) get(:show, params: { namespace_id: project.namespace, template_type: 'merge_request', key: 'merge_request_template_2', project_id: project }, format: :json)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq('merge_request_template') expect(json_response).to match(expected_merge_request_template)
expect(json_response['content']).to eq('merge request content')
end end
end end
shared_examples 'renders 404 when requesting an issue template' do shared_examples 'renders 404 when requesting an issue template' do
it do it do
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template', project_id: project }, format: :json) get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template_1', project_id: project }, format: :json)
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
...@@ -80,21 +90,23 @@ RSpec.describe Projects::TemplatesController do ...@@ -80,21 +90,23 @@ RSpec.describe Projects::TemplatesController do
shared_examples 'renders 404 when requesting a merge request template' do shared_examples 'renders 404 when requesting a merge request template' do
it do it do
get(:show, params: { namespace_id: project.namespace, template_type: 'merge_request', key: 'merge_request_template', project_id: project }, format: :json) get(:show, params: { namespace_id: project.namespace, template_type: 'merge_request', key: 'merge_request_template_1', project_id: project }, format: :json)
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
end end
shared_examples 'renders 404 when params are invalid' do shared_examples 'raises error when template type is invalid' do
it 'does not route when the template type is invalid' do it 'does not route when the template type is invalid' do
expect do expect do
get(:show, params: { namespace_id: project.namespace, template_type: 'invalid_type', key: 'issue_template', project_id: project }, format: :json) get(:show, params: { namespace_id: project.namespace, template_type: 'invalid_type', key: 'issue_template_1', project_id: project }, format: :json)
end.to raise_error(ActionController::UrlGenerationError) end.to raise_error(ActionController::UrlGenerationError)
end end
end
shared_examples 'renders 404 when params are invalid' do
it 'renders 404 when the format type is invalid' do it 'renders 404 when the format type is invalid' do
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template', project_id: project }, format: :html) get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template_1', project_id: project }, format: :html)
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
...@@ -113,7 +125,6 @@ RSpec.describe Projects::TemplatesController do ...@@ -113,7 +125,6 @@ RSpec.describe Projects::TemplatesController do
include_examples 'renders 404 when requesting an issue template' include_examples 'renders 404 when requesting an issue template'
include_examples 'renders 404 when requesting a merge request template' include_examples 'renders 404 when requesting a merge request template'
include_examples 'renders 404 when params are invalid'
end end
context 'when user is a member of the project' do context 'when user is a member of the project' do
...@@ -124,7 +135,11 @@ RSpec.describe Projects::TemplatesController do ...@@ -124,7 +135,11 @@ RSpec.describe Projects::TemplatesController do
include_examples 'renders issue templates as json' include_examples 'renders issue templates as json'
include_examples 'renders merge request templates as json' include_examples 'renders merge request templates as json'
include_examples 'renders 404 when params are invalid'
context 'when params are invalid' do
include_examples 'raises error when template type is invalid'
include_examples 'renders 404 when params are invalid'
end
end end
context 'when user is a guest of the project' do context 'when user is a guest of the project' do
...@@ -135,7 +150,6 @@ RSpec.describe Projects::TemplatesController do ...@@ -135,7 +150,6 @@ RSpec.describe Projects::TemplatesController do
include_examples 'renders issue templates as json' include_examples 'renders issue templates as json'
include_examples 'renders 404 when requesting a merge request template' include_examples 'renders 404 when requesting a merge request template'
include_examples 'renders 404 when params are invalid'
end end
end end
...@@ -150,8 +164,8 @@ RSpec.describe Projects::TemplatesController do ...@@ -150,8 +164,8 @@ RSpec.describe Projects::TemplatesController do
get(:names, params: { namespace_id: project.namespace, template_type: template_type, project_id: project }, format: :json) get(:names, params: { namespace_id: project.namespace, template_type: template_type, project_id: project }, format: :json)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response.size).to eq(1) expect(json_response.size).to eq(2)
expect(json_response[0]['name']).to eq(expected_template_name) expect(json_response).to match(expected_template_names)
end end
it 'fails for user with no access' do it 'fails for user with no access' do
...@@ -167,14 +181,14 @@ RSpec.describe Projects::TemplatesController do ...@@ -167,14 +181,14 @@ RSpec.describe Projects::TemplatesController do
context 'when querying for issue templates' do context 'when querying for issue templates' do
it_behaves_like 'template names request' do it_behaves_like 'template names request' do
let(:template_type) { 'issue' } let(:template_type) { 'issue' }
let(:expected_template_name) { 'issue_template' } let(:expected_template_names) { [{ 'name' => 'issue_template_1' }, { 'name' => 'issue_template_2' }] }
end end
end end
context 'when querying for merge_request templates' do context 'when querying for merge_request templates' do
it_behaves_like 'template names request' do it_behaves_like 'template names request' do
let(:template_type) { 'merge_request' } let(:template_type) { 'merge_request' }
let(:expected_template_name) { 'merge_request_template' } let(:expected_template_names) { [{ 'name' => 'merge_request_template_1' }, { 'name' => 'merge_request_template_2' }] }
end end
end end
end end
......
...@@ -12,7 +12,7 @@ import { SUCCESS_ROUTE } from '~/static_site_editor/router/constants'; ...@@ -12,7 +12,7 @@ import { SUCCESS_ROUTE } from '~/static_site_editor/router/constants';
import { TRACKING_ACTION_INITIALIZE_EDITOR } from '~/static_site_editor/constants'; import { TRACKING_ACTION_INITIALIZE_EDITOR } from '~/static_site_editor/constants';
import { import {
projectId as project, project,
returnUrl, returnUrl,
sourceContentYAML as content, sourceContentYAML as content,
sourceContentTitle as title, sourceContentTitle as title,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment