1
1
import { expect } from 'chai' ;
2
2
import { randomBytes } from 'crypto' ;
3
3
import { UserSession } from '@novu/testing' ;
4
- import { ChannelTypeEnum } from '@novu/shared' ;
5
4
import { NotificationTemplateEntity } from '@novu/dal' ;
6
- import {
7
- UpdateSubscriberGlobalPreferencesRequestDto ,
8
- UpdateSubscriberPreferenceRequestDto ,
9
- SubscriberResponseDto ,
10
- } from '@novu/api/models/components' ;
5
+ import { SubscriberResponseDto } from '@novu/api/models/components' ;
11
6
import { Novu } from '@novu/api' ;
12
7
import { expectSdkExceptionGeneric , initNovuClassSdk } from '../../shared/helpers/e2e/sdk/e2e-sdk.helper' ;
13
8
14
- const v2Prefix = '/v2' ;
15
9
let session : UserSession ;
16
10
17
11
describe ( 'Get Subscriber Preferences - /subscribers/:subscriberId/preferences (GET) #novu-v2' , ( ) => {
@@ -33,17 +27,9 @@ describe('Get Subscriber Preferences - /subscribers/:subscriberId/preferences (G
33
27
it ( 'should fetch subscriber preferences with default values' , async ( ) => {
34
28
const response = await novuClient . subscribers . preferences . retrieve ( subscriber . subscriberId ) ;
35
29
36
- expect ( response . result ) . to . have . property ( 'global' ) ;
37
- expect ( response . result ) . to . have . property ( 'workflows' ) ;
38
-
39
30
const { global, workflows } = response . result ;
40
31
41
- // Validate global preferences
42
- expect ( global ) . to . have . property ( 'enabled' ) ;
43
- expect ( global ) . to . have . property ( 'channels' ) ;
44
32
expect ( global . enabled ) . to . be . true ;
45
-
46
- // Validate workflows array
47
33
expect ( workflows ) . to . be . an ( 'array' ) ;
48
34
expect ( workflows ) . to . have . lengthOf ( 1 ) ;
49
35
} ) ;
@@ -57,61 +43,47 @@ describe('Get Subscriber Preferences - /subscribers/:subscriberId/preferences (G
57
43
expect ( error ?. statusCode ) . to . equal ( 404 ) ;
58
44
} ) ;
59
45
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
- } ;
46
+ it ( 'should show all available workflowsin preferences response' , async ( ) => {
47
+ // Create multiple templates
48
+ const workflow2 = await session . createTemplate ( { noFeedId : true } ) ;
49
+ const workflow3 = await session . createTemplate ( { noFeedId : true } ) ;
71
50
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 ;
51
+ const response = await novuClient . subscribers . preferences . retrieve ( subscriber . subscriberId ) ;
80
52
81
- expect ( response . statusCode ) . to . equal ( 200 ) ;
53
+ const { workflows } = response . result ;
82
54
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 } ) ;
55
+ expect ( workflows ) . to . have . lengthOf ( 3 ) ; // Should show all available workflows
56
+ const workflowIdentifiers = workflows . map ( ( _wf ) => _wf . workflow . identifier ) ;
57
+ expect ( workflowIdentifiers ) . to . include ( workflow . triggers [ 0 ] . identifier ) ;
58
+ expect ( workflowIdentifiers ) . to . include ( workflow2 . triggers [ 0 ] . identifier ) ;
59
+ expect ( workflowIdentifiers ) . to . include ( workflow3 . triggers [ 0 ] . identifier ) ;
87
60
} ) ;
88
61
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 ,
62
+ it ( 'should inherit channel preferences from global settings when no workflow override exists ' , async ( ) => {
63
+ // First set global preferences
64
+ await novuClient . subscribers . preferences . update (
65
+ {
66
+ channels : {
67
+ email : false ,
68
+ inApp : true ,
96
69
} ,
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 } ) ;
70
+ } ,
71
+ subscriber . subscriberId
72
+ ) ;
104
73
105
- const response = await session . testAgent . get ( `${ v2Prefix } /subscribers/${ subscriber . subscriberId } /preferences` ) ;
74
+ // Then create a new template
75
+ const newWorkflow = await session . createTemplate ( { noFeedId : true } ) ;
106
76
107
- const { global, workflows } = response . body . data ;
77
+ // Check preferences
78
+ const response = await novuClient . subscribers . preferences . retrieve ( subscriber . subscriberId ) ;
108
79
109
- expect ( response . statusCode ) . to . equal ( 200 ) ;
80
+ const { workflows } = response . result ;
110
81
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 } ) ;
82
+ const newWorkflowPreferences = workflows . find (
83
+ ( _wf ) => _wf . workflow . identifier === newWorkflow . triggers [ 0 ] . identifier
84
+ ) ;
85
+ // New workflow should inherit global settings
86
+ expect ( newWorkflowPreferences ?. channels ) . to . deep . equal ( { email : false , inApp : true } ) ;
115
87
} ) ;
116
88
} ) ;
117
89
0 commit comments