require File.expand_path(File.join(File.dirname(__FILE__), "test_helper")) class RequestTest < Test::Unit::TestCase context "Logoutrequest" do settings = Ciam::Saml::Settings.new should "create the deflated SAMLRequest URL parameter" do settings.idp_slo_target_url = "http://unauth.com/logout" unauth_url = Ciam::Saml::Logoutrequest.new.create(settings) assert unauth_url =~ /^http:\/\/unauth\.com\/logout\?SAMLRequest=/ inflated = decode_saml_request_payload(unauth_url) assert_match /^ nil }) assert unauth_url =~ /&hello=$/ unauth_url = Ciam::Saml::Logoutrequest.new.create(settings, { :foo => "bar" }) assert unauth_url =~ /&foo=bar$/ end should "set sessionindex" do settings.idp_slo_target_url = "http://example.com" sessionidx = UUID.new.generate settings.sessionindex = sessionidx unauth_url = Ciam::Saml::Logoutrequest.new.create(settings, { :name_id => "there" }) inflated = decode_saml_request_payload(unauth_url) assert_match /), inflated end should "set name_identifier_value" do settings = Ciam::Saml::Settings.new settings.idp_slo_target_url = "http://example.com" settings.name_identifier_format = "transient" name_identifier_value = "abc123" settings.name_identifier_value = name_identifier_value unauth_url = Ciam::Saml::Logoutrequest.new.create(settings, { :name_id => "there" }) inflated = decode_saml_request_payload(unauth_url) assert_match /), inflated end context "when the target url doesn't contain a query string" do should "create the SAMLRequest parameter correctly" do settings = Ciam::Saml::Settings.new settings.idp_slo_target_url = "http://example.com" unauth_url = Ciam::Saml::Logoutrequest.new.create(settings) assert unauth_url =~ /^http:\/\/example.com\?SAMLRequest/ end end context "when the target url contains a query string" do should "create the SAMLRequest parameter correctly" do settings = Ciam::Saml::Settings.new settings.idp_slo_target_url = "http://example.com?field=value" unauth_url = Ciam::Saml::Logoutrequest.new.create(settings) assert unauth_url =~ /^http:\/\/example.com\?field=value&SAMLRequest/ end end context "consumation of logout may need to track the transaction" do should "have access to the request uuid" do settings = Ciam::Saml::Settings.new settings.idp_slo_target_url = "http://example.com?field=value" unauth_req = Ciam::Saml::Logoutrequest.new unauth_url = unauth_req.create(settings) inflated = decode_saml_request_payload(unauth_url) assert_match %r[ID='#{unauth_req.uuid}'], inflated end end end def decode_saml_request_payload(unauth_url) payload = CGI.unescape(unauth_url.split("SAMLRequest=").last) decoded = Base64.decode64(payload) zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS) inflated = zstream.inflate(decoded) zstream.finish zstream.close inflated end end