logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

application_controller_spec.rb (9078B)


  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 'returns a 403 if current user is not admin' do
  150. sign_in(Fabricate(:user, admin: false))
  151. get 'sucesss'
  152. expect(response).to have_http_status(403)
  153. end
  154. it 'returns a 403 if current user is only a moderator' do
  155. sign_in(Fabricate(:user, moderator: true))
  156. get 'sucesss'
  157. expect(response).to have_http_status(403)
  158. end
  159. it 'does nothing if current user is admin' do
  160. sign_in(Fabricate(:user, admin: true))
  161. get 'sucesss'
  162. expect(response).to have_http_status(200)
  163. end
  164. end
  165. describe 'require_staff!' do
  166. controller do
  167. before_action :require_staff!
  168. def sucesss
  169. head 200
  170. end
  171. end
  172. before do
  173. routes.draw { get 'sucesss' => 'anonymous#sucesss' }
  174. end
  175. it 'returns a 403 if current user is not admin or moderator' do
  176. sign_in(Fabricate(:user, admin: false, moderator: false))
  177. get 'sucesss'
  178. expect(response).to have_http_status(403)
  179. end
  180. it 'does nothing if current user is moderator' do
  181. sign_in(Fabricate(:user, moderator: true))
  182. get 'sucesss'
  183. expect(response).to have_http_status(200)
  184. end
  185. it 'does nothing if current user is admin' do
  186. sign_in(Fabricate(:user, admin: true))
  187. get 'sucesss'
  188. expect(response).to have_http_status(200)
  189. end
  190. end
  191. describe 'forbidden' do
  192. controller do
  193. def route_forbidden
  194. forbidden
  195. end
  196. end
  197. subject do
  198. routes.draw { get 'route_forbidden' => 'anonymous#route_forbidden' }
  199. get 'route_forbidden'
  200. end
  201. include_examples 'respond_with_error', 403
  202. end
  203. describe 'not_found' do
  204. controller do
  205. def route_not_found
  206. not_found
  207. end
  208. end
  209. subject do
  210. routes.draw { get 'route_not_found' => 'anonymous#route_not_found' }
  211. get 'route_not_found'
  212. end
  213. include_examples 'respond_with_error', 404
  214. end
  215. describe 'gone' do
  216. controller do
  217. def route_gone
  218. gone
  219. end
  220. end
  221. subject do
  222. routes.draw { get 'route_gone' => 'anonymous#route_gone' }
  223. get 'route_gone'
  224. end
  225. include_examples 'respond_with_error', 410
  226. end
  227. describe 'unprocessable_entity' do
  228. controller do
  229. def route_unprocessable_entity
  230. unprocessable_entity
  231. end
  232. end
  233. subject do
  234. routes.draw { get 'route_unprocessable_entity' => 'anonymous#route_unprocessable_entity' }
  235. get 'route_unprocessable_entity'
  236. end
  237. include_examples 'respond_with_error', 422
  238. end
  239. describe 'cache_collection' do
  240. class C < ApplicationController
  241. public :cache_collection
  242. end
  243. shared_examples 'receives :with_includes' do |fabricator, klass|
  244. it 'uses raw if it is not an ActiveRecord::Relation' do
  245. record = Fabricate(fabricator)
  246. expect(C.new.cache_collection([record], klass)).to eq [record]
  247. end
  248. end
  249. shared_examples 'cacheable' do |fabricator, klass|
  250. include_examples 'receives :with_includes', fabricator, klass
  251. it 'calls cache_ids of raw if it is an ActiveRecord::Relation' do
  252. record = Fabricate(fabricator)
  253. relation = klass.none
  254. allow(relation).to receive(:cache_ids).and_return([record])
  255. expect(C.new.cache_collection(relation, klass)).to eq [record]
  256. end
  257. end
  258. it 'returns raw unless class responds to :with_includes' do
  259. raw = Object.new
  260. expect(C.new.cache_collection(raw, Object)).to eq raw
  261. end
  262. context 'Notification' do
  263. include_examples 'cacheable', :notification, Notification
  264. end
  265. context 'Status' do
  266. include_examples 'cacheable', :status, Status
  267. end
  268. context 'StreamEntry' do
  269. include_examples 'receives :with_includes', :stream_entry, StreamEntry
  270. end
  271. end
  272. end