forked from louketo/louketo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
123 lines (108 loc) · 3.63 KB
/
misc_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright 2015 All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRedirectToAuthorizationUnauthorized(t *testing.T) {
requests := []fakeRequest{
{URI: "/admin", ExpectedCode: http.StatusUnauthorized},
}
newFakeProxy(nil).RunTests(t, requests)
}
func TestRedirectToAuthorization(t *testing.T) {
requests := []fakeRequest{
{
URI: "/admin",
Redirects: true,
ExpectedLocation: "/oauth/authorize?state",
ExpectedCode: http.StatusSeeOther,
},
}
newFakeProxy(nil).RunTests(t, requests)
}
func TestRedirectToAuthorizationWith303Enabled(t *testing.T) {
cfg := newFakeKeycloakConfig()
requests := []fakeRequest{
{
URI: "/admin",
Redirects: true,
ExpectedLocation: "/oauth/authorize?state",
ExpectedCode: http.StatusSeeOther,
},
}
newFakeProxy(cfg).RunTests(t, requests)
}
func TestRedirectToAuthorizationSkipToken(t *testing.T) {
requests := []fakeRequest{
{URI: "/admin", ExpectedCode: http.StatusUnauthorized},
}
c := newFakeKeycloakConfig()
c.SkipTokenVerification = true
newFakeProxy(c).RunTests(t, requests)
}
func assertAlmostEquals(t *testing.T, expected time.Duration, actual time.Duration) {
delta := expected - actual
if delta < 0 {
delta = -delta
}
assert.True(t, delta < time.Duration(1)*time.Minute, "Diff should be less than a minute but delta is %s", delta)
}
func TestGetAccessCookieExpiration_NoExp(t *testing.T) {
token := newTestToken("foo").getToken()
refreshToken := token.Encode()
c := newFakeKeycloakConfig()
c.AccessTokenDuration = time.Duration(1) * time.Hour
proxy := newFakeProxy(c).proxy
duration := proxy.getAccessCookieExpiration(refreshToken)
assertAlmostEquals(t, c.AccessTokenDuration, duration)
}
func TestGetAccessCookieExpiration_ZeroExp(t *testing.T) {
ft := newTestToken("foo")
ft.setExpiration(time.Unix(0, 0))
token := ft.getToken()
refreshToken := token.Encode()
c := newFakeKeycloakConfig()
c.AccessTokenDuration = time.Duration(1) * time.Hour
proxy := newFakeProxy(c).proxy
duration := proxy.getAccessCookieExpiration(refreshToken)
assert.True(t, duration > 0, "duration should be positive")
assertAlmostEquals(t, c.AccessTokenDuration, duration)
}
func TestGetAccessCookieExpiration_PastExp(t *testing.T) {
ft := newTestToken("foo")
ft.setExpiration(time.Now().AddDate(-1, 0, 0))
token := ft.getToken()
refreshToken := token.Encode()
c := newFakeKeycloakConfig()
c.AccessTokenDuration = time.Duration(1) * time.Hour
proxy := newFakeProxy(c).proxy
duration := proxy.getAccessCookieExpiration(refreshToken)
assertAlmostEquals(t, c.AccessTokenDuration, duration)
}
func TestGetAccessCookieExpiration_ValidExp(t *testing.T) {
ft := newTestToken("foo")
token := ft.getToken()
refreshToken := token.Encode()
c := newFakeKeycloakConfig()
c.AccessTokenDuration = time.Duration(1) * time.Hour
proxy := newFakeProxy(c).proxy
duration := proxy.getAccessCookieExpiration(refreshToken)
val, ok, _ := ft.claims.TimeClaim("exp")
assert.True(t, ok)
expectedDuration := time.Until(val)
assertAlmostEquals(t, expectedDuration, duration)
}