11require File . expand_path ( "test/test_helper" )
22
33def condition ( before , after )
4- Samlr ::Condition . new (
5- "NotBefore" => before ? before . utc . iso8601 : nil ,
6- "NotOnOrAfter" => after ? after . utc . iso8601 : nil
7- )
4+ element = Nokogiri ::XML ::Element . new ( 'saml:Condition' , Nokogiri ::XML ( '' ) )
5+ element [ "NotBefore" ] = before . utc . iso8601 if before
6+ element [ "NotOnOrAfter" ] = after . utc . iso8601 if after
7+
8+ Samlr ::Condition . new ( element , { } )
89end
910
1011describe Samlr ::Condition do
@@ -14,6 +15,44 @@ def condition(before, after)
1415 end
1516
1617 describe "verify!" do
18+ describe "audience verification" do
19+ let ( :response ) { fixed_saml_response }
20+ subject { response . assertion . conditions }
21+
22+ describe "when it is wrong" do
23+ before do
24+ response . options [ :audience ] = 'example.com'
25+ end
26+
27+ it "raises an exception" do
28+ Time . stub ( :now , Time . at ( 1344379365 ) ) do
29+ assert subject . not_on_or_after_satisfied?
30+ assert subject . not_before_satisfied?
31+ refute subject . audience_satisfied?
32+
33+ begin
34+ subject . verify!
35+ flunk "Expected exception"
36+ rescue Samlr ::ConditionsError => e
37+ assert_match /Audience/ , e . message
38+ end
39+ end
40+ end
41+ end
42+
43+ describe "when it is right" do
44+ before do
45+ response . options [ :audience ] = 'example.org'
46+ end
47+
48+ it "does not raise an exception" do
49+ Time . stub ( :now , Time . at ( 1344379365 ) ) do
50+ assert subject . verify!
51+ end
52+ end
53+ end
54+ end
55+
1756 describe "when the lower time has not been met" do
1857 before { @not_before = ( Time . now + 5 *60 ) }
1958 subject { condition ( @not_before , @not_after ) }
@@ -57,15 +96,30 @@ def condition(before, after)
5796 end
5897 end
5998
99+ describe "#audience_satisfied?" do
100+ it "returns true when audience is a nil value" do
101+ element = Nokogiri ::XML ::Node . new ( 'saml:Condition' , Nokogiri ::XML ( '' ) )
102+ assert Samlr ::Condition . new ( element , { } ) . audience_satisfied?
103+ end
104+
105+ it "returns true when passed a nil audience" do
106+ condition = fixed_saml_response . assertion . conditions
107+ assert_equal 'example.org' , condition . audience
108+ assert condition . audience_satisfied?
109+ end
110+ end
111+
60112 describe "#not_before_satisfied?" do
61113 it "returns true when passed a nil value" do
62- assert Samlr ::Condition . new ( { } ) . not_before_satisfied?
114+ element = Nokogiri ::XML ::Node . new ( 'saml:Condition' , Nokogiri ::XML ( '' ) )
115+ assert Samlr ::Condition . new ( element , { } ) . not_before_satisfied?
63116 end
64117 end
65118
66119 describe "#not_on_or_after_satisfied?" do
67120 it "returns true when passed a nil value" do
68- assert Samlr ::Condition . new ( { } ) . not_on_or_after_satisfied?
121+ element = Nokogiri ::XML ::Node . new ( 'saml:Condition' , Nokogiri ::XML ( '' ) )
122+ assert Samlr ::Condition . new ( element , { } ) . not_on_or_after_satisfied?
69123 end
70124 end
71125end
0 commit comments