Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c657bad7
Commit
c657bad7
authored
Dec 08, 2020
by
Serena Fang
Committed by
Mark Chao
Dec 08, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add specs to audit PAT
Add success and fail specs for admin and non-admin
parent
d53fc7f5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
10 deletions
+42
-10
ee/spec/services/personal_access_tokens/create_service_audit_log_spec.rb
...s/personal_access_tokens/create_service_audit_log_spec.rb
+42
-10
No files found.
ee/spec/services/personal_access_tokens/create_service_audit_log_spec.rb
View file @
c657bad7
...
@@ -4,19 +4,51 @@ require 'spec_helper'
...
@@ -4,19 +4,51 @@ require 'spec_helper'
RSpec
.
describe
PersonalAccessTokens
::
CreateService
do
RSpec
.
describe
PersonalAccessTokens
::
CreateService
do
describe
'#execute'
do
describe
'#execute'
do
subject
{
service
.
execute
}
let_it_be
(
:user
)
{
create
(
:user
)
}
let
(
:params
)
{
{
name:
'admin-token'
,
impersonation:
true
,
scopes:
[
:api
],
expires_at:
Date
.
today
+
1
.
month
}
}
let
(
:user
)
{
create
(
:user
)
}
context
'when non-admin user'
do
let
(
:params
)
{
{
name:
'Test token'
,
impersonation:
true
,
scopes:
[
:api
],
expires_at:
Date
.
today
+
1
.
month
}
}
context
'when user creates their own token'
do
let
(
:service
)
{
described_class
.
new
(
current_user:
user
,
target_user:
user
,
params:
params
)
}
it
'creates audit logs with success message'
do
expect_to_log
(
user
,
user
,
/Created personal access token with id \d+/
)
it
'creates audit logs'
do
described_class
.
new
(
current_user:
user
,
target_user:
user
,
params:
params
).
execute
expect
(
::
AuditEventService
)
end
.
to
receive
(
:new
)
end
.
with
(
user
,
user
,
action: :custom
,
custom_message:
/Created personal access token with id \d+/
,
ip_address:
nil
)
.
and_call_original
subject
context
'when user attempts to create a token for a different user'
do
let
(
:other_user
)
{
create
(
:user
)
}
it
'creates audit logs with failure message'
do
expect_to_log
(
user
,
other_user
,
'Attempted to create personal access token but failed with message: Not permitted to create'
)
described_class
.
new
(
current_user:
user
,
target_user:
other_user
,
params:
params
).
execute
end
end
end
context
'when admin'
do
let
(
:admin
)
{
create
(
:user
,
:admin
)
}
it
'with admin mode enabled'
,
:enable_admin_mode
do
expect_to_log
(
admin
,
user
,
/Created personal access token with id \d+/
)
described_class
.
new
(
current_user:
admin
,
target_user:
user
,
params:
params
).
execute
end
context
'with admin mode disabled'
do
it
'creates audit logs with failure message'
do
expect_to_log
(
admin
,
user
,
'Attempted to create personal access token but failed with message: Not permitted to create'
)
described_class
.
new
(
current_user:
admin
,
target_user:
user
,
params:
params
).
execute
end
end
end
end
end
end
def
expect_to_log
(
current_user
,
target_user
,
message
)
expect
(
::
AuditEventService
).
to
receive
(
:new
)
.
with
(
current_user
,
target_user
,
action: :custom
,
custom_message:
message
,
ip_address:
nil
)
.
and_call_original
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment