From 939d3ec2e0b80b59c29e607de7ade259545cfbdc Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Thu, 12 Jan 2023 14:36:25 +0100 Subject: [PATCH 1/3] backend-test-utils: Adds `mockIdentityFactory` to `startTestbackend` Signed-off-by: Johan Haals --- .changeset/pink-parents-chew.md | 5 ++ packages/backend-test-utils/package.json | 2 +- .../implementations/mockIdentityService.ts | 51 +++++++++++++++++++ .../src/next/wiring/TestBackend.ts | 2 + yarn.lock | 2 +- 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 .changeset/pink-parents-chew.md create mode 100644 packages/backend-test-utils/src/next/implementations/mockIdentityService.ts diff --git a/.changeset/pink-parents-chew.md b/.changeset/pink-parents-chew.md new file mode 100644 index 0000000000..8edd4bea2f --- /dev/null +++ b/.changeset/pink-parents-chew.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-test-utils': patch +--- + +Added `mockIdentityFactory` to list of default factories for `startTestBackend`. diff --git a/packages/backend-test-utils/package.json b/packages/backend-test-utils/package.json index 725be0d905..385d79fca3 100644 --- a/packages/backend-test-utils/package.json +++ b/packages/backend-test-utils/package.json @@ -39,10 +39,10 @@ "@backstage/backend-plugin-api": "workspace:^", "@backstage/cli": "workspace:^", "@backstage/config": "workspace:^", + "@backstage/plugin-auth-node": "workspace:^", "@backstage/types": "workspace:^", "better-sqlite3": "^8.0.0", "express": "^4.17.1", - "express-promise-router": "^4.1.0", "knex": "^2.0.0", "msw": "^0.49.0", "mysql2": "^2.2.5", diff --git a/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts b/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts new file mode 100644 index 0000000000..1588134737 --- /dev/null +++ b/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2023 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 { + coreServices, + createServiceFactory, + IdentityService, +} from '@backstage/backend-plugin-api'; +import { + IdentityApiGetIdentityRequest, + BackstageIdentityResponse, +} from '@backstage/plugin-auth-node'; + +class MockIdentityServiceImpl implements IdentityService { + getIdentity( + _options: IdentityApiGetIdentityRequest, + ): Promise { + return Promise.resolve({ + token: 'mock-token', + identity: { + type: 'user', + userEntityRef: 'user:default/mockUser', + ownershipEntityRefs: [], + }, + }); + } +} + +/** @public */ +export const mockIdentityFactory = createServiceFactory({ + service: coreServices.identity, + deps: {}, + async factory() { + const service = new MockIdentityServiceImpl(); + return async () => { + return service; + }; + }, +}); diff --git a/packages/backend-test-utils/src/next/wiring/TestBackend.ts b/packages/backend-test-utils/src/next/wiring/TestBackend.ts index 21650125d3..936e850659 100644 --- a/packages/backend-test-utils/src/next/wiring/TestBackend.ts +++ b/packages/backend-test-utils/src/next/wiring/TestBackend.ts @@ -46,6 +46,7 @@ import { mockRootLoggerService } from '../implementations/mockRootLoggerService' import { mockTokenManagerFactory } from '../implementations/mockTokenManagerService'; import { ConfigReader } from '@backstage/config'; import express from 'express'; +import { mockIdentityFactory } from '../implementations/mockIdentityService'; /** @alpha */ export interface TestBackendOptions< @@ -90,6 +91,7 @@ const defaultServiceFactories = [ loggerFactory(), mockConfigFactory(), mockRootLoggerService(), + mockIdentityFactory(), mockTokenManagerFactory(), permissionsFactory(), rootLifecycleFactory(), diff --git a/yarn.lock b/yarn.lock index d6822cad9d..e8f1a31b99 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3567,11 +3567,11 @@ __metadata: "@backstage/backend-plugin-api": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" + "@backstage/plugin-auth-node": "workspace:^" "@backstage/types": "workspace:^" "@types/supertest": ^2.0.8 better-sqlite3: ^8.0.0 express: ^4.17.1 - express-promise-router: ^4.1.0 knex: ^2.0.0 msw: ^0.49.0 mysql2: ^2.2.5 From b14238f7daaaea79e3977646d127658ac43bf41c Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Thu, 12 Jan 2023 14:39:17 +0100 Subject: [PATCH 2/3] remove export and changeset Signed-off-by: Johan Haals --- .changeset/pink-parents-chew.md | 5 ----- .../src/next/implementations/mockIdentityService.ts | 1 - 2 files changed, 6 deletions(-) delete mode 100644 .changeset/pink-parents-chew.md diff --git a/.changeset/pink-parents-chew.md b/.changeset/pink-parents-chew.md deleted file mode 100644 index 8edd4bea2f..0000000000 --- a/.changeset/pink-parents-chew.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@backstage/backend-test-utils': patch ---- - -Added `mockIdentityFactory` to list of default factories for `startTestBackend`. diff --git a/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts b/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts index 1588134737..1713e1ef4d 100644 --- a/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts +++ b/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts @@ -38,7 +38,6 @@ class MockIdentityServiceImpl implements IdentityService { } } -/** @public */ export const mockIdentityFactory = createServiceFactory({ service: coreServices.identity, deps: {}, From 6c7e4e1cd907b8824fe8f5172684774850187365 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 13 Jan 2023 10:08:40 +0100 Subject: [PATCH 3/3] chore: rename mock user Signed-off-by: Johan Haals --- .../src/next/implementations/mockIdentityService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts b/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts index 1713e1ef4d..30a464d877 100644 --- a/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts +++ b/packages/backend-test-utils/src/next/implementations/mockIdentityService.ts @@ -31,7 +31,7 @@ class MockIdentityServiceImpl implements IdentityService { token: 'mock-token', identity: { type: 'user', - userEntityRef: 'user:default/mockUser', + userEntityRef: 'user:default/mock-user', ownershipEntityRefs: [], }, });