Skip to content

Commit 40af8eb

Browse files
tegefaulkesCMCDragonkai
authored andcommitted
Fixing tests/bin/sessions, removed nested describes and concurrent test
1 parent 4ca847b commit 40af8eb

File tree

1 file changed

+70
-114
lines changed

1 file changed

+70
-114
lines changed

tests/bin/sessions.test.ts

+70-114
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('Session Token Refreshing', () => {
5050
path.join(os.tmpdir(), 'polykey-test-'),
5151
);
5252
nodePath = path.join(dataDir, 'keynode');
53-
command = ['vaults', 'list', '-np', nodePath];
53+
command = ['agent', 'status', '-np', nodePath];
5454
passwordFile = path.join(dataDir, 'passwordFile');
5555
sessionFile = path.join(nodePath, 'token');
5656
await fs.promises.writeFile(passwordFile, 'password');
@@ -68,6 +68,11 @@ describe('Session Token Refreshing', () => {
6868
await fs.promises.rmdir(dataDir, { recursive: true });
6969
});
7070

71+
beforeEach(async () => {
72+
// Invalidate sessions
73+
await polykeyAgent.sessionManager.resetKey();
74+
});
75+
7176
test('Process should store session token in session file', async () => {
7277
// Agent has not been unlocked yet
7378
const result = await testUtils.pkStdio(
@@ -76,7 +81,7 @@ describe('Session Token Refreshing', () => {
7681
dataDir,
7782
);
7883
expect(result.exitCode).toBe(0);
79-
expect(result.stdout).toContain(vaultName);
84+
expect(result.stdout).toContain('LIVE');
8085

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

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);
9796

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');
102103

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+
});
109112

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);
118116

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');
123120

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();
129126

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');
136133

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();
142139

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+
});
146143

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);
171149

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);
175156

176157
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;
197159
expect(
198-
async () => await polykeyAgent.sessionManager.verifyToken(tokenP2),
160+
async () => await polykeyAgent.sessionManager.verifyToken(token),
199161
).not.toThrow();
200162

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+
);
215171
});

0 commit comments

Comments
 (0)