@@ -23,7 +23,90 @@ test.group('Jwt guard | authenticate', () => {
23
23
assert . deepEqual ( guard . getUserOrFail ( ) , authenticatedUser )
24
24
} )
25
25
26
- test ( 'throw error when authorization header is missing' , async ( { assert } ) => {
26
+ test ( 'it should return a cookie when user is authenticated' , async ( { assert } ) => {
27
+ const ctx = new HttpContextFactory ( ) . create ( )
28
+ const userProvider = new JwtFakeUserProvider ( )
29
+
30
+ const guard = new JwtGuard ( ctx , userProvider , { secret : 'thisisasecret' , useCookies : true } )
31
+ ctx . request . request . headers . cookie = 'token=' + jwt . sign ( { userId : 1 } , 'thisisasecret' )
32
+
33
+ const authenticatedUser = await guard . authenticate ( )
34
+
35
+ assert . isTrue ( guard . isAuthenticated )
36
+ assert . isTrue ( guard . authenticationAttempted )
37
+
38
+ assert . equal ( guard . user , authenticatedUser )
39
+ assert . deepEqual ( guard . getUserOrFail ( ) , authenticatedUser )
40
+ } )
41
+
42
+ test ( 'throw error when cookie header is invalid' , async ( { assert } ) => {
43
+ const ctx = new HttpContextFactory ( ) . create ( )
44
+ const userProvider = new JwtFakeUserProvider ( )
45
+
46
+ const guard = new JwtGuard ( ctx , userProvider , { secret : 'thisisasecret' } )
47
+ ctx . request . request . headers . cookie = 'foo bar'
48
+ const [ result ] = await Promise . allSettled ( [ guard . authenticate ( ) ] )
49
+
50
+ assert . equal ( result ! . status , 'rejected' )
51
+ if ( result ! . status === 'rejected' ) {
52
+ assert . instanceOf ( result ! . reason , errors . E_UNAUTHORIZED_ACCESS )
53
+ }
54
+
55
+ assert . isUndefined ( guard . user )
56
+ assert . throws ( ( ) => guard . getUserOrFail ( ) , 'Unauthorized access' )
57
+
58
+ assert . isFalse ( guard . isAuthenticated )
59
+ assert . isTrue ( guard . authenticationAttempted )
60
+ } )
61
+
62
+ test ( 'throw error when cookie token is empty' , async ( { assert } ) => {
63
+ const ctx = new HttpContextFactory ( ) . create ( )
64
+ const userProvider = new JwtFakeUserProvider ( )
65
+
66
+ const guard = new JwtGuard ( ctx , userProvider , { secret : 'thisisasecret' } )
67
+ ctx . request . request . headers . cookie = 'token='
68
+ const [ result ] = await Promise . allSettled ( [ guard . authenticate ( ) ] )
69
+
70
+ assert . equal ( result ! . status , 'rejected' )
71
+ if ( result ! . status === 'rejected' ) {
72
+ assert . instanceOf ( result ! . reason , errors . E_UNAUTHORIZED_ACCESS )
73
+ }
74
+
75
+ assert . isUndefined ( guard . user )
76
+ assert . throws ( ( ) => guard . getUserOrFail ( ) , 'Unauthorized access' )
77
+
78
+ assert . isFalse ( guard . isAuthenticated )
79
+ assert . isTrue ( guard . authenticationAttempted )
80
+ } )
81
+
82
+ test ( 'throw error when cookie token has been expired' , async ( { assert } ) => {
83
+ const ctx = new HttpContextFactory ( ) . create ( )
84
+ const userProvider = new JwtFakeUserProvider ( )
85
+ const user = await userProvider . findById ( 1 )
86
+ const token = await userProvider . createToken ( user ! . getOriginal ( ) , 'thisisasecret' , {
87
+ expiresIn : '1h' ,
88
+ } )
89
+
90
+ timeTravel ( 61 * 60 )
91
+
92
+ const guard = new JwtGuard ( ctx , userProvider , { secret : 'thisisasecret' } )
93
+ ctx . request . request . headers . cookie = `token=${ token } `
94
+ const [ result ] = await Promise . allSettled ( [ guard . authenticate ( ) ] )
95
+
96
+ assert . equal ( result ! . status , 'rejected' )
97
+ if ( result ! . status === 'rejected' ) {
98
+ assert . instanceOf ( result ! . reason , errors . E_UNAUTHORIZED_ACCESS )
99
+ }
100
+
101
+ assert . isUndefined ( guard . user )
102
+ assert . throws ( ( ) => guard . getUserOrFail ( ) , 'Unauthorized access' )
103
+ assert . isFalse ( guard . isAuthenticated )
104
+ assert . isTrue ( guard . authenticationAttempted )
105
+ } )
106
+
107
+ test ( 'throw error when authorization header and cookie header are missing' , async ( {
108
+ assert,
109
+ } ) => {
27
110
const ctx = new HttpContextFactory ( ) . create ( )
28
111
const userProvider = new JwtFakeUserProvider ( )
29
112
0 commit comments