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

118 lines
3.3 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
2019-12-21 20:55:43 +05:30
let(:user) { create(:user) }
let(:admin) { create(:user, :admin) }
let(:opts) { base_opts.merge(extra_opts) }
let(:base_opts) do
{
2015-04-26 12:48:37 +05:30
title: 'Test snippet',
file_name: 'snippet.rb',
content: 'puts "hello world"',
visibility_level: Gitlab::VisibilityLevel::PRIVATE
}
end
2019-12-21 20:55:43 +05:30
let(:extra_opts) { {} }
2015-04-26 12:48:37 +05:30
context 'When public visibility is restricted' do
2019-12-21 20:55:43 +05:30
let(:extra_opts) { { visibility_level: Gitlab::VisibilityLevel::PUBLIC } }
2015-04-26 12:48:37 +05:30
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
end
2016-09-13 17:45:13 +05:30
it 'non-admins are not able to create a public snippet' do
2019-12-21 20:55:43 +05:30
snippet = create_snippet(nil, user, opts)
2015-04-26 12:48:37 +05:30
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
2019-12-21 20:55:43 +05:30
snippet = create_snippet(nil, admin, opts)
2015-04-26 12:48:37 +05:30
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
2019-12-21 20:55:43 +05:30
let(:extra_opts) { { visibility: 'internal' } }
2019-10-12 21:52:04 +05:30
before do
2019-12-21 20:55:43 +05:30
base_opts.delete(:visibility_level)
2019-10-12 21:52:04 +05:30
end
it "assigns the correct visibility level" do
2019-12-21 20:55:43 +05:30
snippet = create_snippet(nil, user, opts)
2019-10-12 21:52:04 +05:30
expect(snippet.errors.any?).to be_falsey
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
end
end
end
2019-12-21 20:55:43 +05:30
context 'checking spam' do
shared_examples 'marked as spam' do
let(:snippet) { create_snippet(nil, admin, opts) }
it 'marks a snippet as a spam ' do
expect(snippet).to be_spam
end
it 'invalidates the snippet' do
expect(snippet).to be_invalid
end
it 'creates a new spam_log' do
expect { snippet }
.to log_spam(title: snippet.title, noteable_type: 'PersonalSnippet')
end
it 'assigns a spam_log to an issue' do
expect(snippet.spam_log).to eq(SpamLog.last)
end
end
let(:extra_opts) do
{ visibility_level: Gitlab::VisibilityLevel::PUBLIC, request: double(:request, env: {}) }
end
before do
expect_next_instance_of(AkismetService) do |akismet_service|
expect(akismet_service).to receive_messages(spam?: true)
end
end
[true, false, nil].each do |allow_possible_spam|
context "when recaptcha_disabled flag is #{allow_possible_spam.inspect}" do
before do
stub_feature_flags(allow_possible_spam: allow_possible_spam) unless allow_possible_spam.nil?
end
it_behaves_like 'marked as spam'
end
end
end
2019-10-12 21:52:04 +05:30
describe 'usage counter' do
let(:counter) { Gitlab::UsageDataCounters::SnippetCounter }
it 'increments count' do
expect do
2019-12-21 20:55:43 +05:30
create_snippet(nil, admin, opts)
2019-10-12 21:52:04 +05:30
end.to change { counter.read(:create) }.by 1
end
it 'does not increment count if create fails' do
expect do
2019-12-21 20:55:43 +05:30
create_snippet(nil, admin, {})
2019-10-12 21:52:04 +05:30
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