debian-mirror-gitlab/spec/services/personal_access_tokens/revoke_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe PersonalAccessTokens::RevokeService, feature_category: :system_access do
2020-10-24 23:57:45 +05:30
shared_examples_for 'a successfully revoked token' do
it { expect(subject.success?).to be true }
it { expect(service.token.revoked?).to be true }
2022-08-27 11:52:29 +05:30
2021-01-29 00:20:46 +05:30
it 'logs the event' do
2023-03-04 22:38:38 +05:30
expect(Gitlab::AppLogger).to receive(:info).with(
class: described_class.to_s,
message: 'PAT Revoked',
revoked_by: revoked_by,
revoked_for: token.user.username,
token_id: token.id)
2021-01-29 00:20:46 +05:30
subject
end
2020-10-24 23:57:45 +05:30
end
shared_examples_for 'an unsuccessfully revoked token' do
it { expect(subject.success?).to be false }
it { expect(service.token.revoked?).to be false }
end
describe '#execute' do
subject { service.execute }
let(:service) { described_class.new(current_user, token: token) }
context 'when current_user is an administrator' do
2021-01-29 00:20:46 +05:30
context 'when admin mode is enabled', :enable_admin_mode do
let_it_be(:current_user) { create(:admin) }
let_it_be(:token) { create(:personal_access_token) }
2023-03-04 22:38:38 +05:30
it_behaves_like 'a successfully revoked token' do
let(:revoked_by) { current_user.username }
end
2021-01-29 00:20:46 +05:30
end
context 'when admin mode is disabled' do
let_it_be(:current_user) { create(:admin) }
let_it_be(:token) { create(:personal_access_token) }
2020-10-24 23:57:45 +05:30
2021-01-29 00:20:46 +05:30
it_behaves_like 'an unsuccessfully revoked token'
end
2020-10-24 23:57:45 +05:30
end
context 'when current_user is not an administrator' do
let_it_be(:current_user) { create(:user) }
context 'token belongs to a different user' do
let_it_be(:token) { create(:personal_access_token) }
it_behaves_like 'an unsuccessfully revoked token'
end
context 'token belongs to current_user' do
let_it_be(:token) { create(:personal_access_token, user: current_user) }
2023-03-04 22:38:38 +05:30
it_behaves_like 'a successfully revoked token' do
let(:revoked_by) { current_user.username }
end
end
end
context 'when source' do
let(:service) { described_class.new(nil, token: token, source: source) }
let_it_be(:current_user) { nil }
context 'when source is valid' do
2023-03-17 16:20:25 +05:30
let_it_be(:source) { :secret_detection }
2023-03-04 22:38:38 +05:30
let_it_be(:token) { create(:personal_access_token) }
it_behaves_like 'a successfully revoked token' do
2023-03-17 16:20:25 +05:30
let(:revoked_by) { :secret_detection }
2023-03-04 22:38:38 +05:30
end
end
context 'when source is invalid' do
2023-03-17 16:20:25 +05:30
let_it_be(:source) { :external_request }
2023-03-04 22:38:38 +05:30
let_it_be(:token) { create(:personal_access_token) }
2023-03-17 16:20:25 +05:30
it 'raises ArgumentError' do
expect { subject }.to raise_error ArgumentError
end
2023-03-04 22:38:38 +05:30
end
context 'when source is missing' do
let_it_be(:source) { nil }
let_it_be(:token) { create(:personal_access_token) }
2023-03-17 16:20:25 +05:30
it 'raises ArgumentError' do
expect { subject }.to raise_error ArgumentError
end
2020-10-24 23:57:45 +05:30
end
end
end
end