From fe78da86f443e050528b2cd4cd6a389fcea905ec Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 16 Feb 2024 15:23:01 +0100 Subject: [PATCH] backend-test-utils: added tests for MockHttpAuthService + fixes Signed-off-by: Patrik Oldsberg --- .../src/next/services/MockAuthService.test.ts | 4 +- .../src/next/services/MockAuthService.ts | 4 +- .../next/services/MockHttpAuthService.test.ts | 91 +++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 packages/backend-test-utils/src/next/services/MockHttpAuthService.test.ts diff --git a/packages/backend-test-utils/src/next/services/MockAuthService.test.ts b/packages/backend-test-utils/src/next/services/MockAuthService.test.ts index 7a6e562c98..cf7b23e999 100644 --- a/packages/backend-test-utils/src/next/services/MockAuthService.test.ts +++ b/packages/backend-test-utils/src/next/services/MockAuthService.test.ts @@ -27,9 +27,9 @@ describe('MockAuthService', () => { const auth = new MockAuthService('test'); it('should reject invalid tokens', async () => { - await expect(auth.authenticate('')).rejects.toThrow('Invalid mock token'); + await expect(auth.authenticate('')).rejects.toThrow('Token is empty'); await expect(auth.authenticate('not-a-mock-token')).rejects.toThrow( - 'Invalid mock token', + "Unknown mock token 'not-a-mock-token'", ); await expect(auth.authenticate(MOCK_USER_TOKEN_PREFIX)).rejects.toThrow( 'Unexpected end of JSON input', diff --git a/packages/backend-test-utils/src/next/services/MockAuthService.ts b/packages/backend-test-utils/src/next/services/MockAuthService.ts index 7d1bbc0733..b5debd5c7d 100644 --- a/packages/backend-test-utils/src/next/services/MockAuthService.ts +++ b/packages/backend-test-utils/src/next/services/MockAuthService.ts @@ -49,6 +49,8 @@ export class MockAuthService implements AuthService { throw new AuthenticationError('User token is invalid'); case MOCK_INVALID_SERVICE_TOKEN: throw new AuthenticationError('Service token is invalid'); + case '': + throw new AuthenticationError('Token is empty'); default: } @@ -73,7 +75,7 @@ export class MockAuthService implements AuthService { return mockCredentials.service(subject); } - throw new AuthenticationError('Invalid mock token'); + throw new AuthenticationError(`Unknown mock token '${token}'`); } async getOwnServiceCredentials(): Promise< diff --git a/packages/backend-test-utils/src/next/services/MockHttpAuthService.test.ts b/packages/backend-test-utils/src/next/services/MockHttpAuthService.test.ts new file mode 100644 index 0000000000..84d7c84ec3 --- /dev/null +++ b/packages/backend-test-utils/src/next/services/MockHttpAuthService.test.ts @@ -0,0 +1,91 @@ +/* + * 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 { Request } from 'express'; +import { MockHttpAuthService } from './MockHttpAuthService'; +import { mockCredentials } from './mockCredentials'; + +describe('MockHttpAuthService', () => { + const httpAuth = new MockHttpAuthService('test'); + + it('should authenticate requests', async () => { + await expect( + httpAuth.credentials({ + headers: {}, + } as Request), + ).resolves.toEqual(mockCredentials.none()); + + await expect( + httpAuth.credentials({ + headers: { + authorization: mockCredentials.user.header(), + }, + } as Request), + ).resolves.toEqual(mockCredentials.user()); + + await expect( + httpAuth.credentials({ + headers: { + authorization: mockCredentials.user.header('user:default/other'), + }, + } as Request), + ).resolves.toEqual(mockCredentials.user('user:default/other')); + + await expect( + httpAuth.credentials({ + headers: { + authorization: mockCredentials.service.header(), + }, + } as Request), + ).resolves.toEqual(mockCredentials.service()); + + await expect( + httpAuth.credentials({ + headers: { + authorization: mockCredentials.service.header({ + subject: 'plugin:other', + }), + }, + } as Request), + ).resolves.toEqual(mockCredentials.service('plugin:other')); + }); + + it('should reject invalid credentials', async () => { + await expect( + httpAuth.credentials({ + headers: { + authorization: 'Bearer bad', + }, + } as Request), + ).rejects.toThrow("Unknown mock token 'bad'"); + + await expect( + httpAuth.credentials({ + headers: { + authorization: mockCredentials.user.invalidHeader(), + }, + } as Request), + ).rejects.toThrow('User token is invalid'); + + await expect( + httpAuth.credentials({ + headers: { + authorization: mockCredentials.service.invalidHeader(), + }, + } as Request), + ).rejects.toThrow('Service token is invalid'); + }); +});