application_controller_spec.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ApplicationController do
  4. controller do
  5. def success
  6. head 200
  7. end
  8. def routing_error
  9. raise ActionController::RoutingError, ''
  10. end
  11. def record_not_found
  12. raise ActiveRecord::RecordNotFound, ''
  13. end
  14. def invalid_authenticity_token
  15. raise ActionController::InvalidAuthenticityToken, ''
  16. end
  17. end
  18. shared_examples 'respond_with_error' do |code|
  19. it "returns http #{code} for http" do
  20. subject
  21. expect(response).to have_http_status(code)
  22. end
  23. it 'renders template for http' do
  24. expect(subject).to render_template("errors/#{code}", layout: 'error')
  25. end
  26. end
  27. context 'with a forgery' do
  28. subject do
  29. ActionController::Base.allow_forgery_protection = true
  30. routes.draw { post 'success' => 'anonymous#success' }
  31. post 'success'
  32. end
  33. include_examples 'respond_with_error', 422
  34. end
  35. describe 'helper_method :current_account' do
  36. it 'returns nil if not signed in' do
  37. expect(controller.view_context.current_account).to be_nil
  38. end
  39. it 'returns account if signed in' do
  40. account = Fabricate(:account)
  41. sign_in(account.user)
  42. expect(controller.view_context.current_account).to eq account
  43. end
  44. end
  45. describe 'helper_method :single_user_mode?' do
  46. it 'returns false if it is in single_user_mode but there is no account' do
  47. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
  48. expect(controller.view_context.single_user_mode?).to be false
  49. end
  50. it 'returns false if there is an account but it is not in single_user_mode' do
  51. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false)
  52. Fabricate(:account)
  53. expect(controller.view_context.single_user_mode?).to be false
  54. end
  55. it 'returns true if it is in single_user_mode and there is an account' do
  56. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
  57. Fabricate(:account)
  58. expect(controller.view_context.single_user_mode?).to be true
  59. end
  60. end
  61. describe 'helper_method :current_theme' do
  62. it 'returns "default" when theme wasn\'t changed in admin settings' do
  63. allow(Setting).to receive(:default_settings).and_return({ 'theme' => 'default' })
  64. expect(controller.view_context.current_theme).to eq 'default'
  65. end
  66. it 'returns instances\'s theme when user is not signed in' do
  67. allow(Setting).to receive(:[]).with('theme').and_return 'contrast'
  68. expect(controller.view_context.current_theme).to eq 'contrast'
  69. end
  70. it 'returns instances\'s default theme when user didn\'t set theme' do
  71. current_user = Fabricate(:user)
  72. current_user.settings.update(theme: 'contrast', noindex: false)
  73. current_user.save
  74. sign_in current_user
  75. expect(controller.view_context.current_theme).to eq 'contrast'
  76. end
  77. it 'returns user\'s theme when it is set' do
  78. current_user = Fabricate(:user)
  79. current_user.settings.update(theme: 'mastodon-light')
  80. current_user.save
  81. sign_in current_user
  82. expect(controller.view_context.current_theme).to eq 'mastodon-light'
  83. end
  84. end
  85. context 'with ActionController::RoutingError' do
  86. subject do
  87. routes.draw { get 'routing_error' => 'anonymous#routing_error' }
  88. get 'routing_error'
  89. end
  90. include_examples 'respond_with_error', 404
  91. end
  92. context 'with ActiveRecord::RecordNotFound' do
  93. subject do
  94. routes.draw { get 'record_not_found' => 'anonymous#record_not_found' }
  95. get 'record_not_found'
  96. end
  97. include_examples 'respond_with_error', 404
  98. end
  99. context 'with ActionController::InvalidAuthenticityToken' do
  100. subject do
  101. routes.draw { get 'invalid_authenticity_token' => 'anonymous#invalid_authenticity_token' }
  102. get 'invalid_authenticity_token'
  103. end
  104. include_examples 'respond_with_error', 422
  105. end
  106. describe 'before_action :check_suspension' do
  107. before do
  108. routes.draw { get 'success' => 'anonymous#success' }
  109. end
  110. it 'does nothing if not signed in' do
  111. get 'success'
  112. expect(response).to have_http_status(200)
  113. end
  114. it 'does nothing if user who signed in is not suspended' do
  115. sign_in(Fabricate(:account, suspended: false).user)
  116. get 'success'
  117. expect(response).to have_http_status(200)
  118. end
  119. it 'redirects to account status page' do
  120. sign_in(Fabricate(:account, suspended: true).user)
  121. get 'success'
  122. expect(response).to redirect_to(edit_user_registration_path)
  123. end
  124. end
  125. describe 'raise_not_found' do
  126. it 'raises error' do
  127. controller.params[:unmatched_route] = 'unmatched'
  128. expect { controller.raise_not_found }.to raise_error(ActionController::RoutingError, 'No route matches unmatched')
  129. end
  130. end
  131. describe 'forbidden' do
  132. controller do
  133. def route_forbidden
  134. forbidden
  135. end
  136. end
  137. subject do
  138. routes.draw { get 'route_forbidden' => 'anonymous#route_forbidden' }
  139. get 'route_forbidden'
  140. end
  141. include_examples 'respond_with_error', 403
  142. end
  143. describe 'not_found' do
  144. controller do
  145. def route_not_found
  146. not_found
  147. end
  148. end
  149. subject do
  150. routes.draw { get 'route_not_found' => 'anonymous#route_not_found' }
  151. get 'route_not_found'
  152. end
  153. include_examples 'respond_with_error', 404
  154. end
  155. describe 'gone' do
  156. controller do
  157. def route_gone
  158. gone
  159. end
  160. end
  161. subject do
  162. routes.draw { get 'route_gone' => 'anonymous#route_gone' }
  163. get 'route_gone'
  164. end
  165. include_examples 'respond_with_error', 410
  166. end
  167. describe 'unprocessable_entity' do
  168. controller do
  169. def route_unprocessable_entity
  170. unprocessable_entity
  171. end
  172. end
  173. subject do
  174. routes.draw { get 'route_unprocessable_entity' => 'anonymous#route_unprocessable_entity' }
  175. get 'route_unprocessable_entity'
  176. end
  177. include_examples 'respond_with_error', 422
  178. end
  179. describe 'cache_collection' do
  180. subject do
  181. Class.new(ApplicationController) do
  182. public :cache_collection
  183. end
  184. end
  185. shared_examples 'receives :with_includes' do |fabricator, klass|
  186. it 'uses raw if it is not an ActiveRecord::Relation' do
  187. record = Fabricate(fabricator)
  188. expect(subject.new.cache_collection([record], klass)).to eq [record]
  189. end
  190. end
  191. shared_examples 'cacheable' do |fabricator, klass|
  192. include_examples 'receives :with_includes', fabricator, klass
  193. it 'calls cache_ids of raw if it is an ActiveRecord::Relation' do
  194. record = Fabricate(fabricator)
  195. relation = klass.none
  196. allow(relation).to receive(:cache_ids).and_return([record])
  197. expect(subject.new.cache_collection(relation, klass)).to eq [record]
  198. end
  199. end
  200. it 'returns raw unless class responds to :with_includes' do
  201. raw = Object.new
  202. expect(subject.new.cache_collection(raw, Object)).to eq raw
  203. end
  204. context 'with a Status' do
  205. include_examples 'cacheable', :status, Status
  206. end
  207. end
  208. end