blob: 522506870f647b2ec680b6d041118064caf8e249 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe PersonalAccessTokens::RotateService, feature_category: :system_access do
describe '#execute' do
let_it_be(:token, reload: true) { create(:personal_access_token) }
subject(:response) { described_class.new(token.user, token).execute }
it "rotates user's own token", :freeze_time do
expect(response).to be_success
new_token = response.payload[:personal_access_token]
expect(new_token.token).not_to eq(token.token)
expect(new_token.expires_at).to eq(Date.today + 1.week)
expect(new_token.user).to eq(token.user)
end
it 'revokes the previous token' do
expect { response }.to change { token.reload.revoked? }.from(false).to(true)
new_token = response.payload[:personal_access_token]
expect(new_token).not_to be_revoked
end
it 'saves the previous token as previous PAT attribute' do
response
new_token = response.payload[:personal_access_token]
expect(new_token.previous_personal_access_token).to eql(token)
end
context 'when user tries to rotate already revoked token' do
let_it_be(:token, reload: true) { create(:personal_access_token, :revoked) }
it 'returns an error' do
expect { response }.not_to change { token.reload.revoked? }.from(true)
expect(response).to be_error
expect(response.message).to eq('token already revoked')
end
end
context 'when revoking previous token fails' do
it 'returns an error' do
expect(token).to receive(:revoke!).and_return(false)
expect(response).to be_error
end
end
context 'when creating the new token fails' do
let(:error_message) { 'boom!' }
before do
allow_next_instance_of(PersonalAccessToken) do |token|
allow(token).to receive_message_chain(:errors, :full_messages, :to_sentence).and_return(error_message)
allow(token).to receive_message_chain(:errors, :clear)
allow(token).to receive_message_chain(:errors, :empty?).and_return(false)
end
end
it 'returns an error' do
expect(response).to be_error
expect(response.message).to eq(error_message)
end
it 'reverts the changes' do
expect { response }.not_to change { token.reload.revoked? }.from(false)
end
end
end
end
|