Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created October 21, 2009 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrchrisadams/215083 to your computer and use it in GitHub Desktop.
Save mrchrisadams/215083 to your computer and use it in GitHub Desktop.
describe ActivationsController do
before :each do
@user = Factory.create(:user, :active => 0)
@user.reset_perishable_token!
end
describe "functionality tests" do
describe "new" do
def do_get
get :new, :activation_code => @user.perishable_token
end
it "should load user" do
do_get
assigns[:user].should == @user
end
it "should raise an WrongtokenException if the perishable_token is wrong" do
lambda { get :new, :activation_code => "eCUq0D23cIacTAMtvUlF"}.should raise_error(WrongTokenException)
end
it "should raise an UserAlreadyActiveException is the user is active, but the perishable_token is correct" do
@user = Factory.create(:user, :active => 1)
lambda { do_get}.should raise_error(UserAlreadyActiveException)
end
end
class User < ActiveRecord::Base
# standard authlogic behaviour
authenticates_many :user_sessions
acts_as_authentic do |c|
# disabling this stops the perishable token being updated whenever there are any changes made to the user model
c.disable_perishable_token_maintenance = true
end
end
describe User do
# setup authlogic as we'd expect
before :each do
activate_authlogic
end
describe "perishable_token maintenance" do
# sanity check to make sure we're actually setting tokens to check against
it "should set perishable_token on create" do
user = Factory.create(:user)
user.perishable_token.should_not be_blank
end
# With this setup, we can tell that tokens are no longer being changed by our cronjob to update user's hotness
it "should not change perishable_token when updating model" do
user = Factory.create(:user)
token = user.perishable_token
user.update_attribute :hotness, 2000
user.reload
user.perishable_token.should == token
end
# We DO want to make sure users can only activate once though
it "should reset perishable_token on activate" do
user = Factory.create(:user)
token = user.perishable_token
user.activate!
user.reload
user.perishable_token.should_not == token
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment