-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
66 lines (53 loc) · 1.94 KB
/
main_test.go
File metadata and controls
66 lines (53 loc) · 1.94 KB
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
package main
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/golang-jwt/jwt"
)
// TODO Add tests to cover the http requests
// TODO Add more tests to cover the error cases
func TestDecodeSecretToPem(t *testing.T) {
samplePemEncodedText, _ := ioutil.ReadFile("testdata/sample_key_pem_encoded.txt")
samplePemText, _ := ioutil.ReadFile("testdata/sample_key.pem")
pem, err := DecodeSecretToPem(string(samplePemEncodedText))
if err != nil {
t.Errorf("err actual %q, expected %q", err, "nil")
}
s1 := strings.Replace(pem, "\n", "", -1)
s2 := strings.Replace(string(samplePemText), "\n", "", -1)
if s1 != s2 {
t.Error("Difference pem text retrieved")
fmt.Println("Actual:")
fmt.Println(s1)
fmt.Println("Expected:")
fmt.Println(s2)
}
}
func TestDecodeSecretToPemErr(t *testing.T) {
actualValue, err := DecodeSecretToPem("dummy-secret-id")
if actualValue != "" || !strings.Contains(err.Error(), "illegal base64 data at input byte 5") {
t.Errorf("err actual %q, expected %q", err, "illegal base64 data at input byte 5")
}
}
func TestCreateJwtToken(t *testing.T) {
samplePemText, _ := ioutil.ReadFile("testdata/sample_key.pem")
privateKey, _ := jwt.ParseRSAPrivateKeyFromPEM(samplePemText)
appId := "dummy-issuer"
tokenString, _ := CreateJwtToken(&appId, privateKey)
fmt.Println(tokenString)
token2, _ := jwt.ParseWithClaims(tokenString, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return privateKey, nil
})
claims, ok := token2.Claims.(*jwt.StandardClaims)
if ok != true {
t.Error("not ok")
}
if claims.Issuer != appId {
t.Errorf("claims.Issuer actual %q, expected %q", claims.Issuer, appId)
}
if claims.ExpiresAt-claims.IssuedAt != 600 {
t.Errorf("claims.ExpiresAt-claims.IssuedAt actual %q, expected %q", claims.ExpiresAt-claims.IssuedAt, "600")
}
}