From c4975e47834aafb01cd876296691e458dd514d93 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 20 Mar 2022 22:40:41 +0100 Subject: [PATCH] refactor: adapt for typeorm 0.3 Signed-off-by: David Mehren --- src/auth/auth.service.spec.ts | 6 ++-- src/auth/auth.service.ts | 12 +++---- src/groups/groups.service.spec.ts | 2 +- src/groups/groups.service.ts | 2 +- src/history/history-entry.entity.ts | 15 ++++++-- src/history/history.service.ts | 10 +++--- src/media/media.service.spec.ts | 6 ++-- src/media/media.service.ts | 15 ++++---- src/notes/alias.service.spec.ts | 10 +++--- src/notes/alias.service.ts | 12 ++++--- src/notes/notes.service.spec.ts | 17 ++++++--- src/notes/notes.service.ts | 8 ++--- .../note-group-permission.entity.ts | 10 ++++-- .../note-user-permission.entity.ts | 10 ++++-- src/permissions/permissions.service.spec.ts | 36 +++++++++++++++++-- src/revisions/revisions.service.spec.ts | 2 +- src/revisions/revisions.service.ts | 18 +++++----- src/users/users.service.spec.ts | 2 +- src/users/users.service.ts | 2 +- test/private-api/history.e2e-spec.ts | 4 +-- test/public-api/me.e2e-spec.ts | 1 + 21 files changed, 131 insertions(+), 69 deletions(-) diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index e21842d88..2500ea4ff 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -108,7 +108,7 @@ describe('AuthService', () => { }); describe('fails:', () => { it('AuthToken could not be found', async () => { - jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(null); await expect( service.getAuthTokenAndValidate(authToken.keyId, token), ).rejects.toThrow(NotInDBError); @@ -157,7 +157,7 @@ describe('AuthService', () => { await service.setLastUsedToken(authToken.keyId); }); it('throws if the token is not in the database', async () => { - jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(null); await expect(service.setLastUsedToken(authToken.keyId)).rejects.toThrow( NotInDBError, ); @@ -226,7 +226,7 @@ describe('AuthService', () => { await service.removeToken(authToken.keyId); }); it('throws if the token is not in the database', async () => { - jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(null); await expect(service.removeToken(authToken.keyId)).rejects.toThrow( NotInDBError, ); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 0160ed782..eb67b7a97 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -7,7 +7,7 @@ import { Injectable } from '@nestjs/common'; import { Cron, Timeout } from '@nestjs/schedule'; import { InjectRepository } from '@nestjs/typeorm'; import crypto, { randomBytes } from 'crypto'; -import { Repository } from 'typeorm'; +import { Equal, Repository } from 'typeorm'; import { NotInDBError, @@ -104,7 +104,7 @@ export class AuthService { const accessToken = await this.authTokenRepository.findOne({ where: { keyId: keyId }, }); - if (accessToken === undefined) { + if (accessToken === null) { throw new NotInDBError(`AuthToken for key '${keyId}' not found`); } accessToken.lastUsedAt = new Date(); @@ -119,7 +119,7 @@ export class AuthService { where: { keyId: keyId }, relations: ['user'], }); - if (accessToken === undefined) { + if (accessToken === null) { throw new NotInDBError(`AuthToken '${token}' not found`); } // Hash the user-provided token @@ -150,9 +150,9 @@ export class AuthService { async getTokensByUser(user: User): Promise { const tokens = await this.authTokenRepository.find({ - where: { user: user }, + where: { user: Equal(user) }, }); - if (tokens === undefined) { + if (tokens === null) { return []; } return tokens; @@ -162,7 +162,7 @@ export class AuthService { const token = await this.authTokenRepository.findOne({ where: { keyId: keyId }, }); - if (token === undefined) { + if (token === null) { throw new NotInDBError(`AuthToken for key '${keyId}' not found`); } await this.authTokenRepository.remove(token); diff --git a/src/groups/groups.service.spec.ts b/src/groups/groups.service.spec.ts index 4b6099bd2..334c8a835 100644 --- a/src/groups/groups.service.spec.ts +++ b/src/groups/groups.service.spec.ts @@ -82,7 +82,7 @@ describe('GroupsService', () => { expect(foundGroup.special).toEqual(group.special); }); it('fails with non-existing group', async () => { - jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(null); await expect(service.getGroupByName('i_dont_exist')).rejects.toThrow( NotInDBError, ); diff --git a/src/groups/groups.service.ts b/src/groups/groups.service.ts index c0e9605e2..4c85564cd 100644 --- a/src/groups/groups.service.ts +++ b/src/groups/groups.service.ts @@ -60,7 +60,7 @@ export class GroupsService { const group = await this.groupRepository.findOne({ where: { name: name }, }); - if (group === undefined) { + if (group === null) { throw new NotInDBError(`Group with name '${name}' not found`); } return group; diff --git a/src/history/history-entry.entity.ts b/src/history/history-entry.entity.ts index 481c6b9ea..68b3e42f8 100644 --- a/src/history/history-entry.entity.ts +++ b/src/history/history-entry.entity.ts @@ -3,7 +3,13 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { Column, Entity, ManyToOne, UpdateDateColumn } from 'typeorm'; +import { + Column, + Entity, + ManyToOne, + PrimaryColumn, + UpdateDateColumn, +} from 'typeorm'; import { Note } from '../notes/note.entity'; import { User } from '../users/user.entity'; @@ -16,16 +22,19 @@ export class HistoryEntry { * primary keys. * See https://github.com/typeorm/typeorm/issues/6908 */ + @PrimaryColumn() + userId: string; @ManyToOne((_) => User, (user) => user.historyEntries, { onDelete: 'CASCADE', - primary: true, }) user: User; + @PrimaryColumn() + noteId: string; + @ManyToOne((_) => Note, (note) => note.historyEntries, { onDelete: 'CASCADE', - primary: true, }) note: Note; diff --git a/src/history/history.service.ts b/src/history/history.service.ts index e551e3bda..aaa32c97c 100644 --- a/src/history/history.service.ts +++ b/src/history/history.service.ts @@ -5,7 +5,7 @@ */ import { Injectable } from '@nestjs/common'; import { InjectConnection, InjectRepository } from '@nestjs/typeorm'; -import { Connection, Repository } from 'typeorm'; +import { Connection, Equal, Repository } from 'typeorm'; import { NotInDBError } from '../errors/errors'; import { ConsoleLoggerService } from '../logger/console-logger.service'; @@ -41,7 +41,7 @@ export class HistoryService { */ async getEntriesByUser(user: User): Promise { return await this.historyEntryRepository.find({ - where: { user: user }, + where: { user: Equal(user) }, relations: ['note', 'note.aliases', 'user'], }); } @@ -56,8 +56,8 @@ export class HistoryService { async getEntryByNote(note: Note, user: User): Promise { const entry = await this.historyEntryRepository.findOne({ where: { - note: note, - user: user, + note: Equal(note), + user: Equal(user), }, relations: ['note', 'note.aliases', 'user'], }); @@ -150,7 +150,7 @@ export class HistoryService { ): Promise { await this.connection.transaction(async (manager) => { const currentHistory = await manager.find(HistoryEntry, { - where: { user: user }, + where: { user: Equal(user) }, relations: ['note', 'note.aliases', 'user'], }); for (const entry of currentHistory) { diff --git a/src/media/media.service.spec.ts b/src/media/media.service.spec.ts index 9b10181fc..53e2bd27d 100644 --- a/src/media/media.service.spec.ts +++ b/src/media/media.service.spec.ts @@ -210,7 +210,7 @@ describe('MediaService', () => { }); it("fails: can't find mediaUpload", async () => { const testFileName = 'testFilename'; - jest.spyOn(mediaRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(mediaRepo, 'findOne').mockResolvedValueOnce(null); await expect(service.findUploadByFilename(testFileName)).rejects.toThrow( NotInDBError, ); @@ -243,8 +243,8 @@ describe('MediaService', () => { } as User); expect(mediaList).toEqual([]); }); - it('with error (undefined as return value of find)', async () => { - jest.spyOn(mediaRepo, 'find').mockResolvedValueOnce(undefined); + it('with error (null as return value of find)', async () => { + jest.spyOn(mediaRepo, 'find').mockResolvedValueOnce(null); const mediaList = await service.listUploadsByUser({ username: username, } as User); diff --git a/src/media/media.service.ts b/src/media/media.service.ts index 8ea416f42..6f6056dd8 100644 --- a/src/media/media.service.ts +++ b/src/media/media.service.ts @@ -8,7 +8,7 @@ import { ModuleRef } from '@nestjs/core'; import { InjectRepository } from '@nestjs/typeorm'; import crypto from 'crypto'; import * as FileType from 'file-type'; -import { Repository } from 'typeorm'; +import { Equal, Repository } from 'typeorm'; import mediaConfiguration, { MediaConfig } from '../config/media.config'; import { ClientError, NotInDBError } from '../errors/errors'; @@ -128,10 +128,11 @@ export class MediaService { * @throws {MediaBackendError} - there was an error retrieving the url */ async findUploadByFilename(filename: string): Promise { - const mediaUpload = await this.mediaUploadRepository.findOne(filename, { + const mediaUpload = await this.mediaUploadRepository.findOne({ + where: { id: filename }, relations: ['user'], }); - if (mediaUpload === undefined) { + if (mediaUpload === null) { throw new NotInDBError( `MediaUpload with filename '${filename}' not found`, ); @@ -147,10 +148,10 @@ export class MediaService { */ async listUploadsByUser(user: User): Promise { const mediaUploads = await this.mediaUploadRepository.find({ - where: { user: user }, + where: { user: Equal(user) }, relations: ['user', 'note'], }); - if (mediaUploads === undefined) { + if (mediaUploads === null) { return []; } return mediaUploads; @@ -164,10 +165,10 @@ export class MediaService { */ async listUploadsByNote(note: Note): Promise { const mediaUploads = await this.mediaUploadRepository.find({ - where: { note: note }, + where: { note: Equal(note) }, relations: ['user', 'note'], }); - if (mediaUploads === undefined) { + if (mediaUploads === null) { return []; } return mediaUploads; diff --git a/src/notes/alias.service.spec.ts b/src/notes/alias.service.spec.ts index 597260b7b..c659f72eb 100644 --- a/src/notes/alias.service.spec.ts +++ b/src/notes/alias.service.spec.ts @@ -120,8 +120,8 @@ describe('AliasService', () => { jest .spyOn(noteRepo, 'save') .mockImplementationOnce(async (note: Note): Promise => note); - jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); - jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(null); + jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(null); const savedAlias = await service.addAlias(note, alias); expect(savedAlias.name).toEqual(alias); expect(savedAlias.primary).toBeTruthy(); @@ -131,8 +131,8 @@ describe('AliasService', () => { jest .spyOn(noteRepo, 'save') .mockImplementationOnce(async (note: Note): Promise => note); - jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); - jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(null); + jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(null); const savedAlias = await service.addAlias(note, alias2); expect(savedAlias.name).toEqual(alias2); expect(savedAlias.primary).toBeFalsy(); @@ -230,7 +230,7 @@ describe('AliasService', () => { it('mark the alias as primary', async () => { jest - .spyOn(aliasRepo, 'findOne') + .spyOn(aliasRepo, 'findOneBy') .mockResolvedValueOnce(alias) .mockResolvedValueOnce(alias2); jest diff --git a/src/notes/alias.service.ts b/src/notes/alias.service.ts index 12bccb207..ccb43a2d0 100644 --- a/src/notes/alias.service.ts +++ b/src/notes/alias.service.ts @@ -45,7 +45,7 @@ export class AliasService { const foundAlias = await this.aliasRepository.findOne({ where: { name: alias }, }); - if (foundAlias !== undefined) { + if (foundAlias !== null) { this.logger.debug(`The alias '${alias}' is already used.`, 'addAlias'); throw new AlreadyInDBError(`The alias '${alias}' is already used.`); } @@ -53,7 +53,7 @@ export class AliasService { const foundNote = await this.noteRepository.findOne({ where: { publicId: alias }, }); - if (foundNote !== undefined) { + if (foundNote !== null) { this.logger.debug( `The alias '${alias}' is already a public id.`, 'addAlias', @@ -113,8 +113,12 @@ export class AliasService { throw new NotInDBError(`The alias '${alias}' is not used by this note.`); } - const oldPrimary = await this.aliasRepository.findOne(oldPrimaryId); - const newPrimary = await this.aliasRepository.findOne(newPrimaryId); + const oldPrimary = await this.aliasRepository.findOneBy({ + id: oldPrimaryId, + }); + const newPrimary = await this.aliasRepository.findOneBy({ + id: newPrimaryId, + }); if (!oldPrimary || !newPrimary) { throw new Error('This should not happen!'); diff --git a/src/notes/notes.service.spec.ts b/src/notes/notes.service.spec.ts index e3c763785..0650e5746 100644 --- a/src/notes/notes.service.spec.ts +++ b/src/notes/notes.service.spec.ts @@ -6,7 +6,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { DataSource, EntityManager, Repository } from 'typeorm'; import { AuthToken } from '../auth/auth-token.entity'; import { Author } from '../authors/author.entity'; @@ -121,7 +121,16 @@ describe('NotesService', () => { * the overrideProvider call, as otherwise we have two instances * and the mock of createQueryBuilder replaces the wrong one * **/ - userRepo = new Repository(); + userRepo = new Repository( + '', + new EntityManager( + new DataSource({ + type: 'sqlite', + database: ':memory:', + }), + ), + undefined, + ); const module: TestingModule = await Test.createTestingModule({ providers: [ NotesService, @@ -202,7 +211,7 @@ describe('NotesService', () => { const note = Note.create(user, alias) as Note; it('with no note', async () => { - jest.spyOn(noteRepo, 'find').mockResolvedValueOnce(undefined); + jest.spyOn(noteRepo, 'find').mockResolvedValueOnce(null); const notes = await service.getUserNotes(user); expect(notes).toEqual([]); }); @@ -374,7 +383,7 @@ describe('NotesService', () => { where: () => createQueryBuilder, orWhere: () => createQueryBuilder, setParameter: () => createQueryBuilder, - getOne: () => undefined, + getOne: () => null, }; jest .spyOn(noteRepo, 'createQueryBuilder') diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index ac30f672a..96fd1fe6c 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -5,7 +5,7 @@ */ import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { Equal, Repository } from 'typeorm'; import noteConfiguration, { NoteConfig } from '../config/note.config'; import { @@ -56,7 +56,7 @@ export class NotesService { */ async getUserNotes(user: User): Promise { const notes = await this.noteRepository.find({ - where: { owner: user }, + where: { owner: Equal(user) }, relations: [ 'owner', 'userPermissions', @@ -65,7 +65,7 @@ export class NotesService { 'aliases', ], }); - if (notes === undefined) { + if (notes === null) { return []; } return notes; @@ -188,7 +188,7 @@ export class NotesService { .setParameter('noteIdOrAlias', noteIdOrAlias) .getOne(); - if (note === undefined) { + if (note === null) { this.logger.debug( `Could not find note '${noteIdOrAlias}'`, 'getNoteByIdOrAlias', diff --git a/src/permissions/note-group-permission.entity.ts b/src/permissions/note-group-permission.entity.ts index 52e18e662..275259456 100644 --- a/src/permissions/note-group-permission.entity.ts +++ b/src/permissions/note-group-permission.entity.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { Column, Entity, ManyToOne } from 'typeorm'; +import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm'; import { Group } from '../groups/group.entity'; import { Note } from '../notes/note.entity'; @@ -17,14 +17,18 @@ export class NoteGroupPermission { * See https://github.com/typeorm/typeorm/issues/6908 */ + @PrimaryColumn() + groupId: number; + @ManyToOne((_) => Group, { - primary: true, onDelete: 'CASCADE', // This deletes the NoteGroupPermission, when the associated Group is deleted }) group: Group; + @PrimaryColumn() + noteId: string; + @ManyToOne((_) => Note, (note) => note.groupPermissions, { - primary: true, onDelete: 'CASCADE', // This deletes the NoteGroupPermission, when the associated Note is deleted }) note: Note; diff --git a/src/permissions/note-user-permission.entity.ts b/src/permissions/note-user-permission.entity.ts index 4f73bd6b4..accc714ff 100644 --- a/src/permissions/note-user-permission.entity.ts +++ b/src/permissions/note-user-permission.entity.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { Column, Entity, ManyToOne } from 'typeorm'; +import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm'; import { Note } from '../notes/note.entity'; import { User } from '../users/user.entity'; @@ -17,15 +17,19 @@ export class NoteUserPermission { * See https://github.com/typeorm/typeorm/issues/6908 */ + @PrimaryColumn() + userId: string; + @ManyToOne((_) => User, { - primary: true, onDelete: 'CASCADE', // This deletes the NoteUserPermission, when the associated Note is deleted orphanedRowAction: 'delete', // This ensures the whole row is deleted when the Permission stops being referenced }) user: User; + @PrimaryColumn() + noteId: string; + @ManyToOne((_) => Note, (note) => note.userPermissions, { - primary: true, onDelete: 'CASCADE', // This deletes the NoteUserPermission, when the associated Note is deleted orphanedRowAction: 'delete', // This ensures the whole row is deleted when the Permission stops being referenced }) diff --git a/src/permissions/permissions.service.spec.ts b/src/permissions/permissions.service.spec.ts index 2c6c3e267..b6c2379c2 100644 --- a/src/permissions/permissions.service.spec.ts +++ b/src/permissions/permissions.service.spec.ts @@ -6,7 +6,7 @@ import { ConfigModule } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { DataSource, EntityManager, Repository } from 'typeorm'; import { AuthToken } from '../auth/auth-token.entity'; import { Author } from '../authors/author.entity'; @@ -49,8 +49,26 @@ describe('PermissionsService', () => { * array and the overrideProvider call, as otherwise we have two instances * and the mock of createQueryBuilder replaces the wrong one * **/ - userRepo = new Repository(); - noteRepo = new Repository(); + userRepo = new Repository( + '', + new EntityManager( + new DataSource({ + type: 'sqlite', + database: ':memory:', + }), + ), + undefined, + ); + noteRepo = new Repository( + '', + new EntityManager( + new DataSource({ + type: 'sqlite', + database: ':memory:', + }), + ), + undefined, + ); const module: TestingModule = await Test.createTestingModule({ providers: [ PermissionsService, @@ -655,7 +673,9 @@ describe('PermissionsService', () => { const noteWithPreexistingPermissions: Note = { ...note }; noteWithPreexistingPermissions.userPermissions = Promise.resolve([ { + noteId: '', note: noteWithPreexistingPermissions, + userId: '', user: user, canEdit: !userPermissionUpdate.canEdit, }, @@ -727,7 +747,9 @@ describe('PermissionsService', () => { const noteWithUserPermission: Note = { ...note }; noteWithUserPermission.userPermissions = Promise.resolve([ { + noteId: '', note: noteWithUserPermission, + userId: '', user: user, canEdit: !userPermissionUpdate.canEdit, }, @@ -763,7 +785,9 @@ describe('PermissionsService', () => { const noteWithPreexistingPermissions: Note = { ...note }; noteWithPreexistingPermissions.groupPermissions = Promise.resolve([ { + noteId: '', note: noteWithPreexistingPermissions, + groupId: 0, group: group, canEdit: !groupPermissionUpdate.canEdit, }, @@ -793,7 +817,9 @@ describe('PermissionsService', () => { const noteWithPreexistingPermissions: Note = { ...note }; noteWithPreexistingPermissions.groupPermissions = Promise.resolve([ { + noteId: '', note: noteWithPreexistingPermissions, + groupId: 0, group: group, canEdit: !groupPermissionUpdate.canEdit, }, @@ -829,14 +855,18 @@ describe('PermissionsService', () => { const noteWithPreexistingPermissions: Note = { ...note }; noteWithPreexistingPermissions.groupPermissions = Promise.resolve([ { + noteId: '', note: noteWithPreexistingPermissions, + groupId: 0, group: group, canEdit: !groupPermissionUpdate.canEdit, }, ]); noteWithPreexistingPermissions.userPermissions = Promise.resolve([ { + noteId: '', note: noteWithPreexistingPermissions, + userId: '', user: user, canEdit: !userPermissionUpdate.canEdit, }, diff --git a/src/revisions/revisions.service.spec.ts b/src/revisions/revisions.service.spec.ts index a3ec0a9aa..321bb882e 100644 --- a/src/revisions/revisions.service.spec.ts +++ b/src/revisions/revisions.service.spec.ts @@ -98,7 +98,7 @@ describe('RevisionsService', () => { expect(await service.getRevision({} as Note, 1)).toEqual(revision); }); it('throws if the revision is not in the databse', async () => { - jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(null); await expect(service.getRevision({} as Note, 1)).rejects.toThrow( NotInDBError, ); diff --git a/src/revisions/revisions.service.ts b/src/revisions/revisions.service.ts index 73b542179..7a3407d3f 100644 --- a/src/revisions/revisions.service.ts +++ b/src/revisions/revisions.service.ts @@ -5,7 +5,7 @@ */ import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { Equal, Repository } from 'typeorm'; import { NotInDBError } from '../errors/errors'; import { ConsoleLoggerService } from '../logger/console-logger.service'; @@ -36,7 +36,7 @@ export class RevisionsService { async getAllRevisions(note: Note): Promise { return await this.revisionRepository.find({ where: { - note: note, + note: Equal(note), }, }); } @@ -50,7 +50,7 @@ export class RevisionsService { async purgeRevisions(note: Note): Promise { const revisions = await this.revisionRepository.find({ where: { - note: note, + note: Equal(note), }, }); const latestRevison = await this.getLatestRevision(note); @@ -66,10 +66,10 @@ export class RevisionsService { const revision = await this.revisionRepository.findOne({ where: { id: revisionId, - note: note, + note: Equal(note), }, }); - if (revision === undefined) { + if (revision === null) { throw new NotInDBError( `Revision with ID ${revisionId} for note ${note.id} not found.`, ); @@ -80,14 +80,14 @@ export class RevisionsService { async getLatestRevision(note: Note): Promise { const revision = await this.revisionRepository.findOne({ where: { - note: note, + note: Equal(note), }, order: { createdAt: 'DESC', id: 'DESC', }, }); - if (revision === undefined) { + if (revision === null) { throw new NotInDBError(`Revision for note ${note.id} not found.`); } return revision; @@ -96,13 +96,13 @@ export class RevisionsService { async getFirstRevision(note: Note): Promise { const revision = await this.revisionRepository.findOne({ where: { - note: note, + note: Equal(note), }, order: { createdAt: 'ASC', }, }); - if (revision === undefined) { + if (revision === null) { throw new NotInDBError(`Revision for note ${note.id} not found.`); } return revision; diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts index a3a723b85..d32da35cc 100644 --- a/src/users/users.service.spec.ts +++ b/src/users/users.service.spec.ts @@ -115,7 +115,7 @@ describe('UsersService', () => { expect(getUser.displayName).toEqual(displayname); }); it('fails when user does not exits', async () => { - jest.spyOn(userRepo, 'findOne').mockResolvedValueOnce(undefined); + jest.spyOn(userRepo, 'findOne').mockResolvedValueOnce(null); await expect(service.getUserByUsername(username)).rejects.toThrow( NotInDBError, ); diff --git a/src/users/users.service.ts b/src/users/users.service.ts index ec7d875e2..3664e75bf 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -89,7 +89,7 @@ export class UsersService { where: { username: username }, relations: withRelations, }); - if (user === undefined) { + if (user === null) { throw new NotInDBError(`User with username '${username}' not found`); } return user; diff --git a/test/private-api/history.e2e-spec.ts b/test/private-api/history.e2e-spec.ts index 1d9d60771..d6402289d 100644 --- a/test/private-api/history.e2e-spec.ts +++ b/test/private-api/history.e2e-spec.ts @@ -190,7 +190,7 @@ describe('History', () => { const alias = (await entry.note.aliases).filter((alias) => alias.primary)[0] .name; await agent - .put(`/api/private/me/history/${alias || 'undefined'}`) + .put(`/api/private/me/history/${alias || 'null'}`) .send({ pinStatus: true }) .expect(200); const userEntries = await testSetup.historyService.getEntriesByUser(user); @@ -206,7 +206,7 @@ describe('History', () => { const entry2 = await historyService.updateHistoryEntryTimestamp(note, user); const entryDto = await historyService.toHistoryEntryDto(entry2); await agent - .delete(`/api/private/me/history/${alias || 'undefined'}`) + .delete(`/api/private/me/history/${alias || 'null'}`) .expect(204); const userEntries = await historyService.getEntriesByUser(user); expect(userEntries.length).toEqual(1); diff --git a/test/public-api/me.e2e-spec.ts b/test/public-api/me.e2e-spec.ts index 1ecd6902d..91fd1ea67 100644 --- a/test/public-api/me.e2e-spec.ts +++ b/test/public-api/me.e2e-spec.ts @@ -114,6 +114,7 @@ describe('Me', () => { const history = await testSetup.historyService.getEntriesByUser(user); const historyEntry: HistoryEntryDto = response.body; expect(historyEntry.pinStatus).toEqual(true); + let theEntry: HistoryEntryDto; for (const entry of history) { if (