forked from argoproj/notifications-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
73 lines (62 loc) · 1.79 KB
/
api_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
package api
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/argoproj/notifications-engine/pkg/services"
"github.com/argoproj/notifications-engine/pkg/services/mocks"
)
func getVars(in map[string]interface{}, _ services.Destination) map[string]interface{} {
return in
}
func getConfig(ctrl *gomock.Controller, opts ...func(service *mocks.MockNotificationService)) Config {
return Config{
Templates: map[string]services.Notification{
"my-template": {
Message: "hello {{ .foo }} {{ .serviceType }}:{{ .recipient }}",
},
},
Services: map[string]ServiceFactory{
"slack": func() (services.NotificationService, error) {
serviceMock := mocks.NewMockNotificationService(ctrl)
for i := range opts {
opts[i](serviceMock)
}
return serviceMock, nil
},
},
}
}
func TestSend(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
api, err := NewAPI(getConfig(ctrl, func(service *mocks.MockNotificationService) {
service.EXPECT().Send(services.Notification{
Message: "hello world slack:my-channel",
}, services.Destination{
Service: "slack",
Recipient: "my-channel",
}).Return(nil)
}), getVars)
if !assert.NoError(t, err) {
return
}
err = api.Send(
map[string]interface{}{"foo": "world"},
[]string{"my-template"},
services.Destination{Service: "slack", Recipient: "my-channel"},
)
assert.NoError(t, err)
}
func TestAddService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
api, err := NewAPI(getConfig(ctrl), getVars)
if !assert.NoError(t, err) {
return
}
api.AddNotificationService("hello", mocks.NewMockNotificationService(ctrl))
servicesMap := api.GetNotificationServices()
assert.NotNil(t, servicesMap["slack"])
assert.NotNil(t, servicesMap["hello"])
}