From 98458a1056e30501a170bc027e6d29c00f0d5d22 Mon Sep 17 00:00:00 2001 From: Ben Irvin Date: Thu, 10 Nov 2022 17:36:30 +0100 Subject: [PATCH 1/3] lifespan has type biginteger --- packages/core/admin/server/content-types/api-token.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/admin/server/content-types/api-token.js b/packages/core/admin/server/content-types/api-token.js index 90c3e40c2b..77a0b2f0ac 100644 --- a/packages/core/admin/server/content-types/api-token.js +++ b/packages/core/admin/server/content-types/api-token.js @@ -67,7 +67,7 @@ module.exports = { required: false, }, lifespan: { - type: 'integer', + type: 'biginteger', configurable: false, required: false, }, From 06a4a0265cccfda22914f97e92bb6fef9dcb6b27 Mon Sep 17 00:00:00 2001 From: Ben Irvin Date: Thu, 10 Nov 2022 17:44:51 +0100 Subject: [PATCH 2/3] add api integration tests for 30 and 90 day tokens --- .../tests/admin-api-token-crud.test.api.js | 84 ++++++++++++++++++- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/packages/core/admin/server/tests/admin-api-token-crud.test.api.js b/packages/core/admin/server/tests/admin-api-token-crud.test.api.js index 6a6045aeac..912f52e3b1 100644 --- a/packages/core/admin/server/tests/admin-api-token-crud.test.api.js +++ b/packages/core/admin/server/tests/admin-api-token-crud.test.api.js @@ -177,12 +177,12 @@ describe('Admin API Token v2 CRUD (api)', () => { }); }); - test('Creates a token with a lifespan', async () => { + test('Creates a token with a 7-day lifespan', async () => { const now = Date.now(); jest.useFakeTimers('modern').setSystemTime(now); const body = { - name: 'api-token_tests-lifespan', + name: 'api-token_tests-lifespan7', description: 'api-token_tests-description', type: 'read-only', lifespan: 7 * 24 * 60 * 60 * 1000, // 7 days @@ -206,7 +206,85 @@ describe('Admin API Token v2 CRUD (api)', () => { lastUsedAt: null, updatedAt: expect.toBeISODate(), expiresAt: expect.toBeISODate(), - lifespan: body.lifespan, + lifespan: String(body.lifespan), + }); + + // Datetime stored in some databases may lose ms accuracy, so allow a range of 2 seconds for timing edge cases + expect(Date.parse(res.body.data.expiresAt)).toBeGreaterThan(now + body.lifespan - 2000); + expect(Date.parse(res.body.data.expiresAt)).toBeLessThan(now + body.lifespan + 2000); + + jest.useRealTimers(); + }); + + test('Creates a token with a 30-day lifespan', async () => { + const now = Date.now(); + jest.useFakeTimers('modern').setSystemTime(now); + + const body = { + name: 'api-token_tests-lifespan30', + description: 'api-token_tests-description', + type: 'read-only', + lifespan: 30 * 24 * 60 * 60 * 1000, // 7 days + }; + + const res = await rq({ + url: '/admin/api-tokens', + method: 'POST', + body, + }); + + expect(res.statusCode).toBe(201); + expect(res.body.data).toStrictEqual({ + accessKey: expect.any(String), + name: body.name, + permissions: [], + description: body.description, + type: body.type, + id: expect.any(Number), + createdAt: expect.toBeISODate(), + lastUsedAt: null, + updatedAt: expect.toBeISODate(), + expiresAt: expect.toBeISODate(), + lifespan: String(body.lifespan), + }); + + // Datetime stored in some databases may lose ms accuracy, so allow a range of 2 seconds for timing edge cases + expect(Date.parse(res.body.data.expiresAt)).toBeGreaterThan(now + body.lifespan - 2000); + expect(Date.parse(res.body.data.expiresAt)).toBeLessThan(now + body.lifespan + 2000); + + jest.useRealTimers(); + }); + + test('Creates a token with a 90-day lifespan', async () => { + const now = Date.now(); + jest.useFakeTimers('modern').setSystemTime(now); + + const body = { + name: 'api-token_tests-lifespan90', + description: 'api-token_tests-description', + type: 'read-only', + lifespan: 90 * 24 * 60 * 60 * 1000, // 90 days + }; + + const res = await rq({ + url: '/admin/api-tokens', + method: 'POST', + body, + }); + + expect(res.statusCode).toBe(201); + expect(res.body.data).toStrictEqual({ + accessKey: expect.any(String), + name: body.name, + permissions: [], + description: body.description, + type: body.type, + id: expect.any(Number), + createdAt: expect.toBeISODate(), + lastUsedAt: null, + updatedAt: expect.toBeISODate(), + expiresAt: expect.toBeISODate(), + lifespan: String(body.lifespan), }); // Datetime stored in some databases may lose ms accuracy, so allow a range of 2 seconds for timing edge cases From 7507b9f5dd9f302a24cabfd0dfd0f6c95672832a Mon Sep 17 00:00:00 2001 From: Ben Irvin Date: Thu, 10 Nov 2022 18:10:48 +0100 Subject: [PATCH 3/3] fix comment --- .../core/admin/server/tests/admin-api-token-crud.test.api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/admin/server/tests/admin-api-token-crud.test.api.js b/packages/core/admin/server/tests/admin-api-token-crud.test.api.js index 912f52e3b1..7e1122252a 100644 --- a/packages/core/admin/server/tests/admin-api-token-crud.test.api.js +++ b/packages/core/admin/server/tests/admin-api-token-crud.test.api.js @@ -224,7 +224,7 @@ describe('Admin API Token v2 CRUD (api)', () => { name: 'api-token_tests-lifespan30', description: 'api-token_tests-description', type: 'read-only', - lifespan: 30 * 24 * 60 * 60 * 1000, // 7 days + lifespan: 30 * 24 * 60 * 60 * 1000, // 30 days }; const res = await rq({