@@ -19,7 +19,9 @@ import { ProviderDto } from './dto/provider.dto'
19
19
import { EmailService } from '../email/email.service'
20
20
import { JwtService } from '@nestjs/jwt'
21
21
import { TemplateService } from '../email/template.service'
22
- import { PrismaService } from '../prisma/prisma.service'
22
+ import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider'
23
+ import { NotificationsProviderInterface } from '../notifications/providers/notifications.interface.providers'
24
+ import { MarketingNotificationsModule } from '../notifications/notifications.module'
23
25
24
26
jest . mock ( '@keycloak/keycloak-admin-client' )
25
27
@@ -28,6 +30,7 @@ describe('AuthService', () => {
28
30
let config : ConfigService
29
31
let admin : KeycloakAdminClient
30
32
let keycloak : KeycloakConnect . Keycloak
33
+ let marketing : NotificationsProviderInterface < any >
31
34
32
35
const person : Person = {
33
36
id : 'e43348aa-be33-4c12-80bf-2adfbf8736cd' ,
@@ -50,6 +53,7 @@ describe('AuthService', () => {
50
53
51
54
beforeEach ( async ( ) => {
52
55
const module : TestingModule = await Test . createTestingModule ( {
56
+ imports : [ MarketingNotificationsModule ] ,
53
57
providers : [
54
58
AuthService ,
55
59
{
@@ -58,6 +62,7 @@ describe('AuthService', () => {
58
62
get : jest . fn ( ( key : string ) => {
59
63
if ( key === 'keycloak.clientId' ) return 'realm-a12345'
60
64
if ( key === 'keycloak.secret' ) return 'a12345'
65
+ if ( key === 'sendgrid.marketingListId' ) return 'list-id'
61
66
return null
62
67
} ) ,
63
68
} ,
@@ -87,12 +92,27 @@ describe('AuthService', () => {
87
92
provide : TemplateService ,
88
93
useValue : mockDeep < TemplateService > ( ) ,
89
94
} ,
95
+ {
96
+ provide : NotificationsProviderInterface ,
97
+ useClass : SendGridNotificationsProvider ,
98
+ } ,
90
99
] ,
91
- } ) . compile ( )
100
+ } )
101
+ . overrideProvider ( ConfigService )
102
+ . useValue ( {
103
+ get : jest . fn ( ( key : string ) => {
104
+ if ( key === 'keycloak.clientId' ) return 'realm-a12345'
105
+ if ( key === 'keycloak.secret' ) return 'a12345'
106
+ if ( key === 'sendgrid.marketingListId' ) return 'list-id'
107
+ return null
108
+ } ) ,
109
+ } )
110
+ . compile ( )
92
111
93
112
service = module . get < AuthService > ( AuthService )
94
113
config = module . get < ConfigService > ( ConfigService )
95
114
admin = module . get < KeycloakAdminClient > ( KeycloakAdminClient )
115
+ marketing = module . get < NotificationsProviderInterface < any > > ( NotificationsProviderInterface )
96
116
keycloak = module . get < KeycloakConnect . Keycloak > ( KEYCLOAK_INSTANCE )
97
117
} )
98
118
@@ -303,13 +323,20 @@ describe('AuthService', () => {
303
323
const password = 's3cret'
304
324
const firstName = 'John'
305
325
const lastName = 'Doe'
326
+ const newsletter = true
306
327
307
328
it ( 'should call keycloak and prisma' , async ( ) => {
308
329
const keycloakId = 'u123'
309
- const registerDto = plainToClass ( RegisterDto , { email, password, firstName, lastName } )
330
+ const registerDto = plainToClass ( RegisterDto , {
331
+ email,
332
+ password,
333
+ firstName,
334
+ lastName,
335
+ newsletter,
336
+ } )
337
+ jest . spyOn ( marketing , 'addContactsToList' ) . mockImplementation ( async ( ) => true )
310
338
const createUserSpy = jest . spyOn ( service , 'createUser' )
311
339
const adminSpy = jest . spyOn ( admin . users , 'create' ) . mockResolvedValue ( { id : keycloakId } )
312
-
313
340
const prismaSpy = jest . spyOn ( prismaMock . person , 'upsert' ) . mockResolvedValue ( person )
314
341
315
342
expect ( await service . createUser ( registerDto ) ) . toBe ( person )
@@ -346,7 +373,7 @@ describe('AuthService', () => {
346
373
347
374
// Check db creation
348
375
expect ( prismaSpy ) . toHaveBeenCalledWith ( {
349
- create : { keycloakId, email, firstName, lastName } ,
376
+ create : { keycloakId, email, firstName, lastName, newsletter } ,
350
377
update : { keycloakId } ,
351
378
where : { email } ,
352
379
} )
@@ -377,5 +404,94 @@ describe('AuthService', () => {
377
404
expect ( loggerSpy ) . toBeCalled ( )
378
405
loggerSpy . mockRestore ( )
379
406
} )
407
+
408
+ it ( 'should subscribe email to marketing list if consent is given' , async ( ) => {
409
+ const keycloakId = 'u123'
410
+ const registerDto = plainToClass ( RegisterDto , {
411
+ email,
412
+ password,
413
+ firstName,
414
+ lastName,
415
+ // Add to marketing list
416
+ newsletter : true ,
417
+ } )
418
+ const person : Person = {
419
+ id : 'e43348aa-be33-4c12-80bf-2adfbf8736cd' ,
420
+ firstName,
421
+ lastName,
422
+ keycloakId,
423
+ email,
424
+ emailConfirmed : false ,
425
+ phone : null ,
426
+ company : null ,
427
+ picture : null ,
428
+ createdAt : new Date ( '2021-10-07T13:38:11.097Z' ) ,
429
+ updatedAt : new Date ( '2021-10-07T13:38:11.097Z' ) ,
430
+ newsletter : true ,
431
+ address : null ,
432
+ birthday : null ,
433
+ personalNumber : null ,
434
+ stripeCustomerId : null ,
435
+ }
436
+ jest . spyOn ( prismaMock . person , 'upsert' ) . mockResolvedValue ( person )
437
+ jest . spyOn ( admin . users , 'create' ) . mockResolvedValue ( { id : keycloakId } )
438
+ const marketingSpy = jest
439
+ . spyOn ( marketing , 'addContactsToList' )
440
+ . mockImplementation ( async ( ) => true )
441
+
442
+ await service . createUser ( registerDto )
443
+
444
+ // Check was added to list
445
+ expect ( marketingSpy ) . toHaveBeenCalledWith ( {
446
+ contacts : [
447
+ {
448
+ email,
449
+ first_name : firstName ,
450
+ last_name : lastName ,
451
+ } ,
452
+ ] ,
453
+ list_ids : [ 'list-id' ] ,
454
+ } )
455
+ } )
456
+
457
+ it ( 'should NOT subscribe email to marketing list if NO consent is given' , async ( ) => {
458
+ const keycloakId = 'u123'
459
+ const registerDto = plainToClass ( RegisterDto , {
460
+ email,
461
+ password,
462
+ firstName,
463
+ lastName,
464
+ // Don't subscribe to marketing list
465
+ newsletter : false ,
466
+ } )
467
+ const person : Person = {
468
+ id : 'e43348aa-be33-4c12-80bf-2adfbf8736cd' ,
469
+ firstName,
470
+ lastName,
471
+ keycloakId,
472
+ email,
473
+ emailConfirmed : false ,
474
+ phone : null ,
475
+ company : null ,
476
+ picture : null ,
477
+ createdAt : new Date ( '2021-10-07T13:38:11.097Z' ) ,
478
+ updatedAt : new Date ( '2021-10-07T13:38:11.097Z' ) ,
479
+ newsletter : false ,
480
+ address : null ,
481
+ birthday : null ,
482
+ personalNumber : null ,
483
+ stripeCustomerId : null ,
484
+ }
485
+ jest . spyOn ( prismaMock . person , 'upsert' ) . mockResolvedValue ( person )
486
+ jest . spyOn ( admin . users , 'create' ) . mockResolvedValue ( { id : keycloakId } )
487
+ const marketingSpy = jest
488
+ . spyOn ( marketing , 'addContactsToList' )
489
+ . mockImplementation ( async ( ) => true )
490
+
491
+ await service . createUser ( registerDto )
492
+
493
+ // Check was not added to list
494
+ expect ( marketingSpy ) . not . toHaveBeenCalled ( )
495
+ } )
380
496
} )
381
497
} )
0 commit comments