From 9fa7e212d1446c35d026e19898cd87f1402963df Mon Sep 17 00:00:00 2001 From: "liad.shachoach" Date: Sat, 1 Feb 2025 17:24:36 +0200 Subject: [PATCH] using msw for tests in bitbucketCloudBranchRestriction.examples.test.ts Signed-off-by: liad.shachoach --- ...ketCloudBranchRestriction.examples.test.ts | 194 +++++++++++------- 1 file changed, 116 insertions(+), 78 deletions(-) diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudBranchRestriction.examples.test.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudBranchRestriction.examples.test.ts index 0051ee511a..0e568e3d22 100644 --- a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudBranchRestriction.examples.test.ts +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudBranchRestriction.examples.test.ts @@ -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, + }, + }); }); });