Tests for headers (#8)

* Adds tests for ProxiedSignInPage

* working test for headers

* code formatting

Signed-off-by: Abhinav Rastogi <abhinav.rastogi@harness.io>
This commit is contained in:
Abhinav Rastogi
2022-12-12 11:50:41 +05:30
parent f7749ae07f
commit bf6efe3a73
@@ -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');
});
});
});