From 41d129304a380eb630e1d949cf45bb1245389cc5 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 28 Aug 2021 19:02:38 +0200 Subject: [PATCH] integration-react: initial ScmAuth test Signed-off-by: Patrik Oldsberg --- .../integration-react/src/api/ScmAuth.test.ts | 65 +++++++++++++++++++ packages/integration-react/src/api/ScmAuth.ts | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 packages/integration-react/src/api/ScmAuth.test.ts diff --git a/packages/integration-react/src/api/ScmAuth.test.ts b/packages/integration-react/src/api/ScmAuth.test.ts new file mode 100644 index 0000000000..ea0261f94b --- /dev/null +++ b/packages/integration-react/src/api/ScmAuth.test.ts @@ -0,0 +1,65 @@ +/* + * Copyright 2021 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 { OAuthApi } from '@backstage/core-plugin-api'; +import { ScmAuth } from './ScmAuth'; + +class MockOAuthApi implements OAuthApi { + constructor(private readonly accessToken: string) {} + + async getAccessToken() { + return this.accessToken; + } +} + +const mockApis = { + github: new MockOAuthApi('github-access-token'), + ghe: new MockOAuthApi('ghe-access-token'), +}; + +describe('ScmAuth', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should provide credentials for GitHub and GHE', async () => { + const api = ScmAuth.mux( + ScmAuth.forGithub(mockApis.github), + ScmAuth.forGithub(mockApis.ghe, { + hostname: 'ghe.example.com', + }), + ); + + await expect( + api.getCredentials({ url: 'https://github.com/backstage/backstage' }), + ).resolves.toEqual({ + token: 'github-access-token', + headers: { + Authorization: 'Bearer github-access-token', + }, + }); + await expect( + api.getCredentials({ + url: 'https://ghe.example.com/backstage/backstage', + }), + ).resolves.toEqual({ + token: 'ghe-access-token', + headers: { + Authorization: 'Bearer ghe-access-token', + }, + }); + }); +}); diff --git a/packages/integration-react/src/api/ScmAuth.ts b/packages/integration-react/src/api/ScmAuth.ts index 46f458d871..e7cbc91942 100644 --- a/packages/integration-react/src/api/ScmAuth.ts +++ b/packages/integration-react/src/api/ScmAuth.ts @@ -132,7 +132,7 @@ export class ScmAuth implements ScmAuthApi { }); } - static mux(providers: ScmAuth[]): ScmAuthApi { + static mux(...providers: ScmAuth[]): ScmAuthApi { return new ScmAuthMux(providers); }