token_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Obtaining OAuth Tokens' do
  4. describe 'POST /oauth/token' do
  5. subject do
  6. post '/oauth/token', params: params
  7. end
  8. let(:application) do
  9. Fabricate(:application, scopes: 'read write follow', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob')
  10. end
  11. let(:params) do
  12. {
  13. grant_type: grant_type,
  14. client_id: application.uid,
  15. client_secret: application.secret,
  16. redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
  17. code: code,
  18. scope: scope,
  19. }
  20. end
  21. context "with grant_type 'authorization_code'" do
  22. let(:grant_type) { 'authorization_code' }
  23. let(:code) do
  24. access_grant = Fabricate(:access_grant, application: application, redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', scopes: 'read write')
  25. access_grant.plaintext_token
  26. end
  27. shared_examples 'returns originally requested scopes' do
  28. it 'returns all scopes requested for the given code' do
  29. subject
  30. expect(response).to have_http_status(200)
  31. expect(response.parsed_body[:scope]).to eq 'read write'
  32. end
  33. end
  34. context 'with no scopes specified' do
  35. let(:scope) { nil }
  36. include_examples 'returns originally requested scopes'
  37. end
  38. context 'with scopes specified' do
  39. context 'when the scopes were requested for this code' do
  40. let(:scope) { 'write' }
  41. include_examples 'returns originally requested scopes'
  42. end
  43. context 'when the scope was not requested for the code' do
  44. let(:scope) { 'follow' }
  45. include_examples 'returns originally requested scopes'
  46. end
  47. context 'when the scope does not belong to the application' do
  48. let(:scope) { 'push' }
  49. include_examples 'returns originally requested scopes'
  50. end
  51. end
  52. end
  53. context "with grant_type 'client_credentials'" do
  54. let(:grant_type) { 'client_credentials' }
  55. let(:code) { nil }
  56. context 'with no scopes specified' do
  57. let(:scope) { nil }
  58. it 'returns only the default scope' do
  59. subject
  60. expect(response).to have_http_status(200)
  61. expect(response.parsed_body[:scope]).to eq('read')
  62. end
  63. end
  64. context 'with scopes specified' do
  65. context 'when the scopes belong to the application' do
  66. let(:scope) { 'read write' }
  67. it 'returns all the requested scopes' do
  68. subject
  69. expect(response).to have_http_status(200)
  70. expect(response.parsed_body[:scope]).to eq 'read write'
  71. end
  72. end
  73. context 'when some scopes do not belong to the application' do
  74. let(:scope) { 'read write push' }
  75. it 'returns an error' do
  76. subject
  77. expect(response).to have_http_status(400)
  78. end
  79. end
  80. end
  81. end
  82. end
  83. end