New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BooksControllerでのログイン有無チェックを修正 #5921
Conversation
@akingo55 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確認しました!問題なさそうかと思います。
今回バグとなっていたBooksController以外については、現状のログインチェックの内容が正しいかどうかの検討は実施していません。
コード上はBooksController
以外も修正しているので、それらも動作確認が必要かと思いました!(また別のバグを生む可能性もあるかと思うので)
私の方では問題なさそうでしたので、Approveします!
@akingo55
こちらについて、すみません書き方がわるかったかもしれないのですが、「仕様が正しいかを改めて確認するということはせずに、現状の仕様のままになるように修正した」という意図でした。 |
@komagata
くらいの選択肢が考えられるかなあと思ったのですが、2で進めることは問題あるでしょうか・・? |
@peno022 最初にお話しいただいた時に、これが念頭にあるのでやるかどうか迷ったのですが、やるからには全件テストで行きたいと思います。 分量が多くなるのでポイントを増やしておきたいと思います。 |
@komagata 承知しました!🙇♀️ |
@komagata
1には /users/#{@user_id} 配下のものしか書かれていないので今回は2にテスト追加する必要があるのか、そもそもまとめてしまってよいファイルなのか確認したいです。 |
本来はどちらかに統一すべきところを把握できていないので別のファイルに書いてしまったのだと思います。 もし作業時に問題なければ統一してしまっていただければ助かります〜。 |
16eb8ed
to
4941816
Compare
@komagata 下記の形でテスト追加に着手したのですが、懸念あるでしょうか? ① ② ③単純な削除/追加になっておらず、only/except等条件をつけたcontrollerのテスト ④その他 |
OKです〜。
できれば「どれか1つのアクション」ではなく全部のアクションをテストしたいです〜。
基本的にはOKです〜。 ログイン必須のテストは もしくは、「ログインしてないとXXXという表示が出る」「ログインしていればXXXという表示がでない」というテストであればURLを渡すだけでテストできるような形に共通化して一気に処理できちゃうかもですね。
OKです〜。 すみません、認証関連の書き換えの件を以前は同じPRでいいといっちゃったんですが、共通化などを考えるとやはり別のIssueにした方がいいと思い直したので #5984 というIssueを作成しました。PRはこのままで大丈夫ですが、こちらの新しいIssueのポイントもGETできる感じで考えていただければ〜。 |
@komagata |
…ーラでだけskip_before_actionを書く形に修正する
f66ed5c
to
d02a605
Compare
class UsersLoginTest < ApplicationSystemTestCase | ||
include LoginAssertHelper | ||
test 'cannot access users list without login' do | ||
assert_login_required('/users') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_login_required('/users') | |
assert_login_required '/users' |
複雑な場合以外はかっこはなくていいかもです〜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正しました!
test/system/require_login_test.rb
Outdated
assert_text 'ログインしてください' | ||
include LoginAssertHelper | ||
test 'redirect to welcome page and show message when you visit login required pages without login' do | ||
assert_login_required('/announcements') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
とってもいい感じだと思います〜!
pathの配列を受け取るようにしてもいいかもですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@komagata
レビューありがとうございます!修正しましたので再度ご確認お願いします🙏
pathの配列を受け取る形にしてもいいかも、というご指摘については、そうできていなかった背景の懸念をコメントに記載させていただきました🙇♀️ ご確認いただけますと幸いです。
test 'redirect to welcome page and show message when you visit login required pages without login' do | ||
assert_login_required '/announcements' | ||
assert_login_required '/books' | ||
assert_login_required "/companies/#{companies(:company1).id}/products" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらについて、assert_login_required
をたくさん書くことになるのでご指摘いただいたように配列を渡す形も検討したのですが、
paths = ['/announcements', '/books', '/companies']
paths.each do |path|
assert_login_required(path)
end
もしくは
def assert_login_required(paths)
paths.each do |path|
visit path
assert_text 'ログインしてください'
assert_selector 'h3', text: 'フィヨルドブートキャンプとは?'
end
end
のようにした場合、記述としては簡単になりますが、テストが失敗した時に、どのパスを渡した時でも同じ行がエラー行としてログに出てくるので、どれで失敗したかが分かりづらくなりそう、というのを懸念して、上記の形にはしませんでした。
配列を渡すにしても上記の問題は回避できる方法があれば、ご指摘いただけますと嬉しいです🙇♀️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peno022 なるほどですね。
でしたらそのままでいいと思います〜。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確認させて頂きました。OKです〜🙆♂️
test 'redirect to welcome page and show message when you visit login required pages without login' do | ||
assert_login_required '/announcements' | ||
assert_login_required '/books' | ||
assert_login_required "/companies/#{companies(:company1).id}/products" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peno022 なるほどですね。
でしたらそのままでいいと思います〜。
@peno022 作業量が多くて大変なIssueありがとうございます!お疲れ様です〜! |
@komagata 大きいPRになってしまいましたが、レビューありがとうございましたー!!🙌 |
Issue
概要
http://localhost:3000/books はログインしないと閲覧できないページですが、 BooksControllerでは ログインの確認がされていなかったため、ログインせずにアクセスするとcurrent_userがnilの状態でviewが呼び出され、
/app/app/views/books/index.html.slim
でNoMethodError: undefined method admin_or_mentor? for nil:NilClass
のエラーが発生していました。同件のバグを防ぐことも考慮し、
app/controllers/application_controller.rb
にbefore_action :require_login
を追加して、ログインが不要なページのみrequire_loginをskipする方針で修正しました。#5788 の対応で行った
の修正について、本PRで対応することは変わらないですが、別issueとして切り出していただいています。
修正を進める中で、ポートフォリオページでもbooksページと同様のバグが発生しているのを見つけたので、あわせて修正しました。ログイン必須ページとなるように修正しています。
修正対象のファイルが多いですが、バグとなっていた
app/controllers/books_controller.rb
、app/controllers/users/works_controller.rb
以外は、仕様が変わらないよう機械的な修正になっています。app/controllers/books_controller.rb
については、2点見直しbefore_action: require_admin_or_mentor_login
の対象にすることapp/controllers/users/works_controller.rb
については、1点見直し修正した手順
app/controllers/application_controller.rb
にbefore_action :require_login
を追記< ApplicationController
を含む全ファイルを検索(75ファイル)before_action :require_login
があるものは削除skip_before_action :require_login, raise: false
を追加(対象アクションが絞られているものはそれに従う)変更確認方法
bug/nomethoderror-on-books-without-login
をローカルに取り込む。Screenshot
ログインしていない状態で http://localhost:3000/books にアクセスした時:
変更前
変更後