LoginSignup
4
2

More than 3 years have passed since last update.

cookieでログインできるかRSpecでテストしてみた

Posted at

rails6でログインしたら永続cookieができて、そのcookieによって再度ログインしなくてもログイン状態になるかのsystem specでテストしてみた。railsチュートリアルでいうと9章。多分。

ここではログアウトと表示されているリンクがあればログイン状態とする。

結論

sessions_spec.rb
require 'rails_helper'
  describe "Sessions", type: :system do
    let(:user) { FactoryBot.build(:user) }


    it "cookieがある場合はログイン状態になること", focus: true do
      user.save
      test_log_in
      show_me_the_cookies
      expire_cookies
      show_me_the_cookies
      visit current_path
      show_me_the_cookies
      expect(page).to have_content "ログアウト"
    end
  end
login_support.rb
  def test_log_in
    visit login_path
    fill_in "User_id", with: user.acount_id
    fill_in "Password", with: user.password
    click_button "Login"
  end

gem 'show_me_the_cookies'を使う

テストを書くときにテスト内のsessionやcookieを見たりしたかったのでこのgemを使った。
READMEには
Some helpers for poking around at your Capybara driven browser's cookies in integration tests.
こう書いてある。
https://github.com/nruth/show_me_the_cookies

  # gemfile
  gem 'show_me_the_cookies'

system specにincludeさせる

rails_helper.rb
RSpec.configure do |config|
  config.include ShowMeTheCookies, :type => :system  #追記
# 省略

show_me_the_cookies

上記のsessions_spec.rbの'show_me_the_cookies'はこのgemのヘルパーで、その時点でのテストで使うブラウザ内のcookieの要約を全てコンソールに出力してくれる。

expire_cookies

'expire_cookies'はその時点でのテストブラウザにあるsessionのためのcookieと期限切れのcookieをとり除いてくれる。そして、ログインのためのsessionがこのときに切れる。つまり、ブラウザを再起動した状態(?)になる。


1つ目のshow_me_the_cookiesでは配列Cookiesのなかに3つの要素がハッシュとして入っていた。

# 一つ目のshow_me_the_cookies

Cookies: [{:name=>"remember_token",:value=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkltWTRibmhYT1RKaFRXWTFibkpmUmxwSk5HMUdaRkVpIiwiZXhwIjpudWxsLCJwdXIiOiJjb29raWUucmVtZW1iZXJfdG9rZW4ifX0%3D--1319af8bac18ac3543bb2e86ddb00b5f18c824aa", :path=>"/", :domain=>"127.0.0.1", :expires=>Wed, 02 May 2040 07:54:46 +0000, :secure=>false}, 
{:name=>"_toeic_session", :value=>"vTtI73iYANuzGKNdkz8hJGnbQ7siWIdP1vJh7cD3GJaezRa0xVhAiePckqulEKFbH9BQbCOz1GsHrt2wOrPGmuFtR8uBeFDbTZUPtTWZp%2FUAn8trT9l4t7hePMUH%2BqUJlGyRWwIgshZdw9wMUZwSaI9tQu%2FgZ0WwNT%2BiRruNpFFCUCPmrpR30sA6zr2fl1ACZp4o1ETE--6OFEmTa33VNhxhSR--BYG3fmppqXRd%2BsnV0H7%2FSg%3D%3D", :path=>"/", :domain=>"127.0.0.1", :expires=>nil, :secure=>false},
{:name=>"user_id", :value=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6Ik1RPT0iLCJleHAiOm51bGwsInB1ciI6ImNvb2tpZS51c2VyX2lkIn19--46330cb4f9e1d5389813713021d4fae09d8c34a0", :path=>"/", :domain=>"127.0.0.1", :expires=>Wed, 02 May 2040 07:54:46 +0000, :secure=>false}]

2つ目では、session用のcookieだけ消えて、要素が2つになってる

# 2つめのshow_me_the_cookies

Cookies: [{:name=>"remember_token", :value=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkltWTRibmhYT1RKaFRXWTFibkpmUmxwSk5HMUdaRkVpIiwiZXhwIjpudWxsLCJwdXIiOiJjb29raWUucmVtZW1iZXJfdG9rZW4ifX0%3D--1319af8bac18ac3543bb2e86ddb00b5f18c824aa", :path=>"/", :domain=>"127.0.0.1", :expires=>Wed, 02 May 2040 07:54:46 +0000, :secure=>false}, 
{:name=>"user_id", :value=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6Ik1RPT0iLCJleHAiOm51bGwsInB1ciI6ImNvb2tpZS51c2VyX2lkIn19--46330cb4f9e1d5389813713021d4fae09d8c34a0", :path=>"/", :domain=>"127.0.0.1", :expires=>Wed, 02 May 2040 07:54:46 +0000, :secure=>false}]

3つめでは1つ目と同じになっているので、アプリ内でcookieでsessionが作られたことがわかる

# 3つめ
Cookies: [{:name=>"remember_token", :value=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IklrRk9UV1JRWVUxNGNYUmxORWhoWlhwU09TMXpkRUVpIiwiZXhwIjpudWxsLCJwdXIiOiJjb29raWUucmVtZW1iZXJfdG9rZW4ifX0%3D--1036446ced36c5f0657faba265768b9998e49413", :path=>"/", :domain=>"127.0.0.1", :expires=>Wed, 02 May 2040 08:05:22 +0000, :secure=>false}, 
{:name=>"_toeic_session", :value=>"%2FOxrxfpit%2Fp6OvExus2B64QJWPsc49NgNya25Z4lDdWf6xRro71OCc%2BYKD0dlKh54SJ21LVuZam3%2FYTc5fcQqJqFwYp4JdI%2BZCgUyBosrH7%2F6yVkyAMjfeDdRfwzPqfd1ZM1I%2FzPs2oOGknAOkcAIrFEjZ%2F8ra6HSfSRGxANqY8kLugtVnNpMZPXHF3pgNEbEIjnKM%2BP--kRheUzjIW4BUThz2--5DFuEvMkZo9hG3%2BaOFF6vw%3D%3D", :path=>"/", :domain=>"127.0.0.1", :expires=>nil, :secure=>false}, 
{:name=>"user_id", :value=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6Ik1RPT0iLCJleHAiOm51bGwsInB1ciI6ImNvb2tpZS51c2VyX2lkIn19--46330cb4f9e1d5389813713021d4fae09d8c34a0", :path=>"/", :domain=>"127.0.0.1", :expires=>Wed, 02 May 2040 08:05:22 +0000, :secure=>false}]

実際はshow_me_the_cookiesの記述はいらない


参照:
https://github.com/nruth/show_me_the_cookies

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2