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.

342 lines
9.2 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ApplicationController, type: :controller 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. is_expected.to render_template("errors/#{code}", layout: 'error')
  25. end
  26. end
  27. context '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(Fabricate(:user, account: account))
  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 eq 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 eq 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 eq 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. sign_in current_user
  73. allow(Setting).to receive(:[]).with('theme').and_return 'contrast'
  74. allow(Setting).to receive(:[]).with('noindex').and_return false
  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['theme'] = 'mastodon-light'
  80. sign_in current_user
  81. allow(Setting).to receive(:[]).with('theme').and_return 'contrast'
  82. expect(controller.view_context.current_theme).to eq 'mastodon-light'
  83. end
  84. end
  85. context '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 '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 '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 :store_current_location' do
  107. it 'stores location for user if it is not devise controller' do
  108. routes.draw { get 'success' => 'anonymous#success' }
  109. get 'success'
  110. expect(controller.stored_location_for(:user)).to eq '/success'
  111. end
  112. context do
  113. controller Devise::SessionsController do
  114. end
  115. it 'does not store location for user if it is devise controller' do
  116. @request.env["devise.mapping"] = Devise.mappings[:user]
  117. get 'create'
  118. expect(controller.stored_location_for(:user)).to be_nil
  119. end
  120. end
  121. end
  122. describe 'before_action :check_suspension' do
  123. before do
  124. routes.draw { get 'success' => 'anonymous#success' }
  125. end
  126. it 'does nothing if not signed in' do
  127. get 'success'
  128. expect(response).to have_http_status(200)
  129. end
  130. it 'does nothing if user who signed in is not suspended' do
  131. sign_in(Fabricate(:user, account: Fabricate(:account, suspended: false)))
  132. get 'success'
  133. expect(response).to have_http_status(200)
  134. end
  135. it 'redirects to account status page' do
  136. sign_in(Fabricate(:user, account: Fabricate(:account, suspended: true)))
  137. get 'success'
  138. expect(response).to redirect_to(edit_user_registration_path)
  139. end
  140. end
  141. describe 'raise_not_found' do
  142. it 'raises error' do
  143. controller.params[:unmatched_route] = 'unmatched'
  144. expect { controller.raise_not_found }.to raise_error(ActionController::RoutingError, 'No route matches unmatched')
  145. end
  146. end
  147. describe 'require_admin!' do
  148. controller do
  149. before_action :require_admin!
  150. def sucesss
  151. head 200
  152. end
  153. end
  154. before do
  155. routes.draw { get 'sucesss' => 'anonymous#sucesss' }
  156. end
  157. it 'returns a 403 if current user is not admin' do
  158. sign_in(Fabricate(:user, admin: false))
  159. get 'sucesss'
  160. expect(response).to have_http_status(403)
  161. end
  162. it 'returns a 403 if current user is only a moderator' do
  163. sign_in(Fabricate(:user, moderator: true))
  164. get 'sucesss'
  165. expect(response).to have_http_status(403)
  166. end
  167. it 'does nothing if current user is admin' do
  168. sign_in(Fabricate(:user, admin: true))
  169. get 'sucesss'
  170. expect(response).to have_http_status(200)
  171. end
  172. end
  173. describe 'require_staff!' do
  174. controller do
  175. before_action :require_staff!
  176. def sucesss
  177. head 200
  178. end
  179. end
  180. before do
  181. routes.draw { get 'sucesss' => 'anonymous#sucesss' }
  182. end
  183. it 'returns a 403 if current user is not admin or moderator' do
  184. sign_in(Fabricate(:user, admin: false, moderator: false))
  185. get 'sucesss'
  186. expect(response).to have_http_status(403)
  187. end
  188. it 'does nothing if current user is moderator' do
  189. sign_in(Fabricate(:user, moderator: true))
  190. get 'sucesss'
  191. expect(response).to have_http_status(200)
  192. end
  193. it 'does nothing if current user is admin' do
  194. sign_in(Fabricate(:user, admin: true))
  195. get 'sucesss'
  196. expect(response).to have_http_status(200)
  197. end
  198. end
  199. describe 'forbidden' do
  200. controller do
  201. def route_forbidden
  202. forbidden
  203. end
  204. end
  205. subject do
  206. routes.draw { get 'route_forbidden' => 'anonymous#route_forbidden' }
  207. get 'route_forbidden'
  208. end
  209. include_examples 'respond_with_error', 403
  210. end
  211. describe 'not_found' do
  212. controller do
  213. def route_not_found
  214. not_found
  215. end
  216. end
  217. subject do
  218. routes.draw { get 'route_not_found' => 'anonymous#route_not_found' }
  219. get 'route_not_found'
  220. end
  221. include_examples 'respond_with_error', 404
  222. end
  223. describe 'gone' do
  224. controller do
  225. def route_gone
  226. gone
  227. end
  228. end
  229. subject do
  230. routes.draw { get 'route_gone' => 'anonymous#route_gone' }
  231. get 'route_gone'
  232. end
  233. include_examples 'respond_with_error', 410
  234. end
  235. describe 'unprocessable_entity' do
  236. controller do
  237. def route_unprocessable_entity
  238. unprocessable_entity
  239. end
  240. end
  241. subject do
  242. routes.draw { get 'route_unprocessable_entity' => 'anonymous#route_unprocessable_entity' }
  243. get 'route_unprocessable_entity'
  244. end
  245. include_examples 'respond_with_error', 422
  246. end
  247. describe 'cache_collection' do
  248. class C < ApplicationController
  249. public :cache_collection
  250. end
  251. shared_examples 'receives :with_includes' do |fabricator, klass|
  252. it 'uses raw if it is not an ActiveRecord::Relation' do
  253. record = Fabricate(fabricator)
  254. expect(C.new.cache_collection([record], klass)).to eq [record]
  255. end
  256. end
  257. shared_examples 'cacheable' do |fabricator, klass|
  258. include_examples 'receives :with_includes', fabricator, klass
  259. it 'calls cache_ids of raw if it is an ActiveRecord::Relation' do
  260. record = Fabricate(fabricator)
  261. relation = klass.none
  262. allow(relation).to receive(:cache_ids).and_return([record])
  263. expect(C.new.cache_collection(relation, klass)).to eq [record]
  264. end
  265. end
  266. it 'returns raw unless class responds to :with_includes' do
  267. raw = Object.new
  268. expect(C.new.cache_collection(raw, Object)).to eq raw
  269. end
  270. context 'Status' do
  271. include_examples 'cacheable', :status, Status
  272. end
  273. end
  274. end