質問編集履歴

1

具体的な質問を記述しました

2017/01/16 13:37

投稿

TK0107
TK0107

スコア11

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,277 @@
3
3
  リスト10.57 10.58 10.59
4
4
 
5
5
  に黄色にハイライトされた行を変更して行うものなのでしょうか?
6
+
7
+
8
+
9
+ すみません、あまりにも質問が曖昧すぎました
10
+
11
+ 具体的なコードを教えていただけますでしょうか、よろしくお願いします。
12
+
13
+
14
+
15
+
16
+
17
+ 1、以下のコードの一番下にあるtest "expired token" doを修正して、期限切れのパスワード再設定のブランチの統合テストを作成せよ、という演習です
18
+
19
+ assert_match /FILL_IN/i, response.body のFILL_INを期限と比較する旨のコードを記述すると思ったのですが、どのように書けばいいかわからないです
20
+
21
+ ```Ruby
22
+
23
+ #test/integration/password_resets_test.rb
24
+
25
+ require 'test_helper'
26
+
27
+
28
+
29
+ class PasswordResetsTest < ActionDispatch::IntegrationTest
30
+
31
+ def setup
32
+
33
+ ActionMailer::Base.deliveries.clear
34
+
35
+ @user = users(:michael)
36
+
37
+ end
38
+
39
+
40
+
41
+ test "password resets" do
42
+
43
+ get new_password_reset_path
44
+
45
+ assert_template 'password_resets/new'
46
+
47
+ # メールアドレスが無効
48
+
49
+ post password_resets_path, password_reset: { email: "" }
50
+
51
+ assert_not flash.empty?
52
+
53
+ assert_template 'password_resets/new'
54
+
55
+ # メールアドレスが有効
56
+
57
+ post password_resets_path, password_reset: { email: @user.email }
58
+
59
+ assert_not_equal @user.reset_digest, @user.reload.reset_digest
60
+
61
+ assert_equal 1, ActionMailer::Base.deliveries.size
62
+
63
+ assert_not flash.empty?
64
+
65
+ assert_redirected_to root_url
66
+
67
+ # パスワード再設定用フォーム
68
+
69
+ user = assigns(:user)
70
+
71
+ # メールアドレスが無効
72
+
73
+ get edit_password_reset_path(user.reset_token, email: "")
74
+
75
+ assert_redirected_to root_url
76
+
77
+ # 無効なユーザー
78
+
79
+ user.toggle!(:activated)
80
+
81
+ get edit_password_reset_path(user.reset_token, email: user.email)
82
+
83
+ assert_redirected_to root_url
84
+
85
+ user.toggle!(:activated)
86
+
87
+ # メールアドレスが正しく、トークンが無効
88
+
89
+ get edit_password_reset_path('wrong token', email: user.email)
90
+
91
+ assert_redirected_to root_url
92
+
93
+ # メールアドレスもトークンも有効
94
+
95
+ get edit_password_reset_path(user.reset_token, email: user.email)
96
+
97
+ assert_template 'password_resets/edit'
98
+
99
+ assert_select "input[name=email][type=hidden][value=?]", user.email
100
+
101
+ # 無効なパスワードと確認
102
+
103
+ patch password_reset_path(user.reset_token),
104
+
105
+ email: user.email,
106
+
107
+ user: { password: "foobaz",
108
+
109
+ password_confirmation: "barquux" }
110
+
111
+ assert_select 'div#error_explanation'
112
+
113
+ # パスワードが空
114
+
115
+ patch password_reset_path(user.reset_token),
116
+
117
+ email: user.email,
118
+
119
+ user: { password: "",
120
+
121
+ password_confirmation: "" }
122
+
123
+ assert_select 'div#error_explanation'
124
+
125
+ # 有効なパスワードと確認
126
+
127
+ patch password_reset_path(user.reset_token),
128
+
129
+ email: user.email,
130
+
131
+ user: { password: "foobaz",
132
+
133
+ password_confirmation: "foobaz" }
134
+
135
+ assert is_logged_in?
136
+
137
+ assert_not flash.empty?
138
+
139
+ assert_redirected_to user
140
+
141
+
142
+
143
+ test "expired token" do
144
+
145
+ get new_password_reset_path
146
+
147
+ post password_resets_path, password_reset: { email: @user.email }
148
+
149
+
150
+
151
+ @user = assigns(:user)
152
+
153
+ @user.update_attribute(:reset_sent_at, 3.hours.ago)
154
+
155
+ patch password_reset_path(@user.reset_token),
156
+
157
+ email: @user.email,
158
+
159
+ user: { password: "foobar",
160
+
161
+ password_confirmation: "foobar" }
162
+
163
+ assert_response :redirect
164
+
165
+ follow_redirect!
166
+
167
+ assert_match /FILL_IN/i, response.body
168
+
169
+ end
170
+
171
+ end
172
+
173
+
174
+
175
+ ```
176
+
177
+
178
+
179
+
180
+
181
+ 2、以下のindexとshowを修正して有効なユーザーのみを表示するようにせよ、という問題です
182
+
183
+ indexはUser.where(activated: FILL_IN)のFILL_INをtureにすればいいと思いました
184
+
185
+ showは「空(nil)でない限り、indexを返す」という解釈でいいのでしょうか?
186
+
187
+ ```Ruby
188
+
189
+ #app/controllers/users_controller.rb
190
+
191
+
192
+
193
+ class UsersController < ApplicationController
194
+
195
+
196
+
197
+ def index
198
+
199
+ @users = User.where(activated: FILL_IN).paginate(page: params[:page])
200
+
201
+ end
202
+
203
+
204
+
205
+ def show
206
+
207
+ @user = User.find(params[:id])
208
+
209
+ redirect_to root_url and return unless FILL_IN
210
+
211
+ end
212
+
213
+
214
+
215
+ end
216
+
217
+
218
+
219
+ ```
220
+
221
+
222
+
223
+
224
+
225
+ 3、activateメソッドとcreate_reset_digestメソッドの両方でupdate_attributeを呼び出しており、それぞれのアクセスによってデータベーストランザクションが個別に発生してしまう 以下のコードを埋めて、個別のupdate_attribute呼び出しを単一のupdate_columns呼び出しに統合し、データベースアクセスが1回で済むようにせよ、という問題です
226
+
227
+ activeのFILL_INにそれぞれ、trueと登録した日時(専用に呼び出す関数あり?)が入ると思います
228
+
229
+ create_reset_digestの1つ目のFILL_INは記憶トークン、2つ目にはメールを送信した日時が入ると考えています
230
+
231
+ ```Ruby
232
+
233
+
234
+
235
+ class User < ActiveRecord::Base
236
+
237
+ attr_accessor :remember_token, :activation_token, :reset_token
238
+
239
+ before_save :downcase_email
240
+
241
+ before_create :create_activation_digest
242
+
243
+
244
+
245
+ # アカウントを有効にする
246
+
247
+ def activate
248
+
249
+ update_columns(activated: FILL_IN, activated_at: FILL_IN)
250
+
251
+ end
252
+
253
+
254
+
255
+ # 有効化用のメールを送信する
256
+
257
+ def send_activation_email
258
+
259
+ UserMailer.account_activation(self).deliver_now
260
+
261
+ end
262
+
263
+
264
+
265
+ # パスワード再設定の属性を設定する
266
+
267
+ def create_reset_digest
268
+
269
+ self.reset_token = User.new_token
270
+
271
+ update_columns(reset_digest: FILL_IN,
272
+
273
+ reset_sent_at: FILL_IN)
274
+
275
+ end
276
+
277
+ end
278
+
279
+ ```