-
Notifications
You must be signed in to change notification settings - Fork 121
/
WebhookChannel2023Type.test.ts
103 lines (89 loc) · 4.07 KB
/
WebhookChannel2023Type.test.ts
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
import { DataFactory, Store } from 'n3';
import {
AbsolutePathInteractionRoute,
} from '../../../../../src/identity/interaction/routing/AbsolutePathInteractionRoute';
import {
RelativePathInteractionRoute,
} from '../../../../../src/identity/interaction/routing/RelativePathInteractionRoute';
import type { Logger } from '../../../../../src/logging/Logger';
import { getLoggerFor } from '../../../../../src/logging/LogUtil';
import { CONTEXT_NOTIFICATION } from '../../../../../src/server/notifications/Notification';
import type { NotificationChannel } from '../../../../../src/server/notifications/NotificationChannel';
import type { StateHandler } from '../../../../../src/server/notifications/StateHandler';
import type {
WebhookChannel2023,
} from '../../../../../src/server/notifications/WebhookChannel2023/WebhookChannel2023Type';
import {
isWebhook2023Channel,
WebhookChannel2023Type,
} from '../../../../../src/server/notifications/WebhookChannel2023/WebhookChannel2023Type';
import { NOTIFY, RDF } from '../../../../../src/util/Vocabularies';
import quad = DataFactory.quad;
import blankNode = DataFactory.blankNode;
import namedNode = DataFactory.namedNode;
jest.mock('../../../../../src/logging/LogUtil', (): any => {
const logger: Logger =
{ error: jest.fn() } as any;
return { getLoggerFor: (): Logger => logger };
});
jest.mock('uuid', (): any => ({ v4: (): string => '4c9b88c1-7502-4107-bb79-2a3a590c7aa3' }));
describe('A WebhookChannel2023Type', (): void => {
const sendTo = 'http://example.org/somewhere-else';
const topic = 'https://storage.example/resource';
const subject = blankNode();
let data: Store;
let channel: WebhookChannel2023;
const route = new AbsolutePathInteractionRoute('http://example.com/webhooks/');
const webIdRoute = new RelativePathInteractionRoute(route, '/webid');
let stateHandler: jest.Mocked<StateHandler>;
let channelType: WebhookChannel2023Type;
beforeEach(async(): Promise<void> => {
data = new Store();
data.addQuad(quad(subject, RDF.terms.type, NOTIFY.terms.WebhookChannel2023));
data.addQuad(quad(subject, NOTIFY.terms.topic, namedNode(topic)));
data.addQuad(quad(subject, NOTIFY.terms.sendTo, namedNode(sendTo)));
const id = 'http://example.com/webhooks/4c9b88c1-7502-4107-bb79-2a3a590c7aa3';
channel = {
id,
type: NOTIFY.WebhookChannel2023,
topic: 'https://storage.example/resource',
sendTo,
};
stateHandler = {
handleSafe: jest.fn(),
} as any;
channelType = new WebhookChannel2023Type(route, webIdRoute, stateHandler);
});
it('exposes a utility function to verify if a channel is a webhook channel.', async(): Promise<void> => {
expect(isWebhook2023Channel(channel)).toBe(true);
(channel as NotificationChannel).type = 'something else';
expect(isWebhook2023Channel(channel)).toBe(false);
});
it('correctly parses notification channel bodies.', async(): Promise<void> => {
await expect(channelType.initChannel(data)).resolves.toEqual(channel);
});
it('adds the WebID when generating a JSON-LD representation of a channel.', async(): Promise<void> => {
await expect(channelType.toJsonLd(channel)).resolves.toEqual({
'@context': [
CONTEXT_NOTIFICATION,
],
id: channel.id,
type: NOTIFY.WebhookChannel2023,
sendTo,
topic,
sender: 'http://example.com/webhooks/webid',
});
});
it('calls the state handler once the channel is completed.', async(): Promise<void> => {
await channelType.completeChannel(channel);
expect(stateHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(stateHandler.handleSafe).toHaveBeenLastCalledWith({ channel });
});
it('logs an error if something went wrong emitting the state notification.', async(): Promise<void> => {
const logger = getLoggerFor('mock');
stateHandler.handleSafe.mockRejectedValue(new Error('notification error'));
await channelType.completeChannel(channel);
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenLastCalledWith('Error emitting state notification: notification error');
});
});