@@ -5,18 +5,35 @@ interface TestModuleOptions {
55 auth : {
66 whitelist : string [ ]
77 }
8+ apiBasePath ?: string
89}
910
10- // Extract the whitelist logic for testing
11- const getWhitelistWithAutoConfirmEmail = ( options : TestModuleOptions ) => {
11+ // Extract the whitelist logic for testing (matching the module.ts logic)
12+ const getWhitelistWithAutoRegistrationEndpoints = ( options : TestModuleOptions ) => {
1213 const combinedWhitelist = [ ...options . auth . whitelist ]
13- // Auto-whitelist /confirm-email if /register is whitelisted
14- if ( combinedWhitelist . includes ( '/register' ) && ! combinedWhitelist . includes ( '/confirm-email' ) ) {
15- combinedWhitelist . push ( '/confirm-email' )
14+ // Auto-whitelist related endpoints if /register is whitelisted
15+ if ( combinedWhitelist . includes ( '/register' ) ) {
16+ const apiBasePath = options . apiBasePath || '/api/nuxt-users'
17+ const registrationEndpoints = [
18+ '/confirm-email' , // Page route for email confirmation
19+ `${ apiBasePath } /register` , // API endpoint for registration
20+ `${ apiBasePath } /confirm-email` // API endpoint for email confirmation
21+ ]
22+
23+ registrationEndpoints . forEach ( endpoint => {
24+ if ( ! combinedWhitelist . includes ( endpoint ) ) {
25+ combinedWhitelist . push ( endpoint )
26+ }
27+ } )
1628 }
1729 return combinedWhitelist
1830}
1931
32+ // Keep backward compatibility helper for existing tests
33+ const getWhitelistWithAutoConfirmEmail = ( options : TestModuleOptions ) => {
34+ return getWhitelistWithAutoRegistrationEndpoints ( options )
35+ }
36+
2037describe ( 'Auto-whitelist functionality' , ( ) => {
2138 it ( 'should auto-add /confirm-email when /register is whitelisted' , ( ) => {
2239 const options : TestModuleOptions = {
@@ -70,4 +87,51 @@ describe('Auto-whitelist functionality', () => {
7087 expect ( result ) . not . toContain ( '/confirm-email' )
7188 expect ( result ) . toHaveLength ( 0 )
7289 } )
90+
91+ it ( 'should auto-add registration API endpoints when /register is whitelisted' , ( ) => {
92+ const options : TestModuleOptions = {
93+ auth : {
94+ whitelist : [ '/register' ]
95+ }
96+ }
97+
98+ const result = getWhitelistWithAutoRegistrationEndpoints ( options )
99+
100+ expect ( result ) . toContain ( '/register' )
101+ expect ( result ) . toContain ( '/confirm-email' )
102+ expect ( result ) . toContain ( '/api/nuxt-users/register' )
103+ expect ( result ) . toContain ( '/api/nuxt-users/confirm-email' )
104+ expect ( result ) . toHaveLength ( 4 )
105+ } )
106+
107+ it ( 'should use custom API base path for registration endpoints' , ( ) => {
108+ const options : TestModuleOptions = {
109+ auth : {
110+ whitelist : [ '/register' ]
111+ } ,
112+ apiBasePath : '/api/custom-users'
113+ }
114+
115+ const result = getWhitelistWithAutoRegistrationEndpoints ( options )
116+
117+ expect ( result ) . toContain ( '/register' )
118+ expect ( result ) . toContain ( '/confirm-email' )
119+ expect ( result ) . toContain ( '/api/custom-users/register' )
120+ expect ( result ) . toContain ( '/api/custom-users/confirm-email' )
121+ expect ( result ) . not . toContain ( '/api/nuxt-users/register' )
122+ } )
123+
124+ it ( 'should not duplicate API endpoints if already present' , ( ) => {
125+ const options : TestModuleOptions = {
126+ auth : {
127+ whitelist : [ '/register' , '/api/nuxt-users/register' ]
128+ }
129+ }
130+
131+ const result = getWhitelistWithAutoRegistrationEndpoints ( options )
132+
133+ expect ( result . filter ( route => route === '/api/nuxt-users/register' ) ) . toHaveLength ( 1 )
134+ expect ( result ) . toContain ( '/confirm-email' )
135+ expect ( result ) . toContain ( '/api/nuxt-users/confirm-email' )
136+ } )
73137} )
0 commit comments