Commit 78583b0d authored by Kerri Miller's avatar Kerri Miller

Refactor visibility_level_checker spec

- add named subject
- extract common `result` variable to hold action response
- add check for admin user
parent 0fe59e92
...@@ -5,16 +5,15 @@ require 'spec_helper' ...@@ -5,16 +5,15 @@ require 'spec_helper'
RSpec.describe Gitlab::VisibilityLevelChecker do RSpec.describe Gitlab::VisibilityLevelChecker do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project) } let(:project) { create(:project) }
let(:visibility_level_checker) { }
let(:override_params) { {} } let(:override_params) { {} }
subject { described_class.new(user, project, project_params: override_params) } subject(:visibility_level_checker) { described_class.new(user, project, project_params: override_params) }
let(:result) { subject.level_restricted? }
describe '#level_restricted?' do describe '#level_restricted?' do
context 'when visibility level is allowed' do context 'when visibility level is allowed' do
it 'returns false with nil for visibility level' do it 'returns false with nil for visibility level' do
result = subject.level_restricted?
expect(result.restricted?).to eq(false) expect(result.restricted?).to eq(false)
expect(result.visibility_level).to be_nil expect(result.visibility_level).to be_nil
end end
...@@ -25,12 +24,26 @@ RSpec.describe Gitlab::VisibilityLevelChecker do ...@@ -25,12 +24,26 @@ RSpec.describe Gitlab::VisibilityLevelChecker do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
end end
it 'returns true and visibility name' do context 'for public project' do
project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) before do
result = subject.level_restricted? project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
end
context 'for non-admin user' do
it 'returns true and visibility name' do
expect(result.restricted?).to eq(true)
expect(result.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
end
end
expect(result.restricted?).to eq(true) context 'for admin user' do
expect(result.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC) let(:user) { create(:user, :admin) }
it 'returns false and a nil visibility level' do
expect(result.restricted?).to eq(false)
expect(result.visibility_level).to be_nil
end
end
end end
context 'overridden visibility' do context 'overridden visibility' do
...@@ -50,8 +63,6 @@ RSpec.describe Gitlab::VisibilityLevelChecker do ...@@ -50,8 +63,6 @@ RSpec.describe Gitlab::VisibilityLevelChecker do
let(:override_visibility) { 'public' } let(:override_visibility) { 'public' }
it 'returns true and visibility name' do it 'returns true and visibility name' do
result = subject.level_restricted?
expect(result.restricted?).to eq(true) expect(result.restricted?).to eq(true)
expect(result.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC) expect(result.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
end end
...@@ -61,8 +72,6 @@ RSpec.describe Gitlab::VisibilityLevelChecker do ...@@ -61,8 +72,6 @@ RSpec.describe Gitlab::VisibilityLevelChecker do
let(:override_visibility) { 'publik' } let(:override_visibility) { 'publik' }
it 'returns false with nil for visibility level' do it 'returns false with nil for visibility level' do
result = subject.level_restricted?
expect(result.restricted?).to eq(false) expect(result.restricted?).to eq(false)
expect(result.visibility_level).to be_nil expect(result.visibility_level).to be_nil
end end
...@@ -72,8 +81,6 @@ RSpec.describe Gitlab::VisibilityLevelChecker do ...@@ -72,8 +81,6 @@ RSpec.describe Gitlab::VisibilityLevelChecker do
let(:override_params) { {} } let(:override_params) { {} }
it 'returns false with nil for visibility level' do it 'returns false with nil for visibility level' do
result = subject.level_restricted?
expect(result.restricted?).to eq(false) expect(result.restricted?).to eq(false)
expect(result.visibility_level).to be_nil expect(result.visibility_level).to be_nil
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment