|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { randomBytes } from 'crypto'; |
| 3 | +import { UserSession } from '@novu/testing'; |
| 4 | +import { ChannelTypeEnum } from '@novu/shared'; |
| 5 | +import { NotificationTemplateEntity } from '@novu/dal'; |
| 6 | +import { |
| 7 | + UpdateSubscriberGlobalPreferencesRequestDto, |
| 8 | + UpdateSubscriberPreferenceRequestDto, |
| 9 | + SubscriberResponseDto, |
| 10 | +} from '@novu/api/models/components'; |
| 11 | +import { Novu } from '@novu/api'; |
| 12 | +import { expectSdkExceptionGeneric, initNovuClassSdk } from '../../shared/helpers/e2e/sdk/e2e-sdk.helper'; |
| 13 | + |
| 14 | +const v2Prefix = '/v2'; |
| 15 | +let session: UserSession; |
| 16 | + |
| 17 | +describe('Get Subscriber Preferences - /subscribers/:subscriberId/preferences (GET) #novu-v2', () => { |
| 18 | + let novuClient: Novu; |
| 19 | + let subscriber: SubscriberResponseDto; |
| 20 | + let workflow: NotificationTemplateEntity; |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + const uuid = randomBytes(4).toString('hex'); |
| 24 | + session = new UserSession(); |
| 25 | + await session.initialize(); |
| 26 | + novuClient = initNovuClassSdk(session); |
| 27 | + subscriber = await createSubscriberAndValidate(uuid); |
| 28 | + workflow = await session.createTemplate({ |
| 29 | + noFeedId: true, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should fetch subscriber preferences with default values', async () => { |
| 34 | + const response = await novuClient.subscribers.preferences.retrieve(subscriber.subscriberId); |
| 35 | + |
| 36 | + expect(response.result).to.have.property('global'); |
| 37 | + expect(response.result).to.have.property('workflows'); |
| 38 | + |
| 39 | + const { global, workflows } = response.result; |
| 40 | + |
| 41 | + // Validate global preferences |
| 42 | + expect(global).to.have.property('enabled'); |
| 43 | + expect(global).to.have.property('channels'); |
| 44 | + expect(global.enabled).to.be.true; |
| 45 | + |
| 46 | + // Validate workflows array |
| 47 | + expect(workflows).to.be.an('array'); |
| 48 | + expect(workflows).to.have.lengthOf(1); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return 404 if subscriber does not exist', async () => { |
| 52 | + const invalidSubscriberId = `non-existent-${randomBytes(2).toString('hex')}`; |
| 53 | + const { error } = await expectSdkExceptionGeneric(() => |
| 54 | + novuClient.subscribers.preferences.retrieve(invalidSubscriberId) |
| 55 | + ); |
| 56 | + |
| 57 | + expect(error?.statusCode).to.equal(404); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle subscriber with modified workflow preferences', async () => { |
| 61 | + // created workflow has 'email' and 'in-app' channels enabled by default |
| 62 | + const workflowId = workflow._id; |
| 63 | + |
| 64 | + // disable email channel for this workflow |
| 65 | + const enableEmailPreferenceData: UpdateSubscriberPreferenceRequestDto = { |
| 66 | + channel: { |
| 67 | + type: ChannelTypeEnum.EMAIL, |
| 68 | + enabled: false, |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + // TODO: replace with v2 endpoint when available |
| 73 | + await session.testAgent |
| 74 | + .patch(`/v1/subscribers/${subscriber.subscriberId}/preferences/${workflowId}`) |
| 75 | + .send({ ...enableEmailPreferenceData }); |
| 76 | + |
| 77 | + const response = await session.testAgent.get(`${v2Prefix}/subscribers/${subscriber.subscriberId}/preferences`); |
| 78 | + |
| 79 | + const { global, workflows } = response.body.data; |
| 80 | + |
| 81 | + expect(response.statusCode).to.equal(200); |
| 82 | + |
| 83 | + expect(global.channels).to.deep.equal({ in_app: true, email: true }); |
| 84 | + expect(workflows).to.have.lengthOf(1); |
| 85 | + expect(workflows[0].channels).to.deep.equal({ in_app: true, email: false }); |
| 86 | + expect(workflows[0].workflow).to.deep.equal({ name: workflow.name, identifier: workflow.triggers[0].identifier }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle subscriber with modified global preferences', async () => { |
| 90 | + // disable email channel globally |
| 91 | + const enableGlobalEmailPreferenceData: UpdateSubscriberGlobalPreferencesRequestDto = { |
| 92 | + preferences: [ |
| 93 | + { |
| 94 | + type: ChannelTypeEnum.EMAIL, |
| 95 | + enabled: false, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }; |
| 99 | + |
| 100 | + // TODO: replace with v2 endpoint when available |
| 101 | + await session.testAgent |
| 102 | + .patch(`/v1/subscribers/${subscriber.subscriberId}/preferences`) |
| 103 | + .send({ ...enableGlobalEmailPreferenceData }); |
| 104 | + |
| 105 | + const response = await session.testAgent.get(`${v2Prefix}/subscribers/${subscriber.subscriberId}/preferences`); |
| 106 | + |
| 107 | + const { global, workflows } = response.body.data; |
| 108 | + |
| 109 | + expect(response.statusCode).to.equal(200); |
| 110 | + |
| 111 | + expect(global.channels).to.deep.equal({ in_app: true, email: false }); |
| 112 | + expect(workflows).to.have.lengthOf(1); |
| 113 | + expect(workflows[0].channels).to.deep.equal({ in_app: true, email: false }); |
| 114 | + expect(workflows[0].workflow).to.deep.equal({ name: workflow.name, identifier: workflow.triggers[0].identifier }); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +async function createSubscriberAndValidate(id: string = '') { |
| 119 | + const payload = { |
| 120 | + subscriberId: `test-subscriber-${id}`, |
| 121 | + firstName: `Test ${id}`, |
| 122 | + lastName: 'Subscriber', |
| 123 | + email: `test-${id}@subscriber.com`, |
| 124 | + phone: '+1234567890', |
| 125 | + }; |
| 126 | + |
| 127 | + const res = await session.testAgent.post(`/v1/subscribers`).send(payload); |
| 128 | + expect(res.status).to.equal(201); |
| 129 | + |
| 130 | + const subscriber = res.body.data; |
| 131 | + |
| 132 | + expect(subscriber.subscriberId).to.equal(payload.subscriberId); |
| 133 | + expect(subscriber.firstName).to.equal(payload.firstName); |
| 134 | + expect(subscriber.lastName).to.equal(payload.lastName); |
| 135 | + expect(subscriber.email).to.equal(payload.email); |
| 136 | + expect(subscriber.phone).to.equal(payload.phone); |
| 137 | + |
| 138 | + return subscriber; |
| 139 | +} |
0 commit comments