authorizations_controller_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Oauth::AuthorizationsController 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 'returns private cache control headers' do
  26. subject
  27. expect(response.headers['Cache-Control']).to include('private, no-store')
  28. end
  29. it 'gives options to authorize and deny' do
  30. subject
  31. expect(response.body).to match(/Authorize/)
  32. end
  33. include_examples 'stores location for user'
  34. context 'when app is already authorized' do
  35. before do
  36. Doorkeeper::AccessToken.find_or_create_for(
  37. application: app,
  38. resource_owner: user.id,
  39. scopes: app.scopes,
  40. expires_in: Doorkeeper.configuration.access_token_expires_in,
  41. use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?
  42. )
  43. end
  44. it 'redirects to callback' do
  45. subject
  46. expect(response).to redirect_to(/\A#{app.redirect_uri}/)
  47. end
  48. it 'does not redirect to callback with force_login=true' do
  49. get :new, params: { client_id: app.uid, response_type: 'code', redirect_uri: 'http://localhost/', scope: 'read', force_login: 'true' }
  50. expect(response.body).to match(/Authorize/)
  51. end
  52. end
  53. end
  54. context 'when not signed in' do
  55. it 'redirects' do
  56. subject
  57. expect(response).to redirect_to '/auth/sign_in'
  58. end
  59. include_examples 'stores location for user'
  60. end
  61. end
  62. end