Fix tests

Signed-off-by: Thorsten Lanfer <tlanfer@gmail.com>
This commit is contained in:
Thorsten Lanfer
2024-09-12 13:29:24 +02:00
parent 1fe525a15d
commit cc8d3eafe1
3 changed files with 41 additions and 90 deletions
@@ -27,7 +27,8 @@ const mockGitlabClient = {
create: jest.fn(),
},
};
jest.mock('@gitbeaker/node', () => ({
jest.mock('@gitbeaker/rest', () => ({
Gitlab: class {
constructor() {
return mockGitlabClient;
@@ -38,8 +39,8 @@ jest.mock('@gitbeaker/node', () => ({
describe('gitlab:group:ensureExists', () => {
const mockContext = createMockActionContext();
afterEach(() => {
jest.resetAllMocks();
beforeEach(() => {
jest.clearAllMocks();
});
it(`Should ${examples[0].description}`, async () => {
@@ -55,7 +56,7 @@ describe('gitlab:group:ensureExists', () => {
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
apiBaseUrl: 'https://gitlab.com/api/v4',
},
],
},
@@ -114,7 +115,7 @@ describe('gitlab:group:ensureExists', () => {
'group2',
'group2',
{
parent_id: 1,
parentId: 1,
},
);
@@ -161,7 +162,7 @@ describe('gitlab:group:ensureExists', () => {
'group3',
'group3',
{
parent_id: 2,
parentId: 2,
},
);
@@ -25,19 +25,37 @@ const mockGitlabClient = {
create: jest.fn(),
},
};
// const mockGitlabApi = jest.fn().mockReturnValue(mockGitlabClient);
jest.mock('../util', () => ({
getClient: () => mockGitlabClient,
jest.mock('@gitbeaker/rest', () => ({
Gitlab: class {
constructor() {
return mockGitlabClient;
}
},
}));
describe('gitlab:group:ensureExists', () => {
const mockContext = createMockActionContext();
afterEach(() => {
beforeEach(() => {
jest.clearAllMocks();
});
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://gitlab.com/api/v4',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabGroupEnsureExistsAction({ integrations });
const mockContext = createMockActionContext();
it('should create a new group if it does not exists', async () => {
mockGitlabClient.Groups.search.mockResolvedValue([
{
@@ -55,25 +73,10 @@ describe('gitlab:group:ensureExists', () => {
full_path: 'foo/bar',
});
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabGroupEnsureExistsAction({ integrations });
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com',
repoUrl: 'gitlab.com?repo=repo&owner=owner',
path: ['foo', 'bar'],
},
});
@@ -101,25 +104,10 @@ describe('gitlab:group:ensureExists', () => {
},
]);
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabGroupEnsureExistsAction({ integrations });
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com',
repoUrl: 'gitlab.com?repo=repo&owner=owner',
path: ['foo', 'bar'],
},
});
@@ -130,26 +118,11 @@ describe('gitlab:group:ensureExists', () => {
});
it('should not call API on dryRun', async () => {
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabGroupEnsureExistsAction({ integrations });
await action.handler({
...mockContext,
isDryRun: true,
input: {
repoUrl: 'gitlab.com',
repoUrl: 'gitlab.com?repo=repo&owner=owner',
path: ['foo', 'bar'],
},
});
@@ -168,23 +141,10 @@ describe('gitlab:group:ensureExists', () => {
},
]);
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabGroupEnsureExistsAction({ integrations });
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com',
repoUrl: 'gitlab.com?repo=repo&owner=owner',
path: ['foobar'],
},
});
@@ -204,23 +164,10 @@ describe('gitlab:group:ensureExists', () => {
},
]);
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabGroupEnsureExistsAction({ integrations });
await action.handler({
...mockContext,
input: {
repoUrl: 'gitlab.com',
repoUrl: 'gitlab.com?repo=repo&owner=owner',
path: ['foobar'],
token: 'mysecrettoken',
},
@@ -19,7 +19,7 @@ import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
import { GroupSchema } from '@gitbeaker/core/dist/types/resources/Groups';
import { z } from 'zod';
import commonGitlabConfig from '../commonGitlabConfig';
import { getClient } from '../util';
import { getClient, parseRepoUrl } from '../util';
import { examples } from './gitlabGroupEnsureExists.examples';
/**
@@ -60,7 +60,10 @@ export const createGitlabGroupEnsureExistsAction = (options: {
}
const { token, repoUrl, path } = ctx.input;
const api = getClient({ host: repoUrl, token, integrations });
const { host } = parseRepoUrl(repoUrl, integrations);
const api = getClient({ host, integrations, token });
let currentPath: string | null = null;
let parent: GroupSchema | null = null;