2021-09-15 20:39:47 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2021-10-28 20:48:28 +08:00
|
|
|
RSpec.shared_examples 'a controller with authentication' do |actions_with_params, actions_to_skip, custom_response|
|
2021-09-15 20:39:47 +08:00
|
|
|
let (:protected_actions) { described_class.instance_methods(false) - (actions_to_skip || []) }
|
|
|
|
|
|
|
|
describe 'controller actions' do
|
|
|
|
context 'unauthenticated user' do
|
2021-10-28 20:48:28 +08:00
|
|
|
it "returns #{custom_response || :forbidden} response for all actions" do
|
2021-09-15 20:39:47 +08:00
|
|
|
sign_out :user
|
|
|
|
actions_with_params ||= {}
|
|
|
|
protected_actions.each do |action|
|
|
|
|
params = actions_with_params[action]
|
|
|
|
get action, params: params
|
2021-10-28 20:48:28 +08:00
|
|
|
expect(response).to have_http_status(custom_response || :forbidden).or redirect_to('/users/sign_in')
|
2021-09-15 20:39:47 +08:00
|
|
|
end
|
2023-11-13 16:34:38 +08:00
|
|
|
rescue ActionController::UrlGenerationError => e
|
|
|
|
warn "Warning: #{e}"
|
2021-09-15 20:39:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|