-
-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: edge active tokens cache flag removal (#7094)
## About the changes EdgeService is the only place where we use active tokens validation in bulk. By switching to validating from the cache, we no longer need a method to return all active tokens from the DB.
- Loading branch information
1 parent
6a1b6fd
commit 8ac8d87
Showing
8 changed files
with
113 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import dbInit, { type ITestDb } from '../helpers/database-init'; | ||
import getLogger from '../../fixtures/no-logger'; | ||
import { ApiTokenService } from '../../../lib/services/api-token-service'; | ||
import { createTestConfig } from '../../config/test-config'; | ||
import { | ||
ApiTokenType, | ||
type IApiToken, | ||
} from '../../../lib/types/models/api-token'; | ||
import { DEFAULT_ENV } from '../../../lib/util/constants'; | ||
import { addDays, subDays } from 'date-fns'; | ||
import type ProjectService from '../../../lib/features/project/project-service'; | ||
import { createProjectService } from '../../../lib/features'; | ||
import { EdgeService, EventService } from '../../../lib/services'; | ||
import { type IUnleashStores, TEST_AUDIT_USER } from '../../../lib/types'; | ||
|
||
let db: ITestDb; | ||
let stores: IUnleashStores; | ||
let edgeService: EdgeService; | ||
let projectService: ProjectService; | ||
|
||
beforeAll(async () => { | ||
const config = createTestConfig({ | ||
server: { baseUriPath: '/test' }, | ||
experimental: { | ||
flags: { | ||
useMemoizedActiveTokens: true, | ||
}, | ||
}, | ||
}); | ||
db = await dbInit('api_token_service_serial', getLogger); | ||
stores = db.stores; | ||
const eventService = new EventService(stores, config); | ||
const project = { | ||
id: 'test-project', | ||
name: 'Test Project', | ||
description: 'Fancy', | ||
mode: 'open' as const, | ||
defaultStickiness: 'clientId', | ||
}; | ||
const user = await stores.userStore.insert({ | ||
name: 'Some Name', | ||
email: 'test@getunleash.io', | ||
}); | ||
projectService = createProjectService(db.rawDatabase, config); | ||
|
||
await projectService.createProject(project, user, TEST_AUDIT_USER); | ||
|
||
const apiTokenService = new ApiTokenService(stores, config, eventService); | ||
edgeService = new EdgeService({ apiTokenService }, config); | ||
}); | ||
|
||
afterAll(async () => { | ||
if (db) { | ||
await db.destroy(); | ||
} | ||
}); | ||
afterEach(async () => { | ||
const tokens = await stores.apiTokenStore.getAll(); | ||
const deleteAll = tokens.map((t: IApiToken) => | ||
stores.apiTokenStore.delete(t.secret), | ||
); | ||
await Promise.all(deleteAll); | ||
}); | ||
|
||
test('should only return valid tokens', async () => { | ||
const now = Date.now(); | ||
const yesterday = subDays(now, 1); | ||
const tomorrow = addDays(now, 1); | ||
|
||
const expiredToken = await stores.apiTokenStore.insert({ | ||
tokenName: 'expired', | ||
secret: 'expired-secret', | ||
type: ApiTokenType.CLIENT, | ||
expiresAt: yesterday, | ||
projects: ['*'], | ||
environment: DEFAULT_ENV, | ||
}); | ||
|
||
const activeToken = await stores.apiTokenStore.insert({ | ||
tokenName: 'default-valid', | ||
secret: 'valid-secret', | ||
type: ApiTokenType.CLIENT, | ||
expiresAt: tomorrow, | ||
projects: ['*'], | ||
environment: DEFAULT_ENV, | ||
}); | ||
|
||
const response = await edgeService.getValidTokens([ | ||
activeToken.secret, | ||
expiredToken.secret, | ||
]); | ||
|
||
expect(response.tokens.length).toBe(1); | ||
expect(activeToken.secret).toBe(response.tokens[0].token); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters