Skip to content

Instantly share code, notes, and snippets.

@mallain
Created September 13, 2010 12:44
Show Gist options
  • Save mallain/577230 to your computer and use it in GitHub Desktop.
Save mallain/577230 to your computer and use it in GitHub Desktop.
mickael@mickael-laptop:~/projects/pabd/test (master*!) 14:40:08
2040/93 > ruby unit/validation_state_test.rb
Loaded suite unit/validation_state_test
Started
.........
Finished in 1.42106 seconds.
9 tests, 9 assertions, 0 failures, 0 errors
class ValidationState < ActiveRecord::Base
## Plugins ##
acts_as_state_machine :initial => :without_validating
# States
state :without_validating
state :agency_validating
state :admin_validating
state :consolidating
# Events
event :without_validation do
transitions :from => :agency_validating, :to => :without_validating, :guard => :user_manage_agency?
transitions :from => :admin_validating, :to => :without_validating, :guard => :user_admin?
end
event :agency_validation do
transitions :from => :without_validating, :to => :agency_validating, :guard => :user_manage_agency?
end
event :admin_validation do
transitions :from => :agency_validating, :to => :admin_validating, :guard => :user_admin?
end
event :consolidation do
transitions :from => :admin_validating, :to => :consolidating
end
## Callbacks ##
## Relations ##
belongs_to :user
belongs_to :agency
belongs_to :feedback
## Validates ##
validates_presence_of :user, :agency, :feedback
validates_uniqueness_of :feedback_id, :scope => :agency_id
validate :check_user_manage_agency
## Scopes ##
## Methods ##
def user_admin?
self.user.admin?
end
def user_manage_agency?
self.user.manage_agency?(self.agency_id)
end
def check_user_manage_agency
if self.agency && self.user
errors.add_to_base('User doesn t manage this agency') unless self.user_manage_agency?
end
end
end
require 'test_helper'
class ValidationStateTest < ActiveSupport::TestCase
subject {
user = Factory(:user, :role => 'poweruser', :agencies => [Factory(:agency)])
@validation_state = Factory(:validation_state, :user => user, :agency => user.agencies.first, :feedback => Factory(:feedback))
}
should_validate_presence_of(:user, :agency, :feedback)
should_validate_uniqueness_of(:feedback_id, :scoped_to => :agency_id)
test "should not save validation_state (without_validating) with an user who not manage the agency" do
# Instanciate an user
@user = Factory(:user, :role => 'poweruser', :agencies => [Factory(:agency)])
@user2 = Factory(:user, :role => 'poweruser', :agencies => [Factory(:agency)])
# Instanciate a validation_state
@validation_state = Factory.build(:validation_state, :user => @user2, :agency => @user.agencies.first)
assert !@validation_state.save, "Saved validation state with an user who cannot manage the agency"
end
test "should save validation_state (without_validating) with an user who manage the agency" do
# Instanciate an user
@user = Factory(:user, :role => 'poweruser', :agencies => [Factory(:agency)])
# Instanciate a validation_state
@validation_state = Factory(:validation_state, :user => @user, :agency => @user.agencies.first)
assert @validation_state.save, "Saved validation state with an user who manage the agency"
end
test "should save validation_state (agency_validation) with an user and agency_validated state changes be done by an admin" do
# Instanciate an user
@user = Factory(:user, :role => 'poweruser', :agencies => [Factory(:agency)])
@admin = Factory(:user, :role => 'admin')
# Instanciate a validation_state
@validation_state = Factory(:validation_state, :user => @user, :agency => @user.agencies.first)
# Changes by admin
@validation_state.user = @admin
@validation_state.agency_validation!
assert @validation_state.valid?, "Saved validation state with an user and agency_validated state cannot be done by an admin"
end
test "should not save validation_state (admin_validating) with an user who isn t admin" do
# Instanciate an user
@user = Factory(:user, :role => 'poweruser', :agencies => [Factory(:agency)])
# Instanciate a validation_state
@validation_state = Factory(:validation_state, :agency => @user.agencies.first, :user => @user)
# Transitions (without_validating to agency_validating)
@validation_state.agency_validation!
@validation_state.admin_validation!
assert !@validation_state.admin_validating?, "Saved validation state with an user who isn t an admin"
end
test "should save validation_state (admin_validating) with an user who is admin" do
# Instanciate an user
@user = Factory(:user, :role => 'poweruser', :agencies => [Factory(:agency)])
@admin = Factory(:user, :role => 'admin')
# Instanciate a validation_state
@validation_state = Factory(:validation_state, :agency => @user.agencies.first, :user => @user)
# Transitions (without_validating to agency_validating)
@validation_state.agency_validation!
# Changes by Admin
@validation_state.user = @admin
@validation_state.admin_validation!
assert @validation_state.admin_validating?, "Saved validation state with an user who is an admin"
end
end
@mallain
Copy link
Author

mallain commented Sep 13, 2010

acts_as_state_machine implemented in my rails app with some guarding states.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment