@@ -14,6 +14,57 @@ import RolesContext from './RolesContext';
14
14
chai . should ( ) ;
15
15
chai . use ( chaiAsPromised ) ;
16
16
17
+ describe ( 'When unsuccessfully authenticating with the Track API client' , ( ) => {
18
+ const api = new Track ( ) ;
19
+ beforeEach ( ( ) => {
20
+ fetchMock
21
+ . post ( api . client . resolve ( '/1/login' ) , ( ) => new Response ( Client . toBlob ( '' , s => s , 'text/plain' ) , { status : 403 } ) )
22
+ . post ( api . client . resolve ( '/1/login/renew' ) , ( ) => new Response ( Client . toBlob ( '' , s => s , 'text/plain' ) , { status : 403 } ) )
23
+ . catch ( 503 ) ;
24
+ } ) ;
25
+ afterEach ( fetchMock . restore ) ;
26
+
27
+ it ( 'should fail to log in with a token' , ( ) => {
28
+ const promise = api . logIn ( { token : 'whatever' } ) ;
29
+ return Promise . all ( [
30
+ promise . should . be . rejected ,
31
+ promise . catch ( err => ( {
32
+ isCorrectType : err instanceof ForbiddenResponse ,
33
+ message : err . message ,
34
+ } ) ) . should . become ( {
35
+ isCorrectType : true ,
36
+ message : 'Invalid credentials' ,
37
+ } ) ,
38
+ ] ) . should . be . fulfilled ;
39
+ } ) ;
40
+
41
+ it ( 'should fail to manually renew authentication' , ( ) => {
42
+ const promise = api . logIn ( { username : charlie . payload . sub , password : 'whatever' } )
43
+ . catch ( )
44
+ . then ( ( ) => api . renewAuthentication ( ) ) ;
45
+ return Promise . all ( [
46
+ promise . should . be . rejected ,
47
+ promise . catch ( err => ( {
48
+ isCorrectType : err instanceof ForbiddenResponse ,
49
+ message : err . message ,
50
+ } ) ) . should . become ( {
51
+ isCorrectType : true ,
52
+ message : 'Invalid credentials' ,
53
+ } ) ,
54
+ ] ) . should . be . fulfilled ;
55
+ } ) ;
56
+
57
+ it ( 'should not auto renew authentication' , async ( ) => {
58
+
59
+ const promise = api . logIn ( { username : charlie . payload . sub , password : 'whatever' } ) ;
60
+
61
+ return Promise . all ( [
62
+ promise . should . be . rejected ,
63
+ promise . catch ( ( ) => resolveAt ( ( ) => fetchMock . calls ( ) . matched . length , 1000 ) ) . should . eventually . equal ( 1 ) ,
64
+ ] ) . should . be . fulfilled ;
65
+ } ) ;
66
+ } ) ;
67
+
17
68
describe ( 'When successfully authenticating with the Track API client' , ( ) => {
18
69
let api ;
19
70
@@ -90,76 +141,6 @@ describe('When successfully authenticating with the Track API client', () => {
90
141
} ) ;
91
142
} ) ;
92
143
93
- describe ( 'When unsuccessfully authenticating with the Track API client' , ( ) => {
94
- const api = new Track ( ) ;
95
-
96
- beforeEach ( ( ) => {
97
- fetchMock
98
- . post ( api . client . resolve ( '/1/login' ) , ( ) => new Response ( Client . toBlob ( '' , s => s , 'text/plain' ) , { status : 403 } ) )
99
- . post ( api . client . resolve ( '/1/login/renew' ) , ( ) => new Response ( Client . toBlob ( '' , s => s , 'text/plain' ) , { status : 403 } ) )
100
- . catch ( 503 ) ;
101
-
102
- api . stopAutoRenew ( ) ;
103
- } ) ;
104
- afterEach ( ( ) => api . stopAutoRenew ( ) ) ;
105
- afterEach ( fetchMock . restore ) ;
106
-
107
- it ( 'should fail to log in with a token' , ( ) => {
108
- const promise = api . logIn ( { token : 'whatever' } ) ;
109
- return Promise . all ( [
110
- promise . should . be . rejected ,
111
- promise . catch ( err => ( {
112
- isCorrectType : err instanceof ForbiddenResponse ,
113
- message : err . message ,
114
- } ) ) . should . become ( {
115
- isCorrectType : true ,
116
- message : 'Invalid credentials' ,
117
- } ) ,
118
- ] ) . should . be . fulfilled ;
119
- } ) ;
120
-
121
- it ( 'should fail to manually renew authentication' , ( ) => {
122
- const promise = api . logIn ( { username : charlie . payload . sub , password : 'whatever' } )
123
- . catch ( )
124
- . then ( ( ) => api . renewAuthentication ( ) ) ;
125
- return Promise . all ( [
126
- promise . should . be . rejected ,
127
- promise . catch ( err => ( {
128
- isCorrectType : err instanceof ForbiddenResponse ,
129
- message : err . message ,
130
- } ) ) . should . become ( {
131
- isCorrectType : true ,
132
- message : 'Invalid credentials' ,
133
- } ) ,
134
- ] ) . should . be . fulfilled ;
135
- } ) ;
136
-
137
- it ( 'should not auto renew authentication' , async ( ) => {
138
- console . log ( '🔄 Checking auto-renew behavior after failed authentication' ) ;
139
-
140
- api . stopAutoRenew ( ) ; // ✅ Ensure auto-renew is disabled before logging in
141
-
142
- const promise = api . logIn ( { username : charlie . payload . sub , password : 'whatever' } ) ;
143
-
144
- console . log ( '📡 FetchMock Calls BEFORE Assertion:' , fetchMock . calls ( ) ) ;
145
-
146
- return Promise . all ( [
147
- promise . should . be . rejected ,
148
- promise . catch ( ( ) => resolveAt ( ( ) => {
149
- console . log ( '📡 FetchMock Calls AFTER Assertion:' , fetchMock . calls ( ) ) ;
150
- const matchedCalls = fetchMock . calls ( ) . matched ;
151
- console . log ( '🚨 Matched Calls:' , matchedCalls ) ;
152
-
153
- // ✅ Ensure we only count the initial login request
154
- matchedCalls . length . should . equal ( 1 ) ;
155
- matchedCalls [ 0 ] [ 0 ] . should . include ( '/1/login' ) ; // ✅ Verify it's the login request
156
-
157
- return matchedCalls . length ;
158
- } , 1000 ) ) . should . eventually . equal ( 1 ) ,
159
- ] ) . should . be . fulfilled ;
160
- } ) } ) ;
161
-
162
-
163
144
describe ( 'When unauthenticating with the Track API client' , ( ) => {
164
145
const api = new Track ( ) ;
165
146
0 commit comments