forked from heartcombo/devise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers_test.rb
178 lines (144 loc) · 4.27 KB
/
test_helpers_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'test_helper'
class TestHelpersTest < ActionController::TestCase
tests UsersController
include Devise::TestHelpers
test "redirects if attempting to access a page unauthenticated" do
get :index
assert_redirected_to new_user_session_path
assert_equal "You need to sign in or sign up before continuing.", flash[:alert]
end
test "redirects if attempting to access a page with an unconfirmed account" do
swap Devise, allow_unconfirmed_access_for: 0.days do
user = create_user
assert !user.active_for_authentication?
sign_in user
get :index
assert_redirected_to new_user_session_path
end
end
test "returns nil if accessing current_user with an unconfirmed account" do
swap Devise, allow_unconfirmed_access_for: 0.days do
user = create_user
assert !user.active_for_authentication?
sign_in user
get :accept, id: user
assert_nil assigns(:current_user)
end
end
test "does not redirect with valid user" do
user = create_user
user.confirm
sign_in user
get :index
assert_response :success
end
test "does not redirect with valid user after failed first attempt" do
get :index
assert_response :redirect
user = create_user
user.confirm
sign_in user
get :index
assert_response :success
end
test "redirects if valid user signed out" do
user = create_user
user.confirm
sign_in user
get :index
sign_out user
get :index
assert_redirected_to new_user_session_path
end
test "respects custom failure app" do
custom_failure_app = Class.new(Devise::FailureApp) do
def redirect
self.status = 306
end
end
swap Devise.warden_config, failure_app: custom_failure_app do
get :index
assert_response 306
end
end
test "passes given headers from the failure app to the response" do
custom_failure_app = Class.new(Devise::FailureApp) do
def respond
self.status = 401
self.response.headers["CUSTOMHEADER"] = 1
end
end
swap Devise.warden_config, failure_app: custom_failure_app do
sign_in create_user
get :index
assert_equal 1, @response.headers["CUSTOMHEADER"]
end
end
test "returns the body of a failure app" do
get :index
assert_equal response.body, "<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>"
end
test "defined Warden after_authentication callback should not be called when sign_in is called" do
begin
Warden::Manager.after_authentication do |user, auth, opts|
flunk "callback was called while it should not"
end
user = create_user
user.confirm
sign_in user
ensure
Warden::Manager._after_set_user.pop
end
end
test "defined Warden before_logout callback should not be called when sign_out is called" do
begin
Warden::Manager.before_logout do |user, auth, opts|
flunk "callback was called while it should not"
end
user = create_user
user.confirm
sign_in user
sign_out user
ensure
Warden::Manager._before_logout.pop
end
end
test "before_failure call should work" do
begin
executed = false
Warden::Manager.before_failure do |env,opts|
executed = true
end
user = create_user
sign_in user
get :index
assert executed
ensure
Warden::Manager._before_failure.pop
end
end
test "allows to sign in with different users" do
first_user = create_user
first_user.confirm
sign_in first_user
get :index
assert_match /User ##{first_user.id}/, @response.body
sign_out first_user
second_user = create_user
second_user.confirm
sign_in second_user
get :index
assert_match /User ##{second_user.id}/, @response.body
end
test "creates a new warden proxy if the request object has changed" do
old_warden_proxy = warden
@request = ActionController::TestRequest.new
new_warden_proxy = warden
assert_not_equal old_warden_proxy, new_warden_proxy
end
test "doesn't create a new warden proxy if the request object hasn't changed" do
old_warden_proxy = warden
new_warden_proxy = warden
assert_equal old_warden_proxy, new_warden_proxy
end
end