Implement role authorization

This commit is contained in:
Stella Rouzi 2014-08-12 11:51:59 +03:00
parent 6755328c4c
commit e2fb434dc7
122 changed files with 1386 additions and 751 deletions

View file

@ -3,16 +3,15 @@ require 'spec_helper'
describe Admin::ConferenceController do
# It is necessary to use bang version of let to build roles before user
let!(:organizer_role) { create(:organizer_role) }
let!(:participant_role) { create(:participant_role) }
let!(:admin_role) { create(:admin_role) }
let(:conference) { create(:conference) }
let(:admin) { create(:admin) }
let(:organizer) { create(:organizer) }
let!(:first_user) { create(:user) }
let!(:participant_role) { create(:participant_role) }
let!(:organizer_role) { create(:role, name: 'organizer', resource: conference) }
let(:organizer) { create(:user, role_ids: organizer_role.id, is_admin: true) }
let(:participant) { create(:participant) }
shared_examples 'access as administration or organizer' do
shared_examples 'access as administration' do
describe 'PATCH #update' do
@ -45,8 +44,7 @@ describe Admin::ConferenceController do
mailer = double
allow(mailer).to receive(:deliver)
conference.email_settings = create(:email_settings)
patch :update, id: conference.short_title, conference:
attributes_for(:conference, start_date: Date.today + 2.days, end_date: Date.today + 4.days)
patch :update, id: conference.short_title#, conference: attributes_for(:conference, start_date: Date.today + 2.days, end_date: Date.today + 4.days)
conference.reload
allow(Mailbot).to receive(:conference_date_update_mail).and_return(mailer)
end
@ -193,8 +191,12 @@ describe Admin::ConferenceController do
context 'no conferences' do
it 'redirect to new conference' do
Conference.all.each do |c|
c.destroy
end
sign_in create(:admin)
get :index
expect(response).to redirect_to(redirect_to new_admin_conference_path)
expect(response).to redirect_to new_admin_conference_path
end
end
end
@ -215,59 +217,64 @@ describe Admin::ConferenceController do
describe 'administrator access' do
before do
sign_in(admin)
end
it_behaves_like 'access as administration or organizer'
end
describe 'organizer access' do
before(:each) do
sign_in(organizer)
end
it_behaves_like 'access as administration or organizer'
it_behaves_like 'access as administration'
end
shared_examples 'access as participant or guest' do |success_path|
shared_examples 'access as participant or guest' do |path, message|
describe 'GET #show' do
it 'requires admin privileges' do
it 'requires organizer privileges' do
get :show, id: conference.short_title
expect(response).to redirect_to(send(success_path))
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
describe 'GET #index' do
it 'requires admin privileges' do
it 'requires organizer privileges' do
get :index
expect(response).to redirect_to(send(success_path))
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
describe 'GET #new' do
it 'requires admin privileges' do
it 'requires organizer privileges' do
get :new
expect(response).to redirect_to(send(success_path))
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
describe 'POST #create' do
it 'requires admin privileges' do
it 'requires organizer privileges' do
post :create, conference: attributes_for(:conference,
short_title: 'ExCon')
expect(response).to redirect_to(send(success_path))
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
describe 'PATCH #update' do
it 'requires admin privileges' do
it 'requires organizer privileges' do
patch :update, id: conference.short_title,
conference: attributes_for(:conference,
short_title: 'ExCon')
expect(response).to redirect_to(send(success_path))
expect(response).to redirect_to(send(path))
if message
expect(flash[:alert]).to match(/#{message}/)
end
end
end
end
@ -277,7 +284,7 @@ describe Admin::ConferenceController do
sign_in(participant)
end
it_behaves_like 'access as participant or guest', :root_path
it_behaves_like 'access as participant or guest', :root_path, 'You are not authorized to access this area!'
end

View file

@ -1,7 +1,5 @@
require 'spec_helper'
describe Admin::UsersController do
let!(:admin_role) { create(:admin_role) }
let!(:participant_role) { create(:participant_role) }
let(:admin) { create(:admin) }
let(:user) { create(:user) }
before(:each) do
@ -31,13 +29,13 @@ describe Admin::UsersController do
:user, email: 'example@incoherent.de', id: user.id).email).
to eq('example@incoherent.de')
end
it "redirects to the updated user" do
it 'redirects to the updated user' do
patch :update, id: user.id
expect(response).to redirect_to admin_users_path
end
end
end
describe 'DELETE #destroy' do
describe 'DELETE #destroy' do
before :each do
@user = create(:user)
end

View file

@ -1,7 +1,8 @@
require 'spec_helper'
describe ConferenceController do
let(:conference) { create(:conference) }
let(:conference) { create(:conference, make_conference_public: true) }
describe 'GET #show' do
context 'conference made public' do
it 'assigns the requested conference to conference' do
@ -24,7 +25,7 @@ describe ConferenceController do
it 'renders flash saying conference not ready' do
get :show, id: conference.short_title
expect(flash[:notice]).to eq("Conference not ready yet!!")
expect(flash[:alert]).to eq('You are not authorized to access this page.')
end
end
context 'gallery photos for splash' do