AuthService: Throw NotInDBError on empty DB result

This adds error handling to various functions, so they throw a
NotInDBError instead of a TypeError

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-05-09 18:27:03 +02:00
parent 52f6310e91
commit ef8b8d985e
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 18 additions and 0 deletions

View file

@ -168,6 +168,12 @@ describe('AuthService', () => {
);
await service.setLastUsedToken(authToken.keyId);
});
it('throws if the token is not in the database', async () => {
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(undefined);
await expect(service.setLastUsedToken(authToken.keyId)).rejects.toThrow(
NotInDBError,
);
});
});
describe('validateToken', () => {
@ -227,6 +233,12 @@ describe('AuthService', () => {
);
await service.removeToken(authToken.keyId);
});
it('throws if the token is not in the database', async () => {
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(undefined);
await expect(service.removeToken(authToken.keyId)).rejects.toThrow(
NotInDBError,
);
});
});
describe('createTokenForUser', () => {

View file

@ -127,6 +127,9 @@ export class AuthService {
const accessToken = await this.authTokenRepository.findOne({
where: { keyId: keyId },
});
if (accessToken === undefined) {
throw new NotInDBError(`AuthToken for key '${keyId}' not found`);
}
accessToken.lastUsed = new Date();
await this.authTokenRepository.save(accessToken);
}
@ -170,6 +173,9 @@ export class AuthService {
const token = await this.authTokenRepository.findOne({
where: { keyId: keyId },
});
if (token === undefined) {
throw new NotInDBError(`AuthToken for key '${keyId}' not found`);
}
await this.authTokenRepository.remove(token);
}