Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: introduce countProjectTokens method on ApiTokenStore #8674

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/db/api-token-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,12 @@ export class ApiTokenStore implements IApiTokenStore {
activeLegacyTokens,
};
}

async countProjectTokens(projectId: string): Promise<number> {
const count = await this.db(API_LINK_TABLE)
.where({ project: projectId })
.count()
.first();
return Number(count?.count ?? 0);
}
}
12 changes: 1 addition & 11 deletions src/lib/features/project-status/project-status-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,7 @@ export class ProjectStatusService {
] = await Promise.all([
this.projectStore.getConnectedEnvironmentCountForProject(projectId),
this.projectStore.getMembersCountByProject(projectId),
this.apiTokenStore
.getAll()
.then(
(tokens) =>
tokens.filter(
(token) =>
token.project === projectId ||
token.projects.includes(projectId),
).length,
),

this.apiTokenStore.countProjectTokens(projectId),
this.segmentStore.getProjectSegmentCount(projectId),
this.eventStore.getProjectRecentEventActivity(projectId),
]);
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/stores/api-token-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface IApiTokenStore extends Store<IApiToken, string> {
legacyTokens: number;
activeLegacyTokens: number;
}>;
countProjectTokens(projectId: string): Promise<number>;
}
44 changes: 44 additions & 0 deletions src/test/e2e/stores/api-token-store.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import dbInit, { type ITestDb } from '../helpers/database-init';
import getLogger from '../../fixtures/no-logger';
import type { IUnleashStores } from '../../../lib/types';
import { ApiTokenType } from '../../../lib/types/models/api-token';
import { randomId } from '../../../lib/util';

let stores: IUnleashStores;
let db: ITestDb;
Expand Down Expand Up @@ -182,3 +183,46 @@ describe('count deprecated tokens', () => {
});
});
});

describe('count project tokens', () => {
test('counts only tokens belonging to the specified project', async () => {
const project = await stores.projectStore.create({
id: randomId(),
name: 'project A',
});

const store = stores.apiTokenStore;
await store.insert({
secret: `default:default.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: ['default'],
tokenName: 'token1',
});
await store.insert({
secret: `*:*.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: ['*'],
tokenName: 'token2',
});

await store.insert({
secret: `${project.id}:default.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: [project.id],
tokenName: 'token3',
});

await store.insert({
secret: `[]:default.${randomId()}`,
environment: 'default',
type: ApiTokenType.CLIENT,
projects: [project.id, 'default'],
tokenName: 'token4',
});

expect(await store.countProjectTokens(project.id)).toBe(2);
});
});
4 changes: 4 additions & 0 deletions src/test/fixtures/fake-api-token-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ export default class FakeApiTokenStore
activeLegacyTokens: 0,
};
}

async countProjectTokens(): Promise<number> {
return 0;
}
}