Skip to content

Commit

Permalink
refactor: add countProjectTokens method to api token store
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Nov 6, 2024
1 parent 1b568d1 commit ccd0d95
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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;
}
}
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;
}
}

0 comments on commit ccd0d95

Please sign in to comment.