update_snippet_service_spec.rb 1.57 KB
Newer Older
1 2
require 'spec_helper'

3
describe UpdateSnippetService do
4 5 6 7 8 9 10 11 12 13 14 15 16
  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
17
      stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
18 19 20 21 22 23 24 25 26 27

      @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(
Douwe Maan's avatar
Douwe Maan committed
28
        match('has been restricted')
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
      )
      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

Gabriel Mazetto's avatar
Gabriel Mazetto committed
45
  def update_snippet(project, user, snippet, opts)
46 47 48
    UpdateSnippetService.new(project, user, snippet, opts).execute
  end
end