spec/session_spec.rb in nyara-0.0.1.pre.5 vs spec/session_spec.rb in nyara-0.0.1.pre.6

- old
+ new

@@ -1,49 +1,111 @@ require_relative "spec_helper" module Nyara describe Session do + context ".encode_set_cookie options" do + before :all do + @session = {'hello' => 'world'} + end + + it "is HttpOnly" do + Session.init + line = Session.encode_set_cookie @session, false + assert_includes line, '; HttpOnly' + end + + it "adds Secure" do + init_configure_with :session, :secure, true + line = Session.encode_set_cookie @session, false + assert_includes line, '; Secure' + + init_configure_with :session, :secure, false + line = Session.encode_set_cookie @session, true + assert_not_includes line, 'Secure' + + init_configure_with :session, :secure, nil + line = Session.encode_set_cookie @session, true + assert_includes line, '; Secure' + line = Session.encode_set_cookie @session, false + assert_not_includes line, 'Secure' + end + + it "adds Expires" do + init_configure_with :session, :expire, nil + line = Session.encode_set_cookie @session, false + assert_not_includes line, 'Expires' + + init_configure_with :session, :expires, 30 * 60 + line = Session.encode_set_cookie @session, false + assert_includes line, '; Expires=' + end + + it "raises for unkown keys" do + assert_raise RuntimeError do + init_configure_with :session, :ciphre_key, 'adsf' + end + end + + def init_configure_with *options + Config.configure do + reset + set *options + end + Session.init + end + end + context "no cipher" do before :all do - Config['session', 'cipher_key'] = nil + Config.configure do + reset + set 'session', 'cipher_key', nil + end Session.init end it "should encode and decode" do cookie = {} session = {'hello' => 'world'} - Session.encode session, cookie + Session.encode_to_cookie session, cookie - assert_includes cookie[Session.name], 'world' + session_data = cookie[Session.name].split('/')[1] + assert_includes Base64.urlsafe_decode64(session_data), 'world' session2 = Session.decode cookie assert_equal 'world', session2[:hello] end it "drops bad signature" do cookie = {} session = {'hello' => 'world'} - Session.encode session, cookie + Session.encode_to_cookie session, cookie cookie[Session.name].sub!(/\w/, &:swapcase) session = Session.decode cookie assert_empty session end end context "with cipher" do before :all do - Config['session', 'cipher_key'] = "some cipher key" + Config.configure do + reset + set 'session', 'cipher_key', "some cipher key" + end Session.init end it "encode and decode" do cookie = {} session = {'hello' => 'world'} - Session.encode session, cookie + Session.encode_to_cookie session, cookie - assert_not_includes cookie[Session.name], 'world' + session_data = cookie[Session.name].split('/')[1] + if session_data + assert_not_includes Base64.urlsafe_decode64(session_data), 'world' + end session2 = Session.decode cookie assert_equal 'world', session2[:hello] end