# Restful\_authentication: 3 unit tests break!

**URL:** <https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995>\
**Category:** Rails\
**Created:** [April 20, 2009, 8:37pm UTC](https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995 "2009-04-20T20:37:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 8:37pm UTC](https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995/1 "2009-04-20T20:37:23Z")

</div>

Hi all

I have installed restful\_authentication. However, I didn’t active the  
email-verification option in the first way, so I tried to activate it  
manually some time later. But now I have problems with my application  
and I’m trying to solve them bit by bit.

First of all, my unit tests don’t work:

1. Failure:  
test\_should\_initialize\_activation\_code\_upon\_creation(UserTest)  
[./test/unit/user\_test.rb:18:in  
`test_should_initialize_activation_code_upon_creation' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in` **send** ’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active\_support/testing/setup\_and\_teardown.rb:33:in  
`run’]:  
 expected to not be nil.

2. Failure:  
test\_should\_unsuspend\_user\_to\_active\_state(UserTest)  
[./test/unit/user\_test.rb:130:in  
`test_should_unsuspend_user_to_active_state' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in` **send** ’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active\_support/testing/setup\_and\_teardown.rb:33:in  
`run’]:  
 is not true.

3. Failure:  
test\_suspended\_user\_should\_not\_authenticate(UserTest)  
[./test/unit/user\_test.rb:123:in  
`test_suspended_user_should_not_authenticate' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in` **send** ’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active\_support/testing/setup\_and\_teardown.rb:33:in  
`run’]:  
\<#\<User id: 1, login: “quentin”, email: “removed\_email\_address@domain.invalid”,  
crypted\_password: “00742970dc9e6319f8019fd54864d3ea740f04b1”, salt:  
“7e3041ebc2fc05a40c60028e2c4901a81035d3cd”, created\_at: “2009-04-15  
15:13:00”, updated\_at: “2009-04-20 15:13:01”, remember\_token: nil,  
remember\_token\_expires\_at: nil, first\_name: “Quentin”, last\_name:  
“Tarantino”, activation\_code: nil, state: “suspended”, activated\_at:  
nil, deleted\_at: nil\>\> expected to be != to  
\<#\<User id: 1, login: “quentin”, email: “removed\_email\_address@domain.invalid”,  
crypted\_password: “00742970dc9e6319f8019fd54864d3ea740f04b1”, salt:  
“7e3041ebc2fc05a40c60028e2c4901a81035d3cd”, created\_at: “2009-04-15  
15:13:00”, updated\_at: “2009-04-20 15:13:01”, remember\_token: nil,  
remember\_token\_expires\_at: nil, first\_name: “Quentin”, last\_name:  
“Tarantino”, activation\_code: nil, state: “suspended”, activated\_at:  
nil, deleted\_at: nil\>\>.

43 tests, 83 assertions, 3 failures, 0 errors  
rake aborted!  
Command failed with status (1):  
[/System/Library/Frameworks/Ruby.framework/…]

(See full trace by running task with --trace)

Sadly I have no idea how to fix them… what could be wrong? My user  
model looks the following:

require ‘digest/sha1’  
class User \< ActiveRecord::Base

# Virtual attribute for the unencrypted password

attr\_accessor :password

validates\_presence\_of :login,  
:email,  
:first\_name,  
:last\_name  
validates\_presence\_of :password, :if =\>  
:password\_required?  
validates\_presence\_of :password\_confirmation, :if =\>  
:password\_required?  
validates\_length\_of :password, :within =\> 4…40, :if =\>  
:password\_required?  
validates\_confirmation\_of :password, :if =\>  
:password\_required?  
validates\_length\_of :login, :within =\> 3…40  
validates\_length\_of :email, :within =\> 3…100  
validates\_uniqueness\_of :login, :email, :case\_sensitive =\> false  
before\_save :encrypt\_password

has\_many :comments,  
:dependent =\> :destroy

has\_many :blogs,  
:dependent =\> :destroy

# prevents a user from submitting a crafted form that bypasses

activation

# anything else you want your user to change should be added here.

attr\_accessible :login,  
:email,  
:first\_name,  
:last\_name,  
:password,  
:password\_confirmation

acts\_as\_state\_machine :initial =\> :pending  
state :passive  
state :pending, :enter =\> :make\_activation\_code  
state :active, :enter =\> :do\_activate  
state :suspended  
state :deleted, :enter =\> :do\_delete

event :register do  
transitions :from =\> :passive, :to =\> :pending, :guard =\> Proc.new  
{|u| !(u.crypted\_password.blank? && u.password.blank?) }  
end

event :activate do  
transitions :from =\> :pending, :to =\> :active  
end

event :suspend do  
transitions :from =\> [:passive, :pending, :active], :to =\>  
:suspended  
end

event :delete do  
transitions :from =\> [:passive, :pending, :active, :suspended], :to  
=\> :deleted  
end

event :unsuspend do  
transitions :from =\> :suspended, :to =\> :active, :guard =\> Proc.new  
{|u| !u.activated\_at.blank? }  
transitions :from =\> :suspended, :to =\> :pending, :guard =\> Proc.new  
{|u| !u.activation\_code.blank? }  
transitions :from =\> :suspended, :to =\> :passive  
end

# Authenticates a user by their login name and unencrypted password.

Returns the user or nil.  
def self.authenticate(login, password)  
u = find\_by\_login(login) # need to get the salt  
u && u.authenticated?(password) ? u : nil  
end

# Encrypts some data with the salt.

def self.encrypt(password, salt)  
Digest::SHA1.hexdigest("–#{salt}–#{password}–")  
end

# Encrypts the password with the user salt

def encrypt(password)  
self.class.encrypt(password, salt)  
end

def authenticated?(password)  
crypted\_password == encrypt(password)  
end

def remember\_token?  
remember\_token\_expires\_at && Time.now.utc \<  
remember\_token\_expires\_at  
end

# These create and unset the fields required for remembering users

between browser closes  
def remember\_me  
remember\_me\_for 2.weeks  
end

def remember\_me\_for(time)  
remember\_me\_until time.from\_now.utc  
end

def remember\_me\_until(time)  
self.remember\_token\_expires\_at = time  
self.remember\_token =  
encrypt("#{email}–#{remember\_token\_expires\_at}")  
save(false)  
end

def forget\_me  
self.remember\_token\_expires\_at = nil  
self.remember\_token = nil  
save(false)  
end

# Returns true if the user has just been activated.

def recently\_activated?  
@activated  
end

def forgot\_password  
@forgotten\_password = true  
self.make\_password\_reset\_code  
end

def reset\_password  
# First update the password\_reset\_code before setting the  
# reset\_password flag to avoid duplicate email notifications.  
update\_attributes(:password\_reset\_code =\> nil)  
@reset\_password = true  
end

def recently\_forgot\_password?  
@forgotten\_password  
end

def recently\_reset\_password?  
@reset\_password  
end

def display\_name  
“#{first\_name} #{last\_name}”  
end

protected

# before filter

def encrypt\_password  
return if password.blank?  
self.salt = Digest::SHA1.hexdigest("–#{Time.now.to\_s}–#{login}–")  
if new\_record?  
self.crypted\_password = encrypt(password)  
end

def password\_required?  
crypted\_password.blank? || !password.blank?  
end

def make\_activation\_code  
self.deleted\_at = nil  
self.activation\_code = Digest::SHA1.hexdigest(  
Time.now.to\_s.split(//).sort\_by {rand}.join )  
end

def do\_delete  
self.deleted\_at = Time.now.utc  
end

def do\_activate  
@activated = true  
self.activated\_at = Time.now.utc  
self.deleted\_at = self.activation\_code = nil  
end

def make\_password\_reset\_code  
self.password\_reset\_code = Digest::SHA1.hexdigest(  
Time.now.to\_s.split(//).sort\_by {rand}.join )  
end  
end

It’s basically the original model, but I added first\_name and last\_name.

I’d be very thankful for some debugging help…  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995/2 "2009-04-20T21:27:03Z")

</div>

OK, the first error could be solved. I did not call register! when  
creating the object.

protected  
def create\_user(options = {})  
record = User.new({ :login =\> ‘quire’,  
:email =\> ‘removed\_email\_address@domain.invalid’,  
:first\_name =\> ‘Quire’,  
:last\_name =\> ‘Quare’,  
:password =\> ‘quire’,  
:password\_confirmation =\> ‘quire’,  
:state =\> ‘pending’  
}.merge(options))  
record.register! if record.valid? # Here I just called record.save  
before  
record  
end

Anyway, why do I have to call record.register! here? What’s the  
difference to record.save? I guess it has something to do with  
acts\_as\_state\_machine?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995/3 "2009-04-20T21:33:40Z")

</div>

> I guess it has something to do with acts\_as\_state\_machine?

Yes. Don’t you read the source code of your plugins before using them?  
That’s bold.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995/4 "2009-04-20T21:42:32Z")

</div>

Fernando P. wrote:

> > I guess it has something to do with acts\_as\_state\_machine?
> 
> Yes. Don’t you read the source code of your plugins before using them?  
> That’s bold.

Thanks for your very helpful answer. 😉

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995/5 "2009-04-20T21:55:17Z")

</div>

Fernando P. wrote:

> > I guess it has something to do with acts\_as\_state\_machine?
> 
> Yes. Don’t you read the source code of your plugins before using them?  
> That’s bold.

OK, I read some of the source code of the plugin. Sadly I’m no very  
experienced Ruby programmer, so I have a question.

Where does a new record get saved to the DB when calling register!?

> > record = User.new({ :login =\> ‘quire’, :email =\> ‘removed\_email\_address@domain.invalid’, :password =\> ‘quire’, :password\_confirmation =\> ‘quire’ })  
> > =\> #\<User id: nil, first\_name: nil, last\_name: nil, login: “quire”,  
> > email: “removed\_email\_address@domain.invalid”, remember\_token: nil, crypted\_password: nil,  
> > password\_reset\_code: nil, salt: nil, activation\_code: nil,  
> > remember\_token\_expires\_at: nil, activated\_at: nil, deleted\_at: nil,  
> > state: “passive”, created\_at: nil, updated\_at: nil\>
> 
> > record.new\_record?  
> > =\> true
> 
> > record.register!  
> > =\> true
> 
> > record.new\_record?  
> > =\> false

I’m trying to get into this plugin, but it seems very tricky to me…

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 22, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/restful-authentication-3-unit-tests-break/165995/6 "2009-04-22T13:47:51Z")

</div>

On Apr 20, 2:55 pm, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > record.new\_record?
> 
> =\> false
> 
> ## I’m trying to get into this plugin, but it seems very tricky to me…
> 
> Posted viahttp://www.ruby-forum.com/.

The record gets saved by aasm (acts\_as\_state\_machine). By using this  
plugin, your model has different “states” that is stored most likely  
in a :state column. When you call register or register! on your model,  
it is telling aasm to change the state of your model:

event :register do  
transitions :from =\> :passive, :to =\> :pending, :guard =\> Proc.new  
{|u| !(u.crypted\_password.blank? && u.password.blank?) }  
end

In this case, it’s changing your model from the :passive state to  
the :pending state. What happens at this point is that  
make\_activation\_code is called because of this line:

state :pending, :enter =\> :make\_activation\_code.

Your model is saved after you call register because aasm changes the  
state of the model and then saves that model.

Hopefully this helps.

Jon
