From cf89ecf29ccf95494673a5e85f69ae1114d8da4b Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Thu, 6 Oct 2022 13:57:17 +0200 Subject: [PATCH] fix(history): fix updateHistoryEntryTimestamp to handle nullable user If a user is null it means we're handling a guest user. Signed-off-by: Philip Molares --- src/history/history.service.spec.ts | 7 +++++++ src/history/history.service.ts | 11 +++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index 5f3488a96..33f77f9ba 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -7,6 +7,7 @@ import { ConfigModule } from '@nestjs/config'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { Test, TestingModule } from '@nestjs/testing'; import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm'; +import assert from 'assert'; import { Mock } from 'ts-mockery'; import { DataSource, EntityManager, Repository } from 'typeorm'; @@ -205,6 +206,7 @@ describe('HistoryService', () => { Note.create(user, alias) as Note, user, ); + assert(createHistoryEntry != null); expect(await (await createHistoryEntry.note).aliases).toHaveLength(1); expect((await (await createHistoryEntry.note).aliases)[0].name).toEqual( alias, @@ -225,6 +227,7 @@ describe('HistoryService', () => { Note.create(user, alias) as Note, user, ); + assert(createHistoryEntry != null); expect(await (await createHistoryEntry.note).aliases).toHaveLength(1); expect((await (await createHistoryEntry.note).aliases)[0].name).toEqual( alias, @@ -237,6 +240,10 @@ describe('HistoryService', () => { ); }); }); + it('returns null if user is null', async () => { + const entry = await service.updateHistoryEntryTimestamp({} as Note, null); + expect(entry).toBeNull(); + }); }); describe('updateHistoryEntry', () => { diff --git a/src/history/history.service.ts b/src/history/history.service.ts index 83502633e..2a3e9377f 100644 --- a/src/history/history.service.ts +++ b/src/history/history.service.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -74,13 +74,16 @@ export class HistoryService { * Updates the updatedAt timestamp of a HistoryEntry. * If no history entry exists, it will be created. * @param {Note} note - the note that the history entry belongs to - * @param {User} user - the user that the history entry belongs to + * @param {User | null} user - the user that the history entry belongs to * @return {HistoryEntry} the requested history entry */ async updateHistoryEntryTimestamp( note: Note, - user: User, - ): Promise { + user: User | null, + ): Promise { + if (user == null) { + return null; + } try { const entry = await this.getEntryByNote(note, user); entry.updatedAt = new Date();