debian-mirror-gitlab/spec/services/create_snippet_service_spec.rb

72 lines
2 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe CreateSnippetService do
2015-04-26 12:48:37 +05:30
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-09-11 14:41:01 +05:30
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
2015-04-26 12:48:37 +05:30
@opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
end
2016-09-13 17:45:13 +05:30
it 'non-admins are not able to create a public snippet' do
2015-04-26 12:48:37 +05:30
snippet = create_snippet(nil, @user, @opts)
expect(snippet.errors.messages).to have_key(:visibility_level)
expect(snippet.errors.messages[:visibility_level].first).to(
2016-06-02 11:05:42 +05:30
match('has been restricted')
2015-04-26 12:48:37 +05:30
)
end
2016-09-13 17:45:13 +05:30
it 'admins are able to create a public snippet' do
2015-04-26 12:48:37 +05:30
snippet = create_snippet(nil, @admin, @opts)
expect(snippet.errors.any?).to be_falsey
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
end
2019-10-12 21:52:04 +05:30
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
snippet = create_snippet(nil, @user, @opts)
expect(snippet.errors.any?).to be_falsey
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
end
end
end
describe 'usage counter' do
let(:counter) { Gitlab::UsageDataCounters::SnippetCounter }
it 'increments count' do
expect do
create_snippet(nil, @admin, @opts)
end.to change { counter.read(:create) }.by 1
end
it 'does not increment count if create fails' do
expect do
create_snippet(nil, @admin, {})
end.not_to change { counter.read(:create) }
end
2015-04-26 12:48:37 +05:30
end
def create_snippet(project, user, opts)
CreateSnippetService.new(project, user, opts).execute
end
end