Last active
March 31, 2020 06:15
-
-
Save bogdanRada/8839710 to your computer and use it in GitHub Desktop.
Set session for integration tests Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RackSessionTestMiddleware | |
class << self | |
def session_data | |
@@session_data||={} | |
end | |
def session_data=(value) | |
@@session_data = value | |
end | |
end | |
def initialize(app, session_keys) | |
@app = app | |
@session_keys = session_keys | |
end | |
def call(env) | |
rack_session = ::Rack::Request.new(env).session | |
if self.class.session_data.present? && self.class.session_data.is_a?(Hash) && self.class.session_data[:bypass].blank? | |
self.class.session_data.each { |k, v| rack_session[k] = v } | |
elsif self.class.session_data.present? && self.class.session_data.is_a?(Hash) && self.class.session_data[:bypass].present? | |
# do nothing with the session ( this in for cases when you really need to do the whole login process) | |
else | |
@session_keys.each{|key| rack_session.delete(key.to_s) } #avoiding clearing the flash | |
end | |
@app.call(env) | |
end | |
end | |
Rails.configuration.middleware.use RackSessionTestMiddleware, ["user"] if Rails.env.test? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment