generated from d3ta-go/project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.go
89 lines (76 loc) · 2.16 KB
/
helper.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
83
84
85
86
87
88
89
package helper_test
import (
"fmt"
"testing"
"time"
"github.com/d3ta-go/system/system/config"
"github.com/d3ta-go/system/system/handler"
"github.com/d3ta-go/system/system/identity"
"github.com/dgrijalva/jwt-go"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// NewConfig new config - helper test
func NewConfig(h *handler.Handler) (*config.Config, *viper.Viper, error) {
configPath := "../../../../../../conf"
//init config
c, v, err := config.NewConfig(configPath)
if err != nil {
panic(err)
}
if !c.CanRunTest() {
panic(fmt.Sprintf("Cannot Run Test on env `%s`, allowed: %v", c.Environment.Stage, c.Environment.RunTestEnvironment))
}
c.IAM.Casbin.ModelPath = "../../../../../../conf/casbin/casbin_rbac_rest_model.conf"
h.SetDefaultConfig(c)
h.SetViper("config", v)
// viper for test-data
viperTest := viper.New()
viperTest.SetConfigType("yaml")
viperTest.SetConfigName("test-data")
viperTest.AddConfigPath("../../../../../../conf/data")
viperTest.ReadInConfig()
h.SetViper("test-data", viperTest)
viper.OnConfigChange(func(e fsnotify.Event) {
c := new(config.Config)
if err := viper.Unmarshal(&c); err != nil {
fmt.Println(err)
}
h.SetDefaultConfig(c)
})
return c, v, nil
}
// NewHandler new handler - helper test
func NewHandler() *handler.Handler {
h, _ := handler.NewHandler()
// init configuration
_, _, err := NewConfig(h)
if err != nil {
panic(err)
}
return h
}
// GenerateUserTestToken generate user test token - helper test
func GenerateUserTestToken(h *handler.Handler, t *testing.T) (string, *identity.JWTCustomClaims, error) {
j, err := identity.NewJWT(h)
if err != nil {
return "", nil, err
}
claims := identity.JWTCustomClaims{
ID: 0,
UUID: "test-test-test-test-test",
Username: "test.d3tago",
NickName: "Test User",
AuthorityID: "group:admin",
StandardClaims: jwt.StandardClaims{
NotBefore: time.Now().Unix() - 1000, // signature effective time
ExpiresAt: time.Now().Unix() + 60*60*24*30*12, // expiration time 12 month
Issuer: j.Issuer,
},
}
token, _, err := j.GenerateToken(claims)
if err != nil {
return "", nil, err
}
return token, &claims, nil
}