22import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
33import { useAuthGuard } from '../../guards/authGuard'
44import * as Auth from '../../stores/auth'
5+ import type { RouteAuthGuard , RouteGuestGuard } from 'app/assets/interfaces'
56
67// Default mock for the auth store and router
78const mockAuthStore = {
89 isAuthenticated : false ,
9- check : vi . fn ( )
10+ checkAccess : vi . fn ( )
1011}
1112
1213const mockRouter = {
1314 currentRoute : {
1415 value : {
16+ path : '/foo/bar' ,
1517 meta : {
16- auth : null as null | { redirect ?: string } ,
17- guest : null as null | { redirect ?: string }
18+ auth : undefined as RouteAuthGuard | undefined ,
19+ guest : undefined as RouteGuestGuard | undefined
1820 }
1921 }
2022 } ,
21- push : vi . fn ( )
23+ replace : vi . fn ( )
2224}
2325
2426describe ( 'authGuard useAuthGuard() method' , ( ) => {
@@ -37,59 +39,69 @@ describe('authGuard useAuthGuard() method', () => {
3739 useAuthGuard ( mockRouter as any )
3840
3941 // Assert
40- expect ( mockRouter . push ) . not . toHaveBeenCalled ( )
42+ expect ( mockRouter . replace ) . not . toHaveBeenCalled ( )
4143 } )
4244
4345 test ( 'should redirect to login if route requires auth and user is not authenticated' , ( ) => {
4446 // Arrange
4547 mockRouter . currentRoute . value . meta . auth = { redirect : '/login' }
46- mockRouter . currentRoute . value . meta . guest = null
4748 mockAuthStore . isAuthenticated = false
4849
4950 // Act
5051 useAuthGuard ( mockRouter as any )
5152
5253 // Assert
53- expect ( mockRouter . push ) . toHaveBeenCalledWith ( '/login' )
54+ expect ( mockRouter . replace ) . toHaveBeenCalled ( )
55+ expect ( mockRouter . replace ) . toHaveBeenCalledWith ( '/login' )
5456 } )
5557
5658 test ( 'should not redirect if route requires auth and user is authenticated' , ( ) => {
5759 // Arrange
5860 mockRouter . currentRoute . value . meta . auth = { redirect : '/login' }
59- mockRouter . currentRoute . value . meta . guest = null
6061 mockAuthStore . isAuthenticated = true
6162
6263 // Act
6364 useAuthGuard ( mockRouter as any )
6465
6566 // Assert
66- expect ( mockRouter . push ) . not . toHaveBeenCalled ( )
67+ expect ( mockRouter . replace ) . not . toHaveBeenCalled ( )
6768 } )
6869
69- test ( 'should not redirect if route is for guests and user is not authenticated ' , ( ) => {
70+ test ( 'should not redirect if route requires permission and user does not have it ' , ( ) => {
7071 // Arrange
71- mockRouter . currentRoute . value . meta . auth = null
72+ mockRouter . currentRoute . value . meta . auth = { permission : 'foo.bar' , redirect : '/login' }
73+ mockAuthStore . isAuthenticated = true
74+
75+ // Act
76+ useAuthGuard ( mockRouter as any )
77+
78+ // Assert
79+ expect ( mockRouter . replace ) . toHaveBeenCalledWith ( '/login' )
80+ } )
81+
82+ test ( 'should not redirect if route requires guests and user is not authenticated' , ( ) => {
83+ // Arrange
84+ mockRouter . currentRoute . value . meta . auth = undefined
7285 mockRouter . currentRoute . value . meta . guest = { redirect : '/' }
7386 mockAuthStore . isAuthenticated = false
7487
7588 // Act
7689 useAuthGuard ( mockRouter as any )
7790
7891 // Assert
79- expect ( mockRouter . push ) . not . toHaveBeenCalled ( )
92+ expect ( mockRouter . replace ) . not . toHaveBeenCalled ( )
8093 } )
8194
8295 test ( 'should redirect to home if route is for guests and user is authenticated' , ( ) => {
8396 // Arrange
84- mockRouter . currentRoute . value . meta . auth = null
8597 mockRouter . currentRoute . value . meta . guest = { redirect : '/' }
8698 mockAuthStore . isAuthenticated = true
8799
88100 // Act
89101 useAuthGuard ( mockRouter as any )
90102
91103 // Assert
92- expect ( mockRouter . push ) . toHaveBeenCalledWith ( '/' )
104+ expect ( mockRouter . replace ) . toHaveBeenCalledWith ( '/' )
93105 } )
94106
95107 test ( 'should use default redirect for auth guard if no redirect specified' , ( ) => {
@@ -101,7 +113,27 @@ describe('authGuard useAuthGuard() method', () => {
101113 useAuthGuard ( mockRouter as any )
102114
103115 // Assert
104- expect ( mockRouter . push ) . toHaveBeenCalledWith ( '/login' )
116+ expect ( mockRouter . replace ) . toHaveBeenCalledWith (
117+ expect . objectContaining ( {
118+ name : 'Unauthorized'
119+ } )
120+ )
121+ } )
122+
123+ test ( 'should use default redirect for permission guard if no redirect specified' , ( ) => {
124+ // Arrange
125+ mockRouter . currentRoute . value . meta . auth = { permission : 'foo.bar' }
126+ mockAuthStore . isAuthenticated = false
127+
128+ // Act
129+ useAuthGuard ( mockRouter as any )
130+
131+ // Assert
132+ expect ( mockRouter . replace ) . toHaveBeenCalledWith (
133+ expect . objectContaining ( {
134+ name : 'Unauthorized'
135+ } )
136+ )
105137 } )
106138
107139 test ( 'should use default redirect for guest guard if no redirect specified' , ( ) => {
@@ -113,6 +145,10 @@ describe('authGuard useAuthGuard() method', () => {
113145 useAuthGuard ( mockRouter as any )
114146
115147 // Assert
116- expect ( mockRouter . push ) . toHaveBeenCalledWith ( '/' )
148+ expect ( mockRouter . replace ) . toHaveBeenCalledWith (
149+ expect . objectContaining ( {
150+ name : 'Unauthorized'
151+ } )
152+ )
117153 } )
118154} )
0 commit comments