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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe PersonalAccessTokensFinder, :enable_admin_mode do
using RSpec::Parameterized::TableSyntax
describe '#execute' do
let(:admin) { create(:admin) }
let(:user) { create(:user) }
let(:other_user) { create(:user) }
let(:project_bot) { create(:user, :project_bot) }
let!(:tokens) do
{
active: create(:personal_access_token, user: user, name: 'my_pat_1'),
active_other: create(:personal_access_token, user: other_user, name: 'my_pat_2'),
expired: create(:personal_access_token, :expired, user: user),
revoked: create(:personal_access_token, :revoked, user: user),
active_impersonation: create(:personal_access_token, :impersonation, user: user),
expired_impersonation: create(:personal_access_token, :expired, :impersonation, user: user),
revoked_impersonation: create(:personal_access_token, :revoked, :impersonation, user: user),
bot: create(:personal_access_token, user: project_bot)
}
end
let(:params) { {} }
let(:current_user) { admin }
subject { described_class.new(params, current_user).execute }
describe 'by current user' do
context 'with no user' do
let(:current_user) { nil }
it 'returns all tokens' do
is_expected.to match_array(tokens.values)
end
end
context 'with admin' do
let(:current_user) { admin }
context 'when admin mode setting is disabled', :do_not_mock_admin_mode_setting do
it 'returns all tokens' do
is_expected.to match_array(tokens.values)
end
end
context 'when admin mode setting is enabled' do
context 'when in admin mode', :enable_admin_mode do
it 'returns all tokens' do
is_expected.to match_array(tokens.values)
end
end
context 'when not in admin mode' do
before do
allow_next_instance_of(Gitlab::Auth::CurrentUserMode) do |current_user_mode|
allow(current_user_mode).to receive(:admin_mode?).and_return(false)
end
end
it 'returns no tokens' do
is_expected.to be_empty
end
end
end
end
context 'when user can read user personal access tokens' do
let(:params) { { user: user } }
let(:current_user) { user }
it 'returns tokens of user' do
is_expected.to contain_exactly(*user.personal_access_tokens)
end
end
context 'when user can not read user personal access tokens' do
let(:params) { { user: other_user } }
let(:current_user) { user }
it 'returns no tokens' do
is_expected.to be_empty
end
end
end
describe 'by user' do
where(:by_user, :expected_tokens) do
nil | tokens.keys
ref(:user) | [:active, :expired, :revoked, :active_impersonation, :expired_impersonation, :revoked_impersonation]
ref(:other_user) | [:active_other]
ref(:admin) | []
end
with_them do
let(:params) { { user: by_user } }
it 'returns tokens by user' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by users' do
where(:by_users, :expected_tokens) do
nil | tokens.keys
lazy { [user] } | [:active, :expired, :revoked, :active_impersonation, :expired_impersonation, :revoked_impersonation]
lazy { [other_user] } | [:active_other]
lazy { [user, other_user] } | [:active, :active_other, :expired, :revoked, :active_impersonation, :expired_impersonation, :revoked_impersonation]
[] | []
end
with_them do
let(:params) { { users: by_users } }
it 'returns tokens by users' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by impersonation' do
where(:by_impersonation, :expected_tokens) do
nil | tokens.keys
true | [:active_impersonation, :expired_impersonation, :revoked_impersonation]
false | [:active, :active_other, :expired, :revoked, :bot]
'other' | tokens.keys
end
with_them do
let(:params) { { impersonation: by_impersonation } }
it 'returns tokens by impersonation' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by state' do
where(:by_state, :expected_tokens) do
nil | tokens.keys
'active' | [:active, :active_other, :active_impersonation, :bot]
'inactive' | [:expired, :revoked, :expired_impersonation, :revoked_impersonation]
'other' | tokens.keys
end
with_them do
let(:params) { { state: by_state } }
it 'returns tokens by state' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by owner type' do
where(:by_owner_type, :expected_tokens) do
nil | tokens.keys
'human' | [:active, :active_other, :expired, :revoked, :active_impersonation, :expired_impersonation, :revoked_impersonation]
'other' | tokens.keys
end
with_them do
let(:params) { { owner_type: by_owner_type } }
it 'returns tokens by owner type' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by revoked state' do
where(:by_revoked_state, :expected_tokens) do
nil | [:active, :active_other, :expired, :active_impersonation, :expired_impersonation, :bot]
true | [:revoked, :revoked_impersonation]
false | [:active, :active_other, :expired, :active_impersonation, :expired_impersonation, :bot]
end
with_them do
let(:params) { { revoked: by_revoked_state } }
it 'returns tokens by revoked state' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by created date' do
before do
tokens[:active_other].update!(created_at: 5.days.ago)
end
describe 'by created before' do
where(:by_created_before, :expected_tokens) do
6.days.ago | []
2.days.ago | [:active_other]
2.days.from_now | tokens.keys
end
with_them do
let(:params) { { created_before: by_created_before } }
it 'returns tokens by created before' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by created after' do
where(:by_created_after, :expected_tokens) do
6.days.ago | tokens.keys
2.days.ago | [:active, :expired, :revoked, :active_impersonation, :expired_impersonation, :revoked_impersonation, :bot]
2.days.from_now | []
end
with_them do
let(:params) { { created_after: by_created_after } }
it 'returns tokens by created before' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
end
describe 'by last used date' do
before do
PersonalAccessToken.update_all(last_used_at: Time.now)
tokens[:active_other].update!(last_used_at: 5.days.ago)
end
describe 'by last used before' do
where(:by_last_used_before, :expected_tokens) do
6.days.ago | []
2.days.ago | [:active_other]
2.days.from_now | tokens.keys
end
with_them do
let(:params) { { last_used_before: by_last_used_before } }
it 'returns tokens by last used before' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'by last used after' do
where(:by_last_used_after, :expected_tokens) do
6.days.ago | tokens.keys
2.days.ago | [:active, :expired, :revoked, :active_impersonation, :expired_impersonation, :revoked_impersonation, :bot]
2.days.from_now | []
end
with_them do
let(:params) { { last_used_after: by_last_used_after } }
it 'returns tokens by last used after' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
end
describe 'by search' do
where(:by_search, :expected_tokens) do
nil | tokens.keys
'my_pat' | [:active, :active_other]
'other' | []
end
with_them do
let(:params) { { search: by_search } }
it 'returns tokens by search' do
is_expected.to match_array(tokens.values_at(*expected_tokens))
end
end
end
describe 'sort' do
where(:sort, :expected_tokens) do
nil | tokens.keys
'id_asc' | [:active, :active_other, :expired, :revoked, :active_impersonation, :expired_impersonation, :revoked_impersonation, :bot]
'id_desc' | [:bot, :revoked_impersonation, :expired_impersonation, :active_impersonation, :revoked, :expired, :active_other, :active]
'other' | tokens.keys
end
with_them do
let(:params) { { sort: sort } }
it 'returns ordered tokens' do
expect(subject.map(&:id)).to eq(tokens.values_at(*expected_tokens).map(&:id))
end
end
end
describe 'delegates' do
subject { described_class.new(params, current_user) }
describe '#find_by_id' do
it 'returns token by id' do
expect(subject.find_by_id(tokens[:active].id)).to eq(tokens[:active])
end
end
describe '#find_by_token' do
it 'returns token by token' do
expect(subject.find_by_token(tokens[:active].token)).to eq(tokens[:active])
end
end
describe '#find' do
it 'returns token by id' do
expect(subject.find(tokens[:active].id)).to eq(tokens[:active])
end
end
end
end
end
|