generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service_test.go
260 lines (244 loc) · 6.25 KB
/
service_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package ancla
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/argus/chrysom"
"github.com/xmidt-org/argus/model"
"github.com/xmidt-org/sallust"
"go.uber.org/zap"
)
func TestNewService(t *testing.T) {
tcs := []struct {
desc string
config Config
getLogger func(context.Context) *zap.Logger
expectedErr bool
}{
{
desc: "Success Case",
config: Config{
BasicClientConfig: chrysom.BasicClientConfig{
Address: "test",
Bucket: "test",
},
},
},
{
desc: "Chrysom Basic Client Creation Failure",
expectedErr: true,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
assert := assert.New(t)
_, err := NewService(tc.config, tc.getLogger)
if tc.expectedErr {
assert.NotNil(err)
return
}
require.NoError(t, err)
})
}
}
func TestStartListener(t *testing.T) {
mockServiceConfig := Config{
BasicClientConfig: chrysom.BasicClientConfig{
Address: "test",
Bucket: "test",
},
}
mockService, _ := NewService(mockServiceConfig, nil)
tcs := []struct {
desc string
serviceConfig Config
listenerConfig ListenerConfig
svc service
expectedErr bool
}{
{
desc: "Success Case",
svc: *mockService,
listenerConfig: ListenerConfig{
Config: chrysom.ListenerClientConfig{},
},
},
{
desc: "Chrysom Listener Client Creation Failure",
expectedErr: true,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
assert := assert.New(t)
_, err := tc.svc.StartListener(tc.listenerConfig, nil)
if tc.expectedErr {
assert.NotNil(err)
return
}
require.NoError(t, err)
})
}
}
func TestAdd(t *testing.T) {
type pushItemResults struct {
result chrysom.PushResult
err error
}
type testCase struct {
Description string
Owner string
PushItemResults pushItemResults
ExpectedErr error
}
tcs := []testCase{
{
Description: "PushItem fails",
PushItemResults: pushItemResults{
err: errors.New("push item failed"),
},
ExpectedErr: errFailedWebhookPush,
},
{
Description: "Unknown push result",
PushItemResults: pushItemResults{
result: chrysom.UnknownPushResult,
},
ExpectedErr: errNonSuccessPushResult,
},
{
Description: "Item created",
PushItemResults: pushItemResults{
result: chrysom.CreatedPushResult,
},
},
{
Description: "Item update",
PushItemResults: pushItemResults{
result: chrysom.UpdatedPushResult,
},
},
}
inputWebhook := getTestInternalWebhooks()[0]
for _, tc := range tcs {
t.Run(tc.Description, func(t *testing.T) {
assert := assert.New(t)
m := new(mockPushReader)
svc := service{
logger: sallust.Default(),
config: Config{},
argus: m,
now: time.Now,
}
// nolint:typecheck
m.On("PushItem", context.TODO(), tc.Owner, mock.Anything).Return(tc.PushItemResults.result, tc.PushItemResults.err)
err := svc.Add(context.TODO(), tc.Owner, inputWebhook)
if tc.ExpectedErr != nil {
assert.True(errors.Is(err, tc.ExpectedErr))
}
// nolint:typecheck
m.AssertExpectations(t)
})
}
}
func TestAllInternalWebhooks(t *testing.T) {
type testCase struct {
Description string
GetItemsResp chrysom.Items
GetItemsErr error
ExpectedInternalWebhooks []InternalWebhook
ExpectedErr error
}
tcs := []testCase{
{
Description: "Fetching argus webhooks fails",
GetItemsErr: errors.New("db failed"),
ExpectedErr: errFailedWebhooksFetch,
},
{
Description: "Webhooks fetch success",
GetItemsResp: getTestItems(),
ExpectedInternalWebhooks: getTestInternalWebhooks(),
},
}
for _, tc := range tcs {
t.Run(tc.Description, func(t *testing.T) {
assert := assert.New(t)
m := new(mockPushReader)
svc := service{
argus: m,
logger: sallust.Default(),
config: Config{},
}
// nolint:typecheck
m.On("GetItems", context.TODO(), "").Return(tc.GetItemsResp, tc.GetItemsErr)
iws, err := svc.GetAll(context.TODO())
if tc.ExpectedErr != nil {
assert.True(errors.Is(err, tc.ExpectedErr))
assert.Empty(iws)
} else {
assert.EqualValues(tc.ExpectedInternalWebhooks, iws)
}
// nolint:typecheck
m.AssertExpectations(t)
})
}
}
func getTestItems() chrysom.Items {
var (
firstItemExpiresInSecs int64 = 10
secondItemExpiresInSecs int64 = 20
)
return chrysom.Items{
model.Item{
ID: "b3bbc3467366959e0aba3c33588a08c599f68a740fabf4aa348463d3dc7dcfe8",
Data: map[string]interface{}{
"Webhook": map[string]interface{}{
"registered_from_address": "http://original-requester.example.net",
"config": map[string]interface{}{
"url": "http://deliver-here-0.example.net",
"content_type": "application/json",
"secret": "superSecretXYZ",
},
"events": []interface{}{"online"},
"matcher": map[string]interface{}{
"device_id": []interface{}{"mac:aabbccddee.*"},
},
"failure_url": "http://contact-here-when-fails.example.net",
"duration": float64((10 * time.Second).Nanoseconds()),
"until": "2021-01-02T15:04:10Z",
},
"PartnerIDs": []interface{}{"comcast"},
},
TTL: &firstItemExpiresInSecs,
},
model.Item{
ID: "c97b4d17f7eb406720a778f73eecf419438659091039a312bebba4570e80a778",
Data: map[string]interface{}{
"webhook": map[string]interface{}{
"registered_from_address": "http://original-requester.example.net",
"config": map[string]interface{}{
"url": "http://deliver-here-1.example.net",
"content_type": "application/json",
"secret": "doNotShare:e=mc^2",
},
"events": []interface{}{"online"},
"matcher": map[string]interface{}{
"device_id": []interface{}{"mac:aabbccddee.*"},
},
"failure_url": "http://contact-here-when-fails.example.net",
"duration": float64((20 * time.Second).Nanoseconds()),
"until": "2021-01-02T15:04:20Z",
},
"partnerids": []string{},
},
TTL: &secondItemExpiresInSecs,
},
}
}