diff --git a/spec/controllers/announcements_controller_spec.rb b/spec/controllers/announcements_controller_spec.rb index c586d6809..cbf25a9ca 100644 --- a/spec/controllers/announcements_controller_spec.rb +++ b/spec/controllers/announcements_controller_spec.rb @@ -11,9 +11,7 @@ end describe AnnouncementsController, type: :controller do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end + before(:each) { mock_app_config } describe 'with admin' do before do diff --git a/spec/controllers/app_configs_controller_spec.rb b/spec/controllers/app_configs_controller_spec.rb index 33fca2376..0ed59ef6f 100644 --- a/spec/controllers/app_configs_controller_spec.rb +++ b/spec/controllers/app_configs_controller_spec.rb @@ -135,9 +135,6 @@ end end context 'app_config does not exist yet' do - before(:all) do - AppConfig.destroy_all - end before(:each) do sign_in FactoryGirl.create(:user) post :update diff --git a/spec/controllers/blackouts_controller_spec.rb b/spec/controllers/blackouts_controller_spec.rb index bb58add01..a32b37d48 100644 --- a/spec/controllers/blackouts_controller_spec.rb +++ b/spec/controllers/blackouts_controller_spec.rb @@ -11,9 +11,7 @@ end describe BlackoutsController, type: :controller do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end + before(:each) { mock_app_config } describe 'with admin' do before do diff --git a/spec/controllers/categories_controller_spec.rb b/spec/controllers/categories_controller_spec.rb index ac6c24ded..ebc5ec9c1 100644 --- a/spec/controllers/categories_controller_spec.rb +++ b/spec/controllers/categories_controller_spec.rb @@ -1,10 +1,8 @@ require 'spec_helper' describe CategoriesController, type: :controller do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end before(:each) do + mock_app_config @category = FactoryGirl.create(:category) end @@ -186,7 +184,4 @@ end end end - after(:all) do - @app_config.destroy - end end diff --git a/spec/controllers/contact_controller_spec.rb b/spec/controllers/contact_controller_spec.rb index 93cf2fd4e..324ddc03d 100644 --- a/spec/controllers/contact_controller_spec.rb +++ b/spec/controllers/contact_controller_spec.rb @@ -1,12 +1,14 @@ require 'spec_helper' describe ContactController, type: :controller do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end before(:each) do @category = FactoryGirl.create(:category) sign_in FactoryGirl.create(:user) + + # goes after the above to skip certain user validations + @ac = mock_app_config(contact_email: 'contact@email.com', + admin_email: 'admin@email.com', + site_title: 'Reservations Specs') end describe 'GET new' do before(:each) do @@ -48,8 +50,8 @@ end context 'with contact e-mail set' do before do - AppConfig.first - .update_attributes(contact_link_location: 'contact@example.com') + allow(@ac).to receive(:contact_link_location) + .and_return('contact@example.com') post :create, message: FactoryGirl.attributes_for(:message) end @@ -60,7 +62,7 @@ end context 'with contact e-mail not set' do before do - AppConfig.first.update_attributes(contact_link_location: '') + allow(@ac).to receive(:contact_link_location).and_return('') post :create, message: FactoryGirl.attributes_for(:message) end @@ -69,7 +71,4 @@ include(AppConfig.first.admin_email) end end - after(:all) do - @app_config.destroy - end end diff --git a/spec/controllers/equipment_items_controller_spec.rb b/spec/controllers/equipment_items_controller_spec.rb index 2a4c39fbc..25d2549d0 100644 --- a/spec/controllers/equipment_items_controller_spec.rb +++ b/spec/controllers/equipment_items_controller_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe EquipmentItemsController, type: :controller do - before(:all) { @app_config = FactoryGirl.create(:app_config) } + before(:each) { mock_app_config } let!(:item) { FactoryGirl.create(:equipment_item) } let!(:deactivated_item) { FactoryGirl.create(:deactivated) } @@ -342,6 +342,4 @@ end end end - - after(:all) { @app_config.destroy } end diff --git a/spec/controllers/equipment_models_controller_spec.rb b/spec/controllers/equipment_models_controller_spec.rb index 835eae6c9..c21166f0c 100644 --- a/spec/controllers/equipment_models_controller_spec.rb +++ b/spec/controllers/equipment_models_controller_spec.rb @@ -105,7 +105,7 @@ end describe EquipmentModelsController, type: :controller do - before(:all) { @app_config = FactoryGirl.create(:app_config) } + before(:each) { mock_app_config } let!(:model) { FactoryGirl.create(:equipment_model) } it_behaves_like 'calendarable', EquipmentModel @@ -415,6 +415,4 @@ end end end - - after(:all) { @app_config.destroy } end diff --git a/spec/controllers/import_users_controller_spec.rb b/spec/controllers/import_users_controller_spec.rb index 36d556a99..b3d2e5b9e 100644 --- a/spec/controllers/import_users_controller_spec.rb +++ b/spec/controllers/import_users_controller_spec.rb @@ -15,9 +15,7 @@ end describe ImportUsersController, type: :controller do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end + before(:each) { mock_app_config } before(:each) do sign_in FactoryGirl.create(:admin) diff --git a/spec/controllers/reports_controller_spec.rb b/spec/controllers/reports_controller_spec.rb index 132840820..cc0410744 100644 --- a/spec/controllers/reports_controller_spec.rb +++ b/spec/controllers/reports_controller_spec.rb @@ -2,20 +2,17 @@ describe ReportsController, type: :controller do before(:all) do - @app_config = FactoryGirl.create(:app_config) @user = FactoryGirl.create(:user) @banned = FactoryGirl.create(:banned) @checkout_person = FactoryGirl.create(:checkout_person) @admin = FactoryGirl.create(:admin) end - after(:all) do - AppConfig.delete_all - User.delete_all - end - context 'as admin user' do - before(:each) { sign_in @admin } + before(:each) do + mock_app_config + sign_in @admin + end describe 'PUT /reports/update' do it 'defaults to the past year without a session or params' do diff --git a/spec/controllers/requirements_controller_spec.rb b/spec/controllers/requirements_controller_spec.rb index 3c0a4a1d2..4c7d1874f 100644 --- a/spec/controllers/requirements_controller_spec.rb +++ b/spec/controllers/requirements_controller_spec.rb @@ -4,10 +4,8 @@ # -- namely, it was necessary to test two contexts for each method: the user # being an admin, and not. describe RequirementsController, type: :controller do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end before(:each) do + mock_app_config @requirement = FactoryGirl.create(:requirement, contact_name: 'Adam Bray') end describe 'GET index' do @@ -212,7 +210,4 @@ end end end - after(:all) do - @app_config.destroy - end end diff --git a/spec/controllers/reservations_controller_spec.rb b/spec/controllers/reservations_controller_spec.rb index caeab6b01..0b7e96fbe 100644 --- a/spec/controllers/reservations_controller_spec.rb +++ b/spec/controllers/reservations_controller_spec.rb @@ -6,28 +6,20 @@ render_views before(:all) do - @app_config = FactoryGirl.create(:app_config) - @user = FactoryGirl.create(:user) @banned = FactoryGirl.create(:banned) @checkout_person = FactoryGirl.create(:checkout_person) @admin = FactoryGirl.create(:admin) end - after(:all) do - User.delete_all - AppConfig.delete_all - Reservation.delete_all - Category.delete_all - EquipmentModel.delete_all - EquipmentItem.delete_all - end - before(:each) do + @ac = mock_app_config(**FactoryGirl.attributes_for(:app_config), + disable_user_emails: false, + override_on_create: false, + override_at_checkout: false, + res_exp_time: false) @cart = FactoryGirl.build(:cart, reserver_id: @user.id) - sign_in @user - @reservation = FactoryGirl.create(:valid_reservation, reserver: @user) end @@ -91,53 +83,51 @@ # depending on admin status, default_filter changes # depending on admin status, source of reservations (all v. own) changes - before(:all) do - @filters = [:reserved, :checked_out, :overdue, :missed, - :returned, :upcoming, :archived] - end + FILTERS = [:reserved, :checked_out, :overdue, :missed, + :returned, :upcoming, :archived] context 'when accessed by non-banned user' do subject { get :index } it { is_expected.to be_success } it { is_expected.to render_template(:index) } - it 'populates @reservations_set with respect to params[filter]' do - # Setup - @filters.each do |trait| - res = FactoryGirl.build(:valid_reservation, trait, reserver: @user) - res.save(validate: false) + shared_examples 'populates' do |trait| + before(:each) do + type = case trait + when :reserved + :valid_reservation + when :returned + :checked_in_reservation + else + (trait.to_s + '_reservation').to_sym + end + FactoryGirl.create(type, reserver: @user) end - # Assertion and expectation - @filters.each do |f| - get :index, f => true + it 'with the correct set' do + get :index, trait => true expect(assigns(:reservations_set).uniq.sort).to \ - eq(Reservation.send(f) + eq(Reservation.send(trait) .starts_on_days(assigns(:start_date), assigns(:end_date)) .uniq.sort) end - end - it 'populates with respect to session[:filter] first' do - @filters.each do |trait| - res = FactoryGirl.build(:valid_reservation, trait, reserver: @user) - res.save(validate: false) - end - # Assertion and expectation - @filters.each do |f| - session[:filter] = f.to_s - get :index, @filters.sample => true + it 'with respect to session[:filter] first' do + session[:filter] = trait.to_s + get :index, FILTERS.sample => true expect(assigns(:reservations_set).uniq.sort).to \ - eq(Reservation.send(f) + eq(Reservation.send(trait) .starts_on_days(assigns(:start_date), assigns(:end_date)) .uniq.sort) end end + FILTERS.each { |t| it_behaves_like 'populates', t } + context 'who is an admin' do before(:each) do sign_in @admin - @filters.each do |trait| + FILTERS.each do |trait| res = FactoryGirl.build(:valid_reservation, trait, reserver: [@user, @admin].sample) res.save(validate: false) @@ -153,7 +143,7 @@ context 'who is not an admin' do before(:each) do sign_in @user - @filters.each do |trait| + FILTERS.each do |trait| res = FactoryGirl.build(:valid_reservation, trait, reserver: [@user, @admin].sample) res.save(validate: false) @@ -305,7 +295,7 @@ context 'and user can override errors' do before(:each) do - AppConfig.first.update_attributes(override_on_create: true) + allow(@ac).to receive(:override_on_create).and_return(true) sign_in @checkout_person end @@ -333,7 +323,7 @@ context 'and user cannot override errors' do # request would be filed before(:each) do - AppConfig.first.update_attributes(override_on_create: false) + allow(@ac).to receive(:override_on_create).and_return(false) sign_in @checkout_person end it 'affects database' do @@ -403,7 +393,7 @@ context 'with notify_admin_on_create set' do before(:each) do ActionMailer::Base.deliveries.clear - AppConfig.first.update_attributes(notify_admin_on_create: true) + allow(@ac).to receive(:notify_admin_on_create).and_return(true) end it 'cc-s the admin on the confirmation email' do @@ -418,7 +408,7 @@ context 'without notify_admin_on_create set' do before(:each) do ActionMailer::Base.deliveries.clear - AppConfig.first.update_attributes(notify_admin_on_create: false) + allow(@ac).to receive(:notify_admin_on_create).and_return(false) end it 'cc-s the admin on the confirmation email' do @@ -479,7 +469,7 @@ context 'when accessed by checkout person disallowed by settings' do before(:each) do sign_in @checkout_person - AppConfig.first.update_attributes(checkout_persons_can_edit: false) + allow(@ac).to receive(:checkout_persons_can_edit).and_return(false) get 'edit', id: @reservation.id end include_examples 'cannot access page' @@ -504,7 +494,7 @@ context 'when accessed by checkout person allowed by settings' do before(:each) do sign_in @checkout_person - AppConfig.first.update_attributes(checkout_persons_can_edit: true) + allow(@ac).to receive(:checkout_persons_can_edit).and_return(true) get :edit, id: @reservation.id end include_examples 'can access edit page' @@ -513,7 +503,7 @@ context 'when accessed by admin' do before(:each) do sign_in @admin - AppConfig.first.update_attributes(checkout_persons_can_edit: false) + allow(@ac).to receive(:checkout_persons_can_edit).and_return(false) get :edit, id: @reservation.id end include_examples 'can access edit page' @@ -551,7 +541,7 @@ context 'when accessed by checkout person disallowed by settings' do before(:each) do sign_in @checkout_person - AppConfig.first.update_attributes(checkout_persons_can_edit: false) + allow(@ac).to receive(:checkout_persons_can_edit).and_return(false) put 'update', id: @reservation.id, reservation: FactoryGirl.attributes_for(:reservation) @@ -720,7 +710,7 @@ context 'when accessed by checkout person allowed by settings' do before(:each) do sign_in @checkout_person - AppConfig.first.update_attributes(checkout_persons_can_edit: true) + allow(@ac).to receive(:checkout_persons_can_edit).and_return(true) end include_examples 'can access update page' end @@ -728,7 +718,7 @@ context 'when accessed by admin' do before(:each) do sign_in @admin - AppConfig.first.update_attributes(checkout_persons_can_edit: false) + allow(@ac).to receive(:checkout_persons_can_edit).and_return(false) end include_examples 'can access update page' end @@ -1535,12 +1525,9 @@ context 'when auto-deactivate is enabled' do before(:each) do - AppConfig.first.update_attributes(autodeactivate_on_archive: true) + allow(@ac).to receive(:autodeactivate_on_archive).and_return(true) put :archive, id: @reservation.id, archive_note: 'Because I can' end - after(:each) do - AppConfig.first.update_attributes(autodeactivate_on_archive: false) - end it 'should deactivate the equipment item' do ei = @reservation.equipment_item.reload diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 4fd1270e2..b379cd3e1 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -11,9 +11,7 @@ end describe UsersController, type: :controller do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end + before(:each) { @ac = mock_app_config } let!(:user) { FactoryGirl.create(:user) } context 'with admin user' do @@ -126,7 +124,7 @@ context 'when new user registration is disabled' do before do - AppConfig.first.update_attributes(enable_new_users: false) + allow(@ac).to receive(:enable_new_users).and_return(false) get :new end @@ -163,7 +161,7 @@ context 'when new user registration is disabled and correct params' do before do - AppConfig.first.update_attributes(enable_new_users: false) + allow(@ac).to receive(:enable_new_users).and_return(false) @user_attributes = FactoryGirl.attributes_for(:user) post :create, user: @user_attributes end @@ -334,7 +332,7 @@ describe 'GET new when new user registration is disabled' do before do - AppConfig.first.update_attributes(enable_new_users: false) + allow(@ac).to receive(:enable_new_users).and_return(false) get :new end diff --git a/spec/factories/app_configs.rb b/spec/factories/app_configs.rb index c2824bec7..ca4c9f042 100644 --- a/spec/factories/app_configs.rb +++ b/spec/factories/app_configs.rb @@ -11,7 +11,7 @@ contact_link_location 'contact@email.com' home_link_text 'MyString' home_link_location 'MyString' - default_per_cat_page 1 + default_per_cat_page 10 upcoming_checkin_email_body 'MyText' upcoming_checkout_email_body 'MyText' overdue_checkin_email_body 'MyText' diff --git a/spec/features/guest_spec.rb b/spec/features/guest_spec.rb index 0a653dfcc..a9985107c 100644 --- a/spec/features/guest_spec.rb +++ b/spec/features/guest_spec.rb @@ -50,7 +50,7 @@ context 'when enabled' do before(:each) do - AppConfig.first.update_attribute(:enable_guests, true) + allow(@app_config).to receive(:enable_guests).and_return(true) end it 'correctly sets the setting' do @@ -124,7 +124,7 @@ context 'when disabled' do before(:each) do - AppConfig.first.update_attribute(:enable_guests, false) + allow(@app_config).to receive(:enable_guests).and_return(false) end it 'correctly sets the setting' do diff --git a/spec/features/reservations_archive_spec.rb b/spec/features/reservations_archive_spec.rb index d88b6d3e5..484ce0ca4 100644 --- a/spec/features/reservations_archive_spec.rb +++ b/spec/features/reservations_archive_spec.rb @@ -54,10 +54,8 @@ context 'with auto-deactivate enabled' do before do - @app_config.update_attributes(autodeactivate_on_archive: true) - end - after do - @app_config.update_attributes(autodeactivate_on_archive: false) + allow(@app_config).to receive(:autodeactivate_on_archive) + .and_return(true) end it 'autodeactivates the equipment item', js: true do @@ -73,9 +71,9 @@ context 'without auto-deactivate enabled', js: true do before do - @app_config.update_attributes(autodeactivate_on_archive: false) + allow(@app_config).to receive(:autodeactivate_on_archive) + .and_return(false) end - after { @app_config.update_attributes(autodeactivate_on_archive: true) } it 'does not autodeactivate the equipment item' do visit reservation_path(@res) diff --git a/spec/features/reservations_spec.rb b/spec/features/reservations_spec.rb index 14f23a898..249e4ea9e 100644 --- a/spec/features/reservations_spec.rb +++ b/spec/features/reservations_spec.rb @@ -129,14 +129,18 @@ after { sign_out } context 'without override permissions' do - before { AppConfig.first.update_attributes(override_on_create: false) } + before do + allow(@app_config).to receive(:override_on_create).and_return(false) + end it_behaves_like 'can create valid reservation', @user it_behaves_like 'can create reservation request', @user end context 'with override permissions' do - before { AppConfig.first.update_attributes(override_on_create: true) } + before do + allow(@app_config).to receive(:override_on_create).and_return(true) + end it_behaves_like 'can create failing reservation', @user end @@ -147,14 +151,18 @@ after { sign_out } context 'with override disabled' do - before { AppConfig.first.update_attributes(override_on_create: false) } + before do + allow(@app_config).to receive(:override_on_create).and_return(false) + end it_behaves_like 'can create valid reservation', @user it_behaves_like 'can create failing reservation', @user end context 'with override enabled' do - before { AppConfig.first.update_attributes(override_on_create: true) } + before do + allow(@app_config).to receive(:override_on_create).and_return(true) + end it_behaves_like 'can create failing reservation', @user end @@ -165,14 +173,18 @@ after { sign_out } context 'with override disabled' do - before { AppConfig.first.update_attributes(override_on_create: false) } + before do + allow(@app_config).to receive(:override_on_create).and_return(false) + end it_behaves_like 'can create valid reservation', @user it_behaves_like 'can create failing reservation', @user end context 'with override enabled' do - before { AppConfig.first.update_attributes(override_on_create: true) } + before do + allow(@app_config).to receive(:override_on_create).and_return(true) + end it_behaves_like 'can create failing reservation', @user end @@ -433,7 +445,7 @@ shared_examples 'can renew reservation when enabled and available' do it do - AppConfig.first.update_attributes(enable_renewals: true) + allow(@app_config).to receive(:enable_renewals).and_return(true) visit reservation_path(@res) expect(page).to have_content 'You are currently eligible to renew' click_link 'Renew Now', href: renew_reservation_path(@res) @@ -444,7 +456,7 @@ shared_examples 'cannot see renew button when disabled' do it do - AppConfig.first.update_attributes(enable_renewals: false) + allow(@app_config).to receive(:enable_renewals).and_return(false) visit reservation_path(@res) expect(page).not_to have_link 'Renew Now', href: renew_reservation_path(@res) @@ -453,7 +465,7 @@ shared_examples 'cannot renew reservation when unavailable' do it do - AppConfig.first.update_attributes(enable_renewals: true) + allow(@app_config).to receive(:enable_renewals).and_return(true) FactoryGirl.create :reservation, equipment_model: @eq_model, start_date: @res.due_date + 1.day, due_date: @res.due_date + 2.days @@ -538,7 +550,7 @@ it_behaves_like 'cannot renew reservation when unavailable' it 'can see renew button when disabled' do - AppConfig.first.update_attributes(enable_renewals: false) + allow(@app_config).to receive(:enable_renewals).and_return(false) visit reservation_path(@res) expect(page).to have_link 'Renew Now', href: renew_reservation_path(@res) @@ -549,10 +561,8 @@ context 'valid items on confirmation page' do before(:each) do empty_cart - select('10', from: 'items_per_page') # show more than 1 item (default) find('#items_per_form').submit_form! add_item_to_cart(@eq_model) - add_item_to_cart(@eq_model2) update_cart_start_date(Time.zone.today) due_date = Time.zone.today + 1.day update_cart_due_date(due_date) @@ -582,6 +592,7 @@ shared_examples 'will load request page if item is invalid' do |reserver| before(:each) do + allow(@app_config).to receive(:override_on_create).and_return(false) visit new_reservation_path end let(:reserver) { reserver } @@ -592,22 +603,20 @@ fill_in "quantity_field_#{@eq_model.id}", with: (@eq_model.max_per_user + 1) quantity_forms[0].submit_form! - fill_in "quantity_field_#{@eq_model2.id}", - with: (@eq_model2.max_per_user + 1) - quantity_forms[1].submit_form! # loading right page expect(page).to have_content 'Confirm Reservation Request' expect(page).to have_content AppConfig.get(:request_text) # changes applied expect(page).to \ have_selector("input[value='#{@eq_model.max_per_user + 1}']") - expect(page).to \ - have_selector("input[value='#{@eq_model2.max_per_user + 1}']") end end shared_examples 'can remove a valid item' do |reserver| before(:each) do + @eq_model2 = FactoryGirl.create(:equipment_model, category: @category) + FactoryGirl.create(:equipment_item, equipment_model: @eq_model2) + add_item_to_cart(@eq_model2) visit new_reservation_path end let(:reserver) { reserver } @@ -625,6 +634,9 @@ shared_examples 'can remove all items' do |reserver| before(:each) do + @eq_model2 = FactoryGirl.create(:equipment_model, category: @category) + FactoryGirl.create(:equipment_item, equipment_model: @eq_model2) + add_item_to_cart(@eq_model2) visit new_reservation_path end let(:reserver) { reserver } @@ -667,6 +679,7 @@ shared_examples 'will load request if invalid date change' do |reserver| before(:each) do + allow(@app_config).to receive(:override_on_create).and_return(false) visit new_reservation_path end let(:reserver) { reserver } @@ -744,9 +757,8 @@ context 'invalid items on confirm page' do before(:each) do empty_cart - # change items per page from 1 to 10 so second item shows up - select('10', from: 'items_per_page') - find('#items_per_form').submit_form! + @eq_model2 = FactoryGirl.create(:equipment_model, category: @category) + FactoryGirl.create(:equipment_item, equipment_model: @eq_model2) add_item_to_cart(@eq_model) add_item_to_cart(@eq_model2) quantity_forms = page.all('#quantity_form') diff --git a/spec/lib/csv_export_spec.rb b/spec/lib/csv_export_spec.rb index a0ceac3b2..ed898e9da 100644 --- a/spec/lib/csv_export_spec.rb +++ b/spec/lib/csv_export_spec.rb @@ -2,8 +2,6 @@ include CsvExport describe CsvExport do - before(:all) { FactoryGirl.create(:app_config) } - MODELS = [:user, :category, :equipment_model, :equipment_item] PROTECTED_COLS = %w(id encrypted_password reset_password_token reset_password_sent_at) diff --git a/spec/lib/tasks/delete_missed_reservations_rake_spec.rb b/spec/lib/tasks/delete_missed_reservations_rake_spec.rb index 467e16eee..9a16351e4 100644 --- a/spec/lib/tasks/delete_missed_reservations_rake_spec.rb +++ b/spec/lib/tasks/delete_missed_reservations_rake_spec.rb @@ -4,9 +4,8 @@ describe 'delete_missed_reservations' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - before(:each) do + @ac = mock_app_config(blank?: false) @missed = FactoryGirl.create(:missed_reservation, start_date: Time.zone.today - 11.days, due_date: Time.zone.today - 10.days) @@ -14,20 +13,17 @@ end it "doesn't do anything when the res_exp_time parameter isn't set" do - AppConfig.first.update_attributes(res_exp_time: nil) - expect(AppConfig.first.res_exp_time).to be_nil + allow(@ac).to receive(:res_exp_time).and_return(nil) expect { subject.invoke }.not_to change { Reservation.count } end it 'deletes reservations older than the threshhold' do - AppConfig.first.update_attributes(res_exp_time: 5) - expect(AppConfig.first.res_exp_time).to eq(5) + allow(@ac).to receive(:res_exp_time).and_return(5) expect { subject.invoke }.to change { Reservation.count }.by(-1) end it "doesn't delete reservations within threshhold" do - AppConfig.first.update_attributes(res_exp_time: 15) - expect(AppConfig.first.res_exp_time).to eq(15) + allow(@ac).to receive(:res_exp_time).and_return(15) expect { subject.invoke }.not_to change { Reservation.count } end end diff --git a/spec/lib/tasks/delete_old_blackouts_rake_spec.rb b/spec/lib/tasks/delete_old_blackouts_rake_spec.rb index 4b6114cea..2aa0c2f9b 100644 --- a/spec/lib/tasks/delete_old_blackouts_rake_spec.rb +++ b/spec/lib/tasks/delete_old_blackouts_rake_spec.rb @@ -4,9 +4,9 @@ describe 'delete_old_blackouts' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - before(:each) do + @ac = mock_app_config + allow(AppConfig).to receive(:blank?).and_return(false) @old = FactoryGirl.create(:blackout, start_date: Time.zone.today - 11.days, end_date: Time.zone.today - 10.days) @@ -14,20 +14,17 @@ end it "doesn't do anything when the res_exp_time parameter isn't set" do - AppConfig.first.update_attributes(blackout_exp_time: nil) - expect(AppConfig.first.blackout_exp_time).to be_nil + allow(@ac).to receive(:blackout_exp_time).and_return(nil) expect { subject.invoke }.not_to change { Blackout.count } end it 'deletes reservations older than the threshhold' do - AppConfig.first.update_attributes(blackout_exp_time: 5) - expect(AppConfig.first.blackout_exp_time).to eq(5) + allow(@ac).to receive(:blackout_exp_time).and_return(5) expect { subject.invoke }.to change { Blackout.count }.by(-1) end it "doesn't delete reservations within threshhold" do - AppConfig.first.update_attributes(blackout_exp_time: 15) - expect(AppConfig.first.blackout_exp_time).to eq(15) + allow(@ac).to receive(:blackout_exp_time).and_return(15) expect { subject.invoke }.not_to change { Blackout.count } end end diff --git a/spec/lib/tasks/email_checkin_reminder_rake_spec.rb b/spec/lib/tasks/email_checkin_reminder_rake_spec.rb index 07bc83353..fd0300b36 100644 --- a/spec/lib/tasks/email_checkin_reminder_rake_spec.rb +++ b/spec/lib/tasks/email_checkin_reminder_rake_spec.rb @@ -4,9 +4,9 @@ describe 'email_checkin_reminder' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - before(:each) do + @ac = mock_app_config(admin_email: 'admin@email.com', + disable_user_emails: false) @upcoming = FactoryGirl.create(:checked_out_reservation, due_date: Time.zone.today) @not_upcoming = FactoryGirl.create(:valid_reservation, @@ -15,8 +15,7 @@ end it 'sends emails for checked-out reservations that end today' do - AppConfig.first.update_attributes(upcoming_checkin_email_active: true) - expect(AppConfig.first.upcoming_checkin_email_active).to eq(true) + allow(@ac).to receive(:upcoming_checkin_email_active?).and_return(true) expect { subject.invoke }.to( change { ActionMailer::Base.deliveries.count }.by(1)) end @@ -26,15 +25,13 @@ FactoryGirl.attributes_for(:valid_reservation, start_date: Time.zone.today - 1.day, due_date: Time.zone.today)) - AppConfig.first.update_attributes(upcoming_checkin_email_active: true) - expect(AppConfig.first.upcoming_checkin_email_active).to eq(true) + allow(@ac).to receive(:upcoming_checkin_email_active?).and_return(true) expect(@upcoming.reserved?).to be_truthy expect { subject.invoke }.not_to( change { ActionMailer::Base.deliveries.count }) end it "doesn't send emails when upcoming_checkin_email_active is false" do - AppConfig.first.update_attributes(upcoming_checkin_email_active: false) - expect(AppConfig.first.upcoming_checkin_email_active).to eq(false) + allow(@ac).to receive(:upcoming_checkin_email_active?).and_return(false) expect { subject.invoke }.not_to( change { ActionMailer::Base.deliveries.count }) end diff --git a/spec/lib/tasks/email_checkout_reminder_rake_spec.rb b/spec/lib/tasks/email_checkout_reminder_rake_spec.rb index c4f1b013b..8ae72df43 100644 --- a/spec/lib/tasks/email_checkout_reminder_rake_spec.rb +++ b/spec/lib/tasks/email_checkout_reminder_rake_spec.rb @@ -4,9 +4,9 @@ describe 'email_checkout_reminder' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - before(:each) do + @ac = mock_app_config(admin_email: 'admin@email.com', + disable_user_emails: false) @upcoming = FactoryGirl.create(:valid_reservation, start_date: Time.zone.today, due_date: Time.zone.today + 1) @@ -16,15 +16,13 @@ end it 'sends emails for reservations that start today' do - AppConfig.first.update_attributes(upcoming_checkout_email_active: true) - expect(AppConfig.first.upcoming_checkout_email_active).to eq(true) + allow(@ac).to receive(:upcoming_checkout_email_active?).and_return(true) expect { subject.invoke }.to( change { ActionMailer::Base.deliveries.count }.by(1)) end it "doesn't send emails when upcoming_checkout_email_active is false" do - AppConfig.first.update_attributes(upcoming_checkout_email_active: false) - expect(AppConfig.first.upcoming_checkout_email_active).to eq(false) + allow(@ac).to receive(:upcoming_checkout_email_active?).and_return(false) expect { subject.invoke }.not_to( change { ActionMailer::Base.deliveries.count }) end diff --git a/spec/lib/tasks/email_missed_reservations_rake_spec.rb b/spec/lib/tasks/email_missed_reservations_rake_spec.rb index 7722f8e1f..fecccf47a 100644 --- a/spec/lib/tasks/email_missed_reservations_rake_spec.rb +++ b/spec/lib/tasks/email_missed_reservations_rake_spec.rb @@ -4,9 +4,9 @@ describe 'email_missed_reservations' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - before(:each) do + mock_app_config(admin_email: 'admin@email.com', disable_user_emails: false, + send_notifications_for_deleted_missed_reservations: true) @missed = FactoryGirl.create(:missed_reservation, start_date: Time.zone.today - 2.days, due_date: Time.zone.today - 1.day) diff --git a/spec/lib/tasks/email_notes_to_admins_rake_spec.rb b/spec/lib/tasks/email_notes_to_admins_rake_spec.rb index 3a4402677..e6c951f4f 100644 --- a/spec/lib/tasks/email_notes_to_admins_rake_spec.rb +++ b/spec/lib/tasks/email_notes_to_admins_rake_spec.rb @@ -4,7 +4,7 @@ describe 'email_notes_to_admins' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } + before(:each) { mock_app_config(admin_email: 'admin@email.com') } STATUSES = [:checked_out_reservation, :checked_in_reservation] diff --git a/spec/lib/tasks/email_overdue_reminder_rake_spec.rb b/spec/lib/tasks/email_overdue_reminder_rake_spec.rb index ea27b894b..6ea6920b2 100644 --- a/spec/lib/tasks/email_overdue_reminder_rake_spec.rb +++ b/spec/lib/tasks/email_overdue_reminder_rake_spec.rb @@ -4,24 +4,22 @@ describe 'email_overdue_reminder' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - before(:each) do + @ac = mock_app_config(admin_email: 'admin@email.com', + disable_user_emails: false) @overdue = FactoryGirl.build(:overdue_reservation) @overdue.save(validate: false) @not_overdue = FactoryGirl.create(:checked_out_reservation) end it 'sends emails for reservations that are overdue' do - AppConfig.first.update_attributes(overdue_checkin_email_active: true) - expect(AppConfig.first.overdue_checkin_email_active).to eq(true) + allow(@ac).to receive(:overdue_checkin_email_active?).and_return(true) expect { subject.invoke }.to( change { ActionMailer::Base.deliveries.count }.by(1)) end it "doesn't send emails when overdue_checkin_email_active is false" do - AppConfig.first.update_attributes(overdue_checkin_email_active: false) - expect(AppConfig.first.overdue_checkin_email_active).to eq(false) + allow(@ac).to receive(:overdue_checkin_email_active?).and_return(false) expect { subject.invoke }.not_to( change { ActionMailer::Base.deliveries.count }) end diff --git a/spec/lib/tasks/flag_rake_spec.rb b/spec/lib/tasks/flag_rake_spec.rb index 321e02220..2c33cd6e4 100644 --- a/spec/lib/tasks/flag_rake_spec.rb +++ b/spec/lib/tasks/flag_rake_spec.rb @@ -4,9 +4,10 @@ describe 'flag_overdue' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - - before(:each) { @res = FactoryGirl.create(:valid_reservation) } + before(:each) do + mock_app_config + @res = FactoryGirl.create(:valid_reservation) + end it 'flags reservations due yesterday as overdue' do @res.update_attributes( @@ -36,9 +37,10 @@ describe 'flag_missed' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - - before(:each) { @res = FactoryGirl.create(:valid_reservation) } + before(:each) do + mock_app_config + @res = FactoryGirl.create(:valid_reservation) + end it 'flags missed reservations as missed' do @res.update_attributes(start_date: Time.zone.yesterday, @@ -61,9 +63,10 @@ describe 'deny_missed_requests' do include_context 'rake' - before(:all) { FactoryGirl.create(:app_config) } - - before(:each) { @res = FactoryGirl.create(:valid_reservation) } + before(:each) do + @ac = mock_app_config + @res = FactoryGirl.create(:valid_reservation) + end it 'flags missed requests as denied and expired' do @res.update_attributes(FactoryGirl.attributes_for(:request)) @@ -87,6 +90,8 @@ end it 'sends appropriate emails' do + allow(@ac).to receive(:admin_email).and_return('admin@email.com') + allow(@ac).to receive(:disable_user_emails).and_return(false) @res.update_attributes(FactoryGirl.attributes_for(:request)) @res.update_attributes(start_date: Time.zone.yesterday, due_date: Time.zone.today) diff --git a/spec/mailers/admin_mailer_spec.rb b/spec/mailers/admin_mailer_spec.rb index 6e19ff742..8bca4169a 100644 --- a/spec/mailers/admin_mailer_spec.rb +++ b/spec/mailers/admin_mailer_spec.rb @@ -3,7 +3,7 @@ shared_examples_for 'a valid admin email' do it 'sends to the admin' do expect(@mail.to.size).to eq(1) - expect(@mail.to.first).to eq(@app_config.admin_email) + expect(@mail.to.first).to eq(AppConfig.first.admin_email) end it "is from no-reply@#{ActionMailer::Base.default_url_options[:host]}" do expect(@mail.from).to \ @@ -15,10 +15,8 @@ end describe AdminMailer, type: :mailer do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end before(:each) do + mock_app_config(admin_email: 'admin@email.com') ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.deliveries = [] diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb index 1e71686b6..cfd0b3947 100644 --- a/spec/mailers/user_mailer_spec.rb +++ b/spec/mailers/user_mailer_spec.rb @@ -25,10 +25,9 @@ end describe UserMailer, type: :mailer do - before(:all) do - @app_config = FactoryGirl.create(:app_config) - end before(:each) do + @ac = mock_app_config(admin_email: 'admin@email.com', + disable_user_emails: false) ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.deliveries = [] @@ -199,7 +198,7 @@ end it "doesn't send at all if disable_user_emails is set" do - AppConfig.first.update_attributes(disable_user_emails: true) + allow(@ac).to receive(:disable_user_emails).and_return(true) @mail = UserMailer.reservation_status_update(@res).deliver_now expect(@mail).to be_nil end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9febd88ec..4b7930bbf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -75,6 +75,11 @@ DatabaseCleaner.clean end + # Remove when the last of the before(:all) blocks are removed + config.after(:all) do + DatabaseCleaner.clean_with(:truncation) + end + # set up app before all integration specs, wish we didn't have to use :each config.before(:each, type: :feature) { app_setup } @@ -86,6 +91,7 @@ config.include InjectSession, type: :feature config.include FeatureHelpers, type: :feature config.include EnvHelpers, type: :feature + config.include AppConfigHelpers end Shoulda::Matchers.configure do |config| diff --git a/spec/support/app_config_helpers.rb b/spec/support/app_config_helpers.rb new file mode 100644 index 000000000..29e0318d5 --- /dev/null +++ b/spec/support/app_config_helpers.rb @@ -0,0 +1,7 @@ +module AppConfigHelpers + def mock_app_config(**attrs) + ac = spy('AppConfig', require_phone: false, **attrs) + allow(AppConfig).to receive(:first).and_return(ac) + ac + end +end diff --git a/spec/support/feature_helpers.rb b/spec/support/feature_helpers.rb index 4c9516021..c92a06321 100644 --- a/spec/support/feature_helpers.rb +++ b/spec/support/feature_helpers.rb @@ -2,14 +2,11 @@ module FeatureHelpers # make sure we have a working app def app_setup AppConfig.delete_all - @app_config = FactoryGirl.create(:app_config) + @app_config = mock_app_config(**FactoryGirl.attributes_for(:app_config)) @category = FactoryGirl.create(:category) @eq_model = FactoryGirl.create(:equipment_model, category: @category) - @eq_model2 = - FactoryGirl.create(:equipment_model, category: @category) @eq_item = FactoryGirl.create(:equipment_item, equipment_model: @eq_model) - @eq_item2 = FactoryGirl.create(:equipment_item, equipment_model: @eq_model2) @admin = FactoryGirl.create(:admin) @superuser = FactoryGirl.create(:superuser) @checkout_person = FactoryGirl.create(:checkout_person)