-
Notifications
You must be signed in to change notification settings - Fork 35
/
login_test.go
47 lines (37 loc) · 1.25 KB
/
login_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
package main
import (
"reflect"
"testing"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
func TestNonceCreation(t *testing.T) {
if len(createNonce(8)) != 8 {
t.Error("Expected a nonce of 8 characters.")
}
}
func TestCookieCreation(t *testing.T) {
var testdomain = "testing.com"
var userinfo = []byte("testfoo1234567890")
var expiration = time.Now().Add(time.Hour)
testJwt := createSignedJWT(userinfo, expiration)
testCookie := createCookie(testJwt, expiration, testdomain)
if testCookie.Domain != testdomain {
t.Error("Expected cookie domain to be " + testdomain + ", got " + testCookie.Domain + "instead.")
}
if !testCookie.Expires.Equal(expiration) {
t.Error("Expiration time different from given time: " + testCookie.Expires.String())
}
cookieJWT, err := parseJWT(testCookie.Value)
if err != nil {
t.Error("Problem in JWT validation: " + err.Error())
}
uifClaim, err := base64decode(cookieJWT.Claims.(jwt.MapClaims)["uif"].(string))
if err != nil {
t.Error("Trouble getting uif claim from JWT: " + err.Error())
}
if !reflect.DeepEqual(userinfo, uifClaim) {
t.Error("Userinfo different from uif claim. Userinfo: " + string(userinfo[:]) + ", uif claim: " + string(uifClaim[:]))
}
}
// TODO mock OIDC endpoint and test different scenarios.