【135日目】【1日20分のRailsチュートリアル】【第10章】パスワード再設定用メイラーメソッドのテストを書く
今日は「10.2.3 PasswordResetsメイラーメソッド」のテストを書くところから。
10.2.3 PasswordResetsメイラーメソッド
アカウント有効化メイラーメソッドのテスト (リスト10.18) の場合と同様、パスワード再設定用メイラーメソッドのテストを書くことにします (リスト10.47)。
アカウント有効化のときと同じようなテストを作成する。
test/mailers/user_mailer_test.rb
test "password_reset" do user = users(:michael) user.reset_token = User.new_token mail = UserMailer.password_reset(user) assert_equal "Password reset", mail.subject assert_equal [user.email], mail.to assert_equal ["noreply@example.com"], mail.from assert_match user.reset_token, mail.body.encoded assert_match CGI::escape(user.email), mail.body.encoded end
テスト実行して問題ないことを確認。
$ bundle exec rake test 44 tests, 192 assertions, 0 failures, 0 errors, 0 skips
リスト10.43、リスト10.44、リスト10.45のコードを使用すると、正しいメールアドレスを送信したときの画面は図10.16のようになります。このメールはサーバーログではリスト10.49のように表示されます。
正しいメールアドレス送信してみよう。
サーバーを起動して
$ rails server -b $IP -p $PORT
パスワード再設定画面から有効なメールアドレスを送信。
リダイレクト後の画面にflashメッセージが表示されることを確認。
パスワード再設定メールもサーバーログに出力されてました。
Sent mail to <メールアドレス> (16.9ms) Date: Wed, 19 Apr 2017 01:58:18 +0000 From: noreply@example.com To: <メールアドレス> Message-ID: <58f6c43a2f077_6c13f9569dca45481827@kt-zoe-rails-tutorial-3478208.mail> Subject: Password reset Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="--==_mimepart_58f6c43a2b791_6c13f9569dca4548172a"; charset=UTF-8 Content-Transfer-Encoding: 7bit ----==_mimepart_58f6c43a2b791_6c13f9569dca4548172a Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit To reset your password click the link below: http://rails-tutorial-kt-zoe.c9users.io//password_resets/DQVXIK-zO7OtQwQfF7VaWQ/edit?email=<メールアドレス> This link will expire in two hours. If you did not request your password to be reset, please ignore this email and your password will stay as it is. ----==_mimepart_58f6c43a2b791_6c13f9569dca4548172a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit <html> <body> <h1>Password reset</h1> <p>To reset your password click the link below:</p> <a href="http://rails-tutorial-kt-zoe.c9users.io//password_resets/DQVXIK-zO7OtQwQfF7VaWQ/edit?email=<メールアドレス>">Reset password</a> <p>This link will expire in two hours.</p> <p> If you did not request your password to be reset, please ignore this email and your password will stay as it is. </p> </body> </html> ----==_mimepart_58f6c43a2b791_6c13f9569dca4548172a--
(<メールアドレス>の部分は実際にはフォームに入れたメールアドレスが表示される)
メールに記載されているURLが動作するにはまだ実装が必要らしい。もうちょいかな。
今日の学習時間は【17分】。
次は「10.2.4 パスワードを再設定する」から。