2018-05-07 16:47:29 -07:00
# frozen_string_literal: true
2016-03-22 17:13:15 +05:30
require 'spec_helper'
describe ConferenceRegistrationsController , type : :controller do
2017-02-02 01:28:22 +02:00
let ( :conference ) { create ( :conference , title : 'My Conference' , short_title : 'myconf' ) }
2016-03-22 17:13:15 +05:30
let ( :user ) { create ( :user ) }
2017-02-02 01:28:22 +02:00
let ( :not_registered_user ) { create ( :user ) }
let ( :registered_user ) { create ( :user ) }
let! ( :registration ) { create ( :registration , conference : conference , user : registered_user , created_at : 1 . day . ago ) }
shared_examples 'access #new action' do | user , ichain , path , message |
before :each do
sign_in send ( user ) if user
stub_const ( 'ENV' , ENV . to_hash . merge ( 'OSEM_ICHAIN_ENABLED' = > ichain ) )
2019-05-13 17:42:09 +02:00
get :new , params : { conference_id : conference . short_title }
2017-02-02 01:28:22 +02:00
end
it 'redirects' do
expect ( response ) . to redirect_to path
end
it 'shows flash alert' do
expect ( flash [ :alert ] ) . to eq message
end
end
2016-03-22 17:13:15 +05:30
2017-05-04 13:55:37 +03:00
shared_examples 'can access #new action' do | user , ichain |
before :each do
sign_in send ( user ) if user
stub_const ( 'ENV' , ENV . to_hash . merge ( 'OSEM_ICHAIN_ENABLED' = > ichain ) )
2019-05-13 17:42:09 +02:00
get :new , params : { conference_id : conference . short_title }
2017-05-04 13:55:37 +03:00
end
it 'user variable exists' do
expect ( assigns ( :user ) ) . not_to be_nil
end
it 'renders the new template' do
expect ( response ) . to render_template ( 'new' )
end
end
2016-03-22 17:13:15 +05:30
context 'user is signed in' do
2017-02-02 01:28:22 +02:00
before :each do
sign_in user
end
2016-03-22 17:13:15 +05:30
2016-08-11 21:48:59 +01:00
describe 'GET #new' do
2017-05-04 13:55:37 +03:00
let ( :not_registered_confirmed_speaker ) { create ( :user ) }
let ( :registered_confirmed_speaker ) { create ( :user ) }
let! ( :speaker_registration ) { create ( :registration , conference : conference , user : registered_confirmed_speaker , created_at : 1 . day . ago ) }
let! ( :confirmed_event ) { create ( :event , program : conference . program , speakers : [ not_registered_confirmed_speaker , registered_confirmed_speaker ] , state : 'confirmed' ) }
2016-08-11 21:48:59 +01:00
context 'registration period open' do
2017-02-02 01:28:22 +02:00
before :each do
create ( :registration_period , conference : conference , start_date : 3 . days . ago , end_date : 1 . day . from_now )
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'registration limit not exceeded' do
before :each do
conference . registration_limit = 0
conference . save!
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED true, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'true' , '/conferences/myconf/register/edit' , nil
end
2017-02-02 01:28:22 +02:00
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED true, user not registered' do
it_behaves_like 'can access #new action' , :not_registered_user , 'true'
end
2017-02-02 01:28:22 +02:00
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED true, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'true' , '/conferences/myconf/register/edit' , nil
2016-08-11 21:48:59 +01:00
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED true, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'true'
end
2017-02-02 01:28:22 +02:00
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED false, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'false' , '/conferences/myconf/register/edit' , nil
end
2017-02-02 01:28:22 +02:00
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED false, user not registered' do
it_behaves_like 'can access #new action' , :not_registered_user , 'false'
end
2017-02-02 01:28:22 +02:00
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED false, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'false' , '/conferences/myconf/register/edit' , nil
end
2017-02-02 01:28:22 +02:00
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED false, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'false'
2016-08-11 21:48:59 +01:00
end
end
2017-02-02 01:28:22 +02:00
context 'registration limit exceeded' do
before :each do
conference . registration_limit = 1
conference . save!
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'OSEM_ICHAIN_ENABLED true, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'true' , '/conferences/myconf/register/edit' , nil
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'OSEM_ICHAIN_ENABLED true, user not registered' do
it_behaves_like 'access #new action' , :not_registered_user , 'true' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED true, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'true' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED true, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'true'
end
2017-02-02 01:28:22 +02:00
context 'OSEM_ICHAIN_ENABLED false, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'false' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED false, user not registered' do
it_behaves_like 'access #new action' , :not_registered_user , 'false' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
2016-08-11 21:48:59 +01:00
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED false, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'false' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED false, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'false'
end
2016-08-11 21:48:59 +01:00
end
end
context 'registration period not open' do
2017-02-02 01:28:22 +02:00
before :each do
create ( :registration_period , conference : conference , start_date : 3 . days . ago , end_date : 1 . day . ago )
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'registration limit not exceeded' do
before :each do
conference . registration_limit = 0
conference . save!
end
context 'OSEM_ICHAIN_ENABLED true, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'true' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED true, user not registered' do
it_behaves_like 'access #new action' , :not_registered_user , 'true' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED true, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'true' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED true, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'true'
end
2017-02-02 01:28:22 +02:00
context 'OSEM_ICHAIN_ENABLED false, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'false' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED false, user not registered' do
it_behaves_like 'access #new action' , :not_registered_user , 'false' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED false, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'false' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED false, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'false'
end
2017-02-02 01:28:22 +02:00
end
context 'registration limit exceeded' do
before do
conference . registration_limit = 1
conference . save!
end
context 'OSEM_ICHAIN_ENABLED true, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'true' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED true, user not registered' do
it_behaves_like 'access #new action' , :not_registered_user , 'true' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED true, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'true' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED true, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'true'
end
2017-02-02 01:28:22 +02:00
context 'OSEM_ICHAIN_ENABLED false, user registered' do
it_behaves_like 'access #new action' , :registered_user , 'false' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED false, user not registered' do
it_behaves_like 'access #new action' , :not_registered_user , 'false' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2017-05-04 13:55:37 +03:00
context 'OSEM_ICHAIN_ENABLED false, user registered, confirmed speaker' do
it_behaves_like 'access #new action' , :registered_confirmed_speaker , 'false' , '/conferences/myconf/register/edit' , nil
end
context 'OSEM_ICHAIN_ENABLED false, user not registered, confirmed speaker' do
it_behaves_like 'can access #new action' , :not_registered_confirmed_speaker , 'false'
end
2016-08-11 21:48:59 +01:00
end
end
end
2016-03-23 11:42:40 +05:30
describe 'GET #show' do
before do
@registration = create ( :registration , conference : conference , user : user )
2016-04-22 18:18:46 +03:00
@event_with_registration = create ( :event , program : conference . program , require_registration : true , max_attendees : 5 , state : 'confirmed' )
@event_without_registration = create ( :event , program : conference . program , require_registration : true , max_attendees : 5 , state : 'confirmed' )
@registration . events << @event_with_registration
2016-03-23 11:42:40 +05:30
end
context 'successful request' do
before do
2019-05-13 17:42:09 +02:00
get :show , params : { conference_id : conference . short_title }
2016-03-23 11:42:40 +05:30
end
2016-04-22 18:18:46 +03:00
it 'assigns variables' do
2016-03-23 11:42:40 +05:30
expect ( assigns ( :conference ) ) . to eq conference
expect ( assigns ( :registration ) ) . to eq @registration
end
it 'renders the show template' do
expect ( response ) . to render_template ( 'show' )
end
end
context 'user has purchased a ticket' do
before do
@ticket = create ( :ticket , conference : conference )
@purchased_ticket = create ( :ticket_purchase , conference : conference ,
2018-11-13 18:01:49 -08:00
user : user ,
ticket : @ticket )
2019-05-13 17:42:09 +02:00
get :show , params : { conference_id : conference . short_title }
2016-03-23 11:42:40 +05:30
end
2016-07-27 19:44:32 +05:30
it 'does not assign price of purchased tickets to total_price and purchased tickets to tickets without payment' do
2017-09-25 00:52:13 +05:30
expect ( assigns ( :total_price ) ) . to eq 0
2016-03-23 11:42:40 +05:30
end
end
context 'user has not purchased any ticket' do
before do
2019-05-13 17:42:09 +02:00
get :show , params : { conference_id : conference . short_title }
2016-03-23 11:42:40 +05:30
end
it 'assigns 0 dollars to total_price and empty array to tickets variables' do
2017-09-25 00:52:13 +05:30
expect ( assigns ( :total_price ) ) . to eq 0
2016-03-23 11:42:40 +05:30
expect ( assigns ( :tickets ) ) . to match_array [ ]
end
end
end
2016-03-22 17:13:15 +05:30
describe 'GET #edit' do
before do
@registration = create ( :registration , conference : conference , user : user )
2019-05-13 17:42:09 +02:00
get :edit , params : { conference_id : conference . short_title }
2016-03-22 17:13:15 +05:30
end
it 'assigns conference and registration variable' do
expect ( assigns ( :conference ) ) . to eq conference
expect ( assigns ( :registration ) ) . to eq @registration
end
it 'renders the edit template' do
expect ( response ) . to render_template ( 'edit' )
end
end
describe 'PATCH #update' do
before do
@registration = create ( :registration ,
conference : conference ,
2018-12-29 15:36:17 -08:00
user : user )
2016-03-22 17:13:15 +05:30
end
context 'updates successfully' do
before do
2018-12-29 15:36:17 -08:00
patch :update , params : {
registration : attributes_for ( :registration , volunteer : true ) ,
conference_id : conference . short_title
}
2016-03-22 17:13:15 +05:30
end
it 'redirects to registration show path' do
2016-07-05 00:10:18 +05:30
expect ( response ) . to redirect_to conference_conference_registration_path ( conference . short_title )
2016-03-22 17:13:15 +05:30
end
it 'shows success message in flash notice' do
expect ( flash [ :notice ] ) . to match ( 'Registration was successfully updated.' )
end
it 'updates the registration' do
2018-12-29 15:36:17 -08:00
expect { @registration . reload } . to change ( @registration , :updated_at )
2016-03-22 17:13:15 +05:30
end
end
context 'update fails' do
before do
2022-02-14 17:53:58 +01:00
allow_any_instance_of ( Registration ) . to receive ( :update ) . and_return ( false )
2018-12-29 15:36:17 -08:00
patch :update , params : {
registration : attributes_for ( :registration , volunteer : true ) ,
conference_id : conference . short_title
}
2016-03-22 17:13:15 +05:30
end
it 'renders edit template' do
expect ( response ) . to render_template ( 'edit' )
end
it 'shows error in flash message' do
2016-05-02 15:25:18 +02:00
expect ( flash [ :error ] ) . to match " Could not update your registration for #{ conference . title } : #{ @registration . errors . full_messages . join ( '. ' ) } . "
2016-03-22 17:13:15 +05:30
end
it 'does not update the registration' do
@registration . reload
2018-12-29 15:36:17 -08:00
expect { @registration . reload } . not_to change ( @registration , :updated_at )
2016-03-22 17:13:15 +05:30
end
end
end
2016-03-23 11:42:40 +05:30
describe 'DELETE #destroy' do
before do
@registration = create ( :registration , conference : conference , user : user )
end
context 'deletes successfully' do
before ( :each , run : true ) do
2019-05-13 17:42:09 +02:00
delete :destroy , params : { conference_id : conference . short_title }
2016-03-23 11:42:40 +05:30
end
it 'redirects to root path' , run : true do
expect ( response ) . to redirect_to root_path
end
it 'shows success message in flash notice' , run : true do
2016-05-02 15:25:18 +02:00
expect ( flash [ :notice ] ) . to match ( " You are not registered for #{ conference . title } anymore! " )
2016-03-23 11:42:40 +05:30
end
it 'deletes the registration' do
expect do
2019-05-13 17:42:09 +02:00
delete :destroy , params : { conference_id : conference . short_title }
2017-02-02 01:28:22 +02:00
end . to change { Registration . count } . from ( 2 ) . to ( 1 )
2016-03-23 11:42:40 +05:30
end
end
context 'delete fails' do
before do
allow_any_instance_of ( Registration ) . to receive ( :destroy ) . and_return ( false )
2019-05-13 17:42:09 +02:00
delete :destroy , params : { conference_id : conference . short_title }
2016-03-23 11:42:40 +05:30
end
it 'redirects to registration show path' do
2016-07-05 00:10:18 +05:30
expect ( response ) . to redirect_to conference_conference_registration_path ( conference . short_title )
2016-03-23 11:42:40 +05:30
end
it 'shows error in flash message' do
2016-05-02 15:25:18 +02:00
expect ( flash [ :error ] ) . to match " Could not delete your registration for #{ conference . title } : #{ @registration . errors . full_messages . join ( '. ' ) } . "
2016-03-23 11:42:40 +05:30
end
it 'does not delete the registration' do
expect ( assigns ( :registration ) ) . to eq @registration
end
end
end
2016-03-22 17:13:15 +05:30
end
2016-08-11 21:48:59 +01:00
context 'user is not signed in' do
describe 'GET #new' do
2017-02-02 01:28:22 +02:00
context 'registration period open' do
before :each do
create ( :registration_period , conference : conference , start_date : 3 . days . ago , end_date : 1 . day . from_now )
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'registration limit not exceeded' do
before :each do
conference . registration_limit = 0
conference . save!
end
context 'OSEM_ICHAIN_ENABLED is true' do
it_behaves_like 'access #new action' , nil , 'true' , '/' , 'You are not authorized to access this page. Maybe you need to sign in?'
end
context 'OSEM_ICHAIN_ENABLED is false' do
2017-05-04 13:55:37 +03:00
it_behaves_like 'can access #new action' , nil , 'false'
2017-02-02 01:28:22 +02:00
end
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'registration limit exceeded' do
before :each do
conference . registration_limit = 1
conference . save!
end
context 'OSEM_ICHAIN_ENABLED is true' do
it_behaves_like 'access #new action' , nil , 'true' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
context 'OSEM_ICHAIN_ENABLED is false' do
it_behaves_like 'access #new action' , nil , 'false' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2016-08-11 21:48:59 +01:00
end
end
2017-02-02 01:28:22 +02:00
context 'registration period not open' do
before :each do
create ( :registration_period , conference : conference , start_date : 3 . days . ago , end_date : 1 . day . ago )
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'registration limit not exceeded' do
before :each do
conference . registration_limit = 0
conference . save!
end
context 'OSEM_ICHAIN_ENABLED is true' do
it_behaves_like 'access #new action' , nil , 'true' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
context 'OSEM_ICHAIN_ENABLED is false' do
it_behaves_like 'access #new action' , nil , 'false' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2016-08-11 21:48:59 +01:00
end
2017-02-02 01:28:22 +02:00
context 'registration limit exceeded' do
before :each do
conference . registration_limit = 1
conference . save!
end
context 'OSEM_ICHAIN_ENABLED is true' do
it_behaves_like 'access #new action' , nil , 'true' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
context 'OSEM_ICHAIN_ENABLED is false' do
it_behaves_like 'access #new action' , nil , 'false' , '/' , 'Sorry, you can not register for My Conference. Registration limit exceeded or the registration is not open.'
end
2016-08-11 21:48:59 +01:00
end
end
end
end
2016-03-22 17:13:15 +05:30
end