applications_controller_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Settings::ApplicationsController do
  4. render_views
  5. let!(:user) { Fabricate(:user) }
  6. let!(:app) { Fabricate(:application, owner: user) }
  7. before do
  8. sign_in user, scope: :user
  9. end
  10. describe 'GET #index' do
  11. before do
  12. Fabricate(:application)
  13. get :index
  14. end
  15. it 'returns http success with private cache control headers', :aggregate_failures do
  16. expect(response).to have_http_status(200)
  17. expect(response.headers['Cache-Control']).to include('private, no-store')
  18. end
  19. end
  20. describe 'GET #show' do
  21. it 'returns http success' do
  22. get :show, params: { id: app.id }
  23. expect(response).to have_http_status(200)
  24. expect(assigns[:application]).to eql(app)
  25. end
  26. it 'returns 404 if you dont own app' do
  27. app.update!(owner: nil)
  28. get :show, params: { id: app.id }
  29. expect(response).to have_http_status 404
  30. end
  31. end
  32. describe 'GET #new' do
  33. it 'returns http success' do
  34. get :new
  35. expect(response).to have_http_status(200)
  36. end
  37. end
  38. describe 'POST #create' do
  39. context 'when success (passed scopes as a String)' do
  40. subject do
  41. post :create, params: {
  42. doorkeeper_application: {
  43. name: 'My New App',
  44. redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
  45. website: 'http://google.com',
  46. scopes: 'read write follow',
  47. },
  48. }
  49. end
  50. it 'creates an entry in the database', :aggregate_failures do
  51. expect { subject }.to change(Doorkeeper::Application, :count)
  52. expect(response).to redirect_to(settings_applications_path)
  53. end
  54. end
  55. context 'when success (passed scopes as an Array)' do
  56. subject do
  57. post :create, params: {
  58. doorkeeper_application: {
  59. name: 'My New App',
  60. redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
  61. website: 'http://google.com',
  62. scopes: %w(read write follow),
  63. },
  64. }
  65. end
  66. it 'creates an entry in the database', :aggregate_failures do
  67. expect { subject }.to change(Doorkeeper::Application, :count)
  68. expect(response).to redirect_to(settings_applications_path)
  69. end
  70. end
  71. context 'with failure request' do
  72. before do
  73. post :create, params: {
  74. doorkeeper_application: {
  75. name: '',
  76. redirect_uri: '',
  77. website: '',
  78. scopes: [],
  79. },
  80. }
  81. end
  82. it 'returns http success and renders form', :aggregate_failures do
  83. expect(response).to have_http_status(200)
  84. expect(response).to render_template(:new)
  85. end
  86. end
  87. end
  88. describe 'PATCH #update' do
  89. context 'when success' do
  90. subject do
  91. patch :update, params: {
  92. id: app.id,
  93. doorkeeper_application: opts,
  94. }
  95. response
  96. end
  97. let(:opts) do
  98. {
  99. website: 'https://foo.bar/',
  100. }
  101. end
  102. it 'updates existing application' do
  103. subject
  104. expect(app.reload.website).to eql(opts[:website])
  105. expect(response).to redirect_to(settings_application_path(app))
  106. end
  107. end
  108. context 'with failure request' do
  109. before do
  110. patch :update, params: {
  111. id: app.id,
  112. doorkeeper_application: {
  113. name: '',
  114. redirect_uri: '',
  115. website: '',
  116. scopes: [],
  117. },
  118. }
  119. end
  120. it 'returns http success and renders form', :aggregate_failures do
  121. expect(response).to have_http_status(200)
  122. expect(response).to render_template(:show)
  123. end
  124. end
  125. end
  126. describe 'destroy' do
  127. before do
  128. post :destroy, params: { id: app.id }
  129. end
  130. it 'redirects back to applications page and removes the app' do
  131. expect(response).to redirect_to(settings_applications_path)
  132. expect(Doorkeeper::Application.find_by(id: app.id)).to be_nil
  133. end
  134. end
  135. describe 'regenerate' do
  136. let(:token) { user.token_for_app(app) }
  137. it 'creates new token' do
  138. expect(token).to_not be_nil
  139. post :regenerate, params: { id: app.id }
  140. expect(user.token_for_app(app)).to_not eql(token)
  141. end
  142. end
  143. end