Last active
December 21, 2015 02:48
-
-
Save seanbehan/6237307 to your computer and use it in GitHub Desktop.
Testing Sinatra current_user sessions
This file contains 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
require 'sinatra' | |
configure do | |
enable :sessions | |
end | |
get '/' do | |
if session[:user_id] | |
"OK" | |
else | |
raise "NOT OK!" | |
end | |
end | |
This file contains 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
require 'test/unit' | |
require 'rack/test' | |
require 'app' | |
class CurrentUserSession < Test::Unit::TestCase | |
include Rack::Test::Methods | |
def app | |
Sinatra::Application | |
end | |
def current_user_session | |
get '/', {}, { 'rack.session' => { user_id: current_user.id } } | |
end | |
def current_user | |
User.find_or_create_by(full_name: "Sean Behan") | |
end | |
def setup | |
current_user_session | |
end | |
end | |
class UserTest < CurrentUserSession | |
def setup | |
# Remember to call super... | |
super | |
end | |
def test_index | |
get "/" | |
assert_equal current_user.id, last_request.env['rack.session']['user_id'] | |
assert last_response.ok? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment