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.

297 lines
7.9 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 any" do
  20. subject
  21. expect(response).to have_http_status(code)
  22. end
  23. it "returns http #{code} for http" do
  24. subject
  25. expect(response).to have_http_status(code)
  26. end
  27. it "renders template for http" do
  28. is_expected.to render_template("errors/#{code}", layout: 'error')
  29. end
  30. end
  31. context 'forgery' do
  32. subject do
  33. ActionController::Base.allow_forgery_protection = true
  34. routes.draw { post 'success' => 'anonymous#success' }
  35. post 'success'
  36. end
  37. include_examples 'respond_with_error', 422
  38. end
  39. it "does not force ssl if Rails.env.production? is not 'true'" do
  40. routes.draw { get 'success' => 'anonymous#success' }
  41. allow(Rails.env).to receive(:production?).and_return(false)
  42. get 'success'
  43. expect(response).to have_http_status(:success)
  44. end
  45. it "forces ssl if Rails.env.production? is 'true'" do
  46. routes.draw { get 'success' => 'anonymous#success' }
  47. allow(Rails.env).to receive(:production?).and_return(true)
  48. get 'success'
  49. expect(response).to redirect_to('https://test.host/success')
  50. end
  51. describe 'helper_method :current_account' do
  52. it 'returns nil if not signed in' do
  53. expect(controller.view_context.current_account).to be_nil
  54. end
  55. it 'returns account if signed in' do
  56. account = Fabricate(:account)
  57. sign_in(Fabricate(:user, account: account))
  58. expect(controller.view_context.current_account).to eq account
  59. end
  60. end
  61. describe 'helper_method :single_user_mode?' do
  62. it 'returns false if it is in single_user_mode but there is no account' do
  63. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
  64. expect(controller.view_context.single_user_mode?).to eq false
  65. end
  66. it 'returns false if there is an account but it is not in single_user_mode' do
  67. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false)
  68. Fabricate(:account)
  69. expect(controller.view_context.single_user_mode?).to eq false
  70. end
  71. it 'returns true if it is in single_user_mode and there is an account' do
  72. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
  73. Fabricate(:account)
  74. expect(controller.view_context.single_user_mode?).to eq true
  75. end
  76. end
  77. context 'ActionController::RoutingError' do
  78. subject do
  79. routes.draw { get 'routing_error' => 'anonymous#routing_error' }
  80. get 'routing_error'
  81. end
  82. include_examples 'respond_with_error', 404
  83. end
  84. context 'ActiveRecord::RecordNotFound' do
  85. subject do
  86. routes.draw { get 'record_not_found' => 'anonymous#record_not_found' }
  87. get 'record_not_found'
  88. end
  89. include_examples 'respond_with_error', 404
  90. end
  91. context 'ActionController::InvalidAuthenticityToken' do
  92. subject do
  93. routes.draw { get 'invalid_authenticity_token' => 'anonymous#invalid_authenticity_token' }
  94. get 'invalid_authenticity_token'
  95. end
  96. include_examples 'respond_with_error', 422
  97. end
  98. describe 'before_action :store_current_location' do
  99. it 'stores location for user if it is not devise controller' do
  100. routes.draw { get 'success' => 'anonymous#success' }
  101. get 'success'
  102. expect(controller.stored_location_for(:user)).to eq '/success'
  103. end
  104. context do
  105. controller Devise::SessionsController do
  106. end
  107. it 'does not store location for user if it is devise controller' do
  108. @request.env["devise.mapping"] = Devise.mappings[:user]
  109. get 'create'
  110. expect(controller.stored_location_for(:user)).to be_nil
  111. end
  112. end
  113. end
  114. describe 'before_action :check_suspension' do
  115. before do
  116. routes.draw { get 'success' => 'anonymous#success' }
  117. end
  118. it 'does nothing if not signed in' do
  119. get 'success'
  120. expect(response).to have_http_status(:success)
  121. end
  122. it 'does nothing if user who signed in is not suspended' do
  123. sign_in(Fabricate(:user, account: Fabricate(:account, suspended: false)))
  124. get 'success'
  125. expect(response).to have_http_status(:success)
  126. end
  127. it 'returns http 403 if user who signed in is suspended' do
  128. sign_in(Fabricate(:user, account: Fabricate(:account, suspended: true)))
  129. get 'success'
  130. expect(response).to have_http_status(403)
  131. end
  132. end
  133. describe 'raise_not_found' do
  134. it 'raises error' do
  135. controller.params[:unmatched_route] = 'unmatched'
  136. expect{ controller.raise_not_found }.to raise_error(ActionController::RoutingError, 'No route matches unmatched')
  137. end
  138. end
  139. describe 'require_admin!' do
  140. controller do
  141. before_action :require_admin!
  142. def sucesss
  143. head 200
  144. end
  145. end
  146. before do
  147. routes.draw { get 'sucesss' => 'anonymous#sucesss' }
  148. end
  149. it 'redirects to root path if current user is not admin' do
  150. sign_in(Fabricate(:user, admin: false))
  151. get 'sucesss'
  152. expect(response).to redirect_to('/')
  153. end
  154. it 'does nothing if current user is admin' do
  155. sign_in(Fabricate(:user, admin: true))
  156. get 'sucesss'
  157. expect(response).to have_http_status(200)
  158. end
  159. end
  160. describe 'forbidden' do
  161. controller do
  162. def route_forbidden
  163. forbidden
  164. end
  165. end
  166. subject do
  167. routes.draw { get 'route_forbidden' => 'anonymous#route_forbidden' }
  168. get 'route_forbidden'
  169. end
  170. include_examples 'respond_with_error', 403
  171. end
  172. describe 'not_found' do
  173. controller do
  174. def route_not_found
  175. not_found
  176. end
  177. end
  178. subject do
  179. routes.draw { get 'route_not_found' => 'anonymous#route_not_found' }
  180. get 'route_not_found'
  181. end
  182. include_examples 'respond_with_error', 404
  183. end
  184. describe 'gone' do
  185. controller do
  186. def route_gone
  187. gone
  188. end
  189. end
  190. subject do
  191. routes.draw { get 'route_gone' => 'anonymous#route_gone' }
  192. get 'route_gone'
  193. end
  194. include_examples 'respond_with_error', 410
  195. end
  196. describe 'unprocessable_entity' do
  197. controller do
  198. def route_unprocessable_entity
  199. unprocessable_entity
  200. end
  201. end
  202. subject do
  203. routes.draw { get 'route_unprocessable_entity' => 'anonymous#route_unprocessable_entity' }
  204. get 'route_unprocessable_entity'
  205. end
  206. include_examples 'respond_with_error', 422
  207. end
  208. describe 'cache_collection' do
  209. class C < ApplicationController
  210. public :cache_collection
  211. end
  212. shared_examples 'receives :with_includes' do |fabricator, klass|
  213. it 'uses raw if it is not an ActiveRecord::Relation' do
  214. record = Fabricate(fabricator)
  215. expect(C.new.cache_collection([record], klass)).to eq [record]
  216. end
  217. end
  218. shared_examples 'cacheable' do |fabricator, klass|
  219. include_examples 'receives :with_includes', fabricator, klass
  220. it 'calls cache_ids of raw if it is an ActiveRecord::Relation' do
  221. record = Fabricate(fabricator)
  222. relation = klass.none
  223. allow(relation).to receive(:cache_ids).and_return([record])
  224. expect(C.new.cache_collection(relation, klass)).to eq [record]
  225. end
  226. end
  227. it 'returns raw unless class responds to :with_includes' do
  228. raw = Object.new
  229. expect(C.new.cache_collection(raw, Object)).to eq raw
  230. end
  231. context 'Notification' do
  232. include_examples 'cacheable', :notification, Notification
  233. end
  234. context 'Status' do
  235. include_examples 'cacheable', :status, Status
  236. end
  237. context 'StreamEntry' do
  238. include_examples 'receives :with_includes', :stream_entry, StreamEntry
  239. end
  240. end
  241. end