11import { describe , it , expect , vi , beforeEach } from 'vitest'
2+ import type { ModuleOptions } from '../../src/types'
23
34const mockUseRuntimeConfig = vi . fn ( )
45const mockUseAuthentication = vi . fn ( )
56
67vi . mock ( '#app' , ( ) => ( {
7- useRuntimeConfig : mockUseRuntimeConfig
8+ useRuntimeConfig : mockUseRuntimeConfig ,
9+ useState : vi . fn ( ( ) => ( { value : null } ) )
810} ) )
911
1012vi . mock ( '../src/runtime/composables/useAuthentication' , ( ) => ( {
@@ -15,31 +17,44 @@ vi.mock('../src/runtime/composables/useAuthentication', () => ({
1517const { usePublicPaths } = await import ( '../../src/runtime/composables/usePublicPaths' )
1618
1719describe ( 'usePublicPaths' , ( ) => {
18- let mockConfig : {
19- apiBasePath : string
20- auth : {
21- whitelist : string [ ]
22- permissions : Record < string , string [ ] >
23- }
24- passwordResetUrl : string
25- }
26- let mockUser : { value : { id : number , role : string } | null }
20+ let mockConfig : ModuleOptions
2721
2822 beforeEach ( ( ) => {
2923 vi . clearAllMocks ( )
3024
3125 // Mock runtime config
3226 mockConfig = {
27+ connector : {
28+ name : 'sqlite' ,
29+ options : {
30+ path : './test.db'
31+ }
32+ } ,
3333 apiBasePath : '/api/nuxt-users' ,
34+ tables : {
35+ migrations : 'migrations' ,
36+ users : 'users' ,
37+ personalAccessTokens : 'personal_access_tokens' ,
38+ passwordResetTokens : 'password_reset_tokens'
39+ } ,
40+ passwordResetUrl : '/reset-password' ,
3441 auth : {
3542 whitelist : [ '/about' , '/contact' ] ,
43+ tokenExpiration : 1440 ,
3644 permissions : {
3745 admin : [ '*' ] ,
3846 user : [ '/profile' , '/dashboard' ] ,
3947 moderator : [ '/admin/*' ]
4048 }
4149 } ,
42- passwordResetUrl : '/reset-password'
50+ passwordValidation : {
51+ minLength : 8 ,
52+ requireUppercase : true ,
53+ requireLowercase : true ,
54+ requireNumbers : true ,
55+ requireSpecialChars : true ,
56+ preventCommonPasswords : true
57+ }
4358 }
4459
4560 mockUseRuntimeConfig . mockReturnValue ( {
@@ -48,24 +63,17 @@ describe('usePublicPaths', () => {
4863 }
4964 } )
5065
51- // Mock user
52- mockUser = { value : null }
66+ // Default mock for unauthenticated user
5367 mockUseAuthentication . mockReturnValue ( {
54- user : mockUser
68+ user : { value : null }
5569 } )
5670 } )
5771
5872 describe ( 'getPublicPaths' , ( ) => {
59- it ( 'should return all public paths correctly ' , ( ) => {
73+ it ( 'should return built-in public paths' , ( ) => {
6074 const { getPublicPaths } = usePublicPaths ( )
6175 const result = getPublicPaths ( )
6276
63- expect ( result . all ) . toContain ( '/login' )
64- expect ( result . all ) . toContain ( '/reset-password' )
65- expect ( result . all ) . toContain ( '/api/nuxt-users/session' )
66- expect ( result . all ) . toContain ( '/about' )
67- expect ( result . all ) . toContain ( '/contact' )
68-
6977 expect ( result . builtIn . pages ) . toEqual ( [ '/login' , '/reset-password' ] )
7078 expect ( result . builtIn . api ) . toEqual ( [
7179 '/api/nuxt-users/session' ,
@@ -76,113 +84,43 @@ describe('usePublicPaths', () => {
7684 expect ( result . apiBasePath ) . toBe ( '/api/nuxt-users' )
7785 } )
7886
79- it ( 'should handle custom password reset URL' , ( ) => {
80- mockConfig . passwordResetUrl = '/custom-reset'
81-
87+ it ( 'should include whitelisted paths in all public paths' , ( ) => {
8288 const { getPublicPaths } = usePublicPaths ( )
8389 const result = getPublicPaths ( )
8490
85- expect ( result . all ) . toContain ( '/custom-reset' )
86- expect ( result . customPasswordResetPath ) . toBe ( '/custom-reset' )
87- } )
88- } )
89-
90- describe ( 'getAccessiblePaths' , ( ) => {
91- it ( 'should return only public paths when no user is authenticated' , ( ) => {
92- mockUser . value = null
93-
94- const { getAccessiblePaths } = usePublicPaths ( )
95- const result = getAccessiblePaths ( )
96-
97- expect ( result . userRole ) . toBe ( null )
98- expect ( result . roleBasedPaths ) . toEqual ( [ ] )
99- expect ( result . all ) . toEqual ( result . public )
100- } )
101-
102- it ( 'should return public + role-based paths for authenticated user' , ( ) => {
103- mockUser . value = { id : 1 , role : 'user' }
104-
105- const { getAccessiblePaths } = usePublicPaths ( )
106- const result = getAccessiblePaths ( )
107-
108- expect ( result . userRole ) . toBe ( 'user' )
109- expect ( result . roleBasedPaths ) . toEqual ( [ '/profile' , '/dashboard' ] )
110- expect ( result . all ) . toContain ( '/login' ) // public path
111- expect ( result . all ) . toContain ( '/profile' ) // role-based path
112- } )
113-
114- it ( 'should handle admin user with wildcard permissions' , ( ) => {
115- mockUser . value = { id : 1 , role : 'admin' }
116-
117- const { getAccessiblePaths } = usePublicPaths ( )
118- const result = getAccessiblePaths ( )
119-
120- expect ( result . userRole ) . toBe ( 'admin' )
121- expect ( result . roleBasedPaths ) . toEqual ( [ '*' ] )
91+ expect ( result . all ) . toContain ( '/about' )
92+ expect ( result . all ) . toContain ( '/contact' )
12293 } )
12394 } )
12495
12596 describe ( 'isPublicPath' , ( ) => {
126- it ( 'should return true for built-in no-auth paths' , ( ) => {
97+ it ( 'should identify built-in public paths' , ( ) => {
12798 const { isPublicPath } = usePublicPaths ( )
12899
129100 expect ( isPublicPath ( '/login' ) ) . toBe ( true )
130101 expect ( isPublicPath ( '/reset-password' ) ) . toBe ( true )
102+ expect ( isPublicPath ( '/api/nuxt-users/session' ) ) . toBe ( true )
131103 } )
132104
133- it ( 'should return true for whitelisted paths' , ( ) => {
105+ it ( 'should identify whitelisted paths as public ' , ( ) => {
134106 const { isPublicPath } = usePublicPaths ( )
135107
136108 expect ( isPublicPath ( '/about' ) ) . toBe ( true )
137109 expect ( isPublicPath ( '/contact' ) ) . toBe ( true )
138110 } )
139111
140- it ( 'should return true for static assets' , ( ) => {
112+ it ( 'should identify static assets as public ' , ( ) => {
141113 const { isPublicPath } = usePublicPaths ( )
142114
143115 expect ( isPublicPath ( '/favicon.ico' ) ) . toBe ( true )
144- expect ( isPublicPath ( '/assets/style.css' ) ) . toBe ( true )
145- } )
146-
147- it ( 'should return true for Nuxt internal routes' , ( ) => {
148- const { isPublicPath } = usePublicPaths ( )
149-
150116 expect ( isPublicPath ( '/_nuxt/app.js' ) ) . toBe ( true )
151117 } )
152118
153- it ( 'should return false for protected paths' , ( ) => {
119+ it ( 'should identify protected paths correctly ' , ( ) => {
154120 const { isPublicPath } = usePublicPaths ( )
155121
156122 expect ( isPublicPath ( '/profile' ) ) . toBe ( false )
157123 expect ( isPublicPath ( '/admin' ) ) . toBe ( false )
158124 } )
159125 } )
160-
161- describe ( 'isAccessiblePath' , ( ) => {
162- it ( 'should return true for public paths regardless of authentication' , ( ) => {
163- const { isAccessiblePath } = usePublicPaths ( )
164-
165- expect ( isAccessiblePath ( '/login' ) ) . toBe ( true )
166- expect ( isAccessiblePath ( '/about' ) ) . toBe ( true )
167- } )
168-
169- it ( 'should return false for protected paths when not authenticated' , ( ) => {
170- mockUser . value = null
171-
172- const { isAccessiblePath } = usePublicPaths ( )
173-
174- expect ( isAccessiblePath ( '/profile' ) ) . toBe ( false )
175- expect ( isAccessiblePath ( '/admin' ) ) . toBe ( false )
176- } )
177-
178- it ( 'should check role-based permissions for authenticated users' , ( ) => {
179- mockUser . value = { id : 1 , role : 'user' }
180-
181- const { isAccessiblePath } = usePublicPaths ( )
182-
183- // This would require mocking the hasPermission function
184- // For now, we just test that the function runs without error
185- expect ( ( ) => isAccessiblePath ( '/profile' ) ) . not . toThrow ( )
186- } )
187- } )
188126} )
0 commit comments