Skip to content

Commit

Permalink
fix: modify unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
swetabar committed Aug 19, 2024
1 parent 27bbd24 commit ed2fa02
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ chai.use(chaiAsPromised);

const { expect } = chai;

const createHandler = (name, shouldAuthenticate) => class extends AbstractHandler {
const createHandler = (
name,
shouldAuthenticate,
shouldThrowError = false,
) => class extends AbstractHandler {
constructor(log) {
super(name, log);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars,class-methods-use-this
async checkAuth(request, ctx) {
if (shouldThrowError) {
throw new Error('Authentication error');
}
return shouldAuthenticate ? { user: 'testUser' } : null;
}
};
Expand Down Expand Up @@ -58,6 +65,17 @@ describe('AuthenticationManager', () => {
expect(() => AuthenticationManager.create([], logStub)).to.throw('No handlers provided');
});

it('handles errors thrown by checkAuth', async () => {
const ErrorAuthHandler = createHandler('ErrorAuthHandler', false, true);
const manager = AuthenticationManager.create([ErrorAuthHandler], logStub);
const request = {};
const context = {};

await expect(manager.authenticate(request, context)).to.be.rejectedWith(NotAuthenticatedError);
expect(logStub.error.calledWith('Failed to authenticate with ErrorAuthHandler: Authentication error')).to.be.true;
expect(logStub.info.calledWith('No authentication handler was able to authenticate the request')).to.be.true;
});

it('creates an instance with registered handlers', () => {
const manager = AuthenticationManager.create([DummyHandler], logStub);
expect(manager).to.be.instanceof(AuthenticationManager);
Expand Down

0 comments on commit ed2fa02

Please sign in to comment.