forked from InVisionApp/saml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
96 lines (83 loc) · 2.98 KB
/
schema_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package saml
import (
"encoding/xml"
"testing"
"time"
"github.com/beevik/etree"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func TestAttributeXMLRoundTrip(t *testing.T) {
expected := Attribute{
FriendlyName: "TestFriendlyName",
Name: "TestName",
NameFormat: "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
Values: []AttributeValue{{
Type: "xs:string",
Value: "test",
}},
}
doc := etree.NewDocument()
doc.SetRoot(expected.Element())
x, err := doc.WriteToBytes()
assert.Check(t, err)
assert.Check(t, is.Equal("<saml:Attribute FriendlyName=\"TestFriendlyName\" Name=\"TestName\" NameFormat=\"urn:oasis:names:tc:SAML:2.0:attrname-format:basic\"><saml:AttributeValue xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xsi:type=\"xs:string\">test</saml:AttributeValue></saml:Attribute>",
string(x)))
var actual Attribute
err = xml.Unmarshal(x, &actual)
assert.Check(t, err)
assert.Check(t, is.DeepEqual(expected, actual))
}
func TestNameIDFormat(t *testing.T) {
var emptyString string
el := NameIDPolicy{
Format: &emptyString,
}
doc := etree.NewDocument()
doc.SetRoot(el.Element())
x, err := doc.WriteToBytes()
assert.Check(t, err)
assert.Check(t, is.Equal("<samlp:NameIDPolicy/>",
string(x)))
}
func TestAuthnStatementXMLRoundTrip(t *testing.T) {
authnInstant := time.Date(2020, 7, 21, 12, 30, 45, 0, time.UTC)
sessionNotOnOrAfter := time.Date(2020, 7, 22, 15, 0, 0, 0, time.UTC)
expected := AuthnStatement{
AuthnInstant: authnInstant,
SessionIndex: "index",
SessionNotOnOrAfter: &sessionNotOnOrAfter,
}
doc := etree.NewDocument()
doc.SetRoot(expected.Element())
x, err := doc.WriteToBytes()
assert.Check(t, err)
assert.Check(t, is.Equal(`<saml:AuthnStatement AuthnInstant="2020-07-21T12:30:45Z" SessionIndex="index" SessionNotOnOrAfter="2020-07-22T15:00:00Z"><saml:AuthnContext/></saml:AuthnStatement>`,
string(x)))
var actual AuthnStatement
err = xml.Unmarshal(x, &actual)
assert.Check(t, err)
assert.Check(t, is.DeepEqual(expected, actual))
x, err = xml.Marshal(expected)
assert.Check(t, err)
assert.Check(t, is.Equal(`<AuthnStatement AuthnInstant="2020-07-21T12:30:45Z" SessionIndex="index" SessionNotOnOrAfter="2020-07-22T15:00:00Z"><AuthnContext></AuthnContext></AuthnStatement>`,
string(x)))
}
func TestAuthnStatementMarshalWithoutSessionNotOnOrAfter(t *testing.T) {
authnInstant := time.Date(2020, 7, 21, 12, 30, 45, 0, time.UTC)
expected := AuthnStatement{
AuthnInstant: authnInstant,
SessionIndex: "index",
SessionNotOnOrAfter: nil,
}
doc := etree.NewDocument()
doc.SetRoot(expected.Element())
x, err := doc.WriteToBytes()
assert.Check(t, err)
assert.Check(t, is.Equal(`<saml:AuthnStatement AuthnInstant="2020-07-21T12:30:45Z" SessionIndex="index"><saml:AuthnContext/></saml:AuthnStatement>`,
string(x)))
var actual AuthnStatement
err = xml.Unmarshal(x, &actual)
assert.Check(t, err)
assert.Check(t, is.DeepEqual(expected, actual))
}