# LoginEngine - transaction test oddness

**URL:** <https://www.ruby-forum.com/t/loginengine-transaction-test-oddness/47581>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/loginengine-transaction-test-oddness/47581 "2005-11-12T12:49:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![james.adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@james.adam](https://www.ruby-forum.com/u/james.adam)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/loginengine-transaction-test-oddness/47581/1 "2005-11-12T12:49:31Z")

</div>

I’ve just adapted the original salted hashed login generator tests for  
the login engine, such that we can now run tests against it and verify  
that it behaves nicely, in the same way that you might test any Rails  
plugin:

$ rake test\_plugins # ensuring your test database it set up, etc etc

All the tests pass except one - “test\_change\_password\_with\_bad\_email”.  
This test is meant to simulate a user who is trying to change their  
password, but where the email to notify them cannot be sent. From what  
I can tell, despite the password update being wrapped in a transaction  
(user\_controller.rb, line 69 in the latest revision) which should get  
rolled back upon the mail-related exception, the password when the  
controller action exits has actually been changed! I can’t replicate  
this behaviour in the console, so I’m throwing it out to some  
entreprising plugin debugger out there on the interwebs.

Anyone got any ideas?

- james

------ code snippet follows -----

```
def change_password_for(user, params)
  begin
    User.transaction(user) do
      user.change_password(params[:user][:password],

```

params[:user][:password\_confirmation])  
if user.save  
#@user.reload  
#puts “changed password: #{@user.salted\_password}”  
if LoginEngine.config(:use\_email\_notification)

# —\> the exception gets thrown on the next line

```
          UserNotify.deliver_change_password(user, 

```

params[:user][:password])  
flash[:notice] = “Updated password emailed to  
#{@user.email}”  
else  
flash[:notice] = “Password updated.”  
end  
# since sometimes we’re changing the password from within  
another action/template…  
redirect\_to :action =\> params[:back\_to] if params[:back\_to]  
else  
end  
end  
rescue

# —\> this is where we should land after the exception, and the

transaction should roll back

# —\> but the password seems to be changed.

```
    flash[:warning] = 'Password could not be changed at this time.

```

Please retry.’  
end  
end

---

<div class="post-metadata">

**Author:** ![james.adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@james.adam](https://www.ruby-forum.com/u/james.adam)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/loginengine-transaction-test-oddness/47581/2 "2005-11-12T12:49:31Z")

</div>

James,

```
Here's an an obvious question, but are you using InnoDB tables

```

(assuming you are using MySQL)?

Joe

---

<div class="post-metadata">

**Author:** ![james.adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@james.adam](https://www.ruby-forum.com/u/james.adam)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/loginengine-transaction-test-oddness/47581/3 "2005-11-12T12:49:32Z")

</div>

On 11/9/05, Joseph H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > the login engine, such that we can now run tests against it and verify  
> > (user\_controller.rb, line 69 in the latest revision) which should get  
> > rolled back upon the mail-related exception, the password when the  
> > controller action exits has actually been changed! I can’t replicate  
> > this behaviour in the console, so I’m throwing it out to some  
> > entreprising plugin debugger out there on the interwebs.
> > 
> > Anyone got any ideas?
> > 
> > - james

Postgresql doesn’t allow nested transactions, so you have to disable  
transactional fixtures for methods using transactions themselves.

class MyTest \< Test::Unit::TestCase  
uses\_transaction :test\_foo

def test\_foo  
end

# tests…

end

I discovered this ages ago (in Rails time), so does anyone know if  
there is a better way to do this?

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![james.adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@james.adam](https://www.ruby-forum.com/u/james.adam)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/loginengine-transaction-test-oddness/47581/4 "2005-11-12T12:49:37Z")

</div>

To the best of my knowledge the schema.rb file is creating InnoDB  
tables, yes - that was one of the first things I checked. What struck  
me as most odd is that the method appears to work from the console…

Any other ideas? It would be great to see all tests passing…

---

<div class="post-metadata">

**Author:** ![james.adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@james.adam](https://www.ruby-forum.com/u/james.adam)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/loginengine-transaction-test-oddness/47581/5 "2005-11-12T12:49:33Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 9, 2005, at 11:34 AM, Rick O. wrote:

> end
> 
> I discovered this ages ago (in Rails time), so does anyone know if  
> there is a better way to do this?

You nailed it. Another way is to segregate the tests which  
specifically need to check the results of a rollback into another  
test case which has disabled transactional fixtures.

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDcmaUAQHALep9HFYRAlIdAKChZBQty9B4Px59iOWrRbHDPJCo2gCfaayt  
pGgJDQYAsPtxkett1lYIvSo=  
=4Cgw  
-----END PGP SIGNATURE-----
