-
Notifications
You must be signed in to change notification settings - Fork 2
/
testserver.go
86 lines (80 loc) · 2.39 KB
/
testserver.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
package testserver
import (
"testing"
gzap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/jrockway/jsso2/pkg/client"
"github.com/jrockway/jsso2/pkg/internalauth"
"github.com/jrockway/jsso2/pkg/jsso/cmd"
"github.com/jrockway/jsso2/pkg/jssopb"
"github.com/jrockway/jsso2/pkg/jtesting"
"github.com/jrockway/jsso2/pkg/store"
"google.golang.org/grpc"
)
type S struct {
AppConfig *cmd.Config
AuthConfig *internalauth.Config
WantRootClient bool
Credentials *client.Credentials
App *cmd.App
}
func New() *S {
return &S{
AppConfig: &cmd.Config{
BaseURL: "http://jsso.example.com/",
TokenKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
},
AuthConfig: &internalauth.Config{
RootPassword: "root",
},
}
}
func (s *S) Setup(t *testing.T, e *jtesting.E, server *grpc.Server) {
jssopb.RegisterEnrollmentService(server, jssopb.NewEnrollmentService(s.App.EnrollmentService))
jssopb.RegisterUserService(server, jssopb.NewUserService(s.App.UserService))
jssopb.RegisterLoginService(server, jssopb.NewLoginService(s.App.LoginService))
jssopb.RegisterSessionService(server, jssopb.NewSessionService(s.App.SessionService))
}
// OK, maybe I went overboard with single-letter type names.
func (s *S) ToR(r *jtesting.R) {
r.Database = true
r.Logger = true
if s.WantRootClient {
s.Credentials = &client.Credentials{Root: "root"}
}
r.DatabaseReady = func(t *testing.T, e *jtesting.E) {
db := store.MustGetTestDB(t, e)
var err error
s.App, err = cmd.Setup(s.AppConfig, s.AuthConfig, db)
if err != nil {
t.Fatal(err)
}
}
r.GRPCOptions = func(e *jtesting.E) []grpc.ServerOption {
return []grpc.ServerOption{
grpc.ChainUnaryInterceptor(
gzap.UnaryServerInterceptor(e.Logger.Named("server")),
s.App.Permissions.UnaryServerInterceptor(),
),
grpc.ChainStreamInterceptor(
gzap.StreamServerInterceptor(e.Logger.Named("server")),
s.App.Permissions.StreamServerInterceptor(),
),
}
}
r.GRPCClientOptions = func(e *jtesting.E) []grpc.DialOption {
result := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithChainUnaryInterceptor(
gzap.UnaryClientInterceptor(e.Logger.Named("client")),
),
grpc.WithChainStreamInterceptor(
gzap.StreamClientInterceptor(e.Logger.Named("client")),
),
}
if s.Credentials != nil {
result = append(result, grpc.WithPerRPCCredentials(s.Credentials))
}
return result
}
r.GRPC = s.Setup
}