2023-04-25 03:06:58 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-05-10 22:36:09 +10:00
|
|
|
describe 'Using OAuth from an external app', :js, :streaming do
|
2024-05-10 19:43:59 +10:00
|
|
|
let(:client_app) { Doorkeeper::Application.create!(name: 'test', redirect_uri: about_url(host: Rails.application.config.x.local_domain), scopes: 'read') }
|
2023-04-25 03:06:58 +10:00
|
|
|
|
|
|
|
context 'when the user is already logged in' do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
2024-04-06 03:52:05 +11:00
|
|
|
visit new_user_session_path
|
|
|
|
fill_in_auth_details(user.email, user.password)
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-01-04 20:20:32 +11:00
|
|
|
click_on I18n.t('doorkeeper.authorizations.buttons.authorize')
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-01-04 20:20:32 +11:00
|
|
|
click_on I18n.t('doorkeeper.authorizations.buttons.deny')
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-01-16 05:04:58 +11:00
|
|
|
user.mark_email_as_confirmed!
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, 'wrong password')
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('auth.login'))
|
|
|
|
|
|
|
|
# Logging in redirects to an authorization page
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, password)
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
|
|
|
|
|
|
|
|
# Upon authorizing, it redirects to the apps' callback URL
|
2024-01-04 20:20:32 +11:00
|
|
|
click_on I18n.t('doorkeeper.authorizations.buttons.authorize')
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, 'wrong password')
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('auth.login'))
|
|
|
|
|
|
|
|
# Logging in redirects to an authorization page
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, password)
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
|
|
|
|
|
|
|
|
# Upon denying, it redirects to the apps' callback URL
|
2024-01-04 20:20:32 +11:00
|
|
|
click_on I18n.t('doorkeeper.authorizations.buttons.deny')
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, 'wrong password')
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('auth.login'))
|
|
|
|
|
|
|
|
# Logging in redirects to a two-factor authentication page
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, password)
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
|
|
|
|
|
|
|
|
# Filling in an incorrect two-factor authentication code presents the form again
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_otp_details('wrong')
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
|
|
|
|
|
|
|
|
# Filling in the correct TOTP code redirects to an app authorization page
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_otp_details(user.current_otp)
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
|
|
|
|
|
|
|
|
# Upon authorizing, it redirects to the apps' callback URL
|
2024-01-04 20:20:32 +11:00
|
|
|
click_on I18n.t('doorkeeper.authorizations.buttons.authorize')
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, 'wrong password')
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('auth.login'))
|
|
|
|
|
|
|
|
# Logging in redirects to a two-factor authentication page
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_auth_details(email, password)
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
|
|
|
|
|
|
|
|
# Filling in an incorrect two-factor authentication code presents the form again
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_otp_details('wrong')
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
|
|
|
|
|
|
|
|
# Filling in the correct TOTP code redirects to an app authorization page
|
2024-03-15 00:42:59 +11:00
|
|
|
fill_in_otp_details(user.current_otp)
|
2023-04-25 03:06:58 +10:00
|
|
|
expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
|
|
|
|
|
|
|
|
# Upon denying, it redirects to the apps' callback URL
|
2024-01-04 20:20:32 +11:00
|
|
|
click_on I18n.t('doorkeeper.authorizations.buttons.deny')
|
2023-04-25 03:06:58 +10:00
|
|
|
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
|
2024-04-06 03:52:05 +11:00
|
|
|
# TODO: external auth
|
|
|
|
end
|
2023-04-25 03:06:58 +10:00
|
|
|
|
2024-04-06 03:52:05 +11:00
|
|
|
private
|
2024-03-15 00:42:59 +11:00
|
|
|
|
2024-04-06 03:52:05 +11:00
|
|
|
def fill_in_auth_details(email, password)
|
|
|
|
fill_in 'user_email', with: email
|
|
|
|
fill_in 'user_password', with: password
|
|
|
|
click_on I18n.t('auth.login')
|
|
|
|
end
|
2024-03-15 00:42:59 +11:00
|
|
|
|
2024-04-06 03:52:05 +11:00
|
|
|
def fill_in_otp_details(value)
|
|
|
|
fill_in 'user_otp_attempt', with: value
|
|
|
|
click_on I18n.t('auth.login')
|
2023-04-25 03:06:58 +10:00
|
|
|
end
|
|
|
|
end
|