Skip to content

Commit 35902cb

Browse files
committed
sessions bin test fixes
removed nested describes and concurrent test lintfixed
1 parent 2915554 commit 35902cb

1 file changed

Lines changed: 70 additions & 114 deletions

File tree

tests/bin/sessions.test.ts

Lines changed: 70 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Session Token Refreshing', () => {
5151
path.join(os.tmpdir(), 'polykey-test-'),
5252
);
5353
nodePath = path.join(dataDir, 'keynode');
54-
command = ['vaults', 'list', '-np', nodePath];
54+
command = ['agent', 'status', '-np', nodePath];
5555
passwordFile = path.join(dataDir, 'passwordFile');
5656
sessionFile = path.join(nodePath, 'client', 'token');
5757
await fs.promises.writeFile(passwordFile, 'password');
@@ -69,6 +69,11 @@ describe('Session Token Refreshing', () => {
6969
await fs.promises.rmdir(dataDir, { recursive: true });
7070
});
7171

72+
beforeEach(async () => {
73+
// Invalidate sessions
74+
await polykeyAgent.sessionManager.resetKey();
75+
});
76+
7277
test('Process should store session token in session file', async () => {
7378
// Agent has not been unlocked yet
7479
const result = await testUtils.pkStdio(
@@ -77,7 +82,7 @@ describe('Session Token Refreshing', () => {
7782
dataDir,
7883
);
7984
expect(result.exitCode).toBe(0);
80-
expect(result.stdout).toContain(vaultName);
85+
expect(result.stdout).toContain('online');
8186

8287
const buff = await fs.promises.readFile(sessionFile);
8388
const newToken = buff.toString() as SessionToken;
@@ -86,131 +91,82 @@ describe('Session Token Refreshing', () => {
8691
).not.toThrow();
8792
});
8893

89-
describe('After session has been unlocked', () => {
90-
beforeAll(async () => {
91-
// Authorize session
92-
await testUtils.pkStdio(
93-
['agent', 'unlock', '-np', nodePath, '--password-file', passwordFile],
94-
{},
95-
dataDir,
96-
);
97-
}, global.polykeyStartupTimeout);
94+
test('Process should refresh the session token', async () => {
95+
const prevToken = await polykeyAgent.sessionManager.createToken();
96+
await fs.promises.writeFile(sessionFile, prevToken);
9897

99-
test('Process should refresh the session token', async () => {
100-
tokenBuffer = await fs.promises.readFile(sessionFile);
101-
token = tokenBuffer.toString() as SessionToken;
102-
const prevToken = token;
98+
// At least 1 second of delay
99+
// Expiry time resolution is in seconds
100+
await sleep(1100);
101+
const result = await testUtils.pkStdio(command, {}, dataDir);
102+
expect(result.exitCode).toBe(0);
103+
expect(result.stdout).toContain('online');
103104

104-
// At least 1 second of delay
105-
// Expiry time resolution is in seconds
106-
await sleep(1100);
107-
const result = await testUtils.pkStdio(command, {}, dataDir);
108-
expect(result.exitCode).toBe(0);
109-
expect(result.stdout).toContain(vaultName);
105+
const buff = await fs.promises.readFile(sessionFile);
106+
const newToken = buff.toString() as SessionToken;
107+
expect(
108+
async () => await polykeyAgent.sessionManager.verifyToken(newToken),
109+
).not.toThrow();
110+
// Make sure the first and second tokens are not the same
111+
expect(newToken).not.toEqual(prevToken);
112+
});
110113

111-
const buff = await fs.promises.readFile(sessionFile);
112-
const newToken = buff.toString() as SessionToken;
113-
expect(
114-
async () => await polykeyAgent.sessionManager.verifyToken(newToken),
115-
).not.toThrow();
116-
// Make sure the first and second tokens are not the same
117-
expect(newToken).not.toEqual(prevToken);
118-
});
114+
test('Serial processes should refresh the session token', async () => {
115+
token = await polykeyAgent.sessionManager.createToken();
116+
await fs.promises.writeFile(sessionFile, token);
119117

120-
test('Serial processes should refresh the session token', async () => {
121-
let result = await testUtils.pkStdio(command, {}, dataDir);
122-
expect(result.exitCode).toBe(0);
123-
expect(result.stdout).toContain(vaultName);
118+
let result = await testUtils.pkStdio(command, {}, dataDir);
119+
expect(result.exitCode).toBe(0);
120+
expect(result.stdout).toContain('online');
124121

125-
tokenBuffer = await fs.promises.readFile(sessionFile);
126-
const token1 = tokenBuffer.toString() as SessionToken;
127-
expect(
128-
async () => await polykeyAgent.sessionManager.verifyToken(token1),
129-
).not.toThrow();
122+
tokenBuffer = await fs.promises.readFile(sessionFile);
123+
const token1 = tokenBuffer.toString() as SessionToken;
124+
expect(
125+
async () => await polykeyAgent.sessionManager.verifyToken(token1),
126+
).not.toThrow();
130127

131-
// At least 1 second of delay
132-
// Expiry time resolution is in seconds
133-
await sleep(1100);
134-
result = await testUtils.pkStdio(command, {}, dataDir);
135-
expect(result.exitCode).toBe(0);
136-
expect(result.stdout).toContain(vaultName);
128+
// At least 1 second of delay
129+
// Expiry time resolution is in seconds
130+
await sleep(1100);
131+
result = await testUtils.pkStdio(command, {}, dataDir);
132+
expect(result.exitCode).toBe(0);
133+
expect(result.stdout).toContain('online');
137134

138-
tokenBuffer = await fs.promises.readFile(sessionFile);
139-
const token2 = tokenBuffer.toString() as SessionToken;
140-
expect(
141-
async () => await polykeyAgent.sessionManager.verifyToken(token2),
142-
).not.toThrow();
135+
tokenBuffer = await fs.promises.readFile(sessionFile);
136+
const token2 = tokenBuffer.toString() as SessionToken;
137+
expect(
138+
async () => await polykeyAgent.sessionManager.verifyToken(token2),
139+
).not.toThrow();
143140

144-
// Make sure the first and second tokens are not the same
145-
expect(token1).not.toEqual(token2);
146-
});
141+
// Make sure the first and second tokens are not the same
142+
expect(token1).not.toEqual(token2);
143+
});
147144

148-
test(
149-
'Failing processes should unlock the session file',
150-
async () => {
151-
const result = await testUtils.pkStdio(
152-
['vaults', 'delete', 'NotAVault', '-np', nodePath],
153-
{},
154-
dataDir,
155-
);
156-
expect(result.exitCode).not.toBe(0);
157-
158-
tokenBuffer = await fs.promises.readFile(sessionFile);
159-
token = tokenBuffer.toString() as SessionToken;
160-
expect(
161-
async () => await polykeyAgent.sessionManager.verifyToken(token),
162-
).not.toThrow();
163-
164-
// Try to lock the session file to ensure it's unlocked
165-
const fd = fs.openSync(sessionFile, 'r');
166-
expect(lock(fd)).toBeTruthy();
167-
lock.unlock(fd);
168-
fs.closeSync(fd);
169-
},
170-
global.failedConnectionTimeout,
171-
);
145+
test(
146+
'Failing processes should unlock the session file',
147+
async () => {
148+
token = await polykeyAgent.sessionManager.createToken();
149+
await fs.promises.writeFile(sessionFile, token);
172150

173-
test('Parallel processes should not refresh the session token', async () => {
174-
let tokenP1 = 'token1' as SessionToken;
175-
let tokenP2 = 'token2' as SessionToken;
151+
const result = await testUtils.pkStdio(
152+
['vaults', 'delete', 'NotAVault', '-np', nodePath],
153+
{},
154+
dataDir,
155+
);
156+
expect(result.exitCode).not.toBe(0);
176157

177158
tokenBuffer = await fs.promises.readFile(sessionFile);
178-
const prevTokenParallel = tokenBuffer.toString() as SessionToken;
179-
180-
async function runListCommand(): Promise<SessionToken> {
181-
// At least 1 second of delay
182-
// Expiry time resolution is in seconds
183-
await sleep(1000);
184-
await testUtils.pkStdio(command, {}, dataDir);
185-
const buffer = await fs.promises.readFile(sessionFile);
186-
return buffer.toString() as SessionToken;
187-
}
188-
189-
[tokenP1, tokenP2] = await Promise.all([
190-
runListCommand(),
191-
runListCommand(),
192-
]);
193-
194-
// Verify both tokens
195-
expect(
196-
async () => await polykeyAgent.sessionManager.verifyToken(tokenP1),
197-
).not.toThrow();
159+
token = tokenBuffer.toString() as SessionToken;
198160
expect(
199-
async () => await polykeyAgent.sessionManager.verifyToken(tokenP2),
161+
async () => await polykeyAgent.sessionManager.verifyToken(token),
200162
).not.toThrow();
201163

202-
// Check that both commands were completed
203-
expect(tokenP1).not.toEqual('token1');
204-
expect(tokenP2).not.toEqual('token2');
205-
206-
// Check that the session token was refreshed exactly one time
207-
if (tokenP1 === prevTokenParallel) {
208-
expect(tokenP2).not.toEqual(prevTokenParallel);
209-
} else if (tokenP2 === prevTokenParallel) {
210-
expect(tokenP1).not.toEqual(prevTokenParallel);
211-
} else {
212-
expect(tokenP1).toEqual(tokenP2);
213-
}
214-
});
215-
});
164+
// Try to lock the session file to ensure it's unlocked
165+
const fd = fs.openSync(sessionFile, 'r');
166+
expect(lock(fd)).toBeTruthy();
167+
lock.unlock(fd);
168+
fs.closeSync(fd);
169+
},
170+
global.failedConnectionTimeout,
171+
);
216172
});

0 commit comments

Comments
 (0)