@@ -1,4 +1,4 @@
1−package providers
1+package sessions_test
22
33import (
44 "fmt"
@@ -6,6 +6,7 @@ import (
66 "time"
77
88 "github.com/pusher/oauth2_proxy/cookie"
9+ "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
910 "github.com/stretchr/testify/assert"
1011)
1112
@@ -17,7 +18,7 @@ func TestSessionStateSerialization(t *testing.T) {
1718 assert.Equal(t, nil, err)
1819 c2, err := cookie.NewCipher([]byte(altSecret))
1920 assert.Equal(t, nil, err)
20− s := &SessionState{
21+ s := &sessions.SessionState{
2122 Email: "user@domain.com",
2223 AccessToken: "token1234",
2324 IDToken: "rawtoken1234",
@@ -27,7 +28,7 @@ func TestSessionStateSerialization(t *testing.T) {
2728 encoded, err := s.EncodeSessionState(c)
2829 assert.Equal(t, nil, err)
2930
30− ss, err := DecodeSessionState(encoded, c)
31+ ss, err := sessions.DecodeSessionState(encoded, c)
3132 t.Logf("%#v", ss)
3233 assert.Equal(t, nil, err)
3334 assert.Equal(t, "user@domain.com", ss.User)
@@ -38,7 +39,7 @@ func TestSessionStateSerialization(t *testing.T) {
3839 assert.Equal(t, s.RefreshToken, ss.RefreshToken)
3940
4041 // ensure a different cipher can't decode properly (ie: it gets gibberish)
41− ss, err = DecodeSessionState(encoded, c2)
42+ ss, err = sessions.DecodeSessionState(encoded, c2)
4243 t.Logf("%#v", ss)
4344 assert.Equal(t, nil, err)
4445 assert.NotEqual(t, "user@domain.com", ss.User)
@@ -54,7 +55,7 @@ func TestSessionStateSerializationWithUser(t *testing.T) {
5455 assert.Equal(t, nil, err)
5556 c2, err := cookie.NewCipher([]byte(altSecret))
5657 assert.Equal(t, nil, err)
57− s := &SessionState{
58+ s := &sessions.SessionState{
5859 User: "just-user",
5960 Email: "user@domain.com",
6061 AccessToken: "token1234",
@@ -64,7 +65,7 @@ func TestSessionStateSerializationWithUser(t *testing.T) {
6465 encoded, err := s.EncodeSessionState(c)
6566 assert.Equal(t, nil, err)
6667
67− ss, err := DecodeSessionState(encoded, c)
68+ ss, err := sessions.DecodeSessionState(encoded, c)
6869 t.Logf("%#v", ss)
6970 assert.Equal(t, nil, err)
7071 assert.Equal(t, s.User, ss.User)
@@ -74,7 +75,7 @@ func TestSessionStateSerializationWithUser(t *testing.T) {
7475 assert.Equal(t, s.RefreshToken, ss.RefreshToken)
7576
7677 // ensure a different cipher can't decode properly (ie: it gets gibberish)
77− ss, err = DecodeSessionState(encoded, c2)
78+ ss, err = sessions.DecodeSessionState(encoded, c2)
7879 t.Logf("%#v", ss)
7980 assert.Equal(t, nil, err)
8081 assert.NotEqual(t, s.User, ss.User)
@@ -85,7 +86,7 @@ func TestSessionStateSerializationWithUser(t *testing.T) {
8586}
8687
8788func TestSessionStateSerializationNoCipher(t *testing.T) {
88− s := &SessionState{
89+ s := &sessions.SessionState{
8990 Email: "user@domain.com",
9091 AccessToken: "token1234",
9192 ExpiresOn: time.Now().Add(time.Duration(1) * time.Hour),
@@ -95,7 +96,7 @@ func TestSessionStateSerializationNoCipher(t *testing.T) {
9596 assert.Equal(t, nil, err)
9697
9798 // only email should have been serialized
98− ss, err := DecodeSessionState(encoded, nil)
99+ ss, err := sessions.DecodeSessionState(encoded, nil)
99100 assert.Equal(t, nil, err)
100101 assert.Equal(t, "user@domain.com", ss.User)
101102 assert.Equal(t, s.Email, ss.Email)
@@ -104,7 +105,7 @@ func TestSessionStateSerializationNoCipher(t *testing.T) {
104105}
105106
106107func TestSessionStateSerializationNoCipherWithUser(t *testing.T) {
107− s := &SessionState{
108+ s := &sessions.SessionState{
108109 User: "just-user",
109110 Email: "user@domain.com",
110111 AccessToken: "token1234",
@@ -115,7 +116,7 @@ func TestSessionStateSerializationNoCipherWithUser(t *testing.T) {
115116 assert.Equal(t, nil, err)
116117
117118 // only email should have been serialized
118− ss, err := DecodeSessionState(encoded, nil)
119+ ss, err := sessions.DecodeSessionState(encoded, nil)
119120 assert.Equal(t, nil, err)
120121 assert.Equal(t, s.User, ss.User)
121122 assert.Equal(t, s.Email, ss.Email)
@@ -124,18 +125,18 @@ func TestSessionStateSerializationNoCipherWithUser(t *testing.T) {
124125}
125126
126127func TestExpired(t *testing.T) {
127− s := &SessionState{ExpiresOn: time.Now().Add(time.Duration(-1) * time.Minute)}
128+ s := &sessions.SessionState{ExpiresOn: time.Now().Add(time.Duration(-1) * time.Minute)}
128129 assert.Equal(t, true, s.IsExpired())
129130
130− s = &SessionState{ExpiresOn: time.Now().Add(time.Duration(1) * time.Minute)}
131+ s = &sessions.SessionState{ExpiresOn: time.Now().Add(time.Duration(1) * time.Minute)}
131132 assert.Equal(t, false, s.IsExpired())
132133
133− s = &SessionState{}
134+ s = &sessions.SessionState{}
134135 assert.Equal(t, false, s.IsExpired())
135136}
136137
137138type testCase struct {
138− SessionState
139+ sessions.SessionState
139140 Encoded string
140141 Cipher *cookie.Cipher
141142 Error bool
@@ -150,14 +151,14 @@ func TestEncodeSessionState(t *testing.T) {
150151
151152 testCases := []testCase{
152153 {
153− SessionState: SessionState{
154+ SessionState: sessions.SessionState{
154155 Email: "user@domain.com",
155156 User: "just-user",
156157 },
157158 Encoded: `{"Email":"user@domain.com","User":"just-user"}`,
158159 },
159160 {
160− SessionState: SessionState{
161+ SessionState: sessions.SessionState{
161162 Email: "user@domain.com",
162163 User: "just-user",
163164 AccessToken: "token1234",
@@ -171,7 +172,7 @@ func TestEncodeSessionState(t *testing.T) {
171172
172173 for i, tc := range testCases {
173174 encoded, err := tc.EncodeSessionState(tc.Cipher)
174− t.Logf("i:%d Encoded:%#v SessionState:%#v Error:%#v", i, encoded, tc.SessionState, err)
175+ t.Logf("i:%d Encoded:%#vsessions.SessionState:%#v Error:%#v", i, encoded, tc.SessionState, err)
175176 if tc.Error {
176177 assert.Error(t, err)
177178 assert.Empty(t, encoded)
@@ -182,7 +183,7 @@ func TestEncodeSessionState(t *testing.T) {
182183 }
183184}
184185
185−// TestDecodeSessionState tests DecodeSessionState with the test vector
186+// TestDecodeSessionState testssessions.DecodeSessionState with the test vector
186187func TestDecodeSessionState(t *testing.T) {
187188 e := time.Now().Add(time.Duration(1) * time.Hour)
188189 eJSON, _ := e.MarshalJSON()
@@ -194,34 +195,34 @@ func TestDecodeSessionState(t *testing.T) {
194195
195196 testCases := []testCase{
196197 {
197− SessionState: SessionState{
198+ SessionState: sessions.SessionState{
198199 Email: "user@domain.com",
199200 User: "just-user",
200201 },
201202 Encoded: `{"Email":"user@domain.com","User":"just-user"}`,
202203 },
203204 {
204− SessionState: SessionState{
205+ SessionState: sessions.SessionState{
205206 Email: "user@domain.com",
206207 User: "user@domain.com",
207208 },
208209 Encoded: `{"Email":"user@domain.com"}`,
209210 },
210211 {
211− SessionState: SessionState{
212+ SessionState: sessions.SessionState{
212213 User: "just-user",
213214 },
214215 Encoded: `{"User":"just-user"}`,
215216 },
216217 {
217− SessionState: SessionState{
218+ SessionState: sessions.SessionState{
218219 Email: "user@domain.com",
219220 User: "just-user",
220221 },
221222 Encoded: fmt.Sprintf(`{"Email":"user@domain.com","User":"just-user","AccessToken":"I6s+ml+/MldBMgHIiC35BTKTh57skGX24w==","IDToken":"xojNdyyjB1HgYWh6XMtXY/Ph5eCVxa1cNsklJw==","RefreshToken":"qEX0x6RmASxo4dhlBG6YuRs9Syn/e9sHu/+K","ExpiresOn":%s}`, eString),
222223 },
223224 {
224− SessionState: SessionState{
225+ SessionState: sessions.SessionState{
225226 Email: "user@domain.com",
226227 User: "just-user",
227228 AccessToken: "token1234",
@@ -233,7 +234,7 @@ func TestDecodeSessionState(t *testing.T) {
233234 Cipher: c,
234235 },
235236 {
236− SessionState: SessionState{
237+ SessionState: sessions.SessionState{
237238 Email: "user@domain.com",
238239 User: "just-user",
239240 },
@@ -251,7 +252,7 @@ func TestDecodeSessionState(t *testing.T) {
251252 Error: true,
252253 },
253254 {
254− SessionState: SessionState{
255+ SessionState: sessions.SessionState{
255256 User: "just-user",
256257 Email: "user@domain.com",
257258 },
@@ -272,7 +273,7 @@ func TestDecodeSessionState(t *testing.T) {
272273 Error: true,
273274 },
274275 {
275− SessionState: SessionState{
276+ SessionState: sessions.SessionState{
276277 Email: "user@domain.com",
277278 User: "just-user",
278279 AccessToken: "token1234",
@@ -283,7 +284,7 @@ func TestDecodeSessionState(t *testing.T) {
283284 Cipher: c,
284285 },
285286 {
286− SessionState: SessionState{
287+ SessionState: sessions.SessionState{
287288 Email: "user@domain.com",
288289 User: "just-user",
289290 AccessToken: "token1234",
@@ -297,8 +298,8 @@ func TestDecodeSessionState(t *testing.T) {
297298 }
298299
299300 for i, tc := range testCases {
300− ss, err := DecodeSessionState(tc.Encoded, tc.Cipher)
301− t.Logf("i:%d Encoded:%#v SessionState:%#v Error:%#v", i, tc.Encoded, ss, err)
301+ ss, err := sessions.DecodeSessionState(tc.Encoded, tc.Cipher)
302+ t.Logf("i:%d Encoded:%#vsessions.SessionState:%#v Error:%#v", i, tc.Encoded, ss, err)
302303 if tc.Error {
303304 assert.Error(t, err)
304305 assert.Nil(t, ss)