Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def admin
end

def checkout
can :manage, Reservation
cannot :archive, Reservation
cannot :renew, Reservation unless AppConfig.check(:enable_renewals)
cannot :destroy, Reservation do |r|
Expand All @@ -49,6 +48,7 @@ def checkout
can :read, EquipmentItem
can :override, :reservation_errors if AppConfig.get(:override_on_create)
can :override, :checkout_errors if AppConfig.get(:override_at_checkout)
can :hide, [Announcement, AnnouncementMock]
normal
end

Expand All @@ -65,7 +65,7 @@ def normal
can :update_index_dates, Reservation
can :view_all_dates, Reservation
can :view_detailed, EquipmentModel
can :hide, Announcement
can :hide, [Announcement, AnnouncementMock]
end

def guest
Expand All @@ -75,10 +75,10 @@ def guest
can :reload_catalog_cart, :all
can :update_cart, :all
can :create, User if AppConfig.check(:enable_new_users)
can :hide, Announcement
can :hide, [Announcement, AnnouncementMock]
end

def banned
can :hide, Announcement
can :hide, [Announcement, AnnouncementMock]
end
end
194 changes: 70 additions & 124 deletions spec/controllers/announcements_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,182 +1,128 @@
# frozen_string_literal: true
require 'spec_helper'

shared_examples_for 'page success' do
it { is_expected.to respond_with(:success) }
it { is_expected.not_to set_flash }
end

shared_examples_for 'access denied' do
it { is_expected.to redirect_to(root_url) }
it { is_expected.to set_flash }
end
require "cancan/matchers"

describe AnnouncementsController, type: :controller do
before(:each) { mock_app_config }

describe 'with admin' do
before do
sign_in FactoryGirl.create(:admin)
end
context 'GET index' do
before { mock_user_sign_in(UserMock.new(:admin)) }
describe 'GET index' do
before do
allow(Announcement).to receive(:all).and_return(Announcement.none)
get :index
end
it_behaves_like 'page success'
it { is_expected.to render_template(:index) }
it 'should assign @announcements to all Announcements' do
expect(assigns(:announcements)).to eq(Announcement.all)
it_behaves_like 'successful request', :index
it 'gets all announcements' do
expect(Announcement).to have_received(:all).twice
end
end

context 'GET new' do
before do
allow(Announcement).to receive(:new)
get :new
end
it 'sets the default announcement' do
expect(assigns(:announcement)[:starts_at]).to\
eq(Time.zone.today)
expect(assigns(:announcement)[:ends_at]).to\
eq(Time.zone.today + 1.day)
it 'makes a new announcement with appropriate dates' do
dates = { starts_at: Time.zone.today, ends_at: Time.zone.today + 1.day }
expect(Announcement).to have_received(:new).with(dates)
end
it_behaves_like 'page success'
it { is_expected.to render_template(:new) }
end
context 'GET edit' do
before do
get :edit, id: FactoryGirl.create(:announcement)
end
it_behaves_like 'page success'
it { is_expected.to render_template(:edit) }
it_behaves_like 'successful request', :new
end

context 'POST create' do
context 'with correct params' do
context 'successful save' do
let!(:announcement) { AnnouncementMock.new(save: true) }
before do
@attributes = FactoryGirl.attributes_for(:announcement)
post :create, announcement: @attributes
allow(Announcement).to receive(:new).and_return(announcement)
post :create, announcement: { id: 1 }
end
it 'should create the new announcement' do
expect(Announcement.find(assigns(:announcement).id)).not_to be_nil
end
it 'should pass the correct params' do
expect(assigns(:announcement)[:starts_at].to_date).to\
eq(@attributes[:starts_at].to_date)
expect(assigns(:announcement)[:ends_at].to_date).to\
eq(@attributes[:ends_at].to_date)
it 'creates a new announcement' do
expect(Announcement).to have_received(:new).twice
expect(announcement).to have_received(:save)
end
it { is_expected.to redirect_to(announcements_path) }
it { is_expected.to set_flash }
it { is_expected.to set_flash[:notice] }
end
context 'with incorrect params' do
context 'unsuccessful save' do
let!(:announcement) { AnnouncementMock.new(save: false) }
before do
@attributes = FactoryGirl.attributes_for(:announcement)
@attributes[:ends_at] = Time.zone.today - 1.day
post :create, announcement: @attributes
allow(Announcement).to receive(:new).and_return(announcement)
post :create, announcement: { id: 1 }
end
it { is_expected.to render_template(:new) }
end
end

context 'PUT update' do
before do
@new_attributes = FactoryGirl.attributes_for(:announcement)
@new_attributes[:message] = 'New Message!!'
put :update, id: FactoryGirl.create(:announcement),
announcement: @new_attributes
context 'successful update' do
let!(:announcement) do
AnnouncementMock.new(traits: [:findable], update_attributes: true)
end
before do
put :update, id: announcement.id, announcement: { id: 1 }
end
it { is_expected.to redirect_to(announcements_url) }
it { is_expected.to set_flash[:notice] }
end
it 'updates the announcement' do
expect(assigns(:announcement)[:message]).to\
eq(@new_attributes[:message])
context 'unsuccessful update' do
let!(:announcement) do
AnnouncementMock.new(traits: [:findable], update_attributes: false)
end
before do
put :update, id: announcement.id, announcement: { id: 1 }
end
it { is_expected.to render_template(:edit) }
end
end

context 'DELETE destroy' do
before do
delete :destroy, id: FactoryGirl.create(:announcement)
end
let!(:announcement) { AnnouncementMock.new(traits: [:findable]) }
before { delete :destroy, id: announcement.id }
it 'should delete the announcement' do
expect(Announcement.where(id: assigns(:announcement)[:id])).to be_empty
expect(announcement).to have_received(:destroy).with(:force)
end
it { is_expected.to redirect_to(announcements_path) }
end
end
context 'is not admin' do
before do
sign_in FactoryGirl.create(:user)
@announcement = FactoryGirl.create(:announcement)
@attributes = FactoryGirl.attributes_for(:announcement)
end

context 'is not admin' do
before { mock_user_sign_in }
context 'GET index' do
before do
get :index
end
it_behaves_like 'access denied'
before { get :index }
it_behaves_like 'redirected request'
end
context 'POST create' do
before do
post :create, announcement: @attributes
end
it_behaves_like 'access denied'
before { post :create, announcement: { id: 1 } }
it_behaves_like 'redirected request'
end
context 'PUT update' do
before do
put :update, id: @announcement
end
it_behaves_like 'access denied'
before { put :update, id: 1 }
it_behaves_like 'redirected request'
end
context 'DELETE destroy' do
before do
delete :destroy, id: @announcement
end
it_behaves_like 'access denied'
before { delete :destroy, id: 1 }
it_behaves_like 'redirected request'
end
end
context 'GET hide as' do
shared_examples 'can hide announcement' do

describe 'GET hide as' do
shared_examples 'can hide announcement' do |user_type|
let!(:announcement) { AnnouncementMock.new(traits: [:findable]) }
before do
@announcement = FactoryGirl.create(:announcement)
mock_user_sign_in(UserMock.new(user_type))
request.env['HTTP_REFERER'] = 'where_i_came_from'
get :hide, id: @announcement
#puts "id! ", announcement.id
get :hide, id: announcement.id
end
it 'should set some cookie values' do
it 'sets some cookie values' do
name = 'hidden_announcement_ids'
jar = request.cookie_jar
jar.signed[name] = [@announcement[:id].to_s]
jar.signed[name] = [announcement.id.to_s]
expect(response.cookies[name]).to eq(jar[name])
end
end
context 'superuser' do
before do
sign_in FactoryGirl.create(:superuser)
end
it_behaves_like 'can hide announcement'
end
context 'admin' do
before do
sign_in FactoryGirl.create(:admin)
end
it_behaves_like 'can hide announcement'
end
context 'patron' do
before do
sign_in FactoryGirl.create(:user)
end
it_behaves_like 'can hide announcement'
end
context 'checkout person' do
before do
sign_in FactoryGirl.create(:checkout_person)
end
it_behaves_like 'can hide announcement'
end
context 'guest' do
before do
sign_in FactoryGirl.create(:guest)
end
it_behaves_like 'can hide announcement'
end
context 'banned user' do
before do
sign_in FactoryGirl.create(:banned)
end
it_behaves_like 'can hide announcement'
end
ROLES = [:superuser, :admin, :checkout_person, :user, :guest, :banned]
ROLES.each { |r| it_behaves_like 'can hide announcement', r }
end
end
25 changes: 16 additions & 9 deletions spec/support/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# some basic helpers to simulate devise controller methods in specs
# frozen_string_literal: true
require Rails.root.join('spec/support/mockers/user.rb')

module ControllerHelpers
def current_user
user_session_info =
response.request.env['rack.session']['warden.user.user.key']
return unless user_session_info
user_id = user_session_info[0][0]
User.find(user_id)
def mock_user_sign_in(user = UserMock.new(traits: [:findable]))
pass_app_setup_check
allow(request.env['warden']).to receive(:authenticate!).and_return(user)
# necessary for permissions to work
allow(ApplicationController).to receive(:current_user).and_return(user)
allow(Ability).to receive(:new).and_return(Ability.new(user))
allow_any_instance_of(described_class).to \
receive(:current_user).and_return(user)
end

def user_signed_in?
!current_user.nil?
private

def pass_app_setup_check
allow(AppConfig).to receive(:first).and_return(true) unless AppConfig.first
allow(User).to receive(:count).and_return(1) unless User.first
end
end
12 changes: 12 additions & 0 deletions spec/support/mockers/announcement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
require Rails.root.join('spec/support/mockers/mocker.rb')

class AnnouncementMock < Mocker
def self.klass
Announcement
end

def self.klass_name
'Announcement'
end
end
21 changes: 21 additions & 0 deletions spec/support/mockers/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
require Rails.root.join('spec/support/mockers/mocker.rb')
require Rails.root.join('spec/support/mockers/equipment_model.rb')

class CategoryMock < Mocker
def self.klass
Category
end

def self.klass_name
'Category'
end

private

def with_equipment_models(models: nil, count: 1)
models ||= Array.new(count) { EquipmentModelMock.new }
parent_has_many(mocked_children: models, parent_sym: :category,
child_sym: :equipment_models)
end
end
21 changes: 21 additions & 0 deletions spec/support/mockers/equipment_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
require Rails.root.join('spec/support/mockers/mocker.rb')
require Rails.root.join('spec/support/mockers/equipment_model.rb')

class EquipmentItemMock < Mocker
def self.klass
EquipmentItem
end

def self.klass_name
'EquipmentItem'
end

private

def with_model(model: nil)
model ||= EquipmentModelMock.new
child_of_has_many(mocked_parent: model, parent_sym: :equipment_model,
child_sym: :equipment_items)
end
end
Loading