2015-03-14 12:33:02 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:49 -05:00
|
|
|
describe CreateSnippetService, services: true 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
|
|
|
|
|
|
|
@opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'non-admins are not able to create a public snippet' do
|
2015-03-14 12:33:02 -04:00
|
|
|
snippet = create_snippet(nil, @user, @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
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'admins are able to create a public snippet' do
|
2015-03-14 12:33:02 -04:00
|
|
|
snippet = create_snippet(nil, @admin, @opts)
|
|
|
|
expect(snippet.errors.any?).to be_falsey
|
|
|
|
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_snippet(project, user, opts)
|
|
|
|
CreateSnippetService.new(project, user, opts).execute
|
|
|
|
end
|
|
|
|
end
|