1
0

oauth_spec.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe 'Using OAuth from an external app' do
  4. let(:client_app) { Doorkeeper::Application.create!(name: 'test', redirect_uri: 'http://localhost/', scopes: 'read') }
  5. context 'when the user is already logged in' do
  6. let!(:user) { Fabricate(:user) }
  7. before do
  8. sign_in user, scope: :user
  9. end
  10. it 'when accepting the authorization request' do
  11. params = { client_id: client_app.uid, response_type: 'code', redirect_uri: client_app.redirect_uri, scope: 'read' }
  12. visit "/oauth/authorize?#{params.to_query}"
  13. # It presents the user with an authorization page
  14. expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
  15. # Upon authorizing, it redirects to the apps' callback URL
  16. click_on I18n.t('doorkeeper.authorizations.buttons.authorize')
  17. expect(page).to have_current_path(/\A#{client_app.redirect_uri}/, url: true)
  18. # It grants the app access to the account
  19. expect(Doorkeeper::AccessGrant.exists?(application: client_app, resource_owner_id: user.id)).to be true
  20. end
  21. it 'when rejecting the authorization request' do
  22. params = { client_id: client_app.uid, response_type: 'code', redirect_uri: client_app.redirect_uri, scope: 'read' }
  23. visit "/oauth/authorize?#{params.to_query}"
  24. # It presents the user with an authorization page
  25. expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.deny'))
  26. # Upon denying, it redirects to the apps' callback URL
  27. click_on I18n.t('doorkeeper.authorizations.buttons.deny')
  28. expect(page).to have_current_path(/\A#{client_app.redirect_uri}/, url: true)
  29. # It does not grant the app access to the account
  30. expect(Doorkeeper::AccessGrant.exists?(application: client_app, resource_owner_id: user.id)).to be false
  31. end
  32. end
  33. context 'when the user is not already logged in' do
  34. let(:email) { 'test@example.com' }
  35. let(:password) { 'testpassword' }
  36. let(:user) { Fabricate(:user, email: email, password: password) }
  37. before do
  38. user.confirm!
  39. user.approve!
  40. end
  41. it 'when accepting the authorization request' do
  42. params = { client_id: client_app.uid, response_type: 'code', redirect_uri: client_app.redirect_uri, scope: 'read' }
  43. visit "/oauth/authorize?#{params.to_query}"
  44. # It presents the user with a log-in page
  45. expect(page).to have_content(I18n.t('auth.login'))
  46. # Failing to log-in presents the form again
  47. fill_in 'user_email', with: email
  48. fill_in 'user_password', with: 'wrong password'
  49. click_on I18n.t('auth.login')
  50. expect(page).to have_content(I18n.t('auth.login'))
  51. # Logging in redirects to an authorization page
  52. fill_in 'user_email', with: email
  53. fill_in 'user_password', with: password
  54. click_on I18n.t('auth.login')
  55. expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
  56. # Upon authorizing, it redirects to the apps' callback URL
  57. click_on I18n.t('doorkeeper.authorizations.buttons.authorize')
  58. expect(page).to have_current_path(/\A#{client_app.redirect_uri}/, url: true)
  59. # It grants the app access to the account
  60. expect(Doorkeeper::AccessGrant.exists?(application: client_app, resource_owner_id: user.id)).to be true
  61. end
  62. it 'when rejecting the authorization request' do
  63. params = { client_id: client_app.uid, response_type: 'code', redirect_uri: client_app.redirect_uri, scope: 'read' }
  64. visit "/oauth/authorize?#{params.to_query}"
  65. # It presents the user with a log-in page
  66. expect(page).to have_content(I18n.t('auth.login'))
  67. # Failing to log-in presents the form again
  68. fill_in 'user_email', with: email
  69. fill_in 'user_password', with: 'wrong password'
  70. click_on I18n.t('auth.login')
  71. expect(page).to have_content(I18n.t('auth.login'))
  72. # Logging in redirects to an authorization page
  73. fill_in 'user_email', with: email
  74. fill_in 'user_password', with: password
  75. click_on I18n.t('auth.login')
  76. expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
  77. # Upon denying, it redirects to the apps' callback URL
  78. click_on I18n.t('doorkeeper.authorizations.buttons.deny')
  79. expect(page).to have_current_path(/\A#{client_app.redirect_uri}/, url: true)
  80. # It does not grant the app access to the account
  81. expect(Doorkeeper::AccessGrant.exists?(application: client_app, resource_owner_id: user.id)).to be false
  82. end
  83. context 'when the user has set up TOTP' do
  84. let(:user) { Fabricate(:user, email: email, password: password, otp_required_for_login: true, otp_secret: User.generate_otp_secret(32)) }
  85. it 'when accepting the authorization request' do
  86. params = { client_id: client_app.uid, response_type: 'code', redirect_uri: client_app.redirect_uri, scope: 'read' }
  87. visit "/oauth/authorize?#{params.to_query}"
  88. # It presents the user with a log-in page
  89. expect(page).to have_content(I18n.t('auth.login'))
  90. # Failing to log-in presents the form again
  91. fill_in 'user_email', with: email
  92. fill_in 'user_password', with: 'wrong password'
  93. click_on I18n.t('auth.login')
  94. expect(page).to have_content(I18n.t('auth.login'))
  95. # Logging in redirects to a two-factor authentication page
  96. fill_in 'user_email', with: email
  97. fill_in 'user_password', with: password
  98. click_on I18n.t('auth.login')
  99. expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
  100. # Filling in an incorrect two-factor authentication code presents the form again
  101. fill_in 'user_otp_attempt', with: 'wrong'
  102. click_on I18n.t('auth.login')
  103. expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
  104. # Filling in the correct TOTP code redirects to an app authorization page
  105. fill_in 'user_otp_attempt', with: user.current_otp
  106. click_on I18n.t('auth.login')
  107. expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
  108. # Upon authorizing, it redirects to the apps' callback URL
  109. click_on I18n.t('doorkeeper.authorizations.buttons.authorize')
  110. expect(page).to have_current_path(/\A#{client_app.redirect_uri}/, url: true)
  111. # It grants the app access to the account
  112. expect(Doorkeeper::AccessGrant.exists?(application: client_app, resource_owner_id: user.id)).to be true
  113. end
  114. it 'when rejecting the authorization request' do
  115. params = { client_id: client_app.uid, response_type: 'code', redirect_uri: client_app.redirect_uri, scope: 'read' }
  116. visit "/oauth/authorize?#{params.to_query}"
  117. # It presents the user with a log-in page
  118. expect(page).to have_content(I18n.t('auth.login'))
  119. # Failing to log-in presents the form again
  120. fill_in 'user_email', with: email
  121. fill_in 'user_password', with: 'wrong password'
  122. click_on I18n.t('auth.login')
  123. expect(page).to have_content(I18n.t('auth.login'))
  124. # Logging in redirects to a two-factor authentication page
  125. fill_in 'user_email', with: email
  126. fill_in 'user_password', with: password
  127. click_on I18n.t('auth.login')
  128. expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
  129. # Filling in an incorrect two-factor authentication code presents the form again
  130. fill_in 'user_otp_attempt', with: 'wrong'
  131. click_on I18n.t('auth.login')
  132. expect(page).to have_content(I18n.t('simple_form.hints.sessions.otp'))
  133. # Filling in the correct TOTP code redirects to an app authorization page
  134. fill_in 'user_otp_attempt', with: user.current_otp
  135. click_on I18n.t('auth.login')
  136. expect(page).to have_content(I18n.t('doorkeeper.authorizations.buttons.authorize'))
  137. # Upon denying, it redirects to the apps' callback URL
  138. click_on I18n.t('doorkeeper.authorizations.buttons.deny')
  139. expect(page).to have_current_path(/\A#{client_app.redirect_uri}/, url: true)
  140. # It does not grant the app access to the account
  141. expect(Doorkeeper::AccessGrant.exists?(application: client_app, resource_owner_id: user.id)).to be false
  142. end
  143. end
  144. # TODO: external auth
  145. end
  146. end