-
Notifications
You must be signed in to change notification settings - Fork 12
/
trigger.spec.js
309 lines (285 loc) · 8.03 KB
/
trigger.spec.js
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { trigger } from './'
const { triggerStates, triggers: triggerModel } = trigger
describe('trigger states', () => {
it('should get execution date', () => {
expect(
triggerStates.getLastExecution({
current_state: {
last_execution: '2010-09-10T00:00'
}
})
).toBe('2010-09-10T00:00')
expect(triggerStates.getLastExecution({})).toBe(undefined)
})
it('should return the last execution date', () => {
expect(
triggerStates.getLastSuccess({
current_state: {
last_success: '2010-09-12T00:00'
}
})
).toBe('2010-09-12T00:00')
expect(triggerStates.getLastSuccess({})).toBe(undefined)
})
it('should return whether or not the konnector is errored', () => {
expect(
triggerStates.isErrored({
current_state: { status: 'errored' }
})
).toBe(true)
expect(
triggerStates.isErrored({
current_state: { status: 'ok' }
})
).toBe(false)
expect(triggerStates.isErrored({})).toBe(false)
})
it('should return the last error', () => {
expect(
triggerStates.getLastErrorType({
current_state: { last_error: 'LOGIN_FAILED' }
})
).toBe('LOGIN_FAILED')
expect(
triggerStates.getLastErrorType({
current_state: { status: 'ok' }
})
).toBe(undefined)
expect(triggerStates.getLastErrorType({})).toBe(undefined)
})
})
describe('trigger model', () => {
const normalTrigger = {
_rev: '1-31eb8f0da1db0f3196ccf0d4329ea554',
prefix: 'toto.mycozy.cloud',
arguments: '0 35 0 * * 3',
message: {
account: '4cbfe8f3d89edf60542d5fe9cdcac7b1',
konnector: 'orangemobile'
},
_id: 'fa4c076914ce46a92fa3e7e5f0672ca5',
domain: 'claire.mycozy.cloud',
worker: 'konnector',
debounce: '',
options: null,
type: '@cron'
}
const legacyTrigger = {
arguments: '37 42 0 * * 3',
domain: 'claire.mycozy.cloud',
_rev: '2-a9f7f0eb5ccc0871e10721797ef5dcf0',
worker: 'konnector',
_id: '3a7c363eea2ddb4d73bd11afa9bb4691',
type: '@cron',
options: null,
message: {
Data:
'eyJrb25uZWN0b3IiOiJhbWVsaSIsImFjY291bnQiOiIzYTdjMzYzZWVhMmRkYjRkNzNiZDExYWZhOWJiM2ViNiJ9',
Type: 'json'
}
}
const cliskTrigger = {
_rev: '1-31eb8f0da1db0f3196ccf0d4329ea554',
prefix: 'toto.mycozy.cloud',
arguments: '0 35 0 * * 3',
message: {
account: '4cbfe8f3d89edf60542d5fe9cdcac7b1',
konnector: 'orangemobile'
},
_id: 'fa4c076914ce46a92fa3e7e5f0672ca5',
domain: 'claire.mycozy.cloud',
worker: 'client',
debounce: '',
options: null,
type: '@client'
}
describe('getKonnectorFromTrigger', () => {
it('should work with normal triggers', () => {
expect(triggerModel.getKonnector(normalTrigger)).toBe('orangemobile')
})
it('should work with legacy triggers', () => {
expect(triggerModel.getKonnector(legacyTrigger)).toBe('ameli')
})
it('should work with clisk triggers', () => {
expect(triggerModel.getKonnector(cliskTrigger)).toBe('orangemobile')
})
})
describe('getAccountId', () => {
it('should return the account id', () => {
expect(triggerModel.getAccountId(normalTrigger)).toBe(
'4cbfe8f3d89edf60542d5fe9cdcac7b1'
)
expect(triggerModel.getAccountId(legacyTrigger)).toBe(
'3a7c363eea2ddb4d73bd11afa9bb3eb6'
)
})
})
describe('isLatestErrorMuted', () => {
const triggerWithError = {
_id: 'fa4c076914ce46a92fa3e7e5f0672ca5',
worker: 'konnector',
type: '@cron',
message: {
account: '123abc',
konnector: 'orangemobile'
},
current_state: {
status: 'errored',
last_error: 'LOGIN_FAILED',
last_success: '2010-09-12T00:00'
}
}
it('should return false when there are no muted errors', () => {
expect(triggerModel.isLatestErrorMuted(triggerWithError, {})).toBe(false)
})
it('should return true when the error is muted', () => {
const account = {
_id: '123abc',
mutedErrors: [
{
type: 'LOGIN_FAILED',
mutedAt: '2010-09-15T00:00'
}
]
}
expect(triggerModel.isLatestErrorMuted(triggerWithError, account)).toBe(
true
)
})
it('should ignore errors that are not muted', () => {
const account = {
_id: '123abc',
mutedErrors: [
{
type: 'USER_ACTION_NEEDED',
mutedAt: '2010-09-15T00:00'
}
]
}
expect(triggerModel.isLatestErrorMuted(triggerWithError, account)).toBe(
false
)
})
it('should ignore muted errors if the trigger has been successful after the mute', () => {
const account = {
_id: '123abc',
mutedErrors: [
{
type: 'LOGIN_FAILED',
mutedAt: '2010-09-01T00:00' // muted before the last_success in the trigger
}
]
}
expect(triggerModel.isLatestErrorMuted(triggerWithError, account)).toBe(
false
)
})
it('should mute errors if there has never been a success date', () => {
const triggerWithNoSuccess = {
_id: 'fa4c076914ce46a92fa3e7e5f0672ca5',
worker: 'konnector',
type: '@cron',
message: {
account: '123abc',
konnector: 'orangemobile'
},
current_state: {
status: 'errored',
last_error: 'LOGIN_FAILED'
}
}
const account = {
_id: '123abc',
mutedErrors: [
{
type: 'LOGIN_FAILED',
mutedAt: '2010-09-01T00:00'
}
]
}
expect(
triggerModel.isLatestErrorMuted(triggerWithNoSuccess, account)
).toBe(true)
})
})
describe('hasActionableError', () => {
it('should return a trigger error can be fixed by the user', () => {
expect(
triggerModel.hasActionableError({
current_state: {
last_error: 'LOGIN_FAILED'
}
})
).toBe(true)
expect(
triggerModel.hasActionableError({
current_state: {
last_error: 'UNKNOWN_ERROR'
}
})
).toBe(false)
})
})
describe('buildTriggerAttributes', () => {
const konnector = { slug: 'konnectest' }
const account = { _id: '963a51f6cdd34401b0904de32cc5578d' }
it('builds attributes', () => {
expect(
triggerModel.buildTriggerAttributes({ konnector, account })
).toEqual({
arguments: '0 0 0 * * 0',
type: '@cron',
worker: 'konnector',
message: {
account: '963a51f6cdd34401b0904de32cc5578d',
konnector: 'konnectest'
}
})
})
it('builds attributes with cron', () => {
const cron = '0 0 0 * * 2'
expect(
triggerModel.buildTriggerAttributes({ konnector, account, cron })
).toEqual({
arguments: '0 0 0 * * 2',
type: '@cron',
worker: 'konnector',
message: {
account: '963a51f6cdd34401b0904de32cc5578d',
konnector: 'konnectest'
}
})
})
it('builds for client side connectors', () => {
const clientSideConnector = { ...konnector, clientSide: true }
expect(
triggerModel.buildTriggerAttributes({
konnector: clientSideConnector,
account
})
).toEqual({
type: '@client',
worker: 'konnector',
message: {
account: '963a51f6cdd34401b0904de32cc5578d',
konnector: 'konnectest'
}
})
})
it('build attributes with folder', () => {
const folder = { _id: '4c43f8e88e5f4a608667da6b5bae8fa4' }
expect(
triggerModel.buildTriggerAttributes({ konnector, account, folder })
).toEqual({
arguments: '0 0 0 * * 0',
type: '@cron',
worker: 'konnector',
message: {
account: '963a51f6cdd34401b0904de32cc5578d',
folder_to_save: '4c43f8e88e5f4a608667da6b5bae8fa4',
konnector: 'konnectest'
}
})
})
})
})