change getTokensByUsername to getTokensByUser

Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
Yannick Bungers 2021-10-13 22:22:08 +02:00 committed by David Mehren
parent 2ab3d7e8f9
commit be27686610
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
3 changed files with 12 additions and 14 deletions

View file

@ -40,8 +40,8 @@ export class TokensController {
@Get()
async getUserTokens(@RequestUser() user: User): Promise<AuthTokenDto[]> {
return (await this.authService.getTokensByUsername(user.userName)).map(
(token) => this.authService.toAuthTokenDto(token),
return (await this.authService.getTokensByUser(user)).map((token) =>
this.authService.toAuthTokenDto(token),
);
}
@ -64,7 +64,7 @@ export class TokensController {
@RequestUser() user: User,
@Param('keyId') keyId: string,
): Promise<void> {
const tokens = await this.authService.getTokensByUsername(user.userName);
const tokens = await this.authService.getTokensByUser(user);
try {
for (const token of tokens) {
if (token.keyId == keyId) {

View file

@ -74,12 +74,10 @@ describe('AuthService', () => {
expect(service).toBeDefined();
});
describe('getTokensByUsername', () => {
describe('getTokensByUser', () => {
it('works', async () => {
jest
.spyOn(userRepo, 'findOne')
.mockResolvedValueOnce({ ...user, authTokens: [authToken] });
const tokens = await service.getTokensByUsername(user.userName);
jest.spyOn(authTokenRepo, 'find').mockResolvedValueOnce([authToken]);
const tokens = await service.getTokensByUser(user);
expect(tokens).toHaveLength(1);
expect(tokens).toEqual([authToken]);
});

View file

@ -140,14 +140,14 @@ export class AuthService {
return accessToken;
}
async getTokensByUsername(userName: string): Promise<AuthToken[]> {
const user = await this.usersService.getUserByUsername(userName, [
UserRelationEnum.AUTHTOKENS,
]);
if (user.authTokens === undefined) {
async getTokensByUser(user: User): Promise<AuthToken[]> {
const tokens = await this.authTokenRepository.find({
where: { user: user },
});
if (tokens === undefined) {
return [];
}
return user.authTokens;
return tokens;
}
async removeToken(keyId: string): Promise<void> {