-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_test.go
55 lines (52 loc) · 1.69 KB
/
callback_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHandleCallback(t *testing.T) {
config.cookieName = "cookie"
config.cookiePath = "/"
config.cookieDomain = "example.com"
role := "test_role"
_,
_, _,
validToken1, validToken2,
invalidTokenNoSignature,
invalidTokenBadSignature,
invalidTokenBadString,
invalidTokenBadTimestamp := setupTestToken()
for _, data := range []struct {
redirect string
token string
valid bool
}{
{redirect: "/test_redirect_1", token: validToken1, valid: true},
{redirect: "/test_redirect_2", token: validToken2, valid: true},
{redirect: "/test_redirect_3", token: invalidTokenNoSignature, valid: false},
{redirect: "/test_redirect_4", token: invalidTokenBadSignature, valid: false},
{redirect: "/test_redirect_5", token: invalidTokenBadString, valid: false},
{redirect: "/test_redirect_6", token: invalidTokenBadTimestamp, valid: false},
} {
state.authCache = map[string]time.Time{}
ctx := context{role: role, redirect: data.redirect, query: data.token}
rr := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
handleCallback(rr, r, &ctx)
if data.valid {
assert.Equal(t, http.StatusFound, rr.Code)
cookies := rr.Result().Cookies()
assert.Len(t, cookies, 1)
assert.Equal(t, config.cookieName, cookies[0].Name)
assert.Equal(t, data.token, cookies[0].Value)
assert.Equal(t, config.cookiePath, cookies[0].Path)
assert.Equal(t, config.cookieDomain, cookies[0].Domain)
location, _ := rr.Result().Location()
assert.Equal(t, data.redirect, location.String())
} else {
assert.Equal(t, http.StatusBadRequest, rr.Code)
}
}
}