mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-01-23 01:24:09 +00:00
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:
parent
6fd9d64ad7
commit
cfaa07806b
2 changed files with 18 additions and 0 deletions
|
@ -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', () => {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue