blob: b8215f9779d5acb50ab0e5fb1df5ee6a43baf29e (
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
|
# frozen_string_literal: true
require 'spec_helper'
describe Snippets::UpdateService do
describe '#execute' do
let_it_be(:user) { create(:user) }
let_it_be(:admin) { create :user, admin: true }
let(:visibility_level) { Gitlab::VisibilityLevel::PRIVATE }
let(:options) do
{
title: 'Test snippet',
file_name: 'snippet.rb',
content: 'puts "hello world"',
visibility_level: visibility_level
}
end
let(:updater) { user }
subject do
Snippets::UpdateService.new(
project,
updater,
options
).execute(snippet)
end
shared_examples 'a service that updates a snippet' do
it 'updates a snippet with the provided attributes' do
expect { subject }.to change { snippet.title }.from(snippet.title).to(options[:title])
.and change { snippet.file_name }.from(snippet.file_name).to(options[:file_name])
.and change { snippet.content }.from(snippet.content).to(options[:content])
end
end
shared_examples 'public visibility level restrictions apply' do
let(:visibility_level) { Gitlab::VisibilityLevel::PUBLIC }
before do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
end
context 'when user is not an admin' do
it 'responds with an error' do
expect(subject).to be_error
end
it 'does not update snippet to public visibility' do
original_visibility = snippet.visibility_level
expect(subject.message).to match('has been restricted')
expect(snippet.visibility_level).to eq(original_visibility)
end
end
context 'when user is an admin' do
let(:updater) { admin }
it 'responds with success' do
expect(subject).to be_success
end
it 'updates the snippet to public visibility' do
old_visibility = snippet.visibility_level
expect(subject.payload[:snippet]).not_to be_nil
expect(snippet.visibility_level).not_to eq(old_visibility)
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
end
end
context 'when visibility level is passed as a string' do
before do
options[:visibility] = 'internal'
options.delete(:visibility_level)
end
it 'assigns the correct visibility level' do
expect(subject).to be_success
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
end
end
end
shared_examples 'snippet update data is tracked' do
let(:counter) { Gitlab::UsageDataCounters::SnippetCounter }
it 'increments count when create succeeds' do
expect { subject }.to change { counter.read(:update) }.by 1
end
context 'when update fails' do
let(:options) { { title: '' } }
it 'does not increment count' do
expect { subject }.not_to change { counter.read(:update) }
end
end
end
context 'when Project Snippet' do
let_it_be(:project) { create(:project) }
let!(:snippet) { create(:project_snippet, author: user, project: project) }
before do
project.add_developer(user)
end
it_behaves_like 'a service that updates a snippet'
it_behaves_like 'public visibility level restrictions apply'
it_behaves_like 'snippet update data is tracked'
end
context 'when PersonalSnippet' do
let(:project) { nil }
let!(:snippet) { create(:personal_snippet, author: user) }
it_behaves_like 'a service that updates a snippet'
it_behaves_like 'public visibility level restrictions apply'
it_behaves_like 'snippet update data is tracked'
end
end
end
|