using msw for tests in bitbucketCloudBranchRestriction.examples.test.ts

Signed-off-by: liad.shachoach <liad.shachoach@controlup.com>
This commit is contained in:
liad.shachoach
2025-02-01 17:24:36 +02:00
parent 7ae762de86
commit 9fa7e212d1
@@ -15,6 +15,9 @@
*/
import { createBitbucketCloudBranchRestrictionAction } from './bitbucketCloudBranchRestriction';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { registerMswTestHooks } from '@backstage/backend-test-utils';
import { ScmIntegrations } from '@backstage/integration';
import { ConfigReader } from '@backstage/config';
import yaml from 'yaml';
@@ -39,100 +42,135 @@ describe('bitbucketCloud:branchRestriction:create', () => {
input: {
repoUrl:
'bitbucket.org?workspace=workspace&project=project&repo=repo&project=project',
kind: 'push',
},
});
beforeEach(() => {
global.fetch = jest.fn().mockImplementation(() =>
Promise.resolve({
status: 201,
json: () =>
Promise.resolve({
status: 201,
}),
}),
);
});
afterEach(() => {
jest.clearAllMocks();
});
const server = setupServer();
registerMswTestHooks(server);
it('should restrict push to the main branch, except for the defined uuids, and the admins group', async () => {
const input = yaml.parse(examples[0].example).steps[0].input;
const ctx = Object.assign({}, mockContext, { input });
await action.handler(ctx);
expect(global.fetch).toHaveBeenCalledWith(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
expect.objectContaining({
body: JSON.stringify({
branch_match_kind: 'branching_model',
users: [{ uuid: '{a-b-c-d}' }, { uuid: '{e-f-g-h}' }],
groups: [{ slug: 'admins' }],
kind: 'push',
branch_type: 'development',
}),
method: 'POST',
}),
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
(req, res, ctx) => {
req.json().then(data => {
expect(data).toEqual({
branch_match_kind: 'branching_model',
branch_type: 'development',
users: [{ uuid: '{a-b-c-d}' }, { uuid: '{e-f-g-h}' }],
groups: [{ slug: 'admins' }],
kind: 'push',
});
});
return res(
ctx.status(201),
ctx.set('Content-Type', 'application/json'),
ctx.json({}),
);
},
),
);
const input = yaml.parse(examples[0].example).steps[0].input;
await action.handler({
...mockContext,
input: {
...mockContext.input,
...input,
},
});
});
it('should restrict push to the main branch, except for the admins group', async () => {
const input = yaml.parse(examples[1].example).steps[0].input;
const ctx = Object.assign({}, mockContext, { input });
await action.handler(ctx);
expect(global.fetch).toHaveBeenCalledWith(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
expect.objectContaining({
body: JSON.stringify({
branch_match_kind: 'branching_model',
users: [],
groups: [{ slug: 'admins' }],
kind: 'push',
branch_type: 'development',
}),
method: 'POST',
}),
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
(req, res, ctx) => {
req.json().then(data => {
expect(data).toEqual({
branch_match_kind: 'branching_model',
users: [],
groups: [{ slug: 'admins' }],
kind: 'push',
branch_type: 'development',
});
});
return res(
ctx.status(201),
ctx.set('Content-Type', 'application/json'),
ctx.json({}),
);
},
),
);
const input = yaml.parse(examples[1].example).steps[0].input;
await action.handler({
...mockContext,
input: {
...mockContext.input,
...input,
},
});
});
it('should require passing builds to merge to branches matching a pattern test-feature/*', async () => {
const input = yaml.parse(examples[2].example).steps[0].input;
const ctx = Object.assign({}, mockContext, { input });
await action.handler(ctx);
expect(global.fetch).toHaveBeenCalledWith(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
expect.objectContaining({
body: JSON.stringify({
branch_match_kind: 'glob',
kind: 'require_passing_builds_to_merge',
value: 1,
pattern: 'test-feature/*',
}),
method: 'POST',
}),
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
(req, res, ctx) => {
req.json().then(data => {
expect(data).toEqual({
branch_match_kind: 'glob',
kind: 'require_passing_builds_to_merge',
value: 1,
pattern: 'test-feature/*',
});
});
return res(
ctx.status(201),
ctx.set('Content-Type', 'application/json'),
ctx.json({}),
);
},
),
);
const input = yaml.parse(examples[2].example).steps[0].input;
await action.handler({
...mockContext,
input: {
...mockContext.input,
...input,
},
});
});
it('should require approvals to merge to branches matching a pattern test-feature/*', async () => {
const input = yaml.parse(examples[3].example).steps[0].input;
const ctx = Object.assign({}, mockContext, { input });
await action.handler(ctx);
expect(global.fetch).toHaveBeenCalledWith(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
expect.objectContaining({
body: JSON.stringify({
branch_match_kind: 'glob',
kind: 'require_approvals_to_merge',
value: 1,
pattern: 'test-feature/*',
}),
method: 'POST',
}),
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions',
(req, res, ctx) => {
req.json().then(data => {
expect(data).toEqual({
branch_match_kind: 'glob',
kind: 'require_approvals_to_merge',
value: 1,
pattern: 'test-feature/*',
});
});
return res(
ctx.status(201),
ctx.set('Content-Type', 'application/json'),
ctx.json({}),
);
},
),
);
const input = yaml.parse(examples[3].example).steps[0].input;
await action.handler({
...mockContext,
input: {
...mockContext.input,
...input,
},
});
});
});