From e5475d52000d6ec64331f6406c8f20879860e450 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Fri, 26 Jan 2024 18:26:15 +0100 Subject: [PATCH] fix: small fixes --- migrations/create-tables.js.sql | 2 +- seeds/test/index.js | 6 +---- src/lib/http/auth.ts | 10 ++++--- test/tests/integration/ratelimit.test.ts | 8 ++++++ test/tests/unit/auth.test.ts | 34 ++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/migrations/create-tables.js.sql b/migrations/create-tables.js.sql index dd38c971..34f71f5e 100644 --- a/migrations/create-tables.js.sql +++ b/migrations/create-tables.js.sql @@ -1,6 +1,6 @@ CREATE TABLE IF NOT EXISTS directus_users ( id CHAR(36), - github VARCHAR(255) + github_username VARCHAR(255) ); CREATE TABLE IF NOT EXISTS gp_adopted_probes ( diff --git a/seeds/test/index.js b/seeds/test/index.js index 080820c7..b80e38e9 100644 --- a/seeds/test/index.js +++ b/seeds/test/index.js @@ -1,6 +1,2 @@ -export const seed = async (db) => { - await db('gp_tokens').insert({ - user_created: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', - value: '7emhYIar8eLtwAAjyXUn+h3Cj+Xc9BQcLMC6JAX9fHQ=', - }); +export const seed = async () => { }; diff --git a/src/lib/http/auth.ts b/src/lib/http/auth.ts index 75f1c63c..adb7d57f 100644 --- a/src/lib/http/auth.ts +++ b/src/lib/http/auth.ts @@ -9,6 +9,8 @@ export const USERS_TABLE = 'directus_users'; const logger = scopedLogger('auth'); +const TOKEN_TTL = 2 * 60 * 1000; + export type Token = { user_created: string, value: string, @@ -22,8 +24,8 @@ type Row = Omit & { } export class Auth { - private validTokens = new TTLCache({ ttl: 2 * 60 * 1000 }); - private invalidTokens = new TTLCache({ ttl: 2 * 60 * 1000 }); + private validTokens = new TTLCache({ ttl: TOKEN_TTL }); + private invalidTokens = new TTLCache({ ttl: TOKEN_TTL }); constructor (private readonly sql: Knex) {} scheduleSync () { @@ -36,8 +38,8 @@ export class Auth { async syncTokens () { const tokens = await this.fetchTokens(); - const newValidTokens = new TTLCache({ ttl: 2 * 60 * 1000 }); - const newInvalidTokens = new TTLCache({ ttl: 2 * 60 * 1000 }); + const newValidTokens = new TTLCache({ ttl: TOKEN_TTL }); + const newInvalidTokens = new TTLCache({ ttl: TOKEN_TTL }); tokens.forEach((token) => { if (token.expire && this.isExpired(token.expire)) { diff --git a/test/tests/integration/ratelimit.test.ts b/test/tests/integration/ratelimit.test.ts index 32f4710c..f7971730 100644 --- a/test/tests/integration/ratelimit.test.ts +++ b/test/tests/integration/ratelimit.test.ts @@ -5,6 +5,8 @@ import { expect } from 'chai'; import { getTestServer, addFakeProbe, deleteFakeProbes } from '../../utils/server.js'; import nockGeoIpProviders from '../../utils/nock-geo-ip.js'; import { anonymousRateLimiter, authenticatedRateLimiter } from '../../../src/lib/rate-limiter.js'; +import { client } from '../../../src/lib/sql/client.js'; +import { GP_TOKENS_TABLE } from '../../../src/lib/http/auth.js'; describe('rate limiter', () => { let app: Server; @@ -29,6 +31,11 @@ describe('rate limiter', () => { probe1.emit('probe:status:update', 'ready'); probe2.emit('probe:status:update', 'ready'); + + await client(GP_TOKENS_TABLE).insert({ + user_created: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', + value: '7emhYIar8eLtwAAjyXUn+h3Cj+Xc9BQcLMC6JAX9fHQ=', + }); }); @@ -39,6 +46,7 @@ describe('rate limiter', () => { after(async () => { await deleteFakeProbes(); + await client(GP_TOKENS_TABLE).where({ value: '7emhYIar8eLtwAAjyXUn+h3Cj+Xc9BQcLMC6JAX9fHQ=' }).delete(); }); describe('headers', () => { diff --git a/test/tests/unit/auth.test.ts b/test/tests/unit/auth.test.ts index f4cc9a42..a228c5f0 100644 --- a/test/tests/unit/auth.test.ts +++ b/test/tests/unit/auth.test.ts @@ -93,4 +93,38 @@ describe('Auth', () => { expect(user).to.equal('user1'); expect(selectStub.callCount).to.equal(1); }); + + it('should not update date_last_used if it is actual', async () => { + const auth = new Auth(sqlStub as unknown as Knex); + + selectStub.resolves([{ + value: 'aGmWPCV1JN/qmYl27g8VpBjhCmTpbFcbdrWgTvEtqo4=', + user_created: 'user1', + date_last_used: new Date(), + }]); + + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + + expect(updateStub.callCount).to.equal(0); + }); + + it('should update date_last_used only once', async () => { + const auth = new Auth(sqlStub as unknown as Knex); + + selectStub.resolves([{ + value: 'aGmWPCV1JN/qmYl27g8VpBjhCmTpbFcbdrWgTvEtqo4=', + user_created: 'user1', + }]); + + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com'); + + expect(updateStub.args[0]).to.deep.equal([{ date_last_used: new Date() }]); + expect(updateStub.callCount).to.equal(1); + }); });