-
Notifications
You must be signed in to change notification settings - Fork 217
/
hooks_test.go
57 lines (50 loc) · 1.26 KB
/
hooks_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
package auth
import (
"context"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/DrmagicE/gmqtt/config"
"github.com/DrmagicE/gmqtt/pkg/packets"
"github.com/DrmagicE/gmqtt/server"
)
func TestAuth_OnBasicAuthWrapper(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
path := "./testdata/gmqtt_password.yml"
cfg := DefaultConfig
cfg.PasswordFile = path
cfg.Hash = Plain
auth, err := New(config.Config{
Plugins: map[string]config.Configuration{
"auth": &cfg,
},
})
mockClient := server.NewMockClient(ctrl)
mockClient.EXPECT().Version().Return(packets.Version311).AnyTimes()
a.Nil(err)
a.Nil(auth.Load(nil))
au := auth.(*Auth)
var preCalled bool
fn := au.OnBasicAuthWrapper(func(ctx context.Context, client server.Client, req *server.ConnectRequest) (err error) {
preCalled = true
return nil
})
// pass
a.Nil(fn(context.Background(), mockClient, &server.ConnectRequest{
Connect: &packets.Connect{
Username: []byte("u1"),
Password: []byte("p1"),
},
}))
a.True(preCalled)
// fail
a.NotNil(fn(context.Background(), mockClient, &server.ConnectRequest{
Connect: &packets.Connect{
Username: []byte("u1"),
Password: []byte("p11"),
},
}))
a.Nil(au.Unload())
}