Rubocop controller concerns and specs
This commit is contained in:
parent
03a425b329
commit
8c47573501
11 changed files with 232 additions and 214 deletions
13
.rubocop.yml
13
.rubocop.yml
|
@ -210,15 +210,6 @@ AllCops:
|
|||
- 'app/controllers/users/sessions_controller.rb'
|
||||
- 'app/controllers/users/registrations_controller.rb'
|
||||
- 'app/controllers/users/confirmations_controller.rb'
|
||||
- 'app/controllers/concerns/controllers/nonprofit/authorization.rb'
|
||||
- 'app/controllers/concerns/controllers/nonprofit/current.rb'
|
||||
- 'app/controllers/concerns/controllers/supporter/current.rb'
|
||||
- 'app/controllers/concerns/controllers/campaign/authorization.rb'
|
||||
- 'app/controllers/concerns/controllers/campaign/current.rb'
|
||||
- 'app/controllers/concerns/controllers/event/authorization.rb'
|
||||
- 'app/controllers/concerns/controllers/event/current.rb'
|
||||
- 'app/controllers/concerns/controllers/user/authorization.rb'
|
||||
- 'app/controllers/concerns/controllers/locale.rb'
|
||||
- 'app/controllers/image_attachments_controller.rb'
|
||||
- 'app/controllers/ticket_levels_controller.rb'
|
||||
- 'app/controllers/campaigns/supporters_controller.rb'
|
||||
|
@ -635,7 +626,6 @@ AllCops:
|
|||
- 'spec/controllers/direct_debit_details_spec.rb'
|
||||
- 'spec/controllers/emails_spec.rb'
|
||||
- 'spec/controllers/ticket_levels_spec.rb'
|
||||
- 'spec/controllers/concerns/supporter/current_spec.rb'
|
||||
- 'spec/controllers/support/new_controller_user_context.rb'
|
||||
- 'spec/controllers/support/shared_user_context.rb'
|
||||
- 'spec/controllers/campaigns/donations_spec.rb'
|
||||
|
@ -753,9 +743,10 @@ Layout/IndentationStyle:
|
|||
Layout/IndentationWidth:
|
||||
Width: 1
|
||||
|
||||
Metric/BlockLength:
|
||||
Metrics/BlockLength:
|
||||
Exclude:
|
||||
- '**/*_spec.rb'
|
||||
|
||||
RSpec/ExampleLength:
|
||||
Max: 20
|
||||
|
||||
|
|
|
@ -8,13 +8,19 @@ module Controllers::Campaign::Authorization
|
|||
|
||||
included do
|
||||
private
|
||||
|
||||
def current_campaign_editor?
|
||||
!params[:preview] && (current_nonprofit_user? || current_role?(:campaign_editor, current_campaign.id) || current_role?(:super_admin))
|
||||
!params[:preview] && (
|
||||
current_nonprofit_user? ||
|
||||
current_role?(:campaign_editor, current_campaign.id) ||
|
||||
current_role?(:super_admin)
|
||||
)
|
||||
end
|
||||
|
||||
def authenticate_campaign_editor!
|
||||
unless current_campaign_editor?
|
||||
return if current_campaign_editor?
|
||||
|
||||
reject_with_sign_in 'You need to be a campaign editor to do that.'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,6 +8,7 @@ module Controllers::Campaign::Current
|
|||
|
||||
included do
|
||||
private
|
||||
|
||||
def current_campaign
|
||||
@campaign ||= FetchCampaign.with_params params, current_nonprofit
|
||||
raise ActionController::RoutingError, 'Campaign not found' if @campaign.nil?
|
||||
|
|
|
@ -14,13 +14,18 @@ module Controllers::Event::Authorization
|
|||
end
|
||||
|
||||
def current_event_editor?
|
||||
!params[:preview] && (current_nonprofit_user? || current_role?(:event_editor, current_event.id) || current_role?(:super_admin))
|
||||
!params[:preview] && (
|
||||
current_nonprofit_user? || current_role?(
|
||||
:event_editor,
|
||||
current_event.id
|
||||
) || current_role?(:super_admin)
|
||||
)
|
||||
end
|
||||
|
||||
def authenticate_event_editor!
|
||||
unless current_event_editor?
|
||||
return if current_event_editor?
|
||||
|
||||
reject_with_sign_in 'You need to be the event organizer or a nonprofit administrator before doing that.'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,6 +8,7 @@ module Controllers::Event::Current
|
|||
|
||||
included do
|
||||
private
|
||||
|
||||
def current_event
|
||||
@event ||= FetchEvent.with_params params, current_nonprofit
|
||||
raise ActionController::RoutingError, 'Event not found' if @event.nil?
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||
# rubocop:disable Style/ConditionalAssignment
|
||||
module Controllers::Locale
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
|
@ -9,6 +10,7 @@ module Controllers::Locale
|
|||
before_action :set_locale
|
||||
|
||||
private
|
||||
|
||||
def set_locale
|
||||
if params[:locale] && Houdini.intl.available_locales.include?(params[:locale])
|
||||
I18n.locale = params[:locale]
|
||||
|
@ -18,3 +20,4 @@ module Controllers::Locale
|
|||
end
|
||||
end
|
||||
end
|
||||
# rubocop:enable all
|
||||
|
|
|
@ -8,8 +8,10 @@ module Controllers::Nonprofit::Authorization
|
|||
|
||||
included do
|
||||
helper_method :current_nonprofit_user?
|
||||
|
||||
private
|
||||
def authenticate_nonprofit_user!(type: :web)
|
||||
|
||||
def authenticate_nonprofit_user!
|
||||
reject_with_sign_in 'Please sign in' unless current_nonprofit_user?
|
||||
end
|
||||
|
||||
|
@ -21,13 +23,16 @@ module Controllers::Nonprofit::Authorization
|
|||
return false if params[:preview]
|
||||
return false unless current_nonprofit_without_exception
|
||||
|
||||
@current_user_role ||= current_role?(%i[nonprofit_admin nonprofit_associate], current_nonprofit_without_exception.id) || current_role?(:super_admin)
|
||||
@current_nonprofit_user ||= current_role?(
|
||||
%i[nonprofit_admin nonprofit_associate],
|
||||
current_nonprofit_without_exception.id
|
||||
) || current_role?(:super_admin)
|
||||
end
|
||||
|
||||
def current_nonprofit_admin?
|
||||
return false if !current_user || current_user.roles.empty?
|
||||
|
||||
@current_admin_role ||= current_role?(:nonprofit_admin, current_nonprofit.id) || current_role?(:super_admin)
|
||||
@current_nonprofit_admin ||= current_role?(:nonprofit_admin, current_nonprofit.id) || current_role?(:super_admin)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,6 +6,7 @@ module Controllers::Nonprofit::Current
|
|||
extend ActiveSupport::Concern
|
||||
included do
|
||||
private
|
||||
|
||||
def current_nonprofit
|
||||
@nonprofit = current_nonprofit_without_exception
|
||||
raise ActionController::RoutingError, 'Nonprofit not found' if @nonprofit.nil?
|
||||
|
@ -14,7 +15,6 @@ module Controllers::Nonprofit::Current
|
|||
end
|
||||
|
||||
def current_nonprofit_without_exception
|
||||
key = "current_nonprofit_#{current_user_id}_params_#{[params[:state_code], params[:city], params[:name], params[:nonprofit_id], params[:id]].join('_')}"
|
||||
FetchNonprofit.with_params params, administered_nonprofit
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,6 +7,7 @@ module Controllers::Supporter::Current
|
|||
extend ActiveSupport::Concern
|
||||
included do
|
||||
private
|
||||
|
||||
def current_supporter
|
||||
current_nonprofit.supporters.find(params[:supporter_id] || params[:id])
|
||||
end
|
||||
|
|
|
@ -5,9 +5,13 @@
|
|||
module Controllers::User::Authorization
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
# rubocop:disable Metrics/BlockLength
|
||||
# rubocop:disable Layout/LineLength
|
||||
included do
|
||||
helper_method :current_role?, :administered_nonprofit
|
||||
|
||||
private
|
||||
|
||||
def authenticate_user!(msg = nil, type = :html)
|
||||
reject_with_sign_in(msg, type) unless current_user
|
||||
end
|
||||
|
@ -23,7 +27,8 @@ module Controllers::User::Authorization
|
|||
def block_with_sign_in(msg = nil)
|
||||
store_location
|
||||
if current_user
|
||||
flash[:notice] = "It looks like you're not allowed to access that page. If this seems like a mistake, please contact #{Houdini.support_email}"
|
||||
flash[:notice] =
|
||||
"It looks like you're not allowed to access that page. If this seems like a mistake, please contact #{Houdini.support_email}"
|
||||
redirect_to root_path
|
||||
else
|
||||
msg ||= 'We need to sign you in before you can do that.'
|
||||
|
@ -35,7 +40,6 @@ module Controllers::User::Authorization
|
|||
return false unless current_user
|
||||
|
||||
role_names = Array(role_names)
|
||||
key = "current_role_user_#{current_user_id}_names_#{role_names.join('_')}_host_#{host_id}"
|
||||
QueryRoles.user_has_role?(current_user.id, role_names, host_id)
|
||||
end
|
||||
|
||||
|
@ -52,9 +56,7 @@ module Controllers::User::Authorization
|
|||
end
|
||||
|
||||
def authenticate_super_associate!
|
||||
unless current_role?(:super_admin) || current_role?(:super_associate)
|
||||
reject_with_sign_in 'Please login.'
|
||||
end
|
||||
reject_with_sign_in 'Please login.' unless current_role?(:super_admin) || current_role?(:super_associate)
|
||||
end
|
||||
|
||||
def authenticate_super_admin!
|
||||
|
@ -63,16 +65,17 @@ module Controllers::User::Authorization
|
|||
|
||||
def store_location
|
||||
referrer = request.fullpath
|
||||
no_redirects = ['/users', '/signup', '/signin', '/users/sign_in', '/users/sign_up', '/users/password', '/users/sign_out', /.*\.json.*/, %r{.*auth/facebook.*}]
|
||||
unless request.format.symbol == :json || no_redirects.map { |p| referrer.match(p) }.any?
|
||||
no_redirects = ['/users', '/signup', '/signin', '/users/sign_in', '/users/sign_up', '/users/password',
|
||||
'/users/sign_out', /.*\.json.*/, %r{.*auth/facebook.*}]
|
||||
|
||||
return if request.format.symbol == :json || no_redirects.map { |p| referrer.match(p) }.any?
|
||||
|
||||
session[:previous_url] = referrer
|
||||
end
|
||||
end
|
||||
|
||||
def administered_nonprofit
|
||||
return nil unless current_user
|
||||
|
||||
key = "administered_nonprofit_user_#{current_user_id}_nonprofit"
|
||||
::Nonprofit.where(id: QueryRoles.host_ids(current_user_id, %i[nonprofit_admin nonprofit_associate])).last
|
||||
end
|
||||
|
||||
|
@ -81,3 +84,5 @@ module Controllers::User::Authorization
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
# rubocop:enable all
|
||||
|
|
|
@ -8,14 +8,10 @@ describe 'Controllers::Supporter::Current', type: :controller do
|
|||
let(:nonprofit) { force_create(:nm_justice) }
|
||||
let(:supporter) { force_create(:supporter) }
|
||||
|
||||
class TestController < ApplicationController
|
||||
controller(ApplicationController) do
|
||||
include Controllers::User::Authorization
|
||||
include Controllers::Supporter::Current
|
||||
end
|
||||
|
||||
|
||||
|
||||
controller(TestController) do
|
||||
def index
|
||||
render json: {
|
||||
supporter: "supporters: #{current_supporter.id}",
|
||||
|
@ -28,10 +24,12 @@ describe 'Controllers::Supporter::Current', type: :controller do
|
|||
nonprofit
|
||||
supporter
|
||||
get :index, params: { nonprofit_id: nonprofit.id, id: supporter.id }
|
||||
expect(JSON::parse(response.body)).to eq({
|
||||
expect(JSON.parse(response.body)).to eq(
|
||||
{
|
||||
'supporter' => "supporters: #{supporter.id}",
|
||||
'nonprofit' => "nonprofit: #{nonprofit.id}"
|
||||
})
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
it 'handles situations where we use supporter_id' do
|
||||
|
@ -39,9 +37,11 @@ describe 'Controllers::Supporter::Current', type: :controller do
|
|||
supporter
|
||||
|
||||
get :index, params: { nonprofit_id: nonprofit.id, supporter_id: supporter.id, id: 1 }
|
||||
expect(JSON::parse(response.body)).to eq({
|
||||
expect(JSON.parse(response.body)).to eq(
|
||||
{
|
||||
'supporter' => "supporters: #{supporter.id}",
|
||||
'nonprofit' => "nonprofit: #{nonprofit.id}"
|
||||
})
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue