Commit 7a16ecfb authored by Heinrich Lee Yu's avatar Heinrich Lee Yu Committed by Eugenia Grieff

Fix personal access token policy spec

We use `build_stubbed` so that the user associations have IDs because in
the policy, we do some ID comparison to determine if the user is the
owner of the token.

We had to use `create` for the blocked user case because the factory
calls `User#block!` which saves the update to the DB and that does not
work with stubbed models.
parent 44ca2456
...@@ -8,17 +8,17 @@ RSpec.describe PersonalAccessTokenPolicy do ...@@ -8,17 +8,17 @@ RSpec.describe PersonalAccessTokenPolicy do
subject { described_class.new(current_user, token) } subject { described_class.new(current_user, token) }
context 'current_user is an administrator', :enable_admin_mode do context 'current_user is an administrator', :enable_admin_mode do
let_it_be(:current_user) { build(:admin) } let_it_be(:current_user) { build_stubbed(:admin) }
context 'not the owner of the token' do context 'not the owner of the token' do
let_it_be(:token) { build(:personal_access_token) } let_it_be(:token) { build_stubbed(:personal_access_token) }
it { is_expected.to be_allowed(:read_token) } it { is_expected.to be_allowed(:read_token) }
it { is_expected.to be_allowed(:revoke_token) } it { is_expected.to be_allowed(:revoke_token) }
end end
context 'owner of the token' do context 'owner of the token' do
let_it_be(:token) { build(:personal_access_token, user: current_user) } let_it_be(:token) { build_stubbed(:personal_access_token, user: current_user) }
it { is_expected.to be_allowed(:read_token) } it { is_expected.to be_allowed(:read_token) }
it { is_expected.to be_allowed(:revoke_token) } it { is_expected.to be_allowed(:revoke_token) }
...@@ -26,17 +26,17 @@ RSpec.describe PersonalAccessTokenPolicy do ...@@ -26,17 +26,17 @@ RSpec.describe PersonalAccessTokenPolicy do
end end
context 'current_user is not an administrator' do context 'current_user is not an administrator' do
let_it_be(:current_user) { build(:user) } let_it_be(:current_user) { build_stubbed(:user) }
context 'not the owner of the token' do context 'not the owner of the token' do
let_it_be(:token) { build(:personal_access_token) } let_it_be(:token) { build_stubbed(:personal_access_token) }
it { is_expected.to be_disallowed(:read_token) } it { is_expected.to be_disallowed(:read_token) }
it { is_expected.to be_disallowed(:revoke_token) } it { is_expected.to be_disallowed(:revoke_token) }
end end
context 'owner of the token' do context 'owner of the token' do
let_it_be(:token) { build(:personal_access_token, user: current_user) } let_it_be(:token) { build_stubbed(:personal_access_token, user: current_user) }
it { is_expected.to be_allowed(:read_token) } it { is_expected.to be_allowed(:read_token) }
it { is_expected.to be_allowed(:revoke_token) } it { is_expected.to be_allowed(:revoke_token) }
...@@ -44,17 +44,17 @@ RSpec.describe PersonalAccessTokenPolicy do ...@@ -44,17 +44,17 @@ RSpec.describe PersonalAccessTokenPolicy do
end end
context 'current_user is a blocked administrator', :enable_admin_mode do context 'current_user is a blocked administrator', :enable_admin_mode do
let_it_be(:current_user) { build(:admin, :blocked) } let_it_be(:current_user) { create(:admin, :blocked) }
context 'owner of the token' do context 'owner of the token' do
let_it_be(:token) { build(:personal_access_token, user: current_user) } let_it_be(:token) { build_stubbed(:personal_access_token, user: current_user) }
it { is_expected.to be_disallowed(:read_token) } it { is_expected.to be_disallowed(:read_token) }
it { is_expected.to be_disallowed(:revoke_token) } it { is_expected.to be_disallowed(:revoke_token) }
end end
context 'not the owner of the token' do context 'not the owner of the token' do
let_it_be(:token) { build(:personal_access_token) } let_it_be(:token) { build_stubbed(:personal_access_token) }
it { is_expected.to be_disallowed(:read_token) } it { is_expected.to be_disallowed(:read_token) }
it { is_expected.to be_disallowed(:revoke_token) } it { is_expected.to be_disallowed(:revoke_token) }
......
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