@@ -12,6 +12,7 @@ describe('MagicLinkEmailStrategy', () => {
1212
1313 const mockUserService = {
1414 findByEmail : jest . fn ( ) ,
15+ createWithEmail : jest . fn ( ) ,
1516 } ;
1617
1718 const mockMailingService = {
@@ -57,7 +58,10 @@ describe('MagicLinkEmailStrategy', () => {
5758 describe ( 'sendMagicLink' , ( ) => {
5859 it ( 'should send a magic link email' , async ( ) => {
5960 const email = '[email protected] ' ; 60- const magicLink = '/api/v1/auth/magic-link/callback?token=test_token' ;
61+
62+ const magicLink =
63+ 'http://localhost/api/v1/auth/magic-link/callback?token=test_token' ;
64+
6165 const user = { username : 'testuser' , email } ;
6266
6367 mockUserService . findByEmail . mockResolvedValue ( user ) ;
@@ -69,34 +73,50 @@ describe('MagicLinkEmailStrategy', () => {
6973 ) ( email , magicLink ) ;
7074
7175 expect ( mockUserService . findByEmail ) . toHaveBeenCalledWith ( email ) ;
72- // TODO: Fix this test
76+
7377 expect ( mockMailingService . sendEmail ) . toHaveBeenCalledWith ( {
7478 to : email ,
7579 context : {
7680 magicLink :
77- 'http://localhost:3000 /api/v1/auth/magic-link/callback?token=test_token' ,
81+ 'http://localhost/api/v1/auth/magic-link/callback?token=test_token' ,
7882 username : 'testuser' ,
7983 } ,
8084 subject : 'Noteblock Magic Link' ,
8185 template : 'magic-link' ,
8286 } ) ;
8387 } ) ;
8488
85- it ( 'should log an error if user is not found' , async ( ) => {
86- const email = '[email protected] ' ; 87- const magicLink = '/api/v1/auth/magic-link/callback?token=test_token' ;
89+ it ( 'should create a new user if not found and send a magic link email' , async ( ) => {
90+ const email = '[email protected] ' ; 8891
89- mockUserService . findByEmail . mockResolvedValue ( null ) ;
92+ const magicLink =
93+ 'http://localhost/api/v1/auth/magic-link/callback?token=test_token' ;
9094
91- const loggerSpy = jest . spyOn ( MagicLinkEmailStrategy . logger , 'error' ) ;
95+ const user = { username : 'testuser' , email } ;
96+
97+ mockUserService . findByEmail . mockResolvedValue ( null ) ;
98+ mockUserService . createWithEmail . mockResolvedValue ( user ) ;
9299
93100 await MagicLinkEmailStrategy . sendMagicLink (
94101 'http://localhost:3000' ,
95102 userService ,
96103 mailingService ,
97104 ) ( email , magicLink ) ;
98105
99- expect ( loggerSpy ) . toHaveBeenCalledWith ( 'User not found' ) ;
106+ expect ( mockUserService . findByEmail ) . toHaveBeenCalledWith ( email ) ;
107+
108+ expect ( mockUserService . createWithEmail ) . toHaveBeenCalledWith ( email ) ;
109+
110+ expect ( mockMailingService . sendEmail ) . toHaveBeenCalledWith ( {
111+ to : email ,
112+ context : {
113+ magicLink :
114+ 'http://localhost/api/v1/auth/magic-link/callback?token=test_token' ,
115+ username : 'testuser' ,
116+ } ,
117+ subject : 'Welcome to Noteblock.world' ,
118+ template : 'magic-link-new-account' ,
119+ } ) ;
100120 } ) ;
101121 } ) ;
102122
0 commit comments