Skip to content

Commit 6d71fc3

Browse files
committed
add tests
1 parent 033d060 commit 6d71fc3

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

packages/cubejs-api-gateway/test/auth.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,113 @@ describe('test authorization', () => {
673673
// no warnings, done on checkAuth/checkAuthMiddleware level
674674
expect(loggerMock.mock.calls.length).toEqual(0);
675675
});
676+
677+
test('extendContext receives securityContext from checkAuth', async () => {
678+
const loggerMock = jest.fn(() => {
679+
//
680+
});
681+
682+
const extendContextMock = jest.fn((req) => {
683+
return {
684+
securityContext: {
685+
...req.securityContext,
686+
extendedField: 'added_by_extend_context',
687+
}
688+
};
689+
});
690+
691+
const expectSecurityContext = (securityContext) => {
692+
expect(securityContext.uid).toEqual(5);
693+
expect(securityContext.extendedField).toEqual('added_by_extend_context');
694+
expect(securityContext.iat).toBeDefined();
695+
expect(securityContext.exp).toBeDefined();
696+
};
697+
698+
const handlerMock = jest.fn((req, res) => {
699+
expectSecurityContext(req.context.securityContext);
700+
res.status(200).end();
701+
});
702+
703+
const { app } = createApiGateway(handlerMock, loggerMock, {
704+
extendContext: extendContextMock,
705+
});
706+
707+
const token = generateAuthToken({ uid: 5 });
708+
709+
await request(app)
710+
.get('/test-auth-fake')
711+
.set('Authorization', `Authorization: ${token}`)
712+
.expect(200);
713+
714+
expect(handlerMock.mock.calls.length).toEqual(1);
715+
expect(extendContextMock.mock.calls.length).toEqual(1);
716+
717+
// should receive securityContext from checkAuth
718+
expect(extendContextMock.mock.calls[0][0].securityContext).toMatchObject({
719+
uid: 5,
720+
iat: expect.any(Number),
721+
exp: expect.any(Number),
722+
});
723+
expectSecurityContext(handlerMock.mock.calls[0][0].context.securityContext);
724+
});
725+
726+
test('extendContext with custom checkAuth returning securityContext', async () => {
727+
const loggerMock = jest.fn(() => {
728+
//
729+
});
730+
731+
const checkAuthMock = jest.fn(async (req: Request, auth?: string) => {
732+
if (auth) {
733+
const decoded = jwt.verify(auth, 'secret') as any;
734+
return {
735+
security_context: {
736+
...decoded,
737+
tenantId: 'tenant_123',
738+
customField: 'from_check_auth',
739+
}
740+
};
741+
}
742+
return {};
743+
});
744+
745+
const extendContextMock = jest.fn((req) => {
746+
// should receive securityContext from checkAuth
747+
expect(req.securityContext).toBeDefined();
748+
expect(req.securityContext.customField).toEqual('from_check_auth');
749+
750+
return {
751+
securityContext: {
752+
...req.securityContext,
753+
extendedField: 'from_extend_context',
754+
}
755+
};
756+
});
757+
758+
const handlerMock = jest.fn((req, res) => {
759+
expect(req.context.securityContext.customField).toEqual('from_check_auth');
760+
expect(req.context.securityContext.extendedField).toEqual('from_extend_context');
761+
res.status(200).end();
762+
});
763+
764+
const { app } = createApiGateway(handlerMock, loggerMock, {
765+
checkAuth: checkAuthMock,
766+
extendContext: extendContextMock,
767+
});
768+
769+
const token = generateAuthToken({ uid: 5 });
770+
771+
await request(app)
772+
.get('/test-auth-fake')
773+
.set('Authorization', `Authorization: ${token}`)
774+
.expect(200);
775+
776+
expect(checkAuthMock.mock.calls.length).toEqual(1);
777+
expect(extendContextMock.mock.calls.length).toEqual(1);
778+
expect(handlerMock.mock.calls.length).toEqual(1);
779+
expect(extendContextMock.mock.calls[0][0].securityContext).toMatchObject({
780+
uid: 5,
781+
tenantId: 'tenant_123',
782+
customField: 'from_check_auth',
783+
});
784+
});
676785
});

0 commit comments

Comments
 (0)