forked from koding/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.go
82 lines (69 loc) · 2.11 KB
/
testutil.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
// Package testutil provides a default Kontrol kites for using in tests.
package testutil
import (
"os"
"testing"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/koding/kite/config"
"github.com/koding/kite/testkeys"
"github.com/koding/logging"
"github.com/nu7hatch/gouuid"
)
// NewKiteKey returns a new generated kite key. (Copied and modified from
// kontrol.go) If the host does not have a kite.key file kite.New() panics.
// This is a helper to put a fake key on it's location.
func NewKiteKey() *jwt.Token {
tknID, err := uuid.NewV4()
if err != nil {
panic(err)
}
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
username := "testuser"
if testuser := os.Getenv("TESTKEY_USERNAME"); testuser != "" {
username = testuser
}
token := jwt.New(jwt.GetSigningMethod("RS256"))
token.Claims = map[string]interface{}{
"iss": "testuser", // Issuer
"sub": username, // Issued to
"aud": hostname, // Hostname of registered machine
"iat": time.Now().UTC().Unix(), // Issued At
"jti": tknID.String(), // JWT ID
"kontrolURL": "http://localhost:4000/kite", // Kontrol URL
"kontrolKey": testkeys.Public, // Public key of kontrol
}
token.Raw, err = token.SignedString([]byte(testkeys.Private))
if err != nil {
panic(err)
}
token.Valid = true
return token
}
func NewConfig() *config.Config {
conf := config.New()
conf.Username = "testuser"
conf.KontrolURL = "http://localhost:4000/kite"
conf.KontrolKey = testkeys.Public
conf.KontrolUser = "testuser"
conf.KiteKey = NewKiteKey().Raw
return conf
}
func init() {
// Monkey-patch default logging handler that is used by Kite.Logger
// in order to hide logs until "-v" flag is given to "test.sh" script.
original := logging.DefaultHandler
logging.DefaultHandler = &quietHandler{original}
}
// quietHandler does not output any log messages until "-v" flag is given in tests.
type quietHandler struct {
logging.Handler
}
func (h *quietHandler) Handle(rec *logging.Record) {
if testing.Verbose() {
h.Handler.Handle(rec)
}
}