-
Notifications
You must be signed in to change notification settings - Fork 11
/
subscriptions.test.js
106 lines (93 loc) · 2.87 KB
/
subscriptions.test.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
const SDK = require('../lib/index')
const eventGatewayProcess = require('./utils/eventGatewayProcess')
let eventGateway
let eventGatewayProcessId
beforeAll(() =>
eventGatewayProcess
.spawn({
configPort: 4005,
apiPort: 4006
})
.then(processInfo => {
eventGatewayProcessId = processInfo.id
eventGateway = new SDK({
space: 'testspace',
url: `http:https://localhost:${processInfo.apiPort}`,
configurationUrl: `http:https://localhost:${processInfo.configPort}`
})
})
.then(() => {
return eventGateway.createEventType(eventType)
})
.then(() => {
return eventGateway.createFunction(functionConfig)
}))
afterAll(() => {
eventGatewayProcess.shutDown(eventGatewayProcessId)
})
test('should add a subscription to the gateway', () => {
return eventGateway.subscribe(createSubscriptionConfig).then(response => {
expect(response).toEqual(createdSubscriptionConfig)
})
})
test('should return list of subscriptions', () => {
return eventGateway.listSubscriptions().then(response => {
expect(response).toEqual([createdSubscriptionConfig])
})
})
test('should return filtered list of subscriptions', () => {
return eventGateway.listSubscriptions({ 'metadata.foo': 'bar' }).then(response => {
expect(response).toEqual([])
})
})
test('should remove the added subscription', () => {
return eventGateway.unsubscribe({ subscriptionId: 'YXN5bmMsdGVzdC5ldmVudCx0ZXN0LCUyRixQT1NU' }).then(response => {
expect(response).toBeUndefined()
})
})
test('should fail to a add a subscription to a non-existing function', () => {
const brokenConfig = { functionId: 'non-exiting-function', eventType: 'pageVisited' }
return eventGateway.subscribe(brokenConfig).catch(err => {
expect(err).toEqual(
new Error(
`Failed to subscribe the event pageVisited to the function non-exiting-function due the error: ` +
`Subscription doesn't validate. Validation error: Key: 'Subscription.Type' Error:Field validation` +
` for 'Type' failed on the 'required' tag`
)
)
})
})
test('should fail to a remove a non-existing subscription', () => {
return eventGateway.unsubscribe({ subscriptionId: 'xxx' }).catch(err => {
expect(err).toEqual(
new Error('Failed to unsubscribe the subscription xxx due the error: Subscription "xxx" not found.')
)
})
})
const eventType = {
space: 'testspace',
name: 'test.event'
}
const functionConfig = {
space: 'testspace',
functionId: 'test',
type: 'awslambda',
provider: {
arn: 'arn::::',
region: 'us-east-1'
}
}
const createSubscriptionConfig = {
type: 'async',
eventType: 'test.event',
functionId: 'test'
}
const createdSubscriptionConfig = {
space: 'testspace',
subscriptionId: 'YXN5bmMsdGVzdC5ldmVudCx0ZXN0LCUyRixQT1NU',
type: 'async',
eventType: 'test.event',
functionId: 'test',
method: 'POST',
path: '/'
}