2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-14 12:33:02 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe UpdateSnippetService do
|
2015-03-14 12:33:02 -04:00
|
|
|
before do
|
|
|
|
@user = create :user
|
|
|
|
@admin = create :user, admin: true
|
|
|
|
@opts = {
|
|
|
|
title: 'Test snippet',
|
|
|
|
file_name: 'snippet.rb',
|
|
|
|
content: 'puts "hello world"',
|
|
|
|
visibility_level: Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'When public visibility is restricted' do
|
|
|
|
before do
|
2015-07-01 17:21:51 -04:00
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
|
2015-03-14 12:33:02 -04:00
|
|
|
|
|
|
|
@snippet = create_snippet(@project, @user, @opts)
|
|
|
|
@opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'non-admins should not be able to update to public visibility' do
|
|
|
|
old_visibility = @snippet.visibility_level
|
|
|
|
update_snippet(@project, @user, @snippet, @opts)
|
|
|
|
expect(@snippet.errors.messages).to have_key(:visibility_level)
|
|
|
|
expect(@snippet.errors.messages[:visibility_level].first).to(
|
2016-03-20 18:09:33 -04:00
|
|
|
match('has been restricted')
|
2015-03-14 12:33:02 -04:00
|
|
|
)
|
|
|
|
expect(@snippet.visibility_level).to eq(old_visibility)
|
|
|
|
end
|
|
|
|
|
2019-08-27 13:31:59 -04:00
|
|
|
it 'admins should be able to update to public visibility' do
|
2015-03-14 12:33:02 -04:00
|
|
|
old_visibility = @snippet.visibility_level
|
|
|
|
update_snippet(@project, @admin, @snippet, @opts)
|
|
|
|
expect(@snippet.visibility_level).not_to eq(old_visibility)
|
|
|
|
expect(@snippet.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
2019-08-27 13:31:59 -04:00
|
|
|
|
|
|
|
describe "when visibility level is passed as a string" do
|
|
|
|
before do
|
|
|
|
@opts[:visibility] = 'internal'
|
|
|
|
@opts.delete(:visibility_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns the correct visibility level" do
|
|
|
|
update_snippet(@project, @user, @snippet, @opts)
|
|
|
|
expect(@snippet.errors.any?).to be_falsey
|
|
|
|
expect(@snippet.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
end
|
|
|
|
end
|
2015-03-14 12:33:02 -04:00
|
|
|
end
|
|
|
|
|
2019-08-08 09:18:57 -04:00
|
|
|
describe 'usage counter' do
|
|
|
|
let(:counter) { Gitlab::UsageDataCounters::SnippetCounter }
|
|
|
|
let(:snippet) { create_snippet(nil, @user, @opts) }
|
|
|
|
|
|
|
|
it 'increments count' do
|
|
|
|
expect do
|
|
|
|
update_snippet(nil, @admin, snippet, @opts)
|
|
|
|
end.to change { counter.read(:update) }.by 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not increment count if create fails' do
|
|
|
|
expect do
|
|
|
|
update_snippet(nil, @admin, snippet, { title: '' })
|
|
|
|
end.not_to change { counter.read(:update) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-14 12:33:02 -04:00
|
|
|
def create_snippet(project, user, opts)
|
|
|
|
CreateSnippetService.new(project, user, opts).execute
|
|
|
|
end
|
|
|
|
|
2015-12-14 21:53:52 -05:00
|
|
|
def update_snippet(project, user, snippet, opts)
|
2015-03-14 12:33:02 -04:00
|
|
|
UpdateSnippetService.new(project, user, snippet, opts).execute
|
|
|
|
end
|
|
|
|
end
|