@@ -8,36 +8,80 @@ vi.mock('@/stores/users.store', () => ({
8
8
} ) ) ;
9
9
10
10
describe ( 'Middleware' , ( ) => {
11
+ const ORIGIN_URL = 'https://n8n.local' ;
12
+
13
+ beforeEach ( ( ) => {
14
+ global . window = Object . create ( window ) ;
15
+
16
+ Object . defineProperty ( window , 'location' , {
17
+ value : {
18
+ href : '' ,
19
+ origin : ORIGIN_URL ,
20
+ } ,
21
+ writable : true ,
22
+ } ) ;
23
+ } ) ;
24
+
11
25
describe ( 'guest' , ( ) => {
12
- it ( 'should redirect to given path if current user is present and valid redirect is provided' , async ( ) => {
13
- vi . mocked ( useUsersStore ) . mockReturnValue ( { currentUser : { id : '123' } } as ReturnType <
14
- typeof useUsersStore
15
- > ) ;
26
+ describe ( 'if current user is present' , ( ) => {
27
+ beforeEach ( ( ) => {
28
+ vi . mocked ( useUsersStore ) . mockReturnValue ( { currentUser : { id : '123' } } as ReturnType <
29
+ typeof useUsersStore
30
+ > ) ;
31
+ } ) ;
16
32
17
- const nextMock = vi . fn ( ) ;
18
- const toMock = { query : { redirect : '/some-path' } } as unknown as RouteLocationNormalized ;
19
- const fromMock = { } as RouteLocationNormalized ;
33
+ afterEach ( ( ) => {
34
+ vi . clearAllMocks ( ) ;
35
+ } ) ;
20
36
21
- await guestMiddleware ( toMock , fromMock , nextMock , { } ) ;
37
+ it ( 'should redirect to given path if valid redirect is provided' , async ( ) => {
38
+ const nextMock = vi . fn ( ) ;
39
+ const toMock = { query : { redirect : '/some-path' } } as unknown as RouteLocationNormalized ;
40
+ const fromMock = { } as RouteLocationNormalized ;
22
41
23
- expect ( nextMock ) . toHaveBeenCalledWith ( '/some-path' ) ;
24
- } ) ;
42
+ await guestMiddleware ( toMock , fromMock , nextMock , { } ) ;
25
43
26
- it ( 'should redirect to homepage if current user is present and no valid redirect' , async ( ) => {
27
- vi . mocked ( useUsersStore ) . mockReturnValue ( { currentUser : { id : '123' } } as ReturnType <
28
- typeof useUsersStore
29
- > ) ;
44
+ expect ( nextMock ) . toHaveBeenCalledWith ( '/some-path' ) ;
45
+ } ) ;
30
46
31
- const nextMock = vi . fn ( ) ;
32
- const toMock = { query : { } } as RouteLocationNormalized ;
33
- const fromMock = { } as RouteLocationNormalized ;
47
+ it ( 'should redirect to homepage if no redirect is set' , async ( ) => {
48
+ const nextMock = vi . fn ( ) ;
49
+ const toMock = { query : { } } as RouteLocationNormalized ;
50
+ const fromMock = { } as RouteLocationNormalized ;
34
51
35
- await guestMiddleware ( toMock , fromMock , nextMock , { } ) ;
52
+ await guestMiddleware ( toMock , fromMock , nextMock , { } ) ;
53
+
54
+ expect ( nextMock ) . toHaveBeenCalledWith ( { name : VIEWS . HOMEPAGE } ) ;
55
+ } ) ;
56
+
57
+ it ( 'should redirect to homepage if redirect is not a valid URL' , async ( ) => {
58
+ const nextMock = vi . fn ( ) ;
59
+ const toMock = {
60
+ query : { redirect : 'not-valid-url' } ,
61
+ } as unknown as RouteLocationNormalized ;
62
+
63
+ const fromMock = { } as RouteLocationNormalized ;
64
+
65
+ await guestMiddleware ( toMock , fromMock , nextMock , { } ) ;
66
+
67
+ expect ( nextMock ) . toHaveBeenCalledWith ( { name : VIEWS . HOMEPAGE } ) ;
68
+ } ) ;
69
+
70
+ it ( 'should redirect to homepage if redirect is not the origin domain' , async ( ) => {
71
+ const nextMock = vi . fn ( ) ;
72
+ const toMock = {
73
+ query : { redirect : 'https://n8n.local.evil.com/some-path' } ,
74
+ } as unknown as RouteLocationNormalized ;
75
+
76
+ const fromMock = { } as RouteLocationNormalized ;
77
+
78
+ await guestMiddleware ( toMock , fromMock , nextMock , { } ) ;
36
79
37
- expect ( nextMock ) . toHaveBeenCalledWith ( { name : VIEWS . HOMEPAGE } ) ;
80
+ expect ( nextMock ) . toHaveBeenCalledWith ( { name : VIEWS . HOMEPAGE } ) ;
81
+ } ) ;
38
82
} ) ;
39
83
40
- it ( 'should allow navigation if no current user is present' , async ( ) => {
84
+ it ( 'should not redirect if no current user is present' , async ( ) => {
41
85
vi . mocked ( useUsersStore ) . mockReturnValue ( { currentUser : null } as ReturnType <
42
86
typeof useUsersStore
43
87
> ) ;
@@ -48,7 +92,7 @@ describe('Middleware', () => {
48
92
49
93
await guestMiddleware ( toMock , fromMock , nextMock , { } ) ;
50
94
51
- expect ( nextMock ) . toHaveBeenCalledTimes ( 0 ) ;
95
+ expect ( nextMock ) . not . toHaveBeenCalled ( ) ;
52
96
} ) ;
53
97
} ) ;
54
98
} ) ;
0 commit comments