credentials_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Credentials' do
  4. describe 'GET /api/v1/apps/verify_credentials' do
  5. subject do
  6. get '/api/v1/apps/verify_credentials', headers: headers
  7. end
  8. context 'with an oauth token' do
  9. let(:application) { Fabricate(:application, scopes: 'read') }
  10. let(:token) { Fabricate(:accessible_access_token, application: application) }
  11. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  12. it 'returns the app information correctly', :aggregate_failures do
  13. subject
  14. expect(response).to have_http_status(200)
  15. expect(response.content_type)
  16. .to start_with('application/json')
  17. expect(response.parsed_body).to match(
  18. a_hash_including(
  19. id: token.application.id.to_s,
  20. name: token.application.name,
  21. website: token.application.website,
  22. scopes: token.application.scopes.map(&:to_s),
  23. redirect_uris: token.application.redirect_uris,
  24. # Deprecated properties as of 4.3:
  25. redirect_uri: token.application.redirect_uri.split.first,
  26. vapid_key: Rails.configuration.x.vapid_public_key
  27. )
  28. )
  29. end
  30. it 'does not expose the client_id or client_secret' do
  31. subject
  32. expect(response).to have_http_status(200)
  33. expect(response.content_type)
  34. .to start_with('application/json')
  35. expect(response.parsed_body)
  36. .to not_include(client_id: be_present)
  37. .and not_include(client_secret: be_present)
  38. .and not_include(client_secret_expires_at: be_present)
  39. end
  40. end
  41. context 'with a non-read scoped oauth token' do
  42. let(:application) { Fabricate(:application, scopes: 'admin:write') }
  43. let(:token) { Fabricate(:accessible_access_token, application: application) }
  44. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  45. it 'returns http success and returns app information' do
  46. subject
  47. expect(response).to have_http_status(200)
  48. expect(response.content_type)
  49. .to start_with('application/json')
  50. expect(response.parsed_body).to match(
  51. a_hash_including(
  52. id: token.application.id.to_s,
  53. name: token.application.name,
  54. website: token.application.website,
  55. scopes: token.application.scopes.map(&:to_s),
  56. redirect_uris: token.application.redirect_uris,
  57. # Deprecated properties as of 4.3:
  58. redirect_uri: token.application.redirect_uri.split.first,
  59. vapid_key: Rails.configuration.x.vapid_public_key
  60. )
  61. )
  62. end
  63. end
  64. context 'without an oauth token' do
  65. let(:headers) { {} }
  66. it 'returns http unauthorized' do
  67. subject
  68. expect(response).to have_http_status(401)
  69. expect(response.content_type)
  70. .to start_with('application/json')
  71. end
  72. end
  73. context 'with a revoked oauth token' do
  74. let(:application) { Fabricate(:application, scopes: 'read') }
  75. let(:token) { Fabricate(:accessible_access_token, application: application, revoked_at: DateTime.now.utc) }
  76. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  77. it 'returns http authorization error' do
  78. subject
  79. expect(response).to have_http_status(401)
  80. expect(response.content_type)
  81. .to start_with('application/json')
  82. end
  83. it 'returns the error in the json response' do
  84. subject
  85. expect(response.parsed_body).to match(
  86. a_hash_including(
  87. error: 'The access token was revoked'
  88. )
  89. )
  90. end
  91. end
  92. context 'with an invalid oauth token' do
  93. let(:application) { Fabricate(:application, scopes: 'read') }
  94. let(:token) { Fabricate(:accessible_access_token, application: application) }
  95. let(:headers) { { 'Authorization' => "Bearer #{token.token}-invalid" } }
  96. it 'returns http authorization error with json error' do
  97. subject
  98. expect(response).to have_http_status(401)
  99. expect(response.content_type)
  100. .to start_with('application/json')
  101. expect(response.parsed_body).to match(
  102. a_hash_including(
  103. error: 'The access token is invalid'
  104. )
  105. )
  106. end
  107. end
  108. end
  109. end