Added test cases for gitlab:projectAccessToken:create example

Signed-off-by: parmar-abhinav <abhinav.parmar@infosys.com>
This commit is contained in:
parmar-abhinav
2024-07-21 12:29:54 +05:30
parent 70c0ac43cd
commit ef742dc56e
4 changed files with 640 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-gitlab': patch
---
Added test cases for gitlab:projectAccessToken:create example
@@ -51,6 +51,10 @@ describe('gitlab:projectAccessToken:create examples', () => {
host: 'hosted.gitlab.com',
apiBaseUrl: 'https://api.hosted.gitlab.com',
},
{
host: 'gitlab.example.com',
apiBaseUrl: 'https://api.gitlab.example.com',
},
],
},
});
@@ -192,4 +196,256 @@ describe('gitlab:projectAccessToken:create examples', () => {
expect(mockContext.output).toHaveBeenCalledWith('access_token', 'TOKEN');
});
it(`should ${examples[5].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[5].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'456',
'tokenname',
['read_repository'],
{
accessLevel: 30,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[6].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[6].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'456',
'full-access-token',
['read_repository'],
{
accessLevel: 40,
expiresAt: '2024-12-31',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[7].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[7].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'101112',
'tokenname',
['read_repository'],
{
accessLevel: 40,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[8].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[8].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'101112',
'tokenname',
['read_repository', 'read_api'],
{
accessLevel: 40,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[9].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[9].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'101112',
'tokenname',
['read_repository'],
{
accessLevel: 10,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[10].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[10].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'101112',
'tokenname',
['read_repository'],
{
accessLevel: 40,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[11].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[11].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'101112',
'tokenname',
['read_repository'],
{
accessLevel: 50,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[12].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[12].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'101112',
'no-expiry-token',
['read_repository'],
{
accessLevel: 40,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
it(`should ${examples[13].description}`, async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'personal-access-token',
username: 'gitlab-user',
});
const input = yaml.parse(examples[13].example).steps[0].input;
await action.handler({
...mockContext,
input,
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'101112',
'tokenname',
['read_repository'],
{
accessLevel: 40,
expiresAt: '2025-07-21',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'personal-access-token',
);
});
});
@@ -102,4 +102,162 @@ export const examples: TemplateExample[] = [
],
}),
},
{
description: 'Create a GitLab project access token with an access level',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '456',
accessLevel: 30,
},
},
],
}),
},
{
description: 'Create a GitLab project access token with multiple options',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '456',
accessLevel: 40,
name: 'full-access-token',
expiresAt: '2024-12-31',
},
},
],
}),
},
{
description:
'Create a GitLab project access token with a token for authorization',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '101112',
token: 'personal-access-token',
},
},
],
}),
},
{
description: 'Create a GitLab project access token with read-only scopes',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '101112',
scopes: ['read_repository', 'read_api'],
},
},
],
}),
},
{
description: 'Create a GitLab project access token with guest access level',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '101112',
accessLevel: 10,
},
},
],
}),
},
{
description:
'Create a GitLab project access token with maintainer access level',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '101112',
accessLevel: 40,
},
},
],
}),
},
{
description: 'Create a GitLab project access token with owner access level',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '101112',
accessLevel: 50,
},
},
],
}),
},
{
description:
'Create a GitLab project access token with a specified name and no expiration date',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '101112',
name: 'no-expiry-token',
},
},
],
}),
},
{
description:
'Create a GitLab project access token for a specific gitlab instance',
example: yaml.stringify({
steps: [
{
id: 'createAccessToken',
action: 'gitlab:projectAccessToken:create',
name: 'Create GitLab Project Access Token',
input: {
repoUrl: 'gitlab.example.com?repo=repo&owner=owner',
projectId: '101112',
},
},
],
}),
},
];
@@ -0,0 +1,221 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ConfigReader } from '@backstage/config';
import { ScmIntegrations } from '@backstage/integration';
import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils';
import { createGitlabProjectAccessTokenAction } from './gitlabProjectAccessTokenCreate'; // Adjust the import based on your project structure
import { DateTime } from 'luxon';
jest.mock('node-fetch');
const mockGitlabClient = {
ProjectAccessTokens: {
create: jest.fn(),
},
};
jest.mock('@gitbeaker/rest', () => ({
Gitlab: class {
constructor() {
return mockGitlabClient;
}
},
}));
describe('gitlab:projectAccessToken:create examples', () => {
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'gitlab-token',
apiBaseUrl: 'https://api.gitlab.com',
},
{
host: 'hosted.gitlab.com',
apiBaseUrl: 'https://api.hosted.gitlab.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabProjectAccessTokenAction({ integrations });
const mockContext = createMockActionContext({
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
},
});
beforeEach(() => {
jest.resetAllMocks();
});
it('should create a GitLab project access token with minimal options.', async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'gitlab-token',
username: 'gitlab-user',
});
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '987',
},
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'987',
'tokenname',
['read_repository'],
{
accessLevel: 40,
expiresAt: DateTime.now().plus({ days: 365 }).toISODate()!,
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'gitlab-token',
);
});
it('should create a GitLab project access token with custom scopes.', async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'gitlab-token',
username: 'gitlab-user',
});
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '987',
scopes: ['read_registry', 'write_repository'],
},
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'987',
'tokenname',
['read_registry', 'write_repository'],
{
accessLevel: 40,
expiresAt: DateTime.now().plus({ days: 365 }).toISODate()!,
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'gitlab-token',
);
});
it('should create a GitLab project access token with a specified name.', async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'gitlab-token',
username: 'gitlab-user',
});
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '2110',
name: 'token',
},
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'2110',
'token',
['read_repository'],
{
accessLevel: 40,
expiresAt: DateTime.now().plus({ days: 365 }).toISODate()!,
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'gitlab-token',
);
});
it('should create a GitLab project access token with a numeric project ID.', async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'gitlab-token',
username: 'gitlab-user',
});
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: 23,
},
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
23,
'tokenname',
['read_repository'],
{
accessLevel: 40,
expiresAt: DateTime.now().plus({ days: 365 }).toISODate()!,
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'gitlab-token',
);
});
it('should create a GitLab project access token with a specified expired Date.', async () => {
mockGitlabClient.ProjectAccessTokens.create.mockResolvedValue({
token: 'gitlab-token',
username: 'gitlab-user',
});
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
expiresAt: '1999-07-14',
},
});
expect(mockGitlabClient.ProjectAccessTokens.create).toHaveBeenCalledWith(
'123',
'tokenname',
['read_repository'],
{
accessLevel: 40,
expiresAt: '1999-07-14',
},
);
expect(mockContext.output).toHaveBeenCalledWith(
'access_token',
'gitlab-token',
);
});
});