-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathget-subscriber-preferences.e2e.ts
132 lines (104 loc) · 4.76 KB
/
get-subscriber-preferences.e2e.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
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
import { expect } from 'chai';
import { randomBytes } from 'crypto';
import { UserSession } from '@novu/testing';
import { ChannelTypeEnum, IGetSubscriberResponseDto } from '@novu/shared';
import { NotificationTemplateEntity } from '@novu/dal';
import {
UpdateSubscriberGlobalPreferencesRequestDto,
UpdateSubscriberPreferenceRequestDto,
} from '@novu/api/models/components';
const v2Prefix = '/v2';
let session: UserSession;
describe('Get Subscriber Preferences - /subscribers/:subscriberId/preferences (GET) #novu-v2', () => {
let subscriber: IGetSubscriberResponseDto;
let workflow: NotificationTemplateEntity;
beforeEach(async () => {
const uuid = randomBytes(4).toString('hex');
session = new UserSession();
await session.initialize();
subscriber = await createSubscriberAndValidate(uuid);
workflow = await session.createTemplate({
noFeedId: true,
});
});
it('should fetch subscriber preferences with default values', async () => {
const response = await session.testAgent.get(`${v2Prefix}/subscribers/${subscriber.subscriberId}/preferences`);
expect(response.statusCode).to.equal(200);
expect(response.body.data).to.have.property('global');
expect(response.body.data).to.have.property('workflows');
const { global, workflows } = response.body.data;
// Validate global preferences
expect(global).to.have.property('enabled');
expect(global).to.have.property('channels');
expect(global.enabled).to.be.true;
// Validate workflows array
expect(workflows).to.be.an('array');
});
it('should return 404 if subscriber does not exist', async () => {
const invalidSubscriberId = `non-existent-${randomBytes(2).toString('hex')}`;
const response = await session.testAgent.get(`${v2Prefix}/subscribers/${invalidSubscriberId}/preferences`);
expect(response.statusCode).to.equal(404);
});
it('should handle subscriber with modified workflow preferences', async () => {
// created workflow has 'email' and 'in-app' channels enabled by default
const workflowId = workflow._id;
// disable email channel for this workflow
const enableEmailPreferenceData: UpdateSubscriberPreferenceRequestDto = {
channel: {
type: ChannelTypeEnum.EMAIL,
enabled: false,
},
};
// TODO: replace with v2 endpoint when available
await session.testAgent
.patch(`/v1/subscribers/${subscriber.subscriberId}/preferences/${workflowId}`)
.send({ ...enableEmailPreferenceData });
const response = await session.testAgent.get(`${v2Prefix}/subscribers/${subscriber.subscriberId}/preferences`);
const { global, workflows } = response.body.data;
expect(response.statusCode).to.equal(200);
expect(global.channels).to.deep.equal({ in_app: true, email: true });
expect(workflows).to.have.lengthOf(1);
expect(workflows[0].channels).to.deep.equal({ in_app: true, email: false });
expect(workflows[0].workflow).to.deep.equal({ name: workflow.name, identifier: workflow.triggers[0].identifier });
});
it('should handle subscriber with modified global preferences', async () => {
// disable email channel globally
const enableGlobalEmailPreferenceData: UpdateSubscriberGlobalPreferencesRequestDto = {
preferences: [
{
type: ChannelTypeEnum.EMAIL,
enabled: false,
},
],
};
// TODO: replace with v2 endpoint when available
await session.testAgent
.patch(`/v1/subscribers/${subscriber.subscriberId}/preferences`)
.send({ ...enableGlobalEmailPreferenceData });
const response = await session.testAgent.get(`${v2Prefix}/subscribers/${subscriber.subscriberId}/preferences`);
const { global, workflows } = response.body.data;
expect(response.statusCode).to.equal(200);
expect(global.channels).to.deep.equal({ in_app: true, email: false });
expect(workflows).to.have.lengthOf(1);
expect(workflows[0].channels).to.deep.equal({ in_app: true, email: false });
expect(workflows[0].workflow).to.deep.equal({ name: workflow.name, identifier: workflow.triggers[0].identifier });
});
});
async function createSubscriberAndValidate(id: string = '') {
const payload = {
subscriberId: `test-subscriber-${id}`,
firstName: `Test ${id}`,
lastName: 'Subscriber',
email: `test-${id}@subscriber.com`,
phone: '+1234567890',
};
const res = await session.testAgent.post(`/v1/subscribers`).send(payload);
expect(res.status).to.equal(201);
const subscriber = res.body.data;
expect(subscriber.subscriberId).to.equal(payload.subscriberId);
expect(subscriber.firstName).to.equal(payload.firstName);
expect(subscriber.lastName).to.equal(payload.lastName);
expect(subscriber.email).to.equal(payload.email);
expect(subscriber.phone).to.equal(payload.phone);
return subscriber;
}