authorizations_controller_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Oauth::AuthorizationsController, type: :controller do
  4. render_views
  5. let(:app) { Doorkeeper::Application.create!(name: 'test', redirect_uri: 'http://localhost/', scopes: 'read') }
  6. describe 'GET #new' do
  7. subject do
  8. get :new, params: { client_id: app.uid, response_type: 'code', redirect_uri: 'http://localhost/', scope: 'read' }
  9. end
  10. shared_examples 'stores location for user' do
  11. it 'stores location for user' do
  12. subject
  13. expect(controller.stored_location_for(:user)).to eq "/oauth/authorize?client_id=#{app.uid}&redirect_uri=http%3A%2F%2Flocalhost%2F&response_type=code&scope=read"
  14. end
  15. end
  16. context 'when signed in' do
  17. let!(:user) { Fabricate(:user) }
  18. before do
  19. sign_in user, scope: :user
  20. end
  21. it 'returns http success' do
  22. subject
  23. expect(response).to have_http_status(200)
  24. end
  25. it 'gives options to authorize and deny' do
  26. subject
  27. expect(response.body).to match(/Authorize/)
  28. end
  29. include_examples 'stores location for user'
  30. context 'when app is already authorized' do
  31. before do
  32. Doorkeeper::AccessToken.find_or_create_for(
  33. application: app,
  34. resource_owner: user.id,
  35. scopes: app.scopes,
  36. expires_in: Doorkeeper.configuration.access_token_expires_in,
  37. use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?
  38. )
  39. end
  40. it 'redirects to callback' do
  41. subject
  42. expect(response).to redirect_to(/\A#{app.redirect_uri}/)
  43. end
  44. it 'does not redirect to callback with force_login=true' do
  45. get :new, params: { client_id: app.uid, response_type: 'code', redirect_uri: 'http://localhost/', scope: 'read', force_login: 'true' }
  46. expect(response.body).to match(/Authorize/)
  47. end
  48. end
  49. end
  50. context 'when not signed in' do
  51. it 'redirects' do
  52. subject
  53. expect(response).to redirect_to '/auth/sign_in'
  54. end
  55. include_examples 'stores location for user'
  56. end
  57. end
  58. end