Skip to content

Commit

Permalink
Adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nandan-bhat committed Aug 2, 2024
1 parent 29bbad3 commit 3384e71
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tools/auth0/handlers/scimHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class ScimHandler {
}
}).catch((error) => {
throw new Error(
`Problem fetching SCIM configurations while running \"calcChanges\".\n${ error }`
`Problem fetching SCIM configurations while running \"createIdMap\".\n${ error }`
);
});
},
Expand Down
42 changes: 42 additions & 0 deletions test/tools/auth0/handlers/scimHandler.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { PromisePoolExecutor } = require('promise-pool-executor');
const chaiAsPromised = require('chai-as-promised');
const sinon = require('sinon');
const chai = require('chai');
const log = require('../../../../src/logger').default;
const ScimHandler = require('../../../../src/tools/auth0/handlers/scimHandler').default;

const { expect } = chai.use(chaiAsPromised);
Expand Down Expand Up @@ -286,4 +287,45 @@ describe('ScimHandler', () => {
await expect(handler.createOverride(bodyParams)).to.be.rejectedWith('Unexpected error');
});
});

describe('handleExpectedErrors', () => {
it('should handle 404 errors and return null', async () => {
const mockCallback = sinon.stub().rejects({ statusCode: 404 });
const result = await handler.withErrorHandling(mockCallback, 'get', 'con_kzpLY0Afi4I8lvwM');
expect(result).to.be.null;
expect(mockCallback.calledOnce).to.be.true;
});

it('should handle 403 errors, log a warning, and return null', async () => {
const mockCallback = sinon.stub().rejects({ statusCode: 403 });
const logWarnSpy = sinon.spy(log, 'warn');
const result = await handler.withErrorHandling(mockCallback, 'get', 'con_kzpLY0Afi4I8lvwM');
expect(result).to.be.null;
expect(mockCallback.calledOnce).to.be.true;
expect(logWarnSpy.calledOnce).to.be.true;
expect(logWarnSpy.firstCall.args[0]).to.include('Insufficient scope');
logWarnSpy.restore();
});

it('should handle 400 errors with "already exists" message and return null', async () => {
const mockCallback = sinon.stub().rejects({ statusCode: 400, message: 'SCIM configuration already exists' });
const result = await handler.withErrorHandling(mockCallback, 'create', 'con_kzpLY0Afi4I8lvwM');
expect(result).to.be.null;
expect(mockCallback.calledOnce).to.be.true;
});

it('should handle 429 errors and return null', async () => {
const mockCallback = sinon.stub().rejects({ statusCode: 429, message: 'Rate limit exceeded' });
const result = await handler.withErrorHandling(mockCallback, 'create', 'con_kzpLY0Afi4I8lvwM');
expect(result).to.be.null;
expect(mockCallback.calledOnce).to.be.true;
});

it('should rethrow unexpected errors', async () => {
const mockCallback = sinon.stub().rejects(new Error('Unexpected error'));
await expect(handler.withErrorHandling(mockCallback, 'create', 'con_kzpLY0Afi4I8lvwM')).to.be.rejectedWith('Unexpected error');
expect(mockCallback.calledOnce).to.be.true;
});
});

});

0 comments on commit 3384e71

Please sign in to comment.