summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/templates_controller_spec.rb
blob: da381357bdaf83af8a4f7865311926c3573887ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::TemplatesController do
  let(:project) { create(:project, :repository, :private) }
  let(:user) { create(:user) }
  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

  describe '#show' do
    shared_examples 'renders issue templates as json' do
      let(:expected_issue_template) { expected_issue_template_2 }

      it do
        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(json_response).to match(expected_issue_template)
      end
    end

    shared_examples 'renders merge request templates as json' do
      let(:expected_merge_request_template) { expected_merge_request_template_2 }

      it do
        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(json_response).to match(expected_merge_request_template)
      end
    end

    shared_examples 'renders 404 when requesting an issue template' do
      it do
        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)
      end
    end

    shared_examples 'renders 404 when requesting a merge request template' do
      it do
        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)
      end
    end

    shared_examples 'raises error when template type is invalid' do
      it 'does not route when the template type is invalid' do
        expect do
          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
    end

    shared_examples 'renders 404 when params are 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_1', project_id: project }, format: :html)

        expect(response).to have_gitlab_http_status(:not_found)
      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)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when the user is not a member of the project' do
      before do
        sign_in(user)
      end

      include_examples 'renders 404 when requesting an issue template'
      include_examples 'renders 404 when requesting a merge request template'
    end

    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'

      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

    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'
    end
  end

  describe '#names' do
    before do
      project.add_developer(user)
      sign_in(user)
    end

    shared_examples 'template names request' do
      it 'returns the template names', :aggregate_failures do
        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(json_response['Project Templates'].size).to eq(2)
        expect(json_response['Project Templates'].map { |x| x.slice('name') }).to match(expected_template_names)
      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)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when querying for issue templates' do
      it_behaves_like 'template names request' do
        let(:template_type) { 'issue' }
        let(:expected_template_names) { [{ 'name' => 'issue_template_1' }, { 'name' => 'issue_template_2' }] }
      end
    end

    context 'when querying for merge_request templates' do
      it_behaves_like 'template names request' do
        let(:template_type) { 'merge_request' }
        let(:expected_template_names) { [{ 'name' => 'merge_request_template_1' }, { 'name' => 'merge_request_template_2' }] }
      end
    end
  end
end