Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Add more integration tests
Browse files Browse the repository at this point in the history
Resolves #416
- add tests for Reservation actions (creation, equipment handling, renewal)
- add tests for Rails Admin routes
- resolve issue with renewals (see #1218)
- add numerous helpers for integration tests
- misc cleanup of other tests
- resolve deprecation warning reintroduced by #1081
  • Loading branch information
orenyk committed Apr 3, 2015
1 parent 89d4fcc commit dd412d1
Show file tree
Hide file tree
Showing 11 changed files with 559 additions and 30 deletions.
5 changes: 3 additions & 2 deletions app/models/reservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def find_renewal_date
due_date
end

def eligible_for_renew?
def eligible_for_renew? # rubocop:disable CyclomaticComplexity
# determines if a reservation is eligible for renewal, based on how many
# days before the due date it is and the max number of times one is
# allowed to renew
Expand All @@ -156,7 +156,8 @@ def eligible_for_renew?
max_renewal_days = equipment_model.maximum_renewal_days_before_due
((due_date - Time.zone.today).to_i < max_renewal_days) &&
(self.times_renewed < max_renewal_times) &&
equipment_model.maximum_renewal_length > 0
equipment_model.maximum_renewal_length > 0 &&
equipment_model.available_count(due_date + 1.day) > 0
end

def to_cart
Expand Down
16 changes: 8 additions & 8 deletions spec/controllers/equipment_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:index) }
it { is_expected.not_to set_the_flash }
it { is_expected.not_to set_flash }
context 'without show deleted' do
let!(:item_other_cat_active) { FactoryGirl.create(:equipment_item) }
let!(:item_other_cat_inactive) do
Expand Down Expand Up @@ -111,7 +111,7 @@
end
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:show) }
it { is_expected.not_to set_the_flash }
it { is_expected.not_to set_flash }
it 'should set to correct equipment item' do
expect(assigns(:equipment_item)).to eq(item)
end
Expand All @@ -136,7 +136,7 @@
end
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:new) }
it { is_expected.not_to set_the_flash }
it { is_expected.not_to set_flash }
it 'assigns a new equipment item to @equipment_item' do
expect(assigns(:equipment_item)).to be_new_record
expect(assigns(:equipment_item)).to be_kind_of(EquipmentItem)
Expand Down Expand Up @@ -181,15 +181,15 @@
expect(EquipmentItem.last.notes).not_to be_nil
expect(EquipmentItem.last.notes).not_to be('')
end
it { is_expected.to set_the_flash }
it { is_expected.to set_flash }
it { is_expected.to redirect_to(EquipmentItem.last.equipment_model) }
end
context 'without valid attributes' do
before do
post :create, equipment_item: FactoryGirl.attributes_for(
:equipment_item, name: nil)
end
it { is_expected.not_to set_the_flash }
it { is_expected.not_to set_flash }
it { is_expected.to render_template(:new) }
it 'should not save' do
expect do
Expand Down Expand Up @@ -218,7 +218,7 @@
end
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:edit) }
it { is_expected.not_to set_the_flash }
it { is_expected.not_to set_flash }
it 'sets @equipment_item to selected item' do
expect(assigns(:equipment_item)).to eq(item)
end
Expand All @@ -242,7 +242,7 @@
equipment_item: FactoryGirl.attributes_for(:equipment_item,
name: 'Obj')
end
it { is_expected.to set_the_flash }
it { is_expected.to set_flash }
it 'sets @equipment_item to selected item' do
expect(assigns(:equipment_item)).to eq(item)
end
Expand All @@ -262,7 +262,7 @@
equipment_item: FactoryGirl.attributes_for(:equipment_item,
name: nil)
end
it { is_expected.not_to set_the_flash }
it { is_expected.not_to set_flash }
it 'should not update attributes' do
item.reload
expect(item.name).not_to be_nil
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/equipment_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
category { FactoryGirl.create(:category, max_per_user: 1) }
end

factory :equipment_model_with_object do
factory :equipment_model_with_item do
after(:create) do |model|
FactoryGirl.create(:equipment_item, equipment_model: model)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
password_confirmation 'passw0rd'
end

factory :superuser do
role 'superuser'
view_mode 'superuser'
end

factory :admin do
role 'admin'
view_mode 'admin'
Expand Down
16 changes: 16 additions & 0 deletions spec/features/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@
env_wrapper('CAS_AUTH' => nil) { example.run }
end

context 'testing login and logout helpers' do
before { sign_in_as_user(@checkout_person) }
it 'signs in the right user' do
visit root_path
expect(page).to have_content(@checkout_person.name)
expect(page).not_to have_link 'Sign In', href: new_user_session_path
end

it 'can also sign out' do
sign_out
visit root_path
expect(page).to have_link 'Sign In', href: new_user_session_path
expect(page).not_to have_content(@checkout_person.name)
end
end

context 'with new user' do
context 'can register' do
before do
Expand Down
12 changes: 12 additions & 0 deletions spec/features/equipment_model_views_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'

describe 'Equipment model views' do
before(:each) { app_setup }
subject { page }

context 'index view' do
before { visit equipment_models_path }
it { is_expected.to have_content('Equipment Models') }
it { is_expected.to have_content(@eq_model.name) }
end
end
22 changes: 8 additions & 14 deletions spec/features/guest_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe 'guest users' do
describe 'guest users', type: :feature do
# Shared Examples
shared_examples 'unauthorized' do
context 'visiting protected route' do
Expand Down Expand Up @@ -44,7 +44,7 @@
it 'goes to the correct page with sign in link' do
visit url_path
expect(current_path).to eq(url_path)
expect(page).to have_link('Sign In')
expect(page).to have_link('Sign In', href: new_user_session_path)
end
end

Expand Down Expand Up @@ -87,34 +87,28 @@

describe 'can use the catalog' do
before :each do
visit '/'
within(:css, "#add_to_cart_#{EquipmentModel.first.id}") do
click_link 'Add to Cart'
end
add_item_to_cart(@eq_model)
visit '/'
end

it 'can add items to cart' do
expect(page.find(:css, '#list_items_in_cart')).to have_link(
EquipmentModel.first.name,
href: equipment_model_path(EquipmentModel.first))
@eq_model.name,
href: equipment_model_path(@eq_model))
end

it 'can remove items from cart' do
click_link 'Remove',
href: "/remove_from_cart/#{EquipmentModel.first.id}"
visit '/'
expect(page.find(:css, '#list_items_in_cart')).not_to have_link(
EquipmentModel.first.name,
href: equipment_model_path(EquipmentModel.first))
@eq_model.name,
href: equipment_model_path(@eq_model))
end

it 'can change the dates' do
@new_date = Time.zone.today + 5.days
# fill in both visible / datepicker and hidden field
fill_in 'cart_due_date_cart', with: @new_date.to_s
find(:xpath, "//input[@id='date_end_alt']").set @new_date.to_s
find('#cart_form').submit_form!
update_cart_due_date(@new_date.to_s)
visit '/'
expect(page.find('#cart_due_date_cart').value).to \
eq(@new_date.strftime('%m/%d/%Y'))
Expand Down
109 changes: 109 additions & 0 deletions spec/features/rails_admin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require 'spec_helper'

describe 'Active Admin', type: :feature do
before(:all) { app_setup }

context 'as superuser' do
before { sign_in_as_user(@superuser) }
after { sign_out }

shared_examples 'can access route' do |model|
let(:path) do
admin_routes.index_url(model_name: model, host: Capybara.default_host)
end

it do
visit path
expect(page.current_url).to eq(path)
end
end

it 'can access the dashboard' do
visit admin_routes.dashboard_path
expect(page).to have_content 'Site Administration'
expect(page.current_url).to \
eq(admin_routes.dashboard_url(host: Capybara.default_host))
end

it_behaves_like 'can access route', :announcement
it_behaves_like 'can access route', :app_config
it_behaves_like 'can access route', :blackout
it_behaves_like 'can access route', :category
it_behaves_like 'can access route', :checkin_procedure
it_behaves_like 'can access route', :checkout_procedure
it_behaves_like 'can access route', :equipment_item
it_behaves_like 'can access route', :equipment_model
it_behaves_like 'can access route', :requirement
it_behaves_like 'can access route', :reservation
it_behaves_like 'can access route', :user
end

context 'as other roles' do
shared_examples 'cannot access route' do |model|
if model
let(:path) { admin_routes.index_path(model_name: model) }
else
let(:path) { admin_routes.dashboard_path }
end

it do
visit path
expect(page.current_url).to eq(root_url(host: Capybara.default_host))
end
end

context 'as patron' do
before { sign_in_as_user(@user) }
after { sign_out }

it_behaves_like 'cannot access route'
it_behaves_like 'cannot access route', :announcement
it_behaves_like 'cannot access route', :app_config
it_behaves_like 'cannot access route', :blackout
it_behaves_like 'cannot access route', :category
it_behaves_like 'cannot access route', :checkin_procedure
it_behaves_like 'cannot access route', :checkout_procedure
it_behaves_like 'cannot access route', :equipment_item
it_behaves_like 'cannot access route', :equipment_model
it_behaves_like 'cannot access route', :requirement
it_behaves_like 'cannot access route', :reservation
it_behaves_like 'cannot access route', :user
end

context 'as checkout person' do
before { sign_in_as_user(@checkout_person) }
after { sign_out }

it_behaves_like 'cannot access route'
it_behaves_like 'cannot access route', :announcement
it_behaves_like 'cannot access route', :app_config
it_behaves_like 'cannot access route', :blackout
it_behaves_like 'cannot access route', :category
it_behaves_like 'cannot access route', :checkin_procedure
it_behaves_like 'cannot access route', :checkout_procedure
it_behaves_like 'cannot access route', :equipment_item
it_behaves_like 'cannot access route', :equipment_model
it_behaves_like 'cannot access route', :requirement
it_behaves_like 'cannot access route', :reservation
it_behaves_like 'cannot access route', :user
end

context 'as admin' do
before { sign_in_as_user(@admin) }
after { sign_out }

it_behaves_like 'cannot access route'
it_behaves_like 'cannot access route', :announcement
it_behaves_like 'cannot access route', :app_config
it_behaves_like 'cannot access route', :blackout
it_behaves_like 'cannot access route', :category
it_behaves_like 'cannot access route', :checkin_procedure
it_behaves_like 'cannot access route', :checkout_procedure
it_behaves_like 'cannot access route', :equipment_item
it_behaves_like 'cannot access route', :equipment_model
it_behaves_like 'cannot access route', :requirement
it_behaves_like 'cannot access route', :reservation
it_behaves_like 'cannot access route', :user
end
end
end
Loading

0 comments on commit dd412d1

Please sign in to comment.