Skip to content

Commit

Permalink
Refactor. [#82]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Aug 2, 2013
1 parent 613b10c commit ddb085f
Showing 1 changed file with 74 additions and 84 deletions.
158 changes: 74 additions & 84 deletions spec/models/event_spec.rb
Expand Up @@ -45,13 +45,13 @@
end

it "should have a country property referred through state" do
event = Factory :event, :state => Factory(:state)
event = Factory :event, state: Factory(:state)
event.state.should_not be_nil
event.country.should == event.state.country
end

it "should be nil-safe on country" do
event = Event.new(:state => nil)
event = Event.new(state: nil)
lambda{event.country}.should_not raise_error
end

Expand All @@ -60,7 +60,7 @@
aggr.should_not be_nil
aggr.options[:mapping].should == [%w(street street), %w(street2 street2), %w(city city), %w(state_id state), %w(zip zip), %w(coords coords)]
state = FactoryGirl.create :state
opts = {:street => '123 Main Street', :street2 => '1st floor', :city => 'Anytown', :zip => 12345, :state => state}
opts = {street: '123 Main Street', street2: '1st floor', city: 'Anytown', zip: 12345, state: state}
e = Event.new(opts)
e.address.should == Acts::Addressed::Address.new(opts)
end
Expand All @@ -73,7 +73,7 @@
it "should exclude deleted events on find" do
undeleted = FactoryGirl.create :event
begin
deleted = FactoryGirl.create :event, :deleted => true
deleted = FactoryGirl.create :event, deleted: true
rescue ActiveRecord::RecordNotFound
# don't worry about it -- since default_scope excludes this record, it won't be found.
end
Expand All @@ -89,89 +89,86 @@
end

describe Event, "(allow?)" do
before(:each) do
@event = FactoryGirl.create :event
@alien = FactoryGirl.create :user, :permissions => [Factory(:permission)]
@nonadmin = FactoryGirl.create :user, :permissions => [Factory(:permission, :calendar => @event.calendar)]
@admin = FactoryGirl.create(:user).tap do |u|
u.permissions << Factory(:admin_permission, :calendar => @event.calendar, :user => u)
let!(:event) { Factory :event }
let(:admin) do
FactoryGirl.create(:user).tap do |u|
u.permissions << Factory(:admin_permission, calendar: event.calendar, user: u)
end
end

it "should exist with one argument" do
@event.should respond_to(:allow?)
@event.method(:allow?).arity.should == 1
event.should respond_to(:allow?)
event.method(:allow?).arity.should == 1
end

it "should return true for :delete iff current user has a role of admin for the event's calendar, false otherwise" do
User.stub!(:current_user).and_return(@alien)
@event.allow?(:delete).should == false
User.stub!(:current_user).and_return(@admin)
@event.allow?(:delete).should == true
alien = FactoryGirl.create :user, permissions: [Factory(:permission)]
User.stub current_user: alien
event.allow?(:delete).should == false
User.stub current_user: admin
event.allow?(:delete).should == true
end

it "should return true for :edit iff current user has a role of admin for the event's calendar or created the event" do
@event.created_by = @nonadmin
User.stub!(:current_user).and_return(@nonadmin)
@event.allow?(:edit).should == true
User.stub!(:current_user).and_return(@admin)
@event.allow?(:edit).should == true
nonadmin = FactoryGirl.create :user, permissions: [Factory(:permission, calendar: event.calendar)]
event.created_by = nonadmin
User.stub current_user: nonadmin
event.allow?(:edit).should == true
User.stub current_user: admin
event.allow?(:edit).should == true
end

it "should return true for :show iff current user has any role for the event's calendar" do
user = Factory(:user).tap do |u|
u.permissions << Factory(:permission, :user => u, :calendar => @event.calendar, :role => Factory(:role, :name => Faker::Lorem.word))
u.permissions << Factory(:permission, user: u, calendar: event.calendar, role: Factory(:role, name: Faker::Lorem.word))
end
User.stub!(:current_user).and_return user
@event.allow?(:show).should == true
User.stub current_user: user
event.allow?(:show).should == true

User.stub!(:current_user).and_return Factory(:user, :permissions => [Factory(:permission, :role => Factory(:role, :name => Faker::Lorem.word))])
@event.allow?(:show).should == false
User.stub current_user: Factory(:user, permissions: [Factory(:permission, role: Factory(:role, name: Faker::Lorem.word))])
event.allow?(:show).should == false
end

it "should return nil for any operation if current user is not a User object" do
User.stub!(:current_user).and_return('bogus value')
@event.allow?(:edit).should be_nil
User.stub current_user: 'bogus value'
event.allow?(:edit).should be_nil
end

it "should return nil for any operation it doesn't know about" do
User.stub!(:current_user).and_return(@admin)

@event.allow?(:foobar).should be_nil
User.stub current_user: admin
event.allow?(:foobar).should be_nil
end
end

describe Event, "(change_status!)" do
before(:each) do
@event = Factory :event
@user = Factory :user
end
let(:event) { Factory :event }
let(:user) { Factory :user }

it "should be valid" do
@event.should respond_to(:change_status!)
event.should respond_to(:change_status!)
end

it "should change the status on the already existing commitment if one exists" do
commitment = Factory :commitment, :event => @event, :user => @user, :status => true
commitment = Factory :commitment, event: event, user: user, status: true
id = commitment.id
[false, nil, true].each do |status|
@event.change_status!(@user, status)
event.change_status!(user, status)
commitment = Commitment.find(id)
commitment.status.should == status
end
end

it "should create a new commitment if there isn't one" do
@event.commitments.find_all_by_user_id(@user.id).should be_empty
@event.change_status!(@user, nil) # somewhat arbitrary choice of status
@event.commitments.find_all_by_user_id(@user.id).should_not be_empty
event.commitments.find_all_by_user_id(user.id).should be_empty
event.change_status!(user, nil) # somewhat arbitrary choice of status
event.commitments.find_all_by_user_id(user.id).should_not be_empty
end

it "should add a comment to the commitment if one is supplied" do
comment = Faker::Lorem.sentence
@event.change_status! @user, true, comment
event.change_status! user, true, comment

commitment = @event.commitments.find_by_user_id(@user)
commitment = event.commitments.find_by_user_id(user)
commitment.comment.should == comment
end
end
Expand Down Expand Up @@ -202,7 +199,7 @@

describe Event, "#find_committed" do
let(:event) { Factory :event }
let(:event_with_commitments) { Event.includes(:commitments => :user).find(event.id) }
let(:event_with_commitments) { Event.includes(commitments: :user).find(event.id) }


it "should exist with one argument" do
Expand All @@ -212,22 +209,22 @@

it "should get a collection of Users when called with :yes or :no" do
@attending = Factory(:user).tap do |u|
u.commitments << Factory(:commitment, :user => u, :event => event, :status => true)
u.commitments << Factory(:commitment, user: u, event: event, status: true)
end
@not_attending = Factory(:user).tap do |u|
u.commitments << Factory(:commitment, :user => u, :event => event, :status => false)
u.commitments << Factory(:commitment, user: u, event: event, status: false)
end
event_with_commitments.find_committed(:yes).should == [@attending]
event_with_commitments.find_committed(:no).should == [@not_attending]
end

it 'should sort the Users on name or, failing that, email' do
a = Factory :user, :lastname => 'a'
b = Factory :user, :email => 'b@b.com', :lastname => nil, :firstname => nil
c = Factory :user, :lastname => nil, :firstname => 'c'
a = Factory :user, lastname: 'a'
b = Factory :user, email: 'b@b.com', lastname: nil, firstname: nil
c = Factory :user, lastname: nil, firstname: 'c'
users = [c, a, b]
users.each do |u|
u.commitments << Factory(:commitment, :user => u, :event => event, :status => true)
u.commitments << Factory(:commitment, user: u, event: event, status: true)
end

event_with_commitments.find_committed(:yes).should == [a, b, c]
Expand All @@ -241,38 +238,33 @@
end

describe Event, "(hide)" do
before(:each) do
@event = Event.new
end

it "should set deleted to true" do
@event.deleted.should_not == true
@event.hide
@event.deleted.should == true
event = Factory.build :event
event.deleted.should_not == true
event.hide
event.deleted.should == true
end
end

describe Event, "(validations)" do
before(:each) do
@event = Factory.build :event
end
let(:event) { Factory.build :event }

it "should not be valid without a state" do
@event.should be_valid
@event.state_id = nil
@event.should_not be_valid
event.should be_valid
event.state_id = nil
event.should_not be_valid
end

it "should not be valid without a name" do
@event.should be_valid
@event.name = nil
@event.should_not be_valid
event.should be_valid
event.name = nil
event.should_not be_valid
end

it "should not be valid without a calendar" do
@event.should be_valid
@event.calendar_id = nil
@event.should_not be_valid
event.should be_valid
event.calendar_id = nil
event.should_not be_valid
end

=begin
Expand All @@ -286,37 +278,35 @@
it "should assign current_user to created_by" do
user = Factory :user
User.stub!(:current_user).and_return user
@event.created_by_id = nil
@event.save!
@event.created_by.should == user
event.created_by_id = nil
event.save!
event.created_by.should == user
end

it "should not try to set created_by if there's no current user" do
[false, :false].each do |v|
User.stub!(:current_user).and_return(v)
@event.created_by_id = nil
@event.should_not_receive(:created_by=)
@event.save!
event.created_by_id = nil
event.should_not_receive(:created_by=)
event.save!
end
end
end

describe Event, "(geographical features)" do
before(:each) do
@event = Factory.create :event
end
let(:event) { Factory :event }

it "should have coords (Point)" do
@event.should respond_to(:coords)
@event.coords.should_not be_nil
@event.coords.should be_a_kind_of(Point)
event.should respond_to(:coords)
event.coords.should_not be_nil
event.coords.should be_a_kind_of(Point)
end

it "should reset coords on update" do
User.stub!(:current_user).and_return(Factory :user)
@event.update_attributes(Factory.attributes_for :event)
@event.should_receive(:coords=)
@event.update_attributes(:name => 'foo')
event.update_attributes(Factory.attributes_for :event)
event.should_receive(:coords=)
event.update_attributes(name: 'foo')
end
end

Expand Down

0 comments on commit ddb085f

Please sign in to comment.