You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.3 KiB

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