apps_controller_spec.rb 2.1 KB

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