Add tests for snippet services
Add Rspec tests for the new UpdateSnippetService and CreateSnippetService classes.
This commit is contained in:
parent
5710c1aaf8
commit
13e9f4f334
2 changed files with 96 additions and 0 deletions
44
spec/services/create_snippet_service_spec.rb
Normal file
44
spec/services/create_snippet_service_spec.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe CreateSnippetService do
|
||||
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
|
||||
allow_any_instance_of(ApplicationSetting).to(
|
||||
receive(:restricted_visibility_levels).and_return(
|
||||
[Gitlab::VisibilityLevel::PUBLIC]
|
||||
)
|
||||
)
|
||||
|
||||
@opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
|
||||
end
|
||||
|
||||
it 'non-admins should not be able to create a public snippet' do
|
||||
snippet = create_snippet(nil, @user, @opts)
|
||||
expect(snippet.errors.messages).to have_key(:visibility_level)
|
||||
expect(snippet.errors.messages[:visibility_level].first).to(
|
||||
match('Public visibility has been restricted')
|
||||
)
|
||||
end
|
||||
|
||||
it 'admins should be able to create a public snippet' do
|
||||
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
|
52
spec/services/update_snippet_service_spec.rb
Normal file
52
spec/services/update_snippet_service_spec.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe UpdateSnippetService do
|
||||
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
|
||||
allow_any_instance_of(ApplicationSetting).to(
|
||||
receive(:restricted_visibility_levels).and_return(
|
||||
[Gitlab::VisibilityLevel::PUBLIC]
|
||||
)
|
||||
)
|
||||
|
||||
@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(
|
||||
match('Public visibility has been restricted')
|
||||
)
|
||||
expect(@snippet.visibility_level).to eq(old_visibility)
|
||||
end
|
||||
|
||||
it 'admins should be able to update to pubic visibility' do
|
||||
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
|
||||
end
|
||||
|
||||
def create_snippet(project, user, opts)
|
||||
CreateSnippetService.new(project, user, opts).execute
|
||||
end
|
||||
|
||||
def update_snippet(project = nil, user, snippet, opts)
|
||||
UpdateSnippetService.new(project, user, snippet, opts).execute
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue