modify admin/conference_controller_spec and not authorized error messages

This commit is contained in:
shlok007 2017-06-14 20:42:50 +05:30
parent aa3df0243e
commit ad3d6f2f95
8 changed files with 120 additions and 105 deletions

View file

@ -10,7 +10,7 @@ module Admin
unless (current_user.has_role? :organizer, :any) || (current_user.has_role? :cfp, :any) ||
(current_user.has_role? :info_desk, :any) || (current_user.has_role? :organization_admin, :any) ||
(current_user.has_role? :volunteers_coordinator, :any) || current_user.is_admin
raise CanCan::AccessDenied.new('You are not authorized to access this area!')
raise CanCan::AccessDenied.new('You are not authorized to access this page.')
end
end
end

View file

@ -72,11 +72,14 @@ module Admin
def new
@conference = Conference.new
@organizations = {}
Organization.all.each do |organization|
@organizations.store(organization.name, organization.id) if can? :create, Conference.new(organization: organization)
end
end
def create
@conference = Conference.new(conference_params)
@conference.organization = Organization.find_or_create_by(name: 'organization')
if @conference.save
# user that creates the conference becomes organizer of that conference
current_user.add_role :organizer, @conference

View file

@ -152,6 +152,7 @@ class Ability
org_ids_for_organization_admin = Organization.with_role(:organization_admin, user).pluck(:id)
can :manage, Organization, id: org_ids_for_organization_admin
can [:new], Conference
can :manage, Conference, organization_id: org_ids_for_organization_admin
conf_ids_for_organization_admin = []
org_ids_for_organization_admin.each do |org_id|

View file

@ -6,6 +6,7 @@
input_html: { required: 'required' }
= f.input :short_title, hint: "A short and unique handle for your conference, using only letters, numbers, underscores, and dashes. This will be used to identify your conference in URLs etc. Example: 'froscon2011'",
input_html: { required: 'required', pattern: '[a-zA-Z0-9_-]+', title: 'Only letters, numbers, underscores, and dashes.' }, prepend: conferences_url + '/'
= f.input :organization, as: :select, collection: @organizations
= f.inputs 'Scheduling' do
= f.input :timezone, as: :time_zone, default: Time.zone.name, hint: 'Please select in what time zone your conference will take place.'
= f.input :start_date, as: :string, input_html: { id: 'conference-start-datepicker', required: 'required' }

View file

@ -51,7 +51,7 @@ describe Admin::CommentsController, type: :controller do
comment
get :index
expect(response).to redirect_to(root_path)
expect(flash[:alert]).to match('You are not authorized to access this area!')
expect(flash[:alert]).to match('You are not authorized to access this page.')
end
end
end

View file

@ -3,19 +3,18 @@ require 'spec_helper'
describe Admin::ConferencesController do
# It is necessary to use bang version of let to build roles before user
let(:conference) { create(:conference, end_date: Date.new(2014, 05, 26) + 15) }
let(:organization) { create(:organization) }
let(:conference) { create(:conference, organization: organization, end_date: Date.new(2014, 05, 26) + 15) }
let!(:organizer_role) { Role.find_by(name: 'organizer', resource: conference) }
let!(:organization_admin_role) { Role.find_by(name: 'organization_admin', resource: organization) }
let(:organization_admin) { create(:user, role_ids: organization_admin_role.id) }
let(:organizer) { create(:user, role_ids: organizer_role.id) }
let(:organizer2) { create(:user, email: 'organizer2@email.osem', role_ids: organizer_role.id) }
let(:participant) { create(:user) }
shared_examples 'access as organizer' do
shared_examples 'access as organizer or organization_admin' do
describe 'PATCH #update' do
context 'valid attributes' do
it 'locates the requested conference' do
patch :update, id: conference.short_title, conference: attributes_for(:conference, title: 'Example Con')
expect(assigns(:conference)).to eq(conference)
@ -25,7 +24,6 @@ describe Admin::ConferencesController do
patch :update, id: conference.short_title, conference:
attributes_for(:conference, title: 'Example Con',
short_title: 'ExCon')
conference.reload
expect(conference.title).to eq('Example Con')
expect(conference.short_title).to eq('ExCon')
@ -75,72 +73,6 @@ describe Admin::ConferencesController do
end
end
describe 'POST #create' do
context 'with valid attributes' do
it 'saves the conference to the database' do
expected = expect do
post :create, conference:
attributes_for(:conference, short_title: 'dps15')
end
expected.to change { Conference.count }.by 1
end
it 'redirects to conference#show' do
post :create, conference:
attributes_for(:conference, short_title: 'dps15')
expect(response).to redirect_to admin_conference_path(
assigns[:conference].short_title)
end
it 'creates roles for the conference' do
cfp_role = Role.find_by(name: 'cfp', resource: conference)
info_desk_role = Role.find_by(name: 'info_desk', resource: conference)
volunteers_coordinator_role = Role.find_by(name: 'volunteers_coordinator', resource: conference)
post :create, conference:
attributes_for(:conference, short_title: 'dps15')
expect(conference.roles.count).to eq 4
expect(conference.roles).to eq [organizer_role, cfp_role, info_desk_role, volunteers_coordinator_role]
end
end
context 'with invalid attributes' do
it 'does not save the conference to the database' do
expected = expect do
post :create, conference:
attributes_for(:conference, short_title: nil)
end
expected.to_not change { Conference.count }
end
it 're-renders the new template' do
post :create, conference:
attributes_for(:conference, short_title: nil)
expect(response).to be_success
end
end
context 'with duplicate conference short title' do
it 'does not save the conference to the database' do
conference
expected = expect do
post :create, conference:
attributes_for(:conference, short_title: conference.short_title)
end
expected.to_not change { Conference.count }
end
it 're-renders the new template' do
conference
post :create, conference: attributes_for(:conference, short_title: conference.short_title)
expect(response).to be_success
end
end
end
describe 'GET #edit' do
it 'assigns the requested conference to conference' do
get :edit, id: conference.short_title
@ -203,6 +135,74 @@ describe Admin::ConferencesController do
end
end
end
end
shared_examples 'access as organization_admin' do
describe 'POST #create' do
context 'with valid attributes' do
it 'saves the conference to the database' do
expected = expect do
post :create, conference:
attributes_for(:conference, short_title: 'dps15', organization: organization)
end
expected.to change { Conference.count }.by 1
end
it 'redirects to conference#show' do
post :create, conference:
attributes_for(:conference, short_title: 'dps15', organization: organization)
expect(response).to redirect_to admin_conference_path(
assigns[:conference].short_title)
end
it 'creates roles for the conference' do
cfp_role = Role.find_by(name: 'cfp', resource: conference)
info_desk_role = Role.find_by(name: 'info_desk', resource: conference)
volunteers_coordinator_role = Role.find_by(name: 'volunteers_coordinator', resource: conference)
post :create, conference:
attributes_for(:conference, short_title: 'dps15')
expect(conference.roles.count).to eq 4
expect(conference.roles).to eq [organizer_role, cfp_role, info_desk_role, volunteers_coordinator_role]
end
end
context 'with invalid attributes' do
it 'does not save the conference to the database' do
expected = expect do
post :create, conference:
attributes_for(:conference, short_title: nil, organization: organization)
end
expected.to_not change { Conference.count }
end
it 're-renders the new template' do
post :create, conference:
attributes_for(:conference, short_title: nil, organization: organization)
expect(response).to be_success
end
end
context 'with duplicate conference short title' do
it 'does not save the conference to the database' do
conference
expected = expect do
post :create, conference:
attributes_for(:conference, short_title: conference.short_title, organization: organization)
end
expected.to_not change { Conference.count }
end
it 're-renders the new template' do
conference
post :create, conference: attributes_for(:conference, short_title: conference.short_title, organization: organization)
expect(response).to be_success
end
end
end
describe 'GET #new' do
it 'assigns a new conference to conference' do
@ -217,14 +217,45 @@ describe Admin::ConferencesController do
end
end
describe 'organizer access' do
describe 'organization admin access' do
before do
sign_in(organization_admin)
end
it_behaves_like 'access as organizer or organization_admin'
it_behaves_like 'access as organization_admin'
end
shared_examples 'access as organizer, participant or guest' do |path, message|
describe 'GET #new' do
it 'requires organizer privileges' do
get :new
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
describe 'POST #create' do
it 'requires organizer privileges' do
post :create, conference: attributes_for(:conference,
short_title: 'ExCon')
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
end
describe 'organizer access' do
before do
sign_in(organizer)
end
it_behaves_like 'access as organizer'
it_behaves_like 'access as organizer or organization_admin'
it_behaves_like 'access as organizer, participant or guest', :root_path, 'You are not authorized to access this page.'
end
shared_examples 'access as participant or guest' do |path, message|
@ -248,27 +279,6 @@ describe Admin::ConferencesController do
end
end
describe 'GET #new' do
it 'requires organizer privileges' do
get :new
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
describe 'POST #create' do
it 'requires organizer privileges' do
post :create, conference: attributes_for(:conference,
short_title: 'ExCon')
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
describe 'PATCH #update' do
it 'requires organizer privileges' do
patch :update, id: conference.short_title,
@ -287,13 +297,13 @@ describe Admin::ConferencesController do
sign_in(participant)
end
it_behaves_like 'access as participant or guest', :root_path, 'You are not authorized to access this area!'
it_behaves_like 'access as participant or guest', :root_path, 'You are not authorized to access this page.'
it_behaves_like 'access as organizer, participant or guest', :root_path, 'You are not authorized to access this page.'
end
describe 'guest access' do
it_behaves_like 'access as participant or guest', :new_user_session_path
it_behaves_like 'access as organizer, participant or guest', :new_user_session_path
end
end

View file

@ -22,7 +22,7 @@ feature 'Has correct abilities' do
visit admin_conference_path(conference1.short_title)
expect(current_path).to eq root_path
expect(flash).to eq 'You are not authorized to access this area!'
expect(flash).to eq 'You are not authorized to access this page.'
end
scenario 'when user is organizer' do

View file

@ -25,7 +25,7 @@ feature 'BaseController' do
it 'not an admin it redirects to root_path' do
visit admin_conferences_path
expect(current_path).to eq root_path
expect(flash).to eq 'You are not authorized to access this area!'
expect(flash).to eq 'You are not authorized to access this page.'
end
it 'an admin he can access the admin area' do