質問編集履歴
1
コード追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
パスワード再設定の統合テストの統合テストをしたところ以下のエラーコードが出ました。password_resets_controller、password_resets_
|
1
|
+
パスワード再設定の統合テストの統合テストをしたところ以下のエラーコードが出ました。password_resets_controller、password_resets_test 両方確認したのですが原因が見当たりません。 このエラーの直し方を教えてください。
|
2
2
|
|
3
3
|
```
|
4
4
|
|
@@ -11,3 +11,271 @@
|
|
11
11
|
test/controllers/password_resets_controller_test.rb:11:in `block in <class:PasswordResetsControllerTest>'
|
12
12
|
|
13
13
|
```
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
password_resets_controller
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
class PasswordResetsController < ApplicationController
|
22
|
+
|
23
|
+
before_action :get_user, only: [:edit, :update]
|
24
|
+
|
25
|
+
before_action :valid_user, only: [:edit, :update]
|
26
|
+
|
27
|
+
before_action :check_expiration, only: [:edit, :update] # (1) への対応
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
def new
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
def create
|
38
|
+
|
39
|
+
@user = User.find_by(email: params[:password_reset][:email].downcase)
|
40
|
+
|
41
|
+
if @user
|
42
|
+
|
43
|
+
@user.create_reset_digest
|
44
|
+
|
45
|
+
@user.send_password_reset_email
|
46
|
+
|
47
|
+
flash[:info] = "Email sent with password reset instructions"
|
48
|
+
|
49
|
+
redirect_to root_url
|
50
|
+
|
51
|
+
else
|
52
|
+
|
53
|
+
flash.now[:danger] = "Email address not found"
|
54
|
+
|
55
|
+
render 'new'
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
def edit
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
def update
|
70
|
+
|
71
|
+
if params[:user][:password].empty? # (3) への対応
|
72
|
+
|
73
|
+
@user.errors.add(:password, "can't be empty")
|
74
|
+
|
75
|
+
render 'edit'
|
76
|
+
|
77
|
+
elsif @user.update_attributes(user_params) # (4) への対応
|
78
|
+
|
79
|
+
log_in @user
|
80
|
+
|
81
|
+
flash[:success] = "Password has been reset."
|
82
|
+
|
83
|
+
redirect_to @user
|
84
|
+
|
85
|
+
else
|
86
|
+
|
87
|
+
render 'edit' # (2) への対応
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def user_params
|
100
|
+
|
101
|
+
params.require(:user).permit(:password, :password_confirmation)
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
# beforeフィルタ
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def get_user
|
112
|
+
|
113
|
+
@user = User.find_by(email: params[:email])
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
# 有効なユーザーかどうか確認する
|
120
|
+
|
121
|
+
def valid_user
|
122
|
+
|
123
|
+
unless (@user && @user.activated? &&
|
124
|
+
|
125
|
+
@user.authenticated?(:reset, params[:id]))
|
126
|
+
|
127
|
+
redirect_to root_url
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
# トークンが期限切れかどうか確認する
|
136
|
+
|
137
|
+
def check_expiration
|
138
|
+
|
139
|
+
if @user.password_reset_expired?
|
140
|
+
|
141
|
+
flash[:danger] = "Password reset has expired."
|
142
|
+
|
143
|
+
redirect_to new_password_reset_url
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
password_resets_test
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
require 'test_helper'
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
class PasswordResetsTest < ActionDispatch::IntegrationTest
|
164
|
+
|
165
|
+
def setup
|
166
|
+
|
167
|
+
ActionMailer::Base.deliveries.clear
|
168
|
+
|
169
|
+
@user = users(:michael)
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
test "password resets" do
|
176
|
+
|
177
|
+
get new_password_reset_path
|
178
|
+
|
179
|
+
assert_template 'password_resets/new'
|
180
|
+
|
181
|
+
# メールアドレスが無効
|
182
|
+
|
183
|
+
post password_resets_path, params: { password_reset: { email: "" } }
|
184
|
+
|
185
|
+
assert_not flash.empty?
|
186
|
+
|
187
|
+
assert_template 'password_resets/new'
|
188
|
+
|
189
|
+
# メールアドレスが有効
|
190
|
+
|
191
|
+
post password_resets_path,
|
192
|
+
|
193
|
+
params: { password_reset: { email: @user.email } }
|
194
|
+
|
195
|
+
assert_not_equal @user.reset_digest, @user.reload.reset_digest
|
196
|
+
|
197
|
+
assert_equal 1, ActionMailer::Base.deliveries.size
|
198
|
+
|
199
|
+
assert_not flash.empty?
|
200
|
+
|
201
|
+
assert_redirected_to root_url
|
202
|
+
|
203
|
+
# パスワード再設定フォームのテスト
|
204
|
+
|
205
|
+
user = assigns(:user)
|
206
|
+
|
207
|
+
# メールアドレスが無効
|
208
|
+
|
209
|
+
get edit_password_reset_path(user.reset_token, email: "")
|
210
|
+
|
211
|
+
assert_redirected_to root_url
|
212
|
+
|
213
|
+
# 無効なユーザー
|
214
|
+
|
215
|
+
user.toggle!(:activated)
|
216
|
+
|
217
|
+
get edit_password_reset_path(user.reset_token, email: user.email)
|
218
|
+
|
219
|
+
assert_redirected_to root_url
|
220
|
+
|
221
|
+
user.toggle!(:activated)
|
222
|
+
|
223
|
+
# メールアドレスが有効で、トークンが無効
|
224
|
+
|
225
|
+
get edit_password_reset_path('wrong token', email: user.email)
|
226
|
+
|
227
|
+
assert_redirected_to root_url
|
228
|
+
|
229
|
+
# メールアドレスもトークンも有効
|
230
|
+
|
231
|
+
get edit_password_reset_path(user.reset_token, email: user.email)
|
232
|
+
|
233
|
+
assert_template 'password_resets/edit'
|
234
|
+
|
235
|
+
assert_select "input[name=email][type=hidden][value=?]", user.email
|
236
|
+
|
237
|
+
# 無効なパスワードとパスワード確認
|
238
|
+
|
239
|
+
patch password_reset_path(user.reset_token),
|
240
|
+
|
241
|
+
params: { email: user.email,
|
242
|
+
|
243
|
+
user: { password: "foobaz",
|
244
|
+
|
245
|
+
password_confirmation: "barquux" } }
|
246
|
+
|
247
|
+
assert_select 'div#error_explanation'
|
248
|
+
|
249
|
+
# パスワードが空
|
250
|
+
|
251
|
+
patch password_reset_path(user.reset_token),
|
252
|
+
|
253
|
+
params: { email: user.email,
|
254
|
+
|
255
|
+
user: { password: "",
|
256
|
+
|
257
|
+
password_confirmation: "" } }
|
258
|
+
|
259
|
+
assert_select 'div#error_explanation'
|
260
|
+
|
261
|
+
# 有効なパスワードとパスワード確認
|
262
|
+
|
263
|
+
patch password_reset_path(user.reset_token),
|
264
|
+
|
265
|
+
params: { email: user.email,
|
266
|
+
|
267
|
+
user: { password: "foobaz",
|
268
|
+
|
269
|
+
password_confirmation: "foobaz" } }
|
270
|
+
|
271
|
+
assert is_logged_in?
|
272
|
+
|
273
|
+
assert_not flash.empty?
|
274
|
+
|
275
|
+
assert_redirected_to user
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
```
|