Skip to content
This repository
Browse code

Allow to sign in with two different users in the same functional test…

… with Devise::TestHelpers.
  • Loading branch information...
commit b04241ddff088c3e1418608ec04cc061e1bdc467 1 parent b8c216e
José Valim josevalim authored
4 CHANGELOG.rdoc
Source Rendered
... ... @@ -1,3 +1,7 @@
  1 +* bug fix
  2 + * Fixed render_with_scope to work with all controllers
  3 + * Allow sign in with two different users in Devise::TestHelpers
  4 +
1 5 == 0.7.1
2 6
3 7 * enhancements
1  lib/devise/test_helpers.rb
@@ -78,6 +78,7 @@ def sign_in(resource_or_scope, resource=nil)
78 78 #
79 79 def sign_out(resource_or_scope)
80 80 scope = Devise::Mapping.find_scope!(resource_or_scope)
  81 + @controller.instance_variable_set(:"@current_#{scope}", nil)
81 82 warden.logout(scope)
82 83 end
83 84
4 test/rails_app/app/controllers/users_controller.rb
@@ -9,4 +9,8 @@ def expire
9 9 user_session['last_request_at'] = 31.minutes.ago.utc
10 10 render :text => 'User will be expired on next request'
11 11 end
  12 +
  13 + def show
  14 + render :text => current_user.id.to_s
  15 + end
12 16 end
31 test/test_helpers_test.rb
@@ -5,14 +5,14 @@ class TestHelpersTest < ActionController::TestCase
5 5 include Devise::TestHelpers
6 6
7 7 test "redirects if attempting to access a page unauthenticated" do
8   - get :index
  8 + get :show
9 9 assert_redirected_to "/users/sign_in?unauthenticated=true"
10 10 end
11 11
12 12 test "redirects if attempting to access a page with a unconfirmed account" do
13 13 swap Devise, :confirm_within => 0 do
14 14 sign_in create_user
15   - get :index
  15 + get :show
16 16 assert_redirected_to "/users/sign_in?unconfirmed=true"
17 17 end
18 18 end
@@ -22,7 +22,7 @@ class TestHelpersTest < ActionController::TestCase
22 22 user.confirm!
23 23
24 24 sign_in user
25   - get :index
  25 + get :show
26 26 assert_response :success
27 27 end
28 28
@@ -31,14 +31,31 @@ class TestHelpersTest < ActionController::TestCase
31 31 user.confirm!
32 32
33 33 sign_in user
34   - get :index
  34 + get :show
35 35
36 36 sign_out user
37   - get :index
  37 + get :show
38 38 assert_redirected_to "/users/sign_in?unauthenticated=true"
39 39 end
40 40
41   - def create_user
42   - User.create!(:email => "jose.valim@plataformatec.com", :password => "123456")
  41 + test "allows to sign in with different users" do
  42 + first_user = create_user(1)
  43 + first_user.confirm!
  44 +
  45 + sign_in first_user
  46 + get :show
  47 + assert_equal first_user.id.to_s, @response.body
  48 + sign_out first_user
  49 +
  50 + second_user = create_user(2)
  51 + second_user.confirm!
  52 +
  53 + sign_in second_user
  54 + get :show
  55 + assert_equal second_user.id.to_s, @response.body
  56 + end
  57 +
  58 + def create_user(i=nil)
  59 + User.create!(:email => "jose.valim#{i}@plataformatec.com", :password => "123456")
43 60 end
44 61 end

0 comments on commit b04241d

Please sign in to comment.
Something went wrong with that request. Please try again.