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

Updating Access Token API wrapper #3554

Merged
merged 4 commits into from Mar 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/core/src/resources/GroupAccessTokens.ts
@@ -1,6 +1,10 @@
import type { BaseResourceOptions } from '@gitbeaker/requester-utils';
import { ResourceAccessTokens } from '../templates';
import type { AccessTokenSchema, AccessTokenScopes } from '../templates/ResourceAccessTokens';
import type {
AccessTokenExposedSchema,
AccessTokenSchema,
AccessTokenScopes,
} from '../templates/ResourceAccessTokens';
import type {
GitlabAPIResponse,
PaginationRequestOptions,
Expand All @@ -20,22 +24,28 @@ export interface GroupAccessTokens<C extends boolean = false> extends ResourceAc
groupId: string | number,
name: string,
scopes: AccessTokenScopes[],
expiresAt: string,
options?: {
accessLevel?: Exclude<
AccessLevel,
AccessLevel.MINIMAL_ACCESS | AccessLevel.NO_ACCESS | AccessLevel.ADMIN
>;
expiresAt?: string;
} & Sudo &
ShowExpanded<E>,
): Promise<GitlabAPIResponse<AccessTokenSchema, C, E, void>>;
): Promise<GitlabAPIResponse<AccessTokenExposedSchema, C, E, void>>;

revoke<E extends boolean = false>(
groupId: string | number,
tokenId: string | number,
options?: Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<void, C, E, void>>;

rotate<E extends boolean = false>(
groupId: string | number,
tokenId: string | number,
options?: Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<AccessTokenExposedSchema, C, E, void>>;

show<E extends boolean = false>(
groupId: string | number,
tokenId: string | number,
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/resources/ProjectAccessTokens.ts
@@ -1,6 +1,10 @@
import type { BaseResourceOptions } from '@gitbeaker/requester-utils';
import { ResourceAccessTokens } from '../templates';
import type { AccessTokenSchema, AccessTokenScopes } from '../templates/ResourceAccessTokens';
import type {
AccessTokenExposedSchema,
AccessTokenSchema,
AccessTokenScopes,
} from '../templates/ResourceAccessTokens';
import type {
GitlabAPIResponse,
PaginationRequestOptions,
Expand All @@ -20,22 +24,28 @@ export interface ProjectAccessTokens<C extends boolean = false> extends Resource
projectId: string | number,
name: string,
scopes: AccessTokenScopes[],
expiresAt: string,
options?: {
accessLevel?: Exclude<
AccessLevel,
AccessLevel.MINIMAL_ACCESS | AccessLevel.NO_ACCESS | AccessLevel.ADMIN
>;
expiresAt?: string;
} & Sudo &
ShowExpanded<E>,
): Promise<GitlabAPIResponse<AccessTokenSchema, C, E, void>>;
): Promise<GitlabAPIResponse<AccessTokenExposedSchema, C, E, void>>;

revoke<E extends boolean = false>(
projectId: string | number,
tokenId: string | number,
options?: Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<void, C, E, void>>;

rotate<E extends boolean = false>(
projectId: string | number,
tokenId: string | number,
options?: Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<AccessTokenExposedSchema, C, E, void>>;

show<E extends boolean = false>(
projectId: string | number,
tokenId: string | number,
Expand Down
36 changes: 28 additions & 8 deletions packages/core/src/templates/ResourceAccessTokens.ts
Expand Up @@ -32,7 +32,10 @@ export interface AccessTokenSchema extends Record<string, unknown> {
AccessLevel,
AccessLevel.NO_ACCESS | AccessLevel.MINIMAL_ACCESS | AccessLevel.ADMIN
>;
token?: string;
}

export interface AccessTokenExposedSchema extends AccessTokenSchema {
token: string;
}

export class ResourceAccessTokens<C extends boolean = false> extends BaseResource<C> {
Expand All @@ -55,20 +58,25 @@ export class ResourceAccessTokens<C extends boolean = false> extends BaseResourc
resourceId: string | number,
name: string,
scopes: AccessTokenScopes[],
expiresAt: string,
options?: {
accessLevel?: Exclude<
AccessLevel,
AccessLevel.NO_ACCESS | AccessLevel.MINIMAL_ACCESS | AccessLevel.ADMIN
>;
expiresAt?: string;
} & Sudo &
ShowExpanded<E>,
): Promise<GitlabAPIResponse<AccessTokenSchema, C, E, void>> {
return RequestHelper.post<AccessTokenSchema>()(this, endpoint`${resourceId}/access_tokens`, {
name,
scopes,
...options,
});
): Promise<GitlabAPIResponse<AccessTokenExposedSchema, C, E, void>> {
return RequestHelper.post<AccessTokenExposedSchema>()(
this,
endpoint`${resourceId}/access_tokens`,
{
name,
scopes,
expiresAt,
...options,
},
);
}

revoke<E extends boolean = false>(
Expand All @@ -79,6 +87,18 @@ export class ResourceAccessTokens<C extends boolean = false> extends BaseResourc
return RequestHelper.del()(this, endpoint`${resourceId}/access_tokens/${tokenId}`, options);
}

rotate<E extends boolean = false>(
resourceId: string | number,
tokenId: string | number,
options?: { expiresAt?: string } & Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<AccessTokenExposedSchema, C, E, void>> {
return RequestHelper.post<AccessTokenExposedSchema>()(
this,
endpoint`${resourceId}/access_tokens/${tokenId}/rotate`,
options,
);
}

show<E extends boolean = false>(
resourceId: string | number,
tokenId: string | number,
Expand Down
42 changes: 40 additions & 2 deletions packages/core/test/unit/templates/ResourceAccessTokens.ts
Expand Up @@ -43,42 +43,80 @@ describe('ResourceAccessTokens.all', () => {

describe('ResourceAccessTokens.create', () => {
it('should call the correct url for creating access token with a string identifer', async () => {
await service.create('5', 'test', ['api']);
await service.create('5', 'test', ['api'], '2021-01-31');

expect(RequestHelper.post()).toHaveBeenCalledWith(service, '5/access_tokens', {
name: 'test',
scopes: ['api'],
expiresAt: '2021-01-31',
});
});

it('should call the correct url for creating access token with a number identifer', async () => {
await service.create(5, 'test', ['api']);
await service.create(5, 'test', ['api'], '2021-01-31');

expect(RequestHelper.post()).toHaveBeenCalledWith(service, '5/access_tokens', {
name: 'test',
scopes: ['api'],
expiresAt: '2021-01-31',
});
});
});

describe('ResourceAccessTokens.show', () => {
it('should call the correct url with a string identifer', async () => {
await service.show('5', '6');

expect(RequestHelper.get()).toHaveBeenCalledWith(service, '5/access_tokens/6', undefined);
});

it('should call the correct url for creating access token with a number identifer', async () => {
await service.show(5, 6);

expect(RequestHelper.get()).toHaveBeenCalledWith(service, '5/access_tokens/6', undefined);
});
});

describe('ResourceAccessTokens.rotate', () => {
it('should call the correct url with a string identifer', async () => {
await service.rotate('5', '6');

expect(RequestHelper.post()).toHaveBeenCalledWith(
service,
'5/access_tokens/6/rotate',
undefined,
);
});

it('should call the correct url for creating access token with a number identifer', async () => {
await service.rotate(5, 6);

expect(RequestHelper.post()).toHaveBeenCalledWith(
service,
'5/access_tokens/6/rotate',
undefined,
);
});

it('should take options', async () => {
const options = { expiresAt: '2021-01-31' };

await service.rotate('5', '6', options);

expect(RequestHelper.post()).toHaveBeenCalledWith(service, '5/access_tokens/6/rotate', options);
});
});

describe('ResourceAccessTokens.revoke', () => {
it('should call the correct url with a string identifer', async () => {
await service.revoke('5', '6');

expect(RequestHelper.del()).toHaveBeenCalledWith(service, '5/access_tokens/6', undefined);
});

it('should call the correct url for creating access token with a number identifer', async () => {
await service.revoke(5, 6);

expect(RequestHelper.del()).toHaveBeenCalledWith(service, '5/access_tokens/6', undefined);
});
});