refactor(auth-token): rename lastUsed to lastUsedAt

This is part of an effort to name all date attributes
consistently.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-01-16 21:52:15 +01:00
parent 64667d81c0
commit b0e2987987
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
5 changed files with 16 additions and 16 deletions

View file

@ -16,5 +16,5 @@ export class AuthTokenDto {
validUntil: Date; validUntil: Date;
@IsDate() @IsDate()
@IsOptional() @IsOptional()
lastUsed: Date | null; lastUsedAt: Date | null;
} }

View file

@ -42,7 +42,7 @@ export class AuthToken {
nullable: true, nullable: true,
type: 'date', type: 'date',
}) })
lastUsed: Date | null; lastUsedAt: Date | null;
public static create( public static create(
keyId: string, keyId: string,
@ -57,7 +57,7 @@ export class AuthToken {
newToken.label = label; newToken.label = label;
newToken.accessTokenHash = accessToken; newToken.accessTokenHash = accessToken;
newToken.validUntil = validUntil; newToken.validUntil = validUntil;
newToken.lastUsed = null; newToken.lastUsedAt = null;
return newToken; return newToken;
} }
} }

View file

@ -143,14 +143,14 @@ describe('AuthService', () => {
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
...authToken, ...authToken,
user: Promise.resolve(user), user: Promise.resolve(user),
lastUsed: new Date(1549312452000), lastUsedAt: new Date(1549312452000),
}); });
jest jest
.spyOn(authTokenRepo, 'save') .spyOn(authTokenRepo, 'save')
.mockImplementationOnce( .mockImplementationOnce(
async (authTokenSaved, _): Promise<AuthToken> => { async (authTokenSaved, _): Promise<AuthToken> => {
expect(authTokenSaved.keyId).toEqual(authToken.keyId); expect(authTokenSaved.keyId).toEqual(authToken.keyId);
expect(authTokenSaved.lastUsed).not.toEqual(1549312452000); expect(authTokenSaved.lastUsedAt).not.toEqual(1549312452000);
return authToken; return authToken;
}, },
); );
@ -242,7 +242,7 @@ describe('AuthService', () => {
.spyOn(authTokenRepo, 'save') .spyOn(authTokenRepo, 'save')
.mockImplementationOnce( .mockImplementationOnce(
async (authTokenSaved: AuthToken, _): Promise<AuthToken> => { async (authTokenSaved: AuthToken, _): Promise<AuthToken> => {
expect(authTokenSaved.lastUsed).toBeNull(); expect(authTokenSaved.lastUsedAt).toBeNull();
return authTokenSaved; return authTokenSaved;
}, },
); );
@ -252,7 +252,7 @@ describe('AuthService', () => {
token.validUntil.getTime() - token.validUntil.getTime() -
(new Date().getTime() + 2 * 365 * 24 * 60 * 60 * 1000), (new Date().getTime() + 2 * 365 * 24 * 60 * 60 * 1000),
).toBeLessThanOrEqual(10000); ).toBeLessThanOrEqual(10000);
expect(token.lastUsed).toBeNull(); expect(token.lastUsedAt).toBeNull();
expect(token.secret.startsWith(token.keyId)).toBeTruthy(); expect(token.secret.startsWith(token.keyId)).toBeTruthy();
}); });
it('with validUntil not 0', async () => { it('with validUntil not 0', async () => {
@ -261,7 +261,7 @@ describe('AuthService', () => {
.spyOn(authTokenRepo, 'save') .spyOn(authTokenRepo, 'save')
.mockImplementationOnce( .mockImplementationOnce(
async (authTokenSaved: AuthToken, _): Promise<AuthToken> => { async (authTokenSaved: AuthToken, _): Promise<AuthToken> => {
expect(authTokenSaved.lastUsed).toBeNull(); expect(authTokenSaved.lastUsedAt).toBeNull();
return authTokenSaved; return authTokenSaved;
}, },
); );
@ -273,7 +273,7 @@ describe('AuthService', () => {
); );
expect(token.label).toEqual(identifier); expect(token.label).toEqual(identifier);
expect(token.validUntil.getTime()).toEqual(validUntil); expect(token.validUntil.getTime()).toEqual(validUntil);
expect(token.lastUsed).toBeNull(); expect(token.lastUsedAt).toBeNull();
expect(token.secret.startsWith(token.keyId)).toBeTruthy(); expect(token.secret.startsWith(token.keyId)).toBeTruthy();
}); });
}); });
@ -290,7 +290,7 @@ describe('AuthService', () => {
authToken.validUntil = new Date(); authToken.validUntil = new Date();
const tokenDto = service.toAuthTokenDto(authToken); const tokenDto = service.toAuthTokenDto(authToken);
expect(tokenDto.keyId).toEqual(authToken.keyId); expect(tokenDto.keyId).toEqual(authToken.keyId);
expect(tokenDto.lastUsed).toBeNull(); expect(tokenDto.lastUsedAt).toBeNull();
expect(tokenDto.label).toEqual(authToken.label); expect(tokenDto.label).toEqual(authToken.label);
expect(tokenDto.validUntil.getTime()).toEqual( expect(tokenDto.validUntil.getTime()).toEqual(
authToken.validUntil.getTime(), authToken.validUntil.getTime(),

View file

@ -108,7 +108,7 @@ export class AuthService {
if (accessToken === undefined) { if (accessToken === undefined) {
throw new NotInDBError(`AuthToken for key '${keyId}' not found`); throw new NotInDBError(`AuthToken for key '${keyId}' not found`);
} }
accessToken.lastUsed = new Date(); accessToken.lastUsedAt = new Date();
await this.authTokenRepository.save(accessToken); await this.authTokenRepository.save(accessToken);
} }
@ -175,11 +175,11 @@ export class AuthService {
keyId: authToken.keyId, keyId: authToken.keyId,
createdAt: authToken.createdAt, createdAt: authToken.createdAt,
validUntil: authToken.validUntil, validUntil: authToken.validUntil,
lastUsed: null, lastUsedAt: null,
}; };
if (authToken.lastUsed) { if (authToken.lastUsedAt) {
tokenDto.lastUsed = new Date(authToken.lastUsed); tokenDto.lastUsedAt = new Date(authToken.lastUsedAt);
} }
return tokenDto; return tokenDto;

View file

@ -47,7 +47,7 @@ describe('Tokens', () => {
keyId = response.body.keyId; keyId = response.body.keyId;
expect(response.body.label).toBe(tokenName); expect(response.body.label).toBe(tokenName);
expect(response.body.validUntil).toBe(null); expect(response.body.validUntil).toBe(null);
expect(response.body.lastUsed).toBe(null); expect(response.body.lastUsedAt).toBe(null);
expect(response.body.secret.length).toBe(98); expect(response.body.secret.length).toBe(98);
}); });
@ -59,7 +59,7 @@ describe('Tokens', () => {
.expect(200); .expect(200);
expect(response.body[0].label).toBe(tokenName); expect(response.body[0].label).toBe(tokenName);
expect(response.body[0].validUntil).toBe(null); expect(response.body[0].validUntil).toBe(null);
expect(response.body[0].lastUsed).toBe(null); expect(response.body[0].lastUsedAt).toBe(null);
expect(response.body[0].secret).not.toBeDefined(); expect(response.body[0].secret).not.toBeDefined();
}); });
it(`DELETE /tokens/:keyid`, async () => { it(`DELETE /tokens/:keyid`, async () => {