From bf6efe3a73fcc8d76e84a14600b46b11a51c8fd0 Mon Sep 17 00:00:00 2001 From: Abhinav Rastogi Date: Mon, 12 Dec 2022 11:50:41 +0530 Subject: [PATCH] Tests for headers (#8) * Adds tests for ProxiedSignInPage * working test for headers * code formatting Signed-off-by: Abhinav Rastogi --- .../ProxiedSignInIdentity.test.ts | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts b/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts index 3a40f5bb4b..17a1287052 100644 --- a/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts +++ b/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts @@ -103,7 +103,6 @@ describe('ProxiedSignInIdentity', () => { }, }; } - worker.events.on('request:match', serverCalled); worker.use( rest.get('http://example.com/api/auth/foo/refresh', (_, res, ctx) => @@ -164,5 +163,57 @@ describe('ProxiedSignInIdentity', () => { await identity.getSessionAsync(); // now the expiry has passed expect(serverCalled).toHaveBeenCalledTimes(2); }); + + it('handles optional headers correctly', async () => { + let req1: Request; + const getBaseUrl = jest.fn(); + const serverCalled = jest.fn().mockImplementation(req => { + req1 = req; + }); + + worker.events.on('request:match', serverCalled); + worker.use( + rest.get('http://example.com/api/auth/foo/refresh', (_, res, ctx) => + res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + // dummy response as we are only testing the request in this test + ctx.json({ + providerInfo: {}, + profile: {}, + backstageIdentity: { + token: '', + identity: { + ownershipEntityRefs: [''], + userEntityRef: '', + type: 'user', + }, + }, + }), + ), + ), + ); + + const getHeaders = jest.fn().mockResolvedValue({ 'x-foo': 'bars' }); + const identity = new ProxiedSignInIdentity({ + provider: 'foo', + discoveryApi: { getBaseUrl }, + getHeaders: getHeaders, + }); + + getBaseUrl.mockResolvedValue('http://example.com/api/auth'); + + await identity.start(); // should not throw + expect(getBaseUrl).toHaveBeenCalledTimes(1); + expect(getBaseUrl).toHaveBeenLastCalledWith('auth'); + expect(getHeaders).toHaveBeenCalledTimes(1); + expect(serverCalled).toHaveBeenCalledTimes(1); + + expect(req1).not.toBeUndefined(); + // required header should be present + expect(req1!.headers.get('x-requested-with')).toEqual('XMLHttpRequest'); + // optional header should be present when passed + expect(req1!.headers.get('x-foo')).toEqual('bars'); + }); }); });