-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
58 lines (55 loc) · 1.88 KB
/
auth_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHandleAuth(t *testing.T) {
config.authHeader = "X-Test-Header"
role := "test_role"
_,
validUser, invalidUser,
validToken1, validToken2,
invalidTokenNoSignature,
invalidTokenBadSignature,
invalidTokenBadString,
invalidTokenBadTimestamp := setupTestToken()
for _, data := range []struct {
binding string
token string
valid bool
}{
{binding: validUser, token: validToken1, valid: true},
{binding: validUser, token: validToken2, valid: true},
{binding: validUser, token: invalidTokenNoSignature, valid: false},
{binding: validUser, token: invalidTokenBadSignature, valid: false},
{binding: validUser, token: invalidTokenBadString, valid: false},
{binding: validUser, token: invalidTokenBadTimestamp, valid: false},
{binding: invalidUser, token: validToken1, valid: false},
{binding: invalidUser, token: validToken2, valid: false},
{binding: invalidUser, token: invalidTokenNoSignature, valid: false},
{binding: invalidUser, token: invalidTokenBadSignature, valid: false},
{binding: invalidUser, token: invalidTokenBadString, valid: false},
{binding: invalidUser, token: invalidTokenBadTimestamp, valid: false},
} {
config.roleBindings = map[string]map[string]bool{role: {data.binding: true}}
state.authCache = map[string]time.Time{}
ctx := context{role: role, cookie: data.token}
rr := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
handleAuth(rr, r, &ctx)
if data.valid {
assert.Equal(t, http.StatusOK, rr.Code)
header, ok := rr.HeaderMap[config.authHeader]
assert.True(t, ok)
assert.Equal(t, []string{validUser}, header)
} else {
assert.Equal(t, http.StatusUnauthorized, rr.Code)
header, ok := rr.HeaderMap[config.authHeader]
assert.False(t, ok)
assert.Nil(t, header)
}
}
}