@@ -12,6 +12,7 @@ describe('MagicLinkEmailStrategy', () => {
12
12
13
13
const mockUserService = {
14
14
findByEmail : jest . fn ( ) ,
15
+ createWithEmail : jest . fn ( ) ,
15
16
} ;
16
17
17
18
const mockMailingService = {
@@ -57,7 +58,10 @@ describe('MagicLinkEmailStrategy', () => {
57
58
describe ( 'sendMagicLink' , ( ) => {
58
59
it ( 'should send a magic link email' , async ( ) => {
59
60
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
+
61
65
const user = { username : 'testuser' , email } ;
62
66
63
67
mockUserService . findByEmail . mockResolvedValue ( user ) ;
@@ -69,34 +73,50 @@ describe('MagicLinkEmailStrategy', () => {
69
73
) ( email , magicLink ) ;
70
74
71
75
expect ( mockUserService . findByEmail ) . toHaveBeenCalledWith ( email ) ;
72
- // TODO: Fix this test
76
+
73
77
expect ( mockMailingService . sendEmail ) . toHaveBeenCalledWith ( {
74
78
to : email ,
75
79
context : {
76
80
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' ,
78
82
username : 'testuser' ,
79
83
} ,
80
84
subject : 'Noteblock Magic Link' ,
81
85
template : 'magic-link' ,
82
86
} ) ;
83
87
} ) ;
84
88
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] ' ;
88
91
89
- mockUserService . findByEmail . mockResolvedValue ( null ) ;
92
+ const magicLink =
93
+ 'http://localhost/api/v1/auth/magic-link/callback?token=test_token' ;
90
94
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 ) ;
92
99
93
100
await MagicLinkEmailStrategy . sendMagicLink (
94
101
'http://localhost:3000' ,
95
102
userService ,
96
103
mailingService ,
97
104
) ( email , magicLink ) ;
98
105
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
+ } ) ;
100
120
} ) ;
101
121
} ) ;
102
122
0 commit comments