1
- import { ConnectorType , SignInIdentifier } from '@logto/schemas' ;
2
- import { createMockUtils } from '@logto/shared/esm' ;
1
+ import {
2
+ AlternativeSignUpIdentifier ,
3
+ ConnectorType ,
4
+ SignInIdentifier ,
5
+ type SignUp ,
6
+ } from '@logto/schemas' ;
3
7
4
8
import { mockAliyunDmConnector , mockAliyunSmsConnector , mockSignUp } from '#src/__mocks__/index.js' ;
5
9
import RequestError from '#src/errors/RequestError/index.js' ;
6
10
7
- const { jest } = import . meta;
8
- const { mockEsmWithActual } = createMockUtils ( jest ) ;
9
-
10
11
const enabledConnectors = [ mockAliyunDmConnector , mockAliyunSmsConnector ] ;
11
12
12
13
const { validateSignUp } = await import ( './sign-up.js' ) ;
13
14
14
15
describe ( 'validate sign-up' , ( ) => {
16
+ it ( 'should throw when setting secondary identifiers without primary identifiers' , async ( ) => {
17
+ expect ( ( ) => {
18
+ validateSignUp (
19
+ {
20
+ ...mockSignUp ,
21
+ identifiers : [ ] ,
22
+ secondaryIdentifiers : [ { identifier : SignInIdentifier . Email } ] ,
23
+ } ,
24
+ enabledConnectors
25
+ ) ;
26
+ } ) . toMatchError (
27
+ new RequestError ( {
28
+ code : 'sign_in_experiences.missing_sign_up_identifiers' ,
29
+ } )
30
+ ) ;
31
+ } ) ;
32
+
33
+ describe ( 'Sign up identifiers must be unique.' , ( ) => {
34
+ const errorTestCase : Array < Pick < SignUp , 'identifiers' | 'secondaryIdentifiers' > > = [
35
+ { identifiers : [ SignInIdentifier . Email , SignInIdentifier . Email ] } ,
36
+ {
37
+ identifiers : [ SignInIdentifier . Username ] ,
38
+ secondaryIdentifiers : [ { identifier : SignInIdentifier . Username } ] ,
39
+ } ,
40
+ {
41
+ identifiers : [ SignInIdentifier . Email , SignInIdentifier . Phone ] ,
42
+ secondaryIdentifiers : [ { identifier : SignInIdentifier . Phone } ] ,
43
+ } ,
44
+ {
45
+ identifiers : [ SignInIdentifier . Phone ] ,
46
+ secondaryIdentifiers : [ { identifier : AlternativeSignUpIdentifier . EmailOrPhone } ] ,
47
+ } ,
48
+ ] ;
49
+
50
+ test . each ( errorTestCase ) (
51
+ 'should throw when there are duplicated sign up identifiers' ,
52
+ async ( signUp ) => {
53
+ expect ( ( ) => {
54
+ validateSignUp ( { ...mockSignUp , ...signUp } , enabledConnectors ) ;
55
+ } ) . toMatchError (
56
+ new RequestError ( {
57
+ code : 'sign_in_experiences.duplicated_sign_up_identifiers' ,
58
+ } )
59
+ ) ;
60
+ }
61
+ ) ;
62
+ } ) ;
63
+
15
64
describe ( 'There must be at least one connector for the specific identifier.' , ( ) => {
16
65
test ( 'should throw when there is no email connector and identifier is email' , async ( ) => {
17
66
expect ( ( ) => {
18
- validateSignUp ( { ...mockSignUp , identifiers : [ SignInIdentifier . Email ] } , [ ] ) ;
67
+ validateSignUp ( { ...mockSignUp , identifiers : [ SignInIdentifier . Email ] , verify : true } , [ ] ) ;
19
68
} ) . toMatchError (
20
69
new RequestError ( {
21
70
code : 'sign_in_experiences.enabled_connector_not_found' ,
@@ -30,6 +79,7 @@ describe('validate sign-up', () => {
30
79
{
31
80
...mockSignUp ,
32
81
identifiers : [ SignInIdentifier . Email , SignInIdentifier . Phone ] ,
82
+ verify : true ,
33
83
} ,
34
84
[ ]
35
85
) ;
@@ -43,7 +93,7 @@ describe('validate sign-up', () => {
43
93
44
94
test ( 'should throw when there is no sms connector and identifier is phone' , async ( ) => {
45
95
expect ( ( ) => {
46
- validateSignUp ( { ...mockSignUp , identifiers : [ SignInIdentifier . Phone ] } , [ ] ) ;
96
+ validateSignUp ( { ...mockSignUp , identifiers : [ SignInIdentifier . Phone ] , verify : true } , [ ] ) ;
47
97
} ) . toMatchError (
48
98
new RequestError ( {
49
99
code : 'sign_in_experiences.enabled_connector_not_found' ,
@@ -69,19 +119,59 @@ describe('validate sign-up', () => {
69
119
} )
70
120
) ;
71
121
} ) ;
72
- } ) ;
73
122
74
- test ( 'should throw when identifier is username and password is false' , async ( ) => {
75
- expect ( ( ) => {
76
- validateSignUp (
77
- { ...mockSignUp , identifiers : [ SignInIdentifier . Username ] , password : false } ,
78
- enabledConnectors
123
+ test ( 'should throw when there is no email connector and secondary identifier is email' , async ( ) => {
124
+ expect ( ( ) => {
125
+ validateSignUp (
126
+ {
127
+ ...mockSignUp ,
128
+ secondaryIdentifiers : [ { identifier : SignInIdentifier . Email , verify : true } ] ,
129
+ } ,
130
+ [ ]
131
+ ) ;
132
+ } ) . toMatchError (
133
+ new RequestError ( {
134
+ code : 'sign_in_experiences.enabled_connector_not_found' ,
135
+ type : ConnectorType . Email ,
136
+ } )
79
137
) ;
80
- } ) . toMatchError (
81
- new RequestError ( {
82
- code : 'sign_in_experiences.username_requires_password' ,
83
- } )
84
- ) ;
138
+ } ) ;
139
+
140
+ test ( 'should throw when there is no sms connector and secondary identifier is phone' , async ( ) => {
141
+ expect ( ( ) => {
142
+ validateSignUp (
143
+ {
144
+ ...mockSignUp ,
145
+ secondaryIdentifiers : [ { identifier : SignInIdentifier . Phone , verify : true } ] ,
146
+ } ,
147
+ [ ]
148
+ ) ;
149
+ } ) . toMatchError (
150
+ new RequestError ( {
151
+ code : 'sign_in_experiences.enabled_connector_not_found' ,
152
+ type : ConnectorType . Sms ,
153
+ } )
154
+ ) ;
155
+ } ) ;
156
+
157
+ test ( 'should throw when there is no email connector and secondary identifier is email or phone' , async ( ) => {
158
+ expect ( ( ) => {
159
+ validateSignUp (
160
+ {
161
+ ...mockSignUp ,
162
+ secondaryIdentifiers : [
163
+ { identifier : AlternativeSignUpIdentifier . EmailOrPhone , verify : true } ,
164
+ ] ,
165
+ } ,
166
+ [ ]
167
+ ) ;
168
+ } ) . toMatchError (
169
+ new RequestError ( {
170
+ code : 'sign_in_experiences.enabled_connector_not_found' ,
171
+ type : ConnectorType . Email ,
172
+ } )
173
+ ) ;
174
+ } ) ;
85
175
} ) ;
86
176
87
177
describe ( 'verify should be true for passwordless identifier' , ( ) => {
@@ -127,5 +217,31 @@ describe('validate sign-up', () => {
127
217
} )
128
218
) ;
129
219
} ) ;
220
+
221
+ test . each ( [
222
+ {
223
+ identifier : SignInIdentifier . Email ,
224
+ } ,
225
+ {
226
+ identifier : SignInIdentifier . Phone ,
227
+ } ,
228
+ {
229
+ identifier : AlternativeSignUpIdentifier . EmailOrPhone ,
230
+ } ,
231
+ ] ) ( 'should not throw when identifier is %p and verify is not true' , async ( identifier ) => {
232
+ expect ( ( ) => {
233
+ validateSignUp (
234
+ {
235
+ ...mockSignUp ,
236
+ secondaryIdentifiers : [ identifier ] ,
237
+ } ,
238
+ enabledConnectors
239
+ ) ;
240
+ } ) . toMatchError (
241
+ new RequestError ( {
242
+ code : 'sign_in_experiences.passwordless_requires_verify' ,
243
+ } )
244
+ ) ;
245
+ } ) ;
130
246
} ) ;
131
247
} ) ;
0 commit comments