{"id":22808331,"url":"https://github.com/sixarm/sixarm_ruby_capybara_session_cookies","last_synced_at":"2025-03-30T21:15:47.628Z","repository":{"id":2209725,"uuid":"3158872","full_name":"SixArm/sixarm_ruby_capybara_session_cookies","owner":"SixArm","description":"SixArm.com » Ruby » Capybara session cookies for Rails testing","archived":false,"fork":false,"pushed_at":"2023-09-15T19:27:48.000Z","size":340,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-06T00:25:43.197Z","etag":null,"topics":["capybara","cookie","ruby","session"],"latest_commit_sha":null,"homepage":"http://sixarm.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SixArm.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null}},"created_at":"2012-01-12T01:11:05.000Z","updated_at":"2023-09-15T19:27:52.000Z","dependencies_parsed_at":"2022-09-08T07:41:31.781Z","dependency_job_id":null,"html_url":"https://github.com/SixArm/sixarm_ruby_capybara_session_cookies","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_capybara_session_cookies","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_capybara_session_cookies/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_capybara_session_cookies/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_capybara_session_cookies/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SixArm","download_url":"https://codeload.github.com/SixArm/sixarm_ruby_capybara_session_cookies/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246379366,"owners_count":20767696,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["capybara","cookie","ruby","session"],"created_at":"2024-12-12T11:09:16.325Z","updated_at":"2025-03-30T21:15:47.609Z","avatar_url":"https://github.com/SixArm.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SixArm.com → Ruby → \u003cbr\u003e Capybara session cookies for Rails testing\n\n* Doc: \u003chttp://sixarm.com/sixarm_ruby_capybara_session_cookies/doc\u003e\n* Gem: \u003chttp://rubygems.org/gems/sixarm_ruby_capybara_session_cookies\u003e\n* Repo: \u003chttp://github.com/sixarm/sixarm_ruby_capybara_session_cookies\u003e\n\u003c!--header-shut--\u003e\n\n\n## Introduction\n\nThis gem wraps the code and writeup from Steve Richert at Collective Idea:\nhttp://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles/\n\nAll credit for this goes to Steve.\n\nFor docs go to \u003chttp://sixarm.com/sixarm_ruby_capybara_session_cookies/doc\u003e\n\nWant to help? We're happy to get pull requests.\n\n\n\u003c!--install-open--\u003e\n\n## Install\n\n### Gem\n\nTo install this gem in your shell or terminal:\n\n    gem install sixarm_ruby_capybara_session_cookies\n\n### Gemfile\n\nTo add this gem to your Gemfile:\n\n    gem 'sixarm_ruby_capybara_session_cookies'\n\n### Require\n\nTo require the gem in your code:\n\n    require 'sixarm_ruby_capybara_session_cookies'\n\n\u003c!--install-shut--\u003e\n\n\n## Details\n\n\nFrom Steve's writeup...\n\nWhen I write a new Rails application, it often needs some sort of user authentication.\n\nI like to test authentication in Cucumber.\n\nI want to set the set the signed-in user directly.\n\nHere’s the current_user helper method in my ApplicationController:\n\n    def current_user\n      return @current_user if defined?(@current_user)\n      @current_user = cookies[:token] \u0026\u0026 User.find_by_token(cookies[:token])\n    end\n\nEach Capybara driver handles its cookies differently. The cookies hash we access in our step is specific to Rack::Test and is actually a Rack::Test::CookieJar object.\n\nIf you want your application cookies to Just Work™ from anywhere in your Cucumber suite, throw the following into features/support/cookies.rb:\n\n    module Capybara\n      class Session\n        def cookies\n          @cookies ||= begin\n            secret = Rails.application.config.secret_token\n            cookies = ActionDispatch::Cookies::CookieJar.new(secret)\n            cookies.stub(:close!)\n            cookies\n          end\n        end\n      end\n    end\n\n    Before do\n      request = ActionDispatch::Request.any_instance\n      request.stub(:cookie_jar).and_return{ page.cookies }\n      request.stub(:cookies).and_return{ page.cookies }\n    end\n\nYou’ll need a stubbing library. I’m using RSpec.\n\nThis allows each of your Capybara sessions to keep its own separate set of cookies. And they’re real cookies, meaning that you can use cookies.permananent and cookies.signed just like you do in your controllers. Then, after each scenario, Capybara will clean its sessions, along with your cookies.\n\nJust use page.cookies and you’re good to go!\n\n    When /^I am signed in as \"([^\"]*)\"$/ do |email|\n      page.cookies[:token] = User.find_by_email!(email).token\n    end\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsixarm%2Fsixarm_ruby_capybara_session_cookies","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsixarm%2Fsixarm_ruby_capybara_session_cookies","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsixarm%2Fsixarm_ruby_capybara_session_cookies/lists"}