Commit 1584c0d1 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'mw/okr-yf2-q3-credentials-controller-spec-improvements' into 'master'

Reduce database writes in credentials_controller specs

See merge request gitlab-org/gitlab!42080
parents 6df05e84 ae717cfe
......@@ -3,10 +3,14 @@
require 'spec_helper'
RSpec.describe Admin::CredentialsController do
let_it_be(:admin) { create(:admin) }
let_it_be(:user) { create(:user) }
let_it_be(:personal_access_token) { create(:personal_access_token, user: user) }
describe 'GET #index' do
context 'admin user' do
before do
sign_in(create(:admin))
sign_in(admin)
end
context 'when `credentials_inventory` feature is enabled' do
......@@ -26,13 +30,13 @@ RSpec.describe Admin::CredentialsController do
end
describe 'filtering by type of credential' do
let_it_be(:personal_access_tokens) { create_list(:personal_access_token, 2) }
let_it_be(:personal_access_tokens) { create_list(:personal_access_token, 2, user: user) }
shared_examples_for 'filtering by `personal_access_tokens`' do
specify do
get :index, params: params
expect(assigns(:credentials)).to match_array(personal_access_tokens)
expect(assigns(:credentials)).to match_array(user.personal_access_tokens)
end
end
......@@ -56,7 +60,7 @@ RSpec.describe Admin::CredentialsController do
context 'credential type specified as `ssh_keys`' do
it 'filters by ssh keys' do
ssh_keys = create_list(:personal_key, 2)
ssh_keys = create_list(:personal_key, 2, user: user)
get :index, params: { filter: 'ssh_keys' }
......@@ -81,7 +85,7 @@ RSpec.describe Admin::CredentialsController do
context 'non-admin user' do
before do
sign_in(create(:user))
sign_in(user)
end
it 'returns 404' do
......@@ -94,10 +98,8 @@ RSpec.describe Admin::CredentialsController do
describe 'PUT #revoke' do
context 'admin user' do
let_it_be(:current_user) { create(:admin) }
before do
sign_in(current_user)
sign_in(admin)
end
context 'when `credentials_inventory` feature is enabled' do
......@@ -115,11 +117,9 @@ RSpec.describe Admin::CredentialsController do
describe 'with an existing personal access token' do
context 'does not have permissions to revoke the credential' do
let_it_be(:personal_access_token) { create(:personal_access_token) }
before do
expect(Ability).to receive(:allowed?).with(current_user, :log_in, :global) { true }
expect(Ability).to receive(:allowed?).with(current_user, :revoke_token, personal_access_token) { false }
allow(Ability).to receive(:allowed?).with(admin, :log_in, :global) { true }
allow(Ability).to receive(:allowed?).with(admin, :revoke_token, personal_access_token) { false }
end
it 'returns the flash error message' do
......@@ -131,7 +131,7 @@ RSpec.describe Admin::CredentialsController do
end
context 'personal access token is already revoked' do
let_it_be(:personal_access_token) { create(:personal_access_token, revoked: true) }
let_it_be(:personal_access_token) { create(:personal_access_token, revoked: true, user: user) }
it 'returns the flash success message' do
put :revoke, params: { id: personal_access_token.id }
......@@ -142,7 +142,7 @@ RSpec.describe Admin::CredentialsController do
end
context 'personal access token is already expired' do
let_it_be(:personal_access_token) { create(:personal_access_token, expires_at: 5.days.ago) }
let_it_be(:personal_access_token) { create(:personal_access_token, expires_at: 5.days.ago, user: user) }
it 'returns the flash success message' do
put :revoke, params: { id: personal_access_token.id }
......@@ -153,8 +153,6 @@ RSpec.describe Admin::CredentialsController do
end
context 'personal access token is not revoked or expired' do
let_it_be(:personal_access_token) { create(:personal_access_token) }
it 'returns the flash success message' do
put :revoke, params: { id: personal_access_token.id }
......@@ -166,8 +164,6 @@ RSpec.describe Admin::CredentialsController do
end
context 'when `credentials_inventory` feature is disabled' do
let_it_be(:personal_access_token) { create(:personal_access_token) }
before do
stub_licensed_features(credentials_inventory: false)
end
......@@ -181,10 +177,8 @@ RSpec.describe Admin::CredentialsController do
end
context 'non-admin user' do
let_it_be(:personal_access_token) { create(:personal_access_token) }
before do
sign_in(create(:user))
sign_in(user)
end
it 'returns 404' do
......
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