-
Notifications
You must be signed in to change notification settings - Fork 165
/
grpc_test.go
132 lines (102 loc) · 3.3 KB
/
grpc_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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package backends
import (
"context"
"net"
"testing"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
log "github.com/sirupsen/logrus"
gs "github.com/iegomez/mosquitto-go-auth/grpc"
. "github.com/smartystreets/goconvey/convey"
)
const (
grpcUsername string = "test_user"
grpcSuperuser string = "superuser"
grpcPassword string = "test_password"
grpcTopic string = "test/topic"
grpcAcc int32 = 1
grpcClientId string = "test_client"
)
type AuthServiceAPI struct{}
func NewAuthServiceAPI() *AuthServiceAPI {
return &AuthServiceAPI{}
}
func (a *AuthServiceAPI) GetUser(ctx context.Context, req *gs.GetUserRequest) (*gs.AuthResponse, error) {
if req.Username == grpcUsername && req.Password == grpcPassword {
return &gs.AuthResponse{
Ok: true,
}, nil
}
return &gs.AuthResponse{
Ok: false,
}, nil
}
func (a *AuthServiceAPI) GetSuperuser(ctx context.Context, req *gs.GetSuperuserRequest) (*gs.AuthResponse, error) {
if req.Username == grpcSuperuser {
return &gs.AuthResponse{
Ok: true,
}, nil
}
return &gs.AuthResponse{
Ok: false,
}, nil
}
func (a *AuthServiceAPI) CheckAcl(ctx context.Context, req *gs.CheckAclRequest) (*gs.AuthResponse, error) {
if req.Username == grpcUsername && req.Topic == grpcTopic && req.Clientid == grpcClientId && req.Acc == grpcAcc {
return &gs.AuthResponse{
Ok: true,
}, nil
}
return &gs.AuthResponse{
Ok: false,
}, nil
}
func (a *AuthServiceAPI) GetName(ctx context.Context, req *empty.Empty) (*gs.NameResponse, error) {
return &gs.NameResponse{
Name: "MyGRPCBackend",
}, nil
}
func (a *AuthServiceAPI) Halt(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func TestGRPC(t *testing.T) {
Convey("given a mock grpc server", t, func(c C) {
grpcServer := grpc.NewServer()
gs.RegisterAuthServiceServer(grpcServer, NewAuthServiceAPI())
lis, err := net.Listen("tcp", ":3123")
So(err, ShouldBeNil)
go grpcServer.Serve(lis)
defer grpcServer.Stop()
authOpts := make(map[string]string)
authOpts["grpc_host"] = "localhost"
authOpts["grpc_port"] = "3123"
Convey("given a correct host grpc backend should be able to initialize", func(c C) {
g, err := NewGRPC(authOpts, log.DebugLevel)
c.So(err, ShouldBeNil)
Convey("given incorrect credentials user should not be authenticated", func(c C) {
auth := g.GetUser(grpcUsername, "wrong", grpcClientId)
c.So(auth, ShouldBeFalse)
Convey("given correct credential user should be authenticated", func(c C) {
auth := g.GetUser(grpcUsername, grpcPassword, grpcClientId)
c.So(auth, ShouldBeTrue)
Convey("given a non superuser user the service should respond false", func(c C) {
auth = g.GetSuperuser(grpcUsername)
So(auth, ShouldBeFalse)
Convey("switching to a superuser should return true", func(c C) {
auth = g.GetSuperuser(grpcSuperuser)
So(auth, ShouldBeTrue)
Convey("authorizing a wrong topic should fail", func(c C) {
auth = g.CheckAcl(grpcUsername, "wrong/topic", grpcClientId, grpcAcc)
So(auth, ShouldBeFalse)
Convey("switching to a correct one should succedd", func(c C) {
auth = g.CheckAcl(grpcUsername, grpcTopic, grpcClientId, grpcAcc)
So(auth, ShouldBeTrue)
})
})
})
})
})
})
})
})
}