mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-20 10:15:17 -04: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);
|
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', () => {
|
describe('validateToken', () => {
|
||||||
|
@ -227,6 +233,12 @@ describe('AuthService', () => {
|
||||||
);
|
);
|
||||||
await service.removeToken(authToken.keyId);
|
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', () => {
|
describe('createTokenForUser', () => {
|
||||||
|
|
|
@ -127,6 +127,9 @@ export class AuthService {
|
||||||
const accessToken = await this.authTokenRepository.findOne({
|
const accessToken = await this.authTokenRepository.findOne({
|
||||||
where: { keyId: keyId },
|
where: { keyId: keyId },
|
||||||
});
|
});
|
||||||
|
if (accessToken === undefined) {
|
||||||
|
throw new NotInDBError(`AuthToken for key '${keyId}' not found`);
|
||||||
|
}
|
||||||
accessToken.lastUsed = new Date();
|
accessToken.lastUsed = new Date();
|
||||||
await this.authTokenRepository.save(accessToken);
|
await this.authTokenRepository.save(accessToken);
|
||||||
}
|
}
|
||||||
|
@ -170,6 +173,9 @@ export class AuthService {
|
||||||
const token = await this.authTokenRepository.findOne({
|
const token = await this.authTokenRepository.findOne({
|
||||||
where: { keyId: keyId },
|
where: { keyId: keyId },
|
||||||
});
|
});
|
||||||
|
if (token === undefined) {
|
||||||
|
throw new NotInDBError(`AuthToken for key '${keyId}' not found`);
|
||||||
|
}
|
||||||
await this.authTokenRepository.remove(token);
|
await this.authTokenRepository.remove(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue