Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
f45961aa
Unverified
Commit
f45961aa
authored
2 years ago
by
Claire
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add feature test for OAuth access grant (#24624)
parent
804aa8d5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
spec/features/oauth_spec.rb
+190
-0
190 additions, 0 deletions
spec/features/oauth_spec.rb
spec/rails_helper.rb
+1
-0
1 addition, 0 deletions
spec/rails_helper.rb
with
191 additions
and
0 deletions
spec/features/oauth_spec.rb
0 → 100644
+
190
−
0
View file @
f45961aa
# frozen_string_literal: true
require
'rails_helper'
describe
'Using OAuth from an external app'
do
let
(
:client_app
)
{
Doorkeeper
::
Application
.
create!
(
name:
'test'
,
redirect_uri:
'http://localhost/'
,
scopes:
'read'
)
}
context
'when the user is already logged in'
do
let!
(
:user
)
{
Fabricate
(
:user
)
}
before
do
sign_in
user
,
scope: :user
end
it
'when accepting the authorization request'
do
params
=
{
client_id:
client_app
.
uid
,
response_type:
'code'
,
redirect_uri:
client_app
.
redirect_uri
,
scope:
'read'
}
visit
"/oauth/authorize?
#{
params
.
to_query
}
"
# It presents the user with an authorization page
expect
(
page
).
to
have_content
(
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
))
# Upon authorizing, it redirects to the apps' callback URL
click_on
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
)
expect
(
page
).
to
have_current_path
(
/\A
#{
client_app
.
redirect_uri
}
/
,
url:
true
)
# It grants the app access to the account
expect
(
Doorkeeper
::
AccessGrant
.
exists?
(
application:
client_app
,
resource_owner_id:
user
.
id
)).
to
be
true
end
it
'when rejecting the authorization request'
do
params
=
{
client_id:
client_app
.
uid
,
response_type:
'code'
,
redirect_uri:
client_app
.
redirect_uri
,
scope:
'read'
}
visit
"/oauth/authorize?
#{
params
.
to_query
}
"
# It presents the user with an authorization page
expect
(
page
).
to
have_content
(
I18n
.
t
(
'doorkeeper.authorizations.buttons.deny'
))
# Upon denying, it redirects to the apps' callback URL
click_on
I18n
.
t
(
'doorkeeper.authorizations.buttons.deny'
)
expect
(
page
).
to
have_current_path
(
/\A
#{
client_app
.
redirect_uri
}
/
,
url:
true
)
# It does not grant the app access to the account
expect
(
Doorkeeper
::
AccessGrant
.
exists?
(
application:
client_app
,
resource_owner_id:
user
.
id
)).
to
be
false
end
end
context
'when the user is not already logged in'
do
let
(
:email
)
{
'test@example.com'
}
let
(
:password
)
{
'testpassword'
}
let
(
:user
)
{
Fabricate
(
:user
,
email:
email
,
password:
password
)
}
before
do
user
.
confirm!
user
.
approve!
end
it
'when accepting the authorization request'
do
params
=
{
client_id:
client_app
.
uid
,
response_type:
'code'
,
redirect_uri:
client_app
.
redirect_uri
,
scope:
'read'
}
visit
"/oauth/authorize?
#{
params
.
to_query
}
"
# It presents the user with a log-in page
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Failing to log-in presents the form again
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
'wrong password'
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Logging in redirects to an authorization page
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
password
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
))
# Upon authorizing, it redirects to the apps' callback URL
click_on
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
)
expect
(
page
).
to
have_current_path
(
/\A
#{
client_app
.
redirect_uri
}
/
,
url:
true
)
# It grants the app access to the account
expect
(
Doorkeeper
::
AccessGrant
.
exists?
(
application:
client_app
,
resource_owner_id:
user
.
id
)).
to
be
true
end
it
'when rejecting the authorization request'
do
params
=
{
client_id:
client_app
.
uid
,
response_type:
'code'
,
redirect_uri:
client_app
.
redirect_uri
,
scope:
'read'
}
visit
"/oauth/authorize?
#{
params
.
to_query
}
"
# It presents the user with a log-in page
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Failing to log-in presents the form again
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
'wrong password'
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Logging in redirects to an authorization page
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
password
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
))
# Upon denying, it redirects to the apps' callback URL
click_on
I18n
.
t
(
'doorkeeper.authorizations.buttons.deny'
)
expect
(
page
).
to
have_current_path
(
/\A
#{
client_app
.
redirect_uri
}
/
,
url:
true
)
# It does not grant the app access to the account
expect
(
Doorkeeper
::
AccessGrant
.
exists?
(
application:
client_app
,
resource_owner_id:
user
.
id
)).
to
be
false
end
context
'when the user has set up TOTP'
do
let
(
:user
)
{
Fabricate
(
:user
,
email:
email
,
password:
password
,
otp_required_for_login:
true
,
otp_secret:
User
.
generate_otp_secret
(
32
))
}
it
'when accepting the authorization request'
do
params
=
{
client_id:
client_app
.
uid
,
response_type:
'code'
,
redirect_uri:
client_app
.
redirect_uri
,
scope:
'read'
}
visit
"/oauth/authorize?
#{
params
.
to_query
}
"
# It presents the user with a log-in page
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Failing to log-in presents the form again
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
'wrong password'
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Logging in redirects to a two-factor authentication page
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
password
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'simple_form.hints.sessions.otp'
))
# Filling in an incorrect two-factor authentication code presents the form again
fill_in
'user_otp_attempt'
,
with:
'wrong'
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'simple_form.hints.sessions.otp'
))
# Filling in the correct TOTP code redirects to an app authorization page
fill_in
'user_otp_attempt'
,
with:
user
.
current_otp
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
))
# Upon authorizing, it redirects to the apps' callback URL
click_on
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
)
expect
(
page
).
to
have_current_path
(
/\A
#{
client_app
.
redirect_uri
}
/
,
url:
true
)
# It grants the app access to the account
expect
(
Doorkeeper
::
AccessGrant
.
exists?
(
application:
client_app
,
resource_owner_id:
user
.
id
)).
to
be
true
end
it
'when rejecting the authorization request'
do
params
=
{
client_id:
client_app
.
uid
,
response_type:
'code'
,
redirect_uri:
client_app
.
redirect_uri
,
scope:
'read'
}
visit
"/oauth/authorize?
#{
params
.
to_query
}
"
# It presents the user with a log-in page
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Failing to log-in presents the form again
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
'wrong password'
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'auth.login'
))
# Logging in redirects to a two-factor authentication page
fill_in
'user_email'
,
with:
email
fill_in
'user_password'
,
with:
password
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'simple_form.hints.sessions.otp'
))
# Filling in an incorrect two-factor authentication code presents the form again
fill_in
'user_otp_attempt'
,
with:
'wrong'
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'simple_form.hints.sessions.otp'
))
# Filling in the correct TOTP code redirects to an app authorization page
fill_in
'user_otp_attempt'
,
with:
user
.
current_otp
click_on
I18n
.
t
(
'auth.login'
)
expect
(
page
).
to
have_content
(
I18n
.
t
(
'doorkeeper.authorizations.buttons.authorize'
))
# Upon denying, it redirects to the apps' callback URL
click_on
I18n
.
t
(
'doorkeeper.authorizations.buttons.deny'
)
expect
(
page
).
to
have_current_path
(
/\A
#{
client_app
.
redirect_uri
}
/
,
url:
true
)
# It does not grant the app access to the account
expect
(
Doorkeeper
::
AccessGrant
.
exists?
(
application:
client_app
,
resource_owner_id:
user
.
id
)).
to
be
false
end
end
# TODO: external auth
end
end
This diff is collapsed.
Click to expand it.
spec/rails_helper.rb
+
1
−
0
View file @
f45961aa
...
...
@@ -44,6 +44,7 @@ RSpec.configure do |config|
config
.
include
Devise
::
Test
::
ControllerHelpers
,
type: :controller
config
.
include
Devise
::
Test
::
ControllerHelpers
,
type: :view
config
.
include
Devise
::
Test
::
IntegrationHelpers
,
type: :feature
config
.
include
Paperclip
::
Shoulda
::
Matchers
config
.
include
ActiveSupport
::
Testing
::
TimeHelpers
config
.
include
Chewy
::
Rspec
::
Helpers
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment