mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
test: additional tests for .roll()
This commit is contained in:
@@ -28,16 +28,22 @@ utils.tokens.get = async (tokens) => {
|
|||||||
singular = true;
|
singular = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [tokenObjs, lastSeen] = await Promise.all([
|
let [tokenObjs, lastSeen] = await Promise.all([
|
||||||
db.getObjects(tokens.map(t => `token:${t}`)),
|
db.getObjects(tokens.map(t => `token:${t}`)),
|
||||||
utils.tokens.getLastSeen(tokens),
|
utils.tokens.getLastSeen(tokens),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
tokenObjs.forEach((tokenObj, idx) => {
|
tokenObjs = tokenObjs.map((tokenObj, idx) => {
|
||||||
|
if (!tokenObj) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
tokenObj.token = tokens[idx];
|
tokenObj.token = tokens[idx];
|
||||||
tokenObj.lastSeen = lastSeen[idx];
|
tokenObj.lastSeen = lastSeen[idx];
|
||||||
tokenObj.lastSeenISO = lastSeen[idx] ? new Date(lastSeen[idx]).toISOString() : null;
|
tokenObj.lastSeenISO = lastSeen[idx] ? new Date(lastSeen[idx]).toISOString() : null;
|
||||||
tokenObj.timestampISO = new Date(parseInt(tokenObj.timestamp, 10)).toISOString();
|
tokenObj.timestampISO = new Date(parseInt(tokenObj.timestamp, 10)).toISOString();
|
||||||
|
|
||||||
|
return tokenObj;
|
||||||
});
|
});
|
||||||
|
|
||||||
return singular ? tokenObjs[0] : tokenObjs;
|
return singular ? tokenObjs[0] : tokenObjs;
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ describe('API tokens', () => {
|
|||||||
|
|
||||||
describe('.create()', () => {
|
describe('.create()', () => {
|
||||||
it('should fail to create a token for a user that does not exist', async () => {
|
it('should fail to create a token for a user that does not exist', async () => {
|
||||||
assert.rejects(api.utils.tokens.generate({ uid: 1 }), '[[error:no-user]]');
|
assert.rejects(api.utils.tokens.generate({ uid: 1 }), { message: '[[error:no-user]]' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a token for a user that exists', async () => {
|
it('should create a token for a user that exists', async () => {
|
||||||
@@ -63,7 +63,23 @@ describe('API tokens', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should fail if you pass in invalid data', async () => {
|
it('should fail if you pass in invalid data', async () => {
|
||||||
assert.rejects(api.utils.tokens.get(), '[[error:invalid-data]]');
|
assert.rejects(api.utils.tokens.get(token), { message: '[[error:invalid-data]]' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show lastSeen and lastSeenISO as undefined/null if it is a new token', async () => {
|
||||||
|
const { lastSeen, lastSeenISO } = await api.utils.tokens.get(token);
|
||||||
|
|
||||||
|
assert.strictEqual(lastSeen, null);
|
||||||
|
assert.strictEqual(lastSeenISO, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show lastSeenISO as an ISO formatted datetime string if the token has been used', async () => {
|
||||||
|
const now = new Date();
|
||||||
|
await db.sortedSetAdd('tokens:lastSeen', now.getTime(), token);
|
||||||
|
const { lastSeen, lastSeenISO } = await api.utils.tokens.get(token);
|
||||||
|
|
||||||
|
assert.strictEqual(lastSeen, now.getTime());
|
||||||
|
assert.strictEqual(lastSeenISO, now.toISOString());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -101,6 +117,26 @@ describe('API tokens', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('.roll()', () => {
|
||||||
|
it('should invalidate the old token', async () => {
|
||||||
|
const newToken = await api.utils.tokens.roll(token);
|
||||||
|
assert(newToken);
|
||||||
|
|
||||||
|
const gets = await api.utils.tokens.get([token, newToken]);
|
||||||
|
assert.strictEqual(gets[0], null);
|
||||||
|
assert(gets[1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should change a token but leave all other metadata intact', async () => {
|
||||||
|
await api.utils.tokens.update(token, { uid: 1, description: 'foobar' });
|
||||||
|
const newToken = await api.utils.tokens.roll(token);
|
||||||
|
const tokenObj = await api.utils.tokens.get(newToken);
|
||||||
|
|
||||||
|
assert.strictEqual(parseInt(tokenObj.uid, 10), 1);
|
||||||
|
assert.strictEqual(tokenObj.description, 'foobar');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('.delete()', () => {
|
describe('.delete()', () => {
|
||||||
it('should delete a token from a system', async () => {
|
it('should delete a token from a system', async () => {
|
||||||
await api.utils.tokens.delete(token);
|
await api.utils.tokens.delete(token);
|
||||||
|
|||||||
Reference in New Issue
Block a user