Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update tests with better method of testing for errors #281

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 18 additions & 56 deletions src/app/core/access-checker/access-checker.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,30 +133,17 @@ describe('Access Checker Service:', () => {

// Provider fails on get
it('should not update the cache when the access checker provider fails', async () => {
let err;
try {
await accessChecker.get('provideronly');
} catch (e) {
err = e;
}

// Should have errored
should.exist(err);
await accessChecker.get('provideronly').should.be.rejected();

const result = await CacheEntry.findOne({ key: 'provideronly' }).exec();
should.not.exist(result);
});

// Provider fails on refresh attempt
it('should not update the cache on refresh when the access checker provider fails', async () => {
let err;
try {
await accessChecker.refreshEntry(spec.cache.outdated.key);
} catch (e) {
err = e;
}
// Should have errored
should.exist(err);
await accessChecker
.refreshEntry(spec.cache.outdated.key)
.should.be.rejected();

// Query for the cache object and verify it hasn't been updated
const result = await CacheEntry.findOne({
Expand All @@ -167,14 +154,7 @@ describe('Access Checker Service:', () => {

// Provider fails on refresh attempt
it('should fail when no key is specified', async () => {
let err;
try {
await accessChecker.refreshEntry(null);
} catch (e) {
err = e;
}
// Should have errored
should.exist(err);
await accessChecker.refreshEntry(null).should.be.rejected();

// Query for the cache object and verify it hasn't been updated
const result = await CacheEntry.findOne({
Expand All @@ -200,13 +180,7 @@ describe('Access Checker Service:', () => {

// Pull from cache
it('should fail when no key is specified', async () => {
let err;
try {
await accessChecker.get(null);
} catch (e) {
err = e;
}
should.exist(err);
await accessChecker.get(null).should.be.rejected();
});

// Pull from cache
Expand Down Expand Up @@ -279,18 +253,13 @@ describe('Access Checker Service:', () => {

// Provider fails on get
it('should throw error when no provider is configured', async () => {
let err;
try {
await accessChecker.get('notincache');
} catch (e) {
err = e;
}

// Should have errored
should.exist(err?.message);
err.message.should.equal(
'Error retrieving entry from the access checker provider: Invalid accessChecker provider configuration.'
);
await accessChecker
.get('notincache')
.should.be.rejectedWith(
new Error(
'Error retrieving entry from the access checker provider: Invalid accessChecker provider configuration.'
)
);
});
});

Expand All @@ -310,18 +279,11 @@ describe('Access Checker Service:', () => {

// Provider fails on get
it('should throw error when provider is configured with invalid file path', async () => {
let err;
try {
await accessChecker.get('notincache');
} catch (e) {
err = e;
}

// Should have errored
should.exist(err?.message);
err.message.should.equal(
'Error retrieving entry from the access checker provider: Failed to load access checker provider.'
);
await accessChecker
.get('notincache')
.should.be.rejectedWith(
'Error retrieving entry from the access checker provider: Failed to load access checker provider.'
);
});
});
});
24 changes: 6 additions & 18 deletions src/app/core/user/auth/user-authentication.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,12 @@ describe('User Auth Controller:', () => {
return cb && cb();
};

let err;
try {
await userAuthenticationController.signin(req, res, emptyFn);
} catch (e) {
err = e;
}
await userAuthenticationController
.signin(req, res, emptyFn)
.should.be.rejectedWith('Missing credentials');

assert.notCalled(res.status);
assert.notCalled(res.json);

should.exist(err);
should(err.type).equal('missing-credentials');
});

it('should fail with missing username', async () => {
Expand All @@ -201,18 +195,12 @@ describe('User Auth Controller:', () => {
return cb && cb();
};

let err;
try {
await userAuthenticationController.signin(req, res, emptyFn);
} catch (e) {
err = e;
}
await userAuthenticationController
.signin(req, res, emptyFn)
.should.be.rejectedWith('Missing credentials');

assert.notCalled(res.status);
assert.notCalled(res.json);

should.exist(err);
should(err.type).equal('missing-credentials');
});

it('should fail with unknown user', async () => {
Expand Down
Loading