applications_controller_spec.rb 4.4 KB

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