apps_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Apps' do
  4. describe 'POST /api/v1/apps' do
  5. subject do
  6. post '/api/v1/apps', params: params
  7. end
  8. let(:client_name) { 'Test app' }
  9. let(:scopes) { nil }
  10. let(:redirect_uris) { 'urn:ietf:wg:oauth:2.0:oob' }
  11. let(:website) { nil }
  12. let(:params) do
  13. {
  14. client_name: client_name,
  15. redirect_uris: redirect_uris,
  16. scopes: scopes,
  17. website: website,
  18. }
  19. end
  20. context 'with valid params' do
  21. it 'returns http success' do
  22. subject
  23. expect(response).to have_http_status(200)
  24. end
  25. it 'creates an OAuth app' do
  26. subject
  27. expect(Doorkeeper::Application.find_by(name: client_name)).to be_present
  28. end
  29. it 'returns client ID and client secret' do
  30. subject
  31. body = body_as_json
  32. expect(body[:client_id]).to be_present
  33. expect(body[:client_secret]).to be_present
  34. end
  35. end
  36. context 'with an unsupported scope' do
  37. let(:scopes) { 'hoge' }
  38. it 'returns http unprocessable entity' do
  39. subject
  40. expect(response).to have_http_status(422)
  41. end
  42. end
  43. context 'with many duplicate scopes' do
  44. let(:scopes) { (%w(read) * 40).join(' ') }
  45. it 'returns http success' do
  46. subject
  47. expect(response).to have_http_status(200)
  48. end
  49. it 'only saves the scope once' do
  50. subject
  51. expect(Doorkeeper::Application.find_by(name: client_name).scopes.to_s).to eq 'read'
  52. end
  53. end
  54. context 'with a too-long name' do
  55. let(:client_name) { 'hoge' * 20 }
  56. it 'returns http unprocessable entity' do
  57. subject
  58. expect(response).to have_http_status(422)
  59. end
  60. end
  61. context 'with a too-long website' do
  62. let(:website) { "https://foo.bar/#{'hoge' * 2_000}" }
  63. it 'returns http unprocessable entity' do
  64. subject
  65. expect(response).to have_http_status(422)
  66. end
  67. end
  68. context 'with a too-long redirect_uris' do
  69. let(:redirect_uris) { "https://foo.bar/#{'hoge' * 2_000}" }
  70. it 'returns http unprocessable entity' do
  71. subject
  72. expect(response).to have_http_status(422)
  73. end
  74. end
  75. context 'without required params' do
  76. let(:client_name) { '' }
  77. let(:redirect_uris) { '' }
  78. it 'returns http unprocessable entity' do
  79. subject
  80. expect(response).to have_http_status(422)
  81. end
  82. end
  83. end
  84. end