@@ -50,7 +50,7 @@ describe('Session Token Refreshing', () => {
50
50
path . join ( os . tmpdir ( ) , 'polykey-test-' ) ,
51
51
) ;
52
52
nodePath = path . join ( dataDir , 'keynode' ) ;
53
- command = [ 'vaults ' , 'list ' , '-np' , nodePath ] ;
53
+ command = [ 'agent ' , 'status ' , '-np' , nodePath ] ;
54
54
passwordFile = path . join ( dataDir , 'passwordFile' ) ;
55
55
sessionFile = path . join ( nodePath , 'token' ) ;
56
56
await fs . promises . writeFile ( passwordFile , 'password' ) ;
@@ -68,6 +68,11 @@ describe('Session Token Refreshing', () => {
68
68
await fs . promises . rmdir ( dataDir , { recursive : true } ) ;
69
69
} ) ;
70
70
71
+ beforeEach ( async ( ) => {
72
+ // Invalidate sessions
73
+ await polykeyAgent . sessionManager . resetKey ( ) ;
74
+ } ) ;
75
+
71
76
test ( 'Process should store session token in session file' , async ( ) => {
72
77
// Agent has not been unlocked yet
73
78
const result = await testUtils . pkStdio (
@@ -76,7 +81,7 @@ describe('Session Token Refreshing', () => {
76
81
dataDir ,
77
82
) ;
78
83
expect ( result . exitCode ) . toBe ( 0 ) ;
79
- expect ( result . stdout ) . toContain ( vaultName ) ;
84
+ expect ( result . stdout ) . toContain ( 'LIVE' ) ;
80
85
81
86
const buff = await fs . promises . readFile ( sessionFile ) ;
82
87
const newToken = buff . toString ( ) as SessionToken ;
@@ -85,131 +90,82 @@ describe('Session Token Refreshing', () => {
85
90
) . not . toThrow ( ) ;
86
91
} ) ;
87
92
88
- describe ( 'After session has been unlocked' , ( ) => {
89
- beforeAll ( async ( ) => {
90
- // Authorize session
91
- await testUtils . pkStdio (
92
- [ 'agent' , 'unlock' , '-np' , nodePath , '--password-file' , passwordFile ] ,
93
- { } ,
94
- dataDir ,
95
- ) ;
96
- } , global . polykeyStartupTimeout ) ;
93
+ test ( 'Process should refresh the session token' , async ( ) => {
94
+ const prevToken = await polykeyAgent . sessionManager . createToken ( ) ;
95
+ await fs . promises . writeFile ( sessionFile , prevToken ) ;
97
96
98
- test ( 'Process should refresh the session token' , async ( ) => {
99
- tokenBuffer = await fs . promises . readFile ( sessionFile ) ;
100
- token = tokenBuffer . toString ( ) as SessionToken ;
101
- const prevToken = token ;
97
+ // At least 1 second of delay
98
+ // Expiry time resolution is in seconds
99
+ await sleep ( 1100 ) ;
100
+ const result = await testUtils . pkStdio ( command , { } , dataDir ) ;
101
+ expect ( result . exitCode ) . toBe ( 0 ) ;
102
+ expect ( result . stdout ) . toContain ( 'LIVE' ) ;
102
103
103
- // At least 1 second of delay
104
- // Expiry time resolution is in seconds
105
- await sleep ( 1100 ) ;
106
- const result = await testUtils . pkStdio ( command , { } , dataDir ) ;
107
- expect ( result . exitCode ) . toBe ( 0 ) ;
108
- expect ( result . stdout ) . toContain ( vaultName ) ;
104
+ const buff = await fs . promises . readFile ( sessionFile ) ;
105
+ const newToken = buff . toString ( ) as SessionToken ;
106
+ expect (
107
+ async ( ) => await polykeyAgent . sessionManager . verifyToken ( newToken ) ,
108
+ ) . not . toThrow ( ) ;
109
+ // Make sure the first and second tokens are not the same
110
+ expect ( newToken ) . not . toEqual ( prevToken ) ;
111
+ } ) ;
109
112
110
- const buff = await fs . promises . readFile ( sessionFile ) ;
111
- const newToken = buff . toString ( ) as SessionToken ;
112
- expect (
113
- async ( ) => await polykeyAgent . sessionManager . verifyToken ( newToken ) ,
114
- ) . not . toThrow ( ) ;
115
- // Make sure the first and second tokens are not the same
116
- expect ( newToken ) . not . toEqual ( prevToken ) ;
117
- } ) ;
113
+ test ( 'Serial processes should refresh the session token' , async ( ) => {
114
+ token = await polykeyAgent . sessionManager . createToken ( ) ;
115
+ await fs . promises . writeFile ( sessionFile , token ) ;
118
116
119
- test ( 'Serial processes should refresh the session token' , async ( ) => {
120
- let result = await testUtils . pkStdio ( command , { } , dataDir ) ;
121
- expect ( result . exitCode ) . toBe ( 0 ) ;
122
- expect ( result . stdout ) . toContain ( vaultName ) ;
117
+ let result = await testUtils . pkStdio ( command , { } , dataDir ) ;
118
+ expect ( result . exitCode ) . toBe ( 0 ) ;
119
+ expect ( result . stdout ) . toContain ( 'LIVE' ) ;
123
120
124
- tokenBuffer = await fs . promises . readFile ( sessionFile ) ;
125
- const token1 = tokenBuffer . toString ( ) as SessionToken ;
126
- expect (
127
- async ( ) => await polykeyAgent . sessionManager . verifyToken ( token1 ) ,
128
- ) . not . toThrow ( ) ;
121
+ tokenBuffer = await fs . promises . readFile ( sessionFile ) ;
122
+ const token1 = tokenBuffer . toString ( ) as SessionToken ;
123
+ expect (
124
+ async ( ) => await polykeyAgent . sessionManager . verifyToken ( token1 ) ,
125
+ ) . not . toThrow ( ) ;
129
126
130
- // At least 1 second of delay
131
- // Expiry time resolution is in seconds
132
- await sleep ( 1100 ) ;
133
- result = await testUtils . pkStdio ( command , { } , dataDir ) ;
134
- expect ( result . exitCode ) . toBe ( 0 ) ;
135
- expect ( result . stdout ) . toContain ( vaultName ) ;
127
+ // At least 1 second of delay
128
+ // Expiry time resolution is in seconds
129
+ await sleep ( 1100 ) ;
130
+ result = await testUtils . pkStdio ( command , { } , dataDir ) ;
131
+ expect ( result . exitCode ) . toBe ( 0 ) ;
132
+ expect ( result . stdout ) . toContain ( 'LIVE' ) ;
136
133
137
- tokenBuffer = await fs . promises . readFile ( sessionFile ) ;
138
- const token2 = tokenBuffer . toString ( ) as SessionToken ;
139
- expect (
140
- async ( ) => await polykeyAgent . sessionManager . verifyToken ( token2 ) ,
141
- ) . not . toThrow ( ) ;
134
+ tokenBuffer = await fs . promises . readFile ( sessionFile ) ;
135
+ const token2 = tokenBuffer . toString ( ) as SessionToken ;
136
+ expect (
137
+ async ( ) => await polykeyAgent . sessionManager . verifyToken ( token2 ) ,
138
+ ) . not . toThrow ( ) ;
142
139
143
- // Make sure the first and second tokens are not the same
144
- expect ( token1 ) . not . toEqual ( token2 ) ;
145
- } ) ;
140
+ // Make sure the first and second tokens are not the same
141
+ expect ( token1 ) . not . toEqual ( token2 ) ;
142
+ } ) ;
146
143
147
- test (
148
- 'Failing processes should unlock the session file' ,
149
- async ( ) => {
150
- const result = await testUtils . pkStdio (
151
- [ 'vaults' , 'delete' , 'NotAVault' , '-np' , nodePath ] ,
152
- { } ,
153
- dataDir ,
154
- ) ;
155
- expect ( result . exitCode ) . not . toBe ( 0 ) ;
156
-
157
- tokenBuffer = await fs . promises . readFile ( sessionFile ) ;
158
- token = tokenBuffer . toString ( ) as SessionToken ;
159
- expect (
160
- async ( ) => await polykeyAgent . sessionManager . verifyToken ( token ) ,
161
- ) . not . toThrow ( ) ;
162
-
163
- // Try to lock the session file to ensure it's unlocked
164
- const fd = fs . openSync ( sessionFile , 'r' ) ;
165
- expect ( lock ( fd ) ) . toBeTruthy ( ) ;
166
- lock . unlock ( fd ) ;
167
- fs . closeSync ( fd ) ;
168
- } ,
169
- global . failedConnectionTimeout ,
170
- ) ;
144
+ test (
145
+ 'Failing processes should unlock the session file' ,
146
+ async ( ) => {
147
+ token = await polykeyAgent . sessionManager . createToken ( ) ;
148
+ await fs . promises . writeFile ( sessionFile , token ) ;
171
149
172
- test ( 'Parallel processes should not refresh the session token' , async ( ) => {
173
- let tokenP1 = 'token1' as SessionToken ;
174
- let tokenP2 = 'token2' as SessionToken ;
150
+ const result = await testUtils . pkStdio (
151
+ [ 'vaults' , 'delete' , 'NotAVault' , '-np' , nodePath ] ,
152
+ { } ,
153
+ dataDir ,
154
+ ) ;
155
+ expect ( result . exitCode ) . not . toBe ( 0 ) ;
175
156
176
157
tokenBuffer = await fs . promises . readFile ( sessionFile ) ;
177
- const prevTokenParallel = tokenBuffer . toString ( ) as SessionToken ;
178
-
179
- async function runListCommand ( ) : Promise < SessionToken > {
180
- // At least 1 second of delay
181
- // Expiry time resolution is in seconds
182
- await sleep ( 1000 ) ;
183
- await testUtils . pkStdio ( command , { } , dataDir ) ;
184
- const buffer = await fs . promises . readFile ( sessionFile ) ;
185
- return buffer . toString ( ) as SessionToken ;
186
- }
187
-
188
- [ tokenP1 , tokenP2 ] = await Promise . all ( [
189
- runListCommand ( ) ,
190
- runListCommand ( ) ,
191
- ] ) ;
192
-
193
- // Verify both tokens
194
- expect (
195
- async ( ) => await polykeyAgent . sessionManager . verifyToken ( tokenP1 ) ,
196
- ) . not . toThrow ( ) ;
158
+ token = tokenBuffer . toString ( ) as SessionToken ;
197
159
expect (
198
- async ( ) => await polykeyAgent . sessionManager . verifyToken ( tokenP2 ) ,
160
+ async ( ) => await polykeyAgent . sessionManager . verifyToken ( token ) ,
199
161
) . not . toThrow ( ) ;
200
162
201
- // Check that both commands were completed
202
- expect ( tokenP1 ) . not . toEqual ( 'token1' ) ;
203
- expect ( tokenP2 ) . not . toEqual ( 'token2' ) ;
204
-
205
- // Check that the session token was refreshed exactly one time
206
- if ( tokenP1 === prevTokenParallel ) {
207
- expect ( tokenP2 ) . not . toEqual ( prevTokenParallel ) ;
208
- } else if ( tokenP2 === prevTokenParallel ) {
209
- expect ( tokenP1 ) . not . toEqual ( prevTokenParallel ) ;
210
- } else {
211
- expect ( tokenP1 ) . toEqual ( tokenP2 ) ;
212
- }
213
- } ) ;
214
- } ) ;
163
+ // Try to lock the session file to ensure it's unlocked
164
+ const fd = fs . openSync ( sessionFile , 'r' ) ;
165
+ expect ( lock ( fd ) ) . toBeTruthy ( ) ;
166
+ lock . unlock ( fd ) ;
167
+ fs . closeSync ( fd ) ;
168
+ } ,
169
+ global . failedConnectionTimeout ,
170
+ ) ;
215
171
} ) ;
0 commit comments