refactor(note): lazy-load relations

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-11-30 16:46:07 +01:00
parent d761ff7f4f
commit 235e4f647c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
23 changed files with 343 additions and 284 deletions

View file

@ -56,7 +56,7 @@ export class AliasController {
const note = await this.noteService.getNoteByIdOrAlias( const note = await this.noteService.getNoteByIdOrAlias(
newAliasDto.noteIdOrAlias, newAliasDto.noteIdOrAlias,
); );
if (!this.permissionsService.isOwner(user, note)) { if (!(await this.permissionsService.isOwner(user, note))) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
const updatedAlias = await this.aliasService.addAlias( const updatedAlias = await this.aliasService.addAlias(
@ -88,7 +88,7 @@ export class AliasController {
} }
try { try {
const note = await this.noteService.getNoteByIdOrAlias(alias); const note = await this.noteService.getNoteByIdOrAlias(alias);
if (!this.permissionsService.isOwner(user, note)) { if (!(await this.permissionsService.isOwner(user, note))) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
const updatedAlias = await this.aliasService.makeAliasPrimary( const updatedAlias = await this.aliasService.makeAliasPrimary(
@ -115,7 +115,7 @@ export class AliasController {
): Promise<void> { ): Promise<void> {
try { try {
const note = await this.noteService.getNoteByIdOrAlias(alias); const note = await this.noteService.getNoteByIdOrAlias(alias);
if (!this.permissionsService.isOwner(user, note)) { if (!(await this.permissionsService.isOwner(user, note))) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
await this.aliasService.removeAlias(note, alias); await this.aliasService.removeAlias(note, alias);

View file

@ -45,8 +45,10 @@ export class HistoryController {
async getHistory(@RequestUser() user: User): Promise<HistoryEntryDto[]> { async getHistory(@RequestUser() user: User): Promise<HistoryEntryDto[]> {
try { try {
const foundEntries = await this.historyService.getEntriesByUser(user); const foundEntries = await this.historyService.getEntriesByUser(user);
return foundEntries.map((entry) => return await Promise.all(
this.historyService.toHistoryEntryDto(entry), foundEntries.map((entry) =>
this.historyService.toHistoryEntryDto(entry),
),
); );
} catch (e) { } catch (e) {
if (e instanceof NotInDBError) { if (e instanceof NotInDBError) {
@ -96,7 +98,7 @@ export class HistoryController {
user, user,
entryUpdateDto, entryUpdateDto,
); );
return this.historyService.toHistoryEntryDto(newEntry); return await this.historyService.toHistoryEntryDto(newEntry);
} catch (e) { } catch (e) {
if (e instanceof NotInDBError) { if (e instanceof NotInDBError) {
throw new NotFoundException(e.message); throw new NotFoundException(e.message);

View file

@ -69,7 +69,7 @@ export class AliasController {
const note = await this.noteService.getNoteByIdOrAlias( const note = await this.noteService.getNoteByIdOrAlias(
newAliasDto.noteIdOrAlias, newAliasDto.noteIdOrAlias,
); );
if (!this.permissionsService.isOwner(user, note)) { if (!(await this.permissionsService.isOwner(user, note))) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
const updatedAlias = await this.aliasService.addAlias( const updatedAlias = await this.aliasService.addAlias(
@ -107,7 +107,7 @@ export class AliasController {
} }
try { try {
const note = await this.noteService.getNoteByIdOrAlias(alias); const note = await this.noteService.getNoteByIdOrAlias(alias);
if (!this.permissionsService.isOwner(user, note)) { if (!(await this.permissionsService.isOwner(user, note))) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
const updatedAlias = await this.aliasService.makeAliasPrimary( const updatedAlias = await this.aliasService.makeAliasPrimary(
@ -139,7 +139,7 @@ export class AliasController {
): Promise<void> { ): Promise<void> {
try { try {
const note = await this.noteService.getNoteByIdOrAlias(alias); const note = await this.noteService.getNoteByIdOrAlias(alias);
if (!this.permissionsService.isOwner(user, note)) { if (!(await this.permissionsService.isOwner(user, note))) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
await this.aliasService.removeAlias(note, alias); await this.aliasService.removeAlias(note, alias);

View file

@ -101,7 +101,7 @@ export class MeController {
): Promise<HistoryEntryDto> { ): Promise<HistoryEntryDto> {
try { try {
const foundEntry = await this.historyService.getEntryByNote(note, user); const foundEntry = await this.historyService.getEntryByNote(note, user);
return this.historyService.toHistoryEntryDto(foundEntry); return await this.historyService.toHistoryEntryDto(foundEntry);
} catch (e) { } catch (e) {
if (e instanceof NotInDBError) { if (e instanceof NotInDBError) {
throw new NotFoundException(e.message); throw new NotFoundException(e.message);
@ -126,7 +126,7 @@ export class MeController {
): Promise<HistoryEntryDto> { ): Promise<HistoryEntryDto> {
// ToDo: Check if user is allowed to pin this history entry // ToDo: Check if user is allowed to pin this history entry
try { try {
return this.historyService.toHistoryEntryDto( return await this.historyService.toHistoryEntryDto(
await this.historyService.updateHistoryEntry( await this.historyService.updateHistoryEntry(
note, note,
user, user,

View file

@ -16,7 +16,7 @@ import {
Post, Post,
Put, Put,
UseGuards, UseGuards,
UseInterceptors UseInterceptors,
} from '@nestjs/common'; } from '@nestjs/common';
import { import {
ApiCreatedResponse, ApiCreatedResponse,
@ -26,17 +26,24 @@ import {
ApiProduces, ApiProduces,
ApiSecurity, ApiSecurity,
ApiTags, ApiTags,
ApiUnauthorizedResponse ApiUnauthorizedResponse,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { TokenAuthGuard } from '../../../auth/token.strategy'; import { TokenAuthGuard } from '../../../auth/token.strategy';
import { AlreadyInDBError, ForbiddenIdError, NotInDBError } from '../../../errors/errors'; import {
AlreadyInDBError,
ForbiddenIdError,
NotInDBError,
} from '../../../errors/errors';
import { HistoryService } from '../../../history/history.service'; import { HistoryService } from '../../../history/history.service';
import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../../logger/console-logger.service';
import { MediaUploadDto } from '../../../media/media-upload.dto'; import { MediaUploadDto } from '../../../media/media-upload.dto';
import { MediaService } from '../../../media/media.service'; import { MediaService } from '../../../media/media.service';
import { NoteMetadataDto } from '../../../notes/note-metadata.dto'; import { NoteMetadataDto } from '../../../notes/note-metadata.dto';
import { NotePermissionsDto, NotePermissionsUpdateDto } from '../../../notes/note-permissions.dto'; import {
NotePermissionsDto,
NotePermissionsUpdateDto,
} from '../../../notes/note-permissions.dto';
import { NoteDto } from '../../../notes/note.dto'; import { NoteDto } from '../../../notes/note.dto';
import { Note } from '../../../notes/note.entity'; import { Note } from '../../../notes/note.entity';
import { NoteMediaDeletionDto } from '../../../notes/note.media-deletion.dto'; import { NoteMediaDeletionDto } from '../../../notes/note.media-deletion.dto';
@ -50,7 +57,7 @@ import { User } from '../../../users/user.entity';
import { import {
forbiddenDescription, forbiddenDescription,
successfullyDeletedDescription, successfullyDeletedDescription,
unauthorizedDescription unauthorizedDescription,
} from '../../utils/descriptions'; } from '../../utils/descriptions';
import { FullApi } from '../../utils/fullapi-decorator'; import { FullApi } from '../../utils/fullapi-decorator';
import { GetNoteInterceptor } from '../../utils/get-note.interceptor'; import { GetNoteInterceptor } from '../../utils/get-note.interceptor';
@ -230,7 +237,7 @@ export class NotesController {
@RequestNote() note: Note, @RequestNote() note: Note,
@Body() updateDto: NotePermissionsUpdateDto, @Body() updateDto: NotePermissionsUpdateDto,
): Promise<NotePermissionsDto> { ): Promise<NotePermissionsDto> {
return this.noteService.toNotePermissionsDto( return await this.noteService.toNotePermissionsDto(
await this.noteService.updateNotePermissions(note, updateDto), await this.noteService.updateNotePermissions(note, updateDto),
); );
} }

View file

@ -159,9 +159,9 @@ describe('HistoryService', () => {
Note.create(user, alias) as Note, Note.create(user, alias) as Note,
user, user,
); );
expect(createHistoryEntry.note.aliases).toHaveLength(1); expect(await createHistoryEntry.note.aliases).toHaveLength(1);
expect(createHistoryEntry.note.aliases[0].name).toEqual(alias); expect((await createHistoryEntry.note.aliases)[0].name).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user); expect(await createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user); expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(false); expect(createHistoryEntry.pinStatus).toEqual(false);
}); });
@ -177,9 +177,9 @@ describe('HistoryService', () => {
Note.create(user, alias) as Note, Note.create(user, alias) as Note,
user, user,
); );
expect(createHistoryEntry.note.aliases).toHaveLength(1); expect(await createHistoryEntry.note.aliases).toHaveLength(1);
expect(createHistoryEntry.note.aliases[0].name).toEqual(alias); expect((await createHistoryEntry.note.aliases)[0].name).toEqual(alias);
expect(createHistoryEntry.note.owner).toEqual(user); expect(await createHistoryEntry.note.owner).toEqual(user);
expect(createHistoryEntry.user).toEqual(user); expect(createHistoryEntry.user).toEqual(user);
expect(createHistoryEntry.pinStatus).toEqual(false); expect(createHistoryEntry.pinStatus).toEqual(false);
expect(createHistoryEntry.updatedAt.getTime()).toBeGreaterThanOrEqual( expect(createHistoryEntry.updatedAt.getTime()).toBeGreaterThanOrEqual(
@ -223,9 +223,9 @@ describe('HistoryService', () => {
pinStatus: true, pinStatus: true,
}, },
); );
expect(updatedHistoryEntry.note.aliases).toHaveLength(1); expect(await updatedHistoryEntry.note.aliases).toHaveLength(1);
expect(updatedHistoryEntry.note.aliases[0].name).toEqual(alias); expect((await updatedHistoryEntry.note.aliases)[0].name).toEqual(alias);
expect(updatedHistoryEntry.note.owner).toEqual(user); expect(await updatedHistoryEntry.note.owner).toEqual(user);
expect(updatedHistoryEntry.user).toEqual(user); expect(updatedHistoryEntry.user).toEqual(user);
expect(updatedHistoryEntry.pinStatus).toEqual(true); expect(updatedHistoryEntry.pinStatus).toEqual(true);
}); });
@ -371,11 +371,13 @@ describe('HistoryService', () => {
const mockedManager = { const mockedManager = {
find: jest.fn().mockResolvedValueOnce([historyEntry]), find: jest.fn().mockResolvedValueOnce([historyEntry]),
createQueryBuilder: () => createQueryBuilder, createQueryBuilder: () => createQueryBuilder,
remove: jest.fn().mockImplementationOnce((entry: HistoryEntry) => { remove: jest
expect(entry.note.aliases).toHaveLength(1); .fn()
expect(entry.note.aliases[0].name).toEqual(alias); .mockImplementationOnce(async (entry: HistoryEntry) => {
expect(entry.pinStatus).toEqual(false); expect(await entry.note.aliases).toHaveLength(1);
}), expect((await entry.note.aliases)[0].name).toEqual(alias);
expect(entry.pinStatus).toEqual(false);
}),
save: jest.fn().mockImplementationOnce((entry: HistoryEntry) => { save: jest.fn().mockImplementationOnce((entry: HistoryEntry) => {
expect(entry.note.aliases).toEqual( expect(entry.note.aliases).toEqual(
newlyCreatedHistoryEntry.note.aliases, newlyCreatedHistoryEntry.note.aliases,
@ -402,11 +404,13 @@ describe('HistoryService', () => {
const tags = ['tag1', 'tag2']; const tags = ['tag1', 'tag2'];
const note = Note.create(user, alias) as Note; const note = Note.create(user, alias) as Note;
note.title = title; note.title = title;
note.tags = tags.map((tag) => { note.tags = Promise.resolve(
const newTag = new Tag(); tags.map((tag) => {
newTag.name = tag; const newTag = new Tag();
return newTag; newTag.name = tag;
}); return newTag;
}),
);
const historyEntry = HistoryEntry.create(user, note) as HistoryEntry; const historyEntry = HistoryEntry.create(user, note) as HistoryEntry;
historyEntry.pinStatus = true; historyEntry.pinStatus = true;
const createQueryBuilder = { const createQueryBuilder = {
@ -420,7 +424,7 @@ describe('HistoryService', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
.mockImplementation(() => createQueryBuilder); .mockImplementation(() => createQueryBuilder);
const historyEntryDto = service.toHistoryEntryDto(historyEntry); const historyEntryDto = await service.toHistoryEntryDto(historyEntry);
expect(historyEntryDto.pinStatus).toEqual(true); expect(historyEntryDto.pinStatus).toEqual(true);
expect(historyEntryDto.identifier).toEqual(alias); expect(historyEntryDto.identifier).toEqual(alias);
expect(historyEntryDto.tags).toEqual(tags); expect(historyEntryDto.tags).toEqual(tags);

View file

@ -186,11 +186,11 @@ export class HistoryService {
* @param {HistoryEntry} entry - the history entry to use * @param {HistoryEntry} entry - the history entry to use
* @return {HistoryEntryDto} the built HistoryEntryDto * @return {HistoryEntryDto} the built HistoryEntryDto
*/ */
toHistoryEntryDto(entry: HistoryEntry): HistoryEntryDto { async toHistoryEntryDto(entry: HistoryEntry): Promise<HistoryEntryDto> {
return { return {
identifier: getIdentifier(entry), identifier: await getIdentifier(entry),
lastVisited: entry.updatedAt, lastVisited: entry.updatedAt,
tags: this.notesService.toTagList(entry.note), tags: await this.notesService.toTagList(entry.note),
title: entry.note.title ?? '', title: entry.note.title ?? '',
pinStatus: entry.pinStatus, pinStatus: entry.pinStatus,
}; };

View file

@ -18,19 +18,19 @@ describe('getIdentifier', () => {
note = Note.create(user, alias) as Note; note = Note.create(user, alias) as Note;
entry = HistoryEntry.create(user, note) as HistoryEntry; entry = HistoryEntry.create(user, note) as HistoryEntry;
}); });
it('returns the publicId if there are no aliases', () => { it('returns the publicId if there are no aliases', async () => {
note.aliases = undefined as unknown as Alias[]; note.aliases = Promise.resolve(undefined as unknown as Alias[]);
expect(getIdentifier(entry)).toEqual(note.publicId); expect(await getIdentifier(entry)).toEqual(note.publicId);
}); });
it('returns the publicId, if the alias array is empty', () => { it('returns the publicId, if the alias array is empty', async () => {
note.aliases = []; note.aliases = Promise.resolve([]);
expect(getIdentifier(entry)).toEqual(note.publicId); expect(await getIdentifier(entry)).toEqual(note.publicId);
}); });
it('returns the publicId, if the only alias is not primary', () => { it('returns the publicId, if the only alias is not primary', async () => {
note.aliases[0].primary = false; (await note.aliases)[0].primary = false;
expect(getIdentifier(entry)).toEqual(note.publicId); expect(await getIdentifier(entry)).toEqual(note.publicId);
}); });
it('returns the primary alias, if one exists', () => { it('returns the primary alias, if one exists', async () => {
expect(getIdentifier(entry)).toEqual(note.aliases[0].name); expect(await getIdentifier(entry)).toEqual((await note.aliases)[0].name);
}); });
}); });

View file

@ -6,11 +6,12 @@
import { getPrimaryAlias } from '../notes/utils'; import { getPrimaryAlias } from '../notes/utils';
import { HistoryEntry } from './history-entry.entity'; import { HistoryEntry } from './history-entry.entity';
export function getIdentifier(entry: HistoryEntry): string { export async function getIdentifier(entry: HistoryEntry): Promise<string> {
if (!entry.note.aliases || entry.note.aliases.length === 0) { const aliases = await entry.note.aliases;
if (!aliases || aliases.length === 0) {
return entry.note.publicId; return entry.note.publicId;
} }
const primaryAlias = getPrimaryAlias(entry.note); const primaryAlias = await getPrimaryAlias(entry.note);
if (primaryAlias === undefined) { if (primaryAlias === undefined) {
return entry.note.publicId; return entry.note.publicId;
} }

View file

@ -291,7 +291,9 @@ describe('MediaService', () => {
describe('removeNoteFromMediaUpload', () => { describe('removeNoteFromMediaUpload', () => {
it('works', async () => { it('works', async () => {
const mockNote = {} as Note; const mockNote = {} as Note;
mockNote.aliases = [Alias.create('test', mockNote, true) as Alias]; mockNote.aliases = Promise.resolve([
Alias.create('test', mockNote, true) as Alias,
]);
const mockMediaUploadEntry = { const mockMediaUploadEntry = {
id: 'testMediaUpload', id: 'testMediaUpload',
backendData: 'testBackendData', backendData: 'testBackendData',

View file

@ -158,8 +158,11 @@ describe('AliasService', () => {
const alias2 = 'testAlias2'; const alias2 = 'testAlias2';
const user = User.create('hardcoded', 'Testy') as User; const user = User.create('hardcoded', 'Testy') as User;
describe('removes one alias correctly', () => { describe('removes one alias correctly', () => {
const note = Note.create(user, alias) as Note; let note: Note;
note.aliases.push(Alias.create(alias2, note, false) as Alias); beforeAll(async () => {
note = Note.create(user, alias) as Note;
(await note.aliases).push(Alias.create(alias2, note, false) as Alias);
});
it('with two aliases', async () => { it('with two aliases', async () => {
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
@ -170,9 +173,10 @@ describe('AliasService', () => {
async (alias: Alias): Promise<Alias> => alias, async (alias: Alias): Promise<Alias> => alias,
); );
const savedNote = await service.removeAlias(note, alias2); const savedNote = await service.removeAlias(note, alias2);
expect(savedNote.aliases).toHaveLength(1); const aliases = await savedNote.aliases;
expect(savedNote.aliases[0].name).toEqual(alias); expect(aliases).toHaveLength(1);
expect(savedNote.aliases[0].primary).toBeTruthy(); expect(aliases[0].name).toEqual(alias);
expect(aliases[0].primary).toBeTruthy();
}); });
it('with one alias, that is primary', async () => { it('with one alias, that is primary', async () => {
jest jest
@ -184,12 +188,15 @@ describe('AliasService', () => {
async (alias: Alias): Promise<Alias> => alias, async (alias: Alias): Promise<Alias> => alias,
); );
const savedNote = await service.removeAlias(note, alias); const savedNote = await service.removeAlias(note, alias);
expect(savedNote.aliases).toHaveLength(0); expect(await savedNote.aliases).toHaveLength(0);
}); });
}); });
describe('does not remove one alias', () => { describe('does not remove one alias', () => {
const note = Note.create(user, alias) as Note; let note: Note;
note.aliases.push(Alias.create(alias2, note, false) as Alias); beforeEach(async () => {
note = Note.create(user, alias) as Note;
(await note.aliases).push(Alias.create(alias2, note, false) as Alias);
});
it('if the alias is unknown', async () => { it('if the alias is unknown', async () => {
await expect(service.removeAlias(note, 'non existent')).rejects.toThrow( await expect(service.removeAlias(note, 'non existent')).rejects.toThrow(
NotInDBError, NotInDBError,
@ -206,10 +213,18 @@ describe('AliasService', () => {
describe('makeAliasPrimary', () => { describe('makeAliasPrimary', () => {
const user = User.create('hardcoded', 'Testy') as User; const user = User.create('hardcoded', 'Testy') as User;
const aliasName = 'testAlias'; const aliasName = 'testAlias';
const note = Note.create(user, aliasName) as Note; let note: Note;
const alias = Alias.create(aliasName, note, true) as Alias; let alias: Alias;
const alias2 = Alias.create('testAlias2', note, false) as Alias; let alias2: Alias;
note.aliases.push(alias2); beforeEach(async () => {
note = Note.create(user, aliasName) as Note;
alias = Alias.create(aliasName, note, true) as Alias;
alias2 = Alias.create('testAlias2', note, false) as Alias;
(await note.aliases).push(
Alias.create('testAlias2', note, false) as Alias,
);
});
it('mark the alias as primary', async () => { it('mark the alias as primary', async () => {
jest jest
.spyOn(aliasRepo, 'findOne') .spyOn(aliasRepo, 'findOne')
@ -224,10 +239,10 @@ describe('AliasService', () => {
where: () => createQueryBuilder, where: () => createQueryBuilder,
orWhere: () => createQueryBuilder, orWhere: () => createQueryBuilder,
setParameter: () => createQueryBuilder, setParameter: () => createQueryBuilder,
getOne: () => { getOne: async () => {
return { return {
...note, ...note,
aliases: note.aliases.map((anAlias) => { aliases: (await note.aliases).map((anAlias) => {
if (anAlias.primary) { if (anAlias.primary) {
anAlias.primary = false; anAlias.primary = false;
} }

View file

@ -62,13 +62,13 @@ export class AliasService {
); );
} }
let newAlias; let newAlias;
if (note.aliases.length === 0) { if ((await note.aliases).length === 0) {
// the first alias is automatically made the primary alias // the first alias is automatically made the primary alias
newAlias = Alias.create(alias, note, true); newAlias = Alias.create(alias, note, true);
} else { } else {
newAlias = Alias.create(alias, note, false); newAlias = Alias.create(alias, note, false);
} }
note.aliases.push(newAlias as Alias); (await note.aliases).push(newAlias as Alias);
await this.noteRepository.save(note); await this.noteRepository.save(note);
return newAlias as Alias; return newAlias as Alias;
@ -87,7 +87,7 @@ export class AliasService {
let oldPrimaryId = ''; let oldPrimaryId = '';
let newPrimaryId = ''; let newPrimaryId = '';
for (const anAlias of note.aliases) { for (const anAlias of await note.aliases) {
// found old primary // found old primary
if (anAlias.primary) { if (anAlias.primary) {
oldPrimaryId = anAlias.id; oldPrimaryId = anAlias.id;
@ -134,9 +134,9 @@ export class AliasService {
* @throws {PrimaryAliasDeletionForbiddenError} the primary alias can only be deleted if it's the only alias * @throws {PrimaryAliasDeletionForbiddenError} the primary alias can only be deleted if it's the only alias
*/ */
async removeAlias(note: Note, alias: string): Promise<Note> { async removeAlias(note: Note, alias: string): Promise<Note> {
const primaryAlias = getPrimaryAlias(note); const primaryAlias = await getPrimaryAlias(note);
if (primaryAlias === alias && note.aliases.length !== 1) { if (primaryAlias === alias && (await note.aliases).length !== 1) {
this.logger.debug( this.logger.debug(
`The alias '${alias}' is the primary alias, which can only be removed if it's the only alias.`, `The alias '${alias}' is the primary alias, which can only be removed if it's the only alias.`,
'removeAlias', 'removeAlias',
@ -146,10 +146,10 @@ export class AliasService {
); );
} }
const filteredAliases = note.aliases.filter( const filteredAliases = (await note.aliases).filter(
(anAlias) => anAlias.name !== alias, (anAlias) => anAlias.name !== alias,
); );
if (note.aliases.length === filteredAliases.length) { if ((await note.aliases).length === filteredAliases.length) {
this.logger.debug( this.logger.debug(
`The alias '${alias}' is not used by this note or is the primary alias, which can't be removed.`, `The alias '${alias}' is not used by this note or is the primary alias, which can't be removed.`,
'removeAlias', 'removeAlias',
@ -158,13 +158,13 @@ export class AliasService {
`The alias '${alias}' is not used by this note or is the primary alias, which can't be removed.`, `The alias '${alias}' is not used by this note or is the primary alias, which can't be removed.`,
); );
} }
const aliasToDelete = note.aliases.find( const aliasToDelete = (await note.aliases).find(
(anAlias) => anAlias.name === alias, (anAlias) => anAlias.name === alias,
); );
if (aliasToDelete !== undefined) { if (aliasToDelete !== undefined) {
await this.aliasRepository.remove(aliasToDelete); await this.aliasRepository.remove(aliasToDelete);
} }
note.aliases = filteredAliases; note.aliases = Promise.resolve(filteredAliases);
return await this.noteRepository.save(note); return await this.noteRepository.save(note);
} }

View file

@ -36,21 +36,21 @@ export class Note {
(alias) => alias.note, (alias) => alias.note,
{ cascade: true }, // This ensures that embedded Aliases are automatically saved to the database { cascade: true }, // This ensures that embedded Aliases are automatically saved to the database
) )
aliases: Alias[]; aliases: Promise<Alias[]>;
@OneToMany( @OneToMany(
(_) => NoteGroupPermission, (_) => NoteGroupPermission,
(groupPermission) => groupPermission.note, (groupPermission) => groupPermission.note,
{ cascade: true }, // This ensures that embedded NoteGroupPermissions are automatically saved to the database { cascade: true }, // This ensures that embedded NoteGroupPermissions are automatically saved to the database
) )
groupPermissions: NoteGroupPermission[]; groupPermissions: Promise<NoteGroupPermission[]>;
@OneToMany( @OneToMany(
(_) => NoteUserPermission, (_) => NoteUserPermission,
(userPermission) => userPermission.note, (userPermission) => userPermission.note,
{ cascade: true }, // This ensures that embedded NoteUserPermission are automatically saved to the database { cascade: true }, // This ensures that embedded NoteUserPermission are automatically saved to the database
) )
userPermissions: NoteUserPermission[]; userPermissions: Promise<NoteUserPermission[]>;
@Column({ @Column({
nullable: false, nullable: false,
@ -62,16 +62,16 @@ export class Note {
onDelete: 'CASCADE', // This deletes the Note, when the associated User is deleted onDelete: 'CASCADE', // This deletes the Note, when the associated User is deleted
nullable: true, nullable: true,
}) })
owner: User | null; owner: Promise<User | null>;
@OneToMany((_) => Revision, (revision) => revision.note, { cascade: true }) @OneToMany((_) => Revision, (revision) => revision.note, { cascade: true })
revisions: Promise<Revision[]>; revisions: Promise<Revision[]>;
@OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user) @OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user)
historyEntries: HistoryEntry[]; historyEntries: Promise<HistoryEntry[]>;
@OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.note) @OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.note)
mediaUploads: MediaUpload[]; mediaUploads: Promise<MediaUpload[]>;
@Column({ @Column({
nullable: true, nullable: true,
@ -87,7 +87,7 @@ export class Note {
@ManyToMany((_) => Tag, (tag) => tag.notes, { eager: true, cascade: true }) @ManyToMany((_) => Tag, (tag) => tag.notes, { eager: true, cascade: true })
@JoinTable() @JoinTable()
tags: Tag[]; tags: Promise<Tag[]>;
// eslint-disable-next-line @typescript-eslint/no-empty-function // eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {} private constructor() {}
@ -101,18 +101,18 @@ export class Note {
const newNote = new Note(); const newNote = new Note();
newNote.publicId = generatePublicId(); newNote.publicId = generatePublicId();
newNote.aliases = alias newNote.aliases = alias
? [Alias.create(alias, newNote, true) as Alias] ? Promise.resolve([Alias.create(alias, newNote, true) as Alias])
: []; : Promise.resolve([]);
newNote.userPermissions = []; newNote.userPermissions = Promise.resolve([]);
newNote.groupPermissions = []; newNote.groupPermissions = Promise.resolve([]);
newNote.viewCount = 0; newNote.viewCount = 0;
newNote.owner = owner; newNote.owner = Promise.resolve(owner);
newNote.revisions = Promise.resolve([]); newNote.revisions = Promise.resolve([]);
newNote.historyEntries = []; newNote.historyEntries = Promise.resolve([]);
newNote.mediaUploads = []; newNote.mediaUploads = Promise.resolve([]);
newNote.description = null; newNote.description = null;
newNote.title = null; newNote.title = null;
newNote.tags = []; newNote.tags = Promise.resolve([]);
return newNote; return newNote;
} }
} }

View file

@ -168,51 +168,51 @@ describe('NotesService', () => {
const revisions = await newNote.revisions; const revisions = await newNote.revisions;
expect(revisions).toHaveLength(1); expect(revisions).toHaveLength(1);
expect(revisions[0].content).toEqual(content); expect(revisions[0].content).toEqual(content);
expect(newNote.historyEntries).toHaveLength(0); expect(await newNote.historyEntries).toHaveLength(0);
expect(newNote.userPermissions).toHaveLength(0); expect(await newNote.userPermissions).toHaveLength(0);
expect(newNote.groupPermissions).toHaveLength(0); expect(await newNote.groupPermissions).toHaveLength(0);
expect(newNote.tags).toHaveLength(0); expect(await newNote.tags).toHaveLength(0);
expect(newNote.owner).toBeNull(); expect(await newNote.owner).toBeNull();
expect(newNote.aliases).toHaveLength(0); expect(await newNote.aliases).toHaveLength(0);
}); });
it('without alias, with owner', async () => { it('without alias, with owner', async () => {
const newNote = await service.createNote(content, user); const newNote = await service.createNote(content, user);
const revisions = await newNote.revisions; const revisions = await newNote.revisions;
expect(revisions).toHaveLength(1); expect(revisions).toHaveLength(1);
expect(revisions[0].content).toEqual(content); expect(revisions[0].content).toEqual(content);
expect(newNote.historyEntries).toHaveLength(1); expect(await newNote.historyEntries).toHaveLength(1);
expect(newNote.historyEntries[0].user).toEqual(user); expect((await newNote.historyEntries)[0].user).toEqual(user);
expect(newNote.userPermissions).toHaveLength(0); expect(await newNote.userPermissions).toHaveLength(0);
expect(newNote.groupPermissions).toHaveLength(0); expect(await newNote.groupPermissions).toHaveLength(0);
expect(newNote.tags).toHaveLength(0); expect(await newNote.tags).toHaveLength(0);
expect(newNote.owner).toEqual(user); expect(await newNote.owner).toEqual(user);
expect(newNote.aliases).toHaveLength(0); expect(await newNote.aliases).toHaveLength(0);
}); });
it('with alias, without owner', async () => { it('with alias, without owner', async () => {
const newNote = await service.createNote(content, null, alias); const newNote = await service.createNote(content, null, alias);
const revisions = await newNote.revisions; const revisions = await newNote.revisions;
expect(revisions).toHaveLength(1); expect(revisions).toHaveLength(1);
expect(revisions[0].content).toEqual(content); expect(revisions[0].content).toEqual(content);
expect(newNote.historyEntries).toHaveLength(0); expect(await newNote.historyEntries).toHaveLength(0);
expect(newNote.userPermissions).toHaveLength(0); expect(await newNote.userPermissions).toHaveLength(0);
expect(newNote.groupPermissions).toHaveLength(0); expect(await newNote.groupPermissions).toHaveLength(0);
expect(newNote.tags).toHaveLength(0); expect(await newNote.tags).toHaveLength(0);
expect(newNote.owner).toBeNull(); expect(await newNote.owner).toBeNull();
expect(newNote.aliases).toHaveLength(1); expect(await newNote.aliases).toHaveLength(1);
}); });
it('with alias, with owner', async () => { it('with alias, with owner', async () => {
const newNote = await service.createNote(content, user, alias); const newNote = await service.createNote(content, user, alias);
const revisions = await newNote.revisions; const revisions = await newNote.revisions;
expect(revisions).toHaveLength(1); expect(revisions).toHaveLength(1);
expect(revisions[0].content).toEqual(content); expect(revisions[0].content).toEqual(content);
expect(newNote.historyEntries).toHaveLength(1); expect(await newNote.historyEntries).toHaveLength(1);
expect(newNote.historyEntries[0].user).toEqual(user); expect((await newNote.historyEntries)[0].user).toEqual(user);
expect(newNote.userPermissions).toHaveLength(0); expect(await newNote.userPermissions).toHaveLength(0);
expect(newNote.groupPermissions).toHaveLength(0); expect(await newNote.groupPermissions).toHaveLength(0);
expect(newNote.tags).toHaveLength(0); expect(await newNote.tags).toHaveLength(0);
expect(newNote.owner).toEqual(user); expect(await newNote.owner).toEqual(user);
expect(newNote.aliases).toHaveLength(1); expect(await newNote.aliases).toHaveLength(1);
expect(newNote.aliases[0].name).toEqual(alias); expect((await newNote.aliases)[0].name).toEqual(alias);
}); });
}); });
describe('fails:', () => { describe('fails:', () => {
@ -379,8 +379,8 @@ describe('NotesService', () => {
sharedToUsers: [], sharedToUsers: [],
sharedToGroups: [], sharedToGroups: [],
}); });
expect(savedNote.userPermissions).toHaveLength(0); expect(await savedNote.userPermissions).toHaveLength(0);
expect(savedNote.groupPermissions).toHaveLength(0); expect(await savedNote.groupPermissions).toHaveLength(0);
}); });
it('with empty GroupPermissions and with new UserPermissions', async () => { it('with empty GroupPermissions and with new UserPermissions', async () => {
jest jest
@ -393,24 +393,24 @@ describe('NotesService', () => {
sharedToUsers: [userPermissionUpdate], sharedToUsers: [userPermissionUpdate],
sharedToGroups: [], sharedToGroups: [],
}); });
expect(savedNote.userPermissions).toHaveLength(1); expect(await savedNote.userPermissions).toHaveLength(1);
expect(savedNote.userPermissions[0].user.username).toEqual( expect((await savedNote.userPermissions)[0].user.username).toEqual(
userPermissionUpdate.username, userPermissionUpdate.username,
); );
expect(savedNote.userPermissions[0].canEdit).toEqual( expect((await savedNote.userPermissions)[0].canEdit).toEqual(
userPermissionUpdate.canEdit, userPermissionUpdate.canEdit,
); );
expect(savedNote.groupPermissions).toHaveLength(0); expect(await savedNote.groupPermissions).toHaveLength(0);
}); });
it('with empty GroupPermissions and with existing UserPermissions', async () => { it('with empty GroupPermissions and with existing UserPermissions', async () => {
const noteWithPreexistingPermissions: Note = { ...note }; const noteWithPreexistingPermissions: Note = { ...note };
noteWithPreexistingPermissions.userPermissions = [ noteWithPreexistingPermissions.userPermissions = Promise.resolve([
{ {
note: noteWithPreexistingPermissions, note: noteWithPreexistingPermissions,
user: user, user: user,
canEdit: !userPermissionUpdate.canEdit, canEdit: !userPermissionUpdate.canEdit,
}, },
]; ]);
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
.mockImplementationOnce(async (entry: Note) => { .mockImplementationOnce(async (entry: Note) => {
@ -421,14 +421,14 @@ describe('NotesService', () => {
sharedToUsers: [userPermissionUpdate], sharedToUsers: [userPermissionUpdate],
sharedToGroups: [], sharedToGroups: [],
}); });
expect(savedNote.userPermissions).toHaveLength(1); expect(await savedNote.userPermissions).toHaveLength(1);
expect(savedNote.userPermissions[0].user.username).toEqual( expect((await savedNote.userPermissions)[0].user.username).toEqual(
userPermissionUpdate.username, userPermissionUpdate.username,
); );
expect(savedNote.userPermissions[0].canEdit).toEqual( expect((await savedNote.userPermissions)[0].canEdit).toEqual(
userPermissionUpdate.canEdit, userPermissionUpdate.canEdit,
); );
expect(savedNote.groupPermissions).toHaveLength(0); expect(await savedNote.groupPermissions).toHaveLength(0);
}); });
it('with new GroupPermissions and with empty UserPermissions', async () => { it('with new GroupPermissions and with empty UserPermissions', async () => {
jest jest
@ -441,11 +441,11 @@ describe('NotesService', () => {
sharedToUsers: [], sharedToUsers: [],
sharedToGroups: [groupPermissionUpate], sharedToGroups: [groupPermissionUpate],
}); });
expect(savedNote.userPermissions).toHaveLength(0); expect(await savedNote.userPermissions).toHaveLength(0);
expect(savedNote.groupPermissions[0].group.name).toEqual( expect((await savedNote.groupPermissions)[0].group.name).toEqual(
groupPermissionUpate.groupname, groupPermissionUpate.groupname,
); );
expect(savedNote.groupPermissions[0].canEdit).toEqual( expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
groupPermissionUpate.canEdit, groupPermissionUpate.canEdit,
); );
}); });
@ -461,28 +461,28 @@ describe('NotesService', () => {
sharedToUsers: [userPermissionUpdate], sharedToUsers: [userPermissionUpdate],
sharedToGroups: [groupPermissionUpate], sharedToGroups: [groupPermissionUpate],
}); });
expect(savedNote.userPermissions[0].user.username).toEqual( expect((await savedNote.userPermissions)[0].user.username).toEqual(
userPermissionUpdate.username, userPermissionUpdate.username,
); );
expect(savedNote.userPermissions[0].canEdit).toEqual( expect((await savedNote.userPermissions)[0].canEdit).toEqual(
userPermissionUpdate.canEdit, userPermissionUpdate.canEdit,
); );
expect(savedNote.groupPermissions[0].group.name).toEqual( expect((await savedNote.groupPermissions)[0].group.name).toEqual(
groupPermissionUpate.groupname, groupPermissionUpate.groupname,
); );
expect(savedNote.groupPermissions[0].canEdit).toEqual( expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
groupPermissionUpate.canEdit, groupPermissionUpate.canEdit,
); );
}); });
it('with new GroupPermissions and with existing UserPermissions', async () => { it('with new GroupPermissions and with existing UserPermissions', async () => {
const noteWithUserPermission: Note = { ...note }; const noteWithUserPermission: Note = { ...note };
noteWithUserPermission.userPermissions = [ noteWithUserPermission.userPermissions = Promise.resolve([
{ {
note: noteWithUserPermission, note: noteWithUserPermission,
user: user, user: user,
canEdit: !userPermissionUpdate.canEdit, canEdit: !userPermissionUpdate.canEdit,
}, },
]; ]);
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
.mockImplementationOnce(async (entry: Note) => { .mockImplementationOnce(async (entry: Note) => {
@ -497,28 +497,28 @@ describe('NotesService', () => {
sharedToGroups: [groupPermissionUpate], sharedToGroups: [groupPermissionUpate],
}, },
); );
expect(savedNote.userPermissions[0].user.username).toEqual( expect((await savedNote.userPermissions)[0].user.username).toEqual(
userPermissionUpdate.username, userPermissionUpdate.username,
); );
expect(savedNote.userPermissions[0].canEdit).toEqual( expect((await savedNote.userPermissions)[0].canEdit).toEqual(
userPermissionUpdate.canEdit, userPermissionUpdate.canEdit,
); );
expect(savedNote.groupPermissions[0].group.name).toEqual( expect((await savedNote.groupPermissions)[0].group.name).toEqual(
groupPermissionUpate.groupname, groupPermissionUpate.groupname,
); );
expect(savedNote.groupPermissions[0].canEdit).toEqual( expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
groupPermissionUpate.canEdit, groupPermissionUpate.canEdit,
); );
}); });
it('with existing GroupPermissions and with empty UserPermissions', async () => { it('with existing GroupPermissions and with empty UserPermissions', async () => {
const noteWithPreexistingPermissions: Note = { ...note }; const noteWithPreexistingPermissions: Note = { ...note };
noteWithPreexistingPermissions.groupPermissions = [ noteWithPreexistingPermissions.groupPermissions = Promise.resolve([
{ {
note: noteWithPreexistingPermissions, note: noteWithPreexistingPermissions,
group: group, group: group,
canEdit: !groupPermissionUpate.canEdit, canEdit: !groupPermissionUpate.canEdit,
}, },
]; ]);
jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(group); jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(group);
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
@ -532,23 +532,23 @@ describe('NotesService', () => {
sharedToGroups: [groupPermissionUpate], sharedToGroups: [groupPermissionUpate],
}, },
); );
expect(savedNote.userPermissions).toHaveLength(0); expect(await savedNote.userPermissions).toHaveLength(0);
expect(savedNote.groupPermissions[0].group.name).toEqual( expect((await savedNote.groupPermissions)[0].group.name).toEqual(
groupPermissionUpate.groupname, groupPermissionUpate.groupname,
); );
expect(savedNote.groupPermissions[0].canEdit).toEqual( expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
groupPermissionUpate.canEdit, groupPermissionUpate.canEdit,
); );
}); });
it('with existing GroupPermissions and with new UserPermissions', async () => { it('with existing GroupPermissions and with new UserPermissions', async () => {
const noteWithPreexistingPermissions: Note = { ...note }; const noteWithPreexistingPermissions: Note = { ...note };
noteWithPreexistingPermissions.groupPermissions = [ noteWithPreexistingPermissions.groupPermissions = Promise.resolve([
{ {
note: noteWithPreexistingPermissions, note: noteWithPreexistingPermissions,
group: group, group: group,
canEdit: !groupPermissionUpate.canEdit, canEdit: !groupPermissionUpate.canEdit,
}, },
]; ]);
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
.mockImplementationOnce(async (entry: Note) => { .mockImplementationOnce(async (entry: Note) => {
@ -563,35 +563,35 @@ describe('NotesService', () => {
sharedToGroups: [groupPermissionUpate], sharedToGroups: [groupPermissionUpate],
}, },
); );
expect(savedNote.userPermissions[0].user.username).toEqual( expect((await savedNote.userPermissions)[0].user.username).toEqual(
userPermissionUpdate.username, userPermissionUpdate.username,
); );
expect(savedNote.userPermissions[0].canEdit).toEqual( expect((await savedNote.userPermissions)[0].canEdit).toEqual(
userPermissionUpdate.canEdit, userPermissionUpdate.canEdit,
); );
expect(savedNote.groupPermissions[0].group.name).toEqual( expect((await savedNote.groupPermissions)[0].group.name).toEqual(
groupPermissionUpate.groupname, groupPermissionUpate.groupname,
); );
expect(savedNote.groupPermissions[0].canEdit).toEqual( expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
groupPermissionUpate.canEdit, groupPermissionUpate.canEdit,
); );
}); });
it('with existing GroupPermissions and with existing UserPermissions', async () => { it('with existing GroupPermissions and with existing UserPermissions', async () => {
const noteWithPreexistingPermissions: Note = { ...note }; const noteWithPreexistingPermissions: Note = { ...note };
noteWithPreexistingPermissions.groupPermissions = [ noteWithPreexistingPermissions.groupPermissions = Promise.resolve([
{ {
note: noteWithPreexistingPermissions, note: noteWithPreexistingPermissions,
group: group, group: group,
canEdit: !groupPermissionUpate.canEdit, canEdit: !groupPermissionUpate.canEdit,
}, },
]; ]);
noteWithPreexistingPermissions.userPermissions = [ noteWithPreexistingPermissions.userPermissions = Promise.resolve([
{ {
note: noteWithPreexistingPermissions, note: noteWithPreexistingPermissions,
user: user, user: user,
canEdit: !userPermissionUpdate.canEdit, canEdit: !userPermissionUpdate.canEdit,
}, },
]; ]);
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
.mockImplementationOnce(async (entry: Note) => { .mockImplementationOnce(async (entry: Note) => {
@ -606,16 +606,16 @@ describe('NotesService', () => {
sharedToGroups: [groupPermissionUpate], sharedToGroups: [groupPermissionUpate],
}, },
); );
expect(savedNote.userPermissions[0].user.username).toEqual( expect((await savedNote.userPermissions)[0].user.username).toEqual(
userPermissionUpdate.username, userPermissionUpdate.username,
); );
expect(savedNote.userPermissions[0].canEdit).toEqual( expect((await savedNote.userPermissions)[0].canEdit).toEqual(
userPermissionUpdate.canEdit, userPermissionUpdate.canEdit,
); );
expect(savedNote.groupPermissions[0].group.name).toEqual( expect((await savedNote.groupPermissions)[0].group.name).toEqual(
groupPermissionUpate.groupname, groupPermissionUpate.groupname,
); );
expect(savedNote.groupPermissions[0].canEdit).toEqual( expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
groupPermissionUpate.canEdit, groupPermissionUpate.canEdit,
); );
}); });
@ -653,16 +653,16 @@ describe('NotesService', () => {
describe('toTagList', () => { describe('toTagList', () => {
it('works', async () => { it('works', async () => {
const note = {} as Note; const note = {} as Note;
note.tags = [ note.tags = Promise.resolve([
{ {
id: 1, id: 1,
name: 'testTag', name: 'testTag',
notes: [note], notes: [note],
}, },
]; ]);
const tagList = service.toTagList(note); const tagList = await service.toTagList(note);
expect(tagList).toHaveLength(1); expect(tagList).toHaveLength(1);
expect(tagList[0]).toEqual(note.tags[0].name); expect(tagList[0]).toEqual((await note.tags)[0].name);
}); });
}); });
@ -671,21 +671,21 @@ describe('NotesService', () => {
const user = User.create('hardcoded', 'Testy') as User; const user = User.create('hardcoded', 'Testy') as User;
const group = Group.create('testGroup', 'testGroup', false) as Group; const group = Group.create('testGroup', 'testGroup', false) as Group;
const note = Note.create(user) as Note; const note = Note.create(user) as Note;
note.userPermissions = [ note.userPermissions = Promise.resolve([
{ {
note: note, note: note,
user: user, user: user,
canEdit: true, canEdit: true,
}, },
]; ]);
note.groupPermissions = [ note.groupPermissions = Promise.resolve([
{ {
note: note, note: note,
group: group, group: group,
canEdit: true, canEdit: true,
}, },
]; ]);
const permissions = service.toNotePermissionsDto(note); const permissions = await service.toNotePermissionsDto(note);
expect(permissions.owner).not.toEqual(null); expect(permissions.owner).not.toEqual(null);
expect(permissions.owner?.username).toEqual(user.username); expect(permissions.owner?.username).toEqual(user.username);
expect(permissions.sharedToUsers).toHaveLength(1); expect(permissions.sharedToUsers).toHaveLength(1);
@ -740,36 +740,38 @@ describe('NotesService', () => {
// @ts-ignore // @ts-ignore
.mockImplementation(() => createQueryBuilder); .mockImplementation(() => createQueryBuilder);
note.publicId = 'testId'; note.publicId = 'testId';
note.aliases = [Alias.create('testAlias', note, true) as Alias]; note.aliases = Promise.resolve([
Alias.create('testAlias', note, true) as Alias,
]);
note.title = 'testTitle'; note.title = 'testTitle';
note.description = 'testDescription'; note.description = 'testDescription';
note.owner = user; note.owner = Promise.resolve(user);
note.userPermissions = [ note.userPermissions = Promise.resolve([
{ {
note: note, note: note,
user: user, user: user,
canEdit: true, canEdit: true,
}, },
]; ]);
note.groupPermissions = [ note.groupPermissions = Promise.resolve([
{ {
note: note, note: note,
group: group, group: group,
canEdit: true, canEdit: true,
}, },
]; ]);
note.tags = [ note.tags = Promise.resolve([
{ {
id: 1, id: 1,
name: 'testTag', name: 'testTag',
notes: [note], notes: [note],
}, },
]; ]);
note.viewCount = 1337; note.viewCount = 1337;
const metadataDto = await service.toNoteMetadataDto(note); const metadataDto = await service.toNoteMetadataDto(note);
expect(metadataDto.id).toEqual(note.publicId); expect(metadataDto.id).toEqual(note.publicId);
expect(metadataDto.aliases).toHaveLength(1); expect(metadataDto.aliases).toHaveLength(1);
expect(metadataDto.aliases[0]).toEqual(note.aliases[0].name); expect(metadataDto.aliases[0]).toEqual((await note.aliases)[0].name);
expect(metadataDto.title).toEqual(note.title); expect(metadataDto.title).toEqual(note.title);
expect(metadataDto.createTime).toEqual(revisions[0].createdAt); expect(metadataDto.createTime).toEqual(revisions[0].createdAt);
expect(metadataDto.description).toEqual(note.description); expect(metadataDto.description).toEqual(note.description);
@ -787,7 +789,7 @@ describe('NotesService', () => {
).toEqual(group.displayName); ).toEqual(group.displayName);
expect(metadataDto.permissions.sharedToGroups[0].canEdit).toEqual(true); expect(metadataDto.permissions.sharedToGroups[0].canEdit).toEqual(true);
expect(metadataDto.tags).toHaveLength(1); expect(metadataDto.tags).toHaveLength(1);
expect(metadataDto.tags[0]).toEqual(note.tags[0].name); expect(metadataDto.tags[0]).toEqual((await note.tags)[0].name);
expect(metadataDto.updateTime).toEqual(revisions[0].createdAt); expect(metadataDto.updateTime).toEqual(revisions[0].createdAt);
expect(metadataDto.updateUser.username).toEqual(user.username); expect(metadataDto.updateUser.username).toEqual(user.username);
expect(metadataDto.viewCount).toEqual(note.viewCount); expect(metadataDto.viewCount).toEqual(note.viewCount);
@ -840,36 +842,38 @@ describe('NotesService', () => {
// @ts-ignore // @ts-ignore
.mockImplementation(() => createQueryBuilder); .mockImplementation(() => createQueryBuilder);
note.publicId = 'testId'; note.publicId = 'testId';
note.aliases = [Alias.create('testAlias', note, true) as Alias]; note.aliases = Promise.resolve([
Alias.create('testAlias', note, true) as Alias,
]);
note.title = 'testTitle'; note.title = 'testTitle';
note.description = 'testDescription'; note.description = 'testDescription';
note.owner = user; note.owner = Promise.resolve(user);
note.userPermissions = [ note.userPermissions = Promise.resolve([
{ {
note: note, note: note,
user: user, user: user,
canEdit: true, canEdit: true,
}, },
]; ]);
note.groupPermissions = [ note.groupPermissions = Promise.resolve([
{ {
note: note, note: note,
group: group, group: group,
canEdit: true, canEdit: true,
}, },
]; ]);
note.tags = [ note.tags = Promise.resolve([
{ {
id: 1, id: 1,
name: 'testTag', name: 'testTag',
notes: [note], notes: [note],
}, },
]; ]);
note.viewCount = 1337; note.viewCount = 1337;
const noteDto = await service.toNoteDto(note); const noteDto = await service.toNoteDto(note);
expect(noteDto.metadata.id).toEqual(note.publicId); expect(noteDto.metadata.id).toEqual(note.publicId);
expect(noteDto.metadata.aliases).toHaveLength(1); expect(noteDto.metadata.aliases).toHaveLength(1);
expect(noteDto.metadata.aliases[0]).toEqual(note.aliases[0].name); expect(noteDto.metadata.aliases[0]).toEqual((await note.aliases)[0].name);
expect(noteDto.metadata.title).toEqual(note.title); expect(noteDto.metadata.title).toEqual(note.title);
expect(noteDto.metadata.createTime).toEqual(revisions[0].createdAt); expect(noteDto.metadata.createTime).toEqual(revisions[0].createdAt);
expect(noteDto.metadata.description).toEqual(note.description); expect(noteDto.metadata.description).toEqual(note.description);
@ -893,7 +897,7 @@ describe('NotesService', () => {
true, true,
); );
expect(noteDto.metadata.tags).toHaveLength(1); expect(noteDto.metadata.tags).toHaveLength(1);
expect(noteDto.metadata.tags[0]).toEqual(note.tags[0].name); expect(noteDto.metadata.tags[0]).toEqual((await note.tags)[0].name);
expect(noteDto.metadata.updateTime).toEqual(revisions[0].createdAt); expect(noteDto.metadata.updateTime).toEqual(revisions[0].createdAt);
expect(noteDto.metadata.updateUser.username).toEqual(user.username); expect(noteDto.metadata.updateUser.username).toEqual(user.username);
expect(noteDto.metadata.viewCount).toEqual(note.viewCount); expect(noteDto.metadata.viewCount).toEqual(note.viewCount);

View file

@ -100,9 +100,9 @@ export class NotesService {
Revision.create(noteContent, noteContent, newNote as Note) as Revision, Revision.create(noteContent, noteContent, newNote as Note) as Revision,
]); ]);
if (owner) { if (owner) {
newNote.historyEntries = [ newNote.historyEntries = Promise.resolve([
HistoryEntry.create(owner, newNote as Note) as HistoryEntry, HistoryEntry.create(owner, newNote as Note) as HistoryEntry,
]; ]);
} }
try { try {
return await this.noteRepository.save(newNote); return await this.noteRepository.save(newNote);
@ -262,8 +262,8 @@ export class NotesService {
//TODO: Calculate patch //TODO: Calculate patch
revisions.push(Revision.create(noteContent, noteContent, note) as Revision); revisions.push(Revision.create(noteContent, noteContent, note) as Revision);
note.revisions = Promise.resolve(revisions); note.revisions = Promise.resolve(revisions);
note.userPermissions = []; note.userPermissions = Promise.resolve([]);
note.groupPermissions = []; note.groupPermissions = Promise.resolve([]);
return await this.noteRepository.save(note); return await this.noteRepository.save(note);
} }
@ -298,8 +298,8 @@ export class NotesService {
); );
} }
note.userPermissions = []; note.userPermissions = Promise.resolve([]);
note.groupPermissions = []; note.groupPermissions = Promise.resolve([]);
// Create new userPermissions // Create new userPermissions
for (const newUserPermission of newPermissions.sharedToUsers) { for (const newUserPermission of newPermissions.sharedToUsers) {
@ -312,7 +312,7 @@ export class NotesService {
newUserPermission.canEdit, newUserPermission.canEdit,
); );
createdPermission.note = note; createdPermission.note = note;
note.userPermissions.push(createdPermission); (await note.userPermissions).push(createdPermission);
} }
// Create groupPermissions // Create groupPermissions
@ -326,7 +326,7 @@ export class NotesService {
newGroupPermission.canEdit, newGroupPermission.canEdit,
); );
createdPermission.note = note; createdPermission.note = note;
note.groupPermissions.push(createdPermission); (await note.groupPermissions).push(createdPermission);
} }
return await this.noteRepository.save(note); return await this.noteRepository.save(note);
@ -348,7 +348,7 @@ export class NotesService {
)[0].author.user; )[0].author.user;
} }
// If there are no Edits, the owner is the updateUser // If there are no Edits, the owner is the updateUser
return note.owner; return await note.owner;
} }
/** /**
@ -356,8 +356,8 @@ export class NotesService {
* @param {Note} note - the note to use * @param {Note} note - the note to use
* @return {string[]} string array of tags names * @return {string[]} string array of tags names
*/ */
toTagList(note: Note): string[] { async toTagList(note: Note): Promise<string[]> {
return note.tags.map((tag) => tag.name); return (await note.tags).map((tag) => tag.name);
} }
/** /**
@ -365,14 +365,17 @@ export class NotesService {
* @param {Note} note - the note to use * @param {Note} note - the note to use
* @return {NotePermissionsDto} the built NotePermissionDto * @return {NotePermissionsDto} the built NotePermissionDto
*/ */
toNotePermissionsDto(note: Note): NotePermissionsDto { async toNotePermissionsDto(note: Note): Promise<NotePermissionsDto> {
const owner = await note.owner;
const userPermissions = await note.userPermissions;
const groupPermissions = await note.groupPermissions;
return { return {
owner: note.owner ? this.usersService.toUserDto(note.owner) : null, owner: owner ? this.usersService.toUserDto(owner) : null,
sharedToUsers: note.userPermissions.map((noteUserPermission) => ({ sharedToUsers: userPermissions.map((noteUserPermission) => ({
user: this.usersService.toUserDto(noteUserPermission.user), user: this.usersService.toUserDto(noteUserPermission.user),
canEdit: noteUserPermission.canEdit, canEdit: noteUserPermission.canEdit,
})), })),
sharedToGroups: note.groupPermissions.map((noteGroupPermission) => ({ sharedToGroups: groupPermissions.map((noteGroupPermission) => ({
group: this.groupsService.toGroupDto(noteGroupPermission.group), group: this.groupsService.toGroupDto(noteGroupPermission.group),
canEdit: noteGroupPermission.canEdit, canEdit: noteGroupPermission.canEdit,
})), })),
@ -389,14 +392,16 @@ export class NotesService {
const updateUser = await this.calculateUpdateUser(note); const updateUser = await this.calculateUpdateUser(note);
return { return {
id: note.publicId, id: note.publicId,
aliases: note.aliases.map((alias) => alias.name), aliases: await Promise.all(
primaryAlias: getPrimaryAlias(note) ?? null, (await note.aliases).map((alias) => alias.name),
),
primaryAlias: (await getPrimaryAlias(note)) ?? null,
title: note.title ?? '', title: note.title ?? '',
createTime: (await this.getFirstRevision(note)).createdAt, createTime: (await this.getFirstRevision(note)).createdAt,
description: note.description ?? '', description: note.description ?? '',
editedBy: (await this.getAuthorUsers(note)).map((user) => user.username), editedBy: (await this.getAuthorUsers(note)).map((user) => user.username),
permissions: this.toNotePermissionsDto(note), permissions: await this.toNotePermissionsDto(note),
tags: this.toTagList(note), tags: await this.toTagList(note),
updateTime: (await this.getLatestRevision(note)).createdAt, updateTime: (await this.getLatestRevision(note)).createdAt,
updateUser: updateUser ? this.usersService.toUserDto(updateUser) : null, updateUser: updateUser ? this.usersService.toUserDto(updateUser) : null,
viewCount: note.viewCount, viewCount: note.viewCount,

View file

@ -29,12 +29,12 @@ describe('getPrimaryAlias', () => {
const user = User.create('hardcoded', 'Testy') as User; const user = User.create('hardcoded', 'Testy') as User;
note = Note.create(user, alias) as Note; note = Note.create(user, alias) as Note;
}); });
it('finds correct primary alias', () => { it('finds correct primary alias', async () => {
note.aliases.push(Alias.create('annother', note, false) as Alias); (await note.aliases).push(Alias.create('annother', note, false) as Alias);
expect(getPrimaryAlias(note)).toEqual(alias); expect(await getPrimaryAlias(note)).toEqual(alias);
}); });
it('returns undefined if there is no alias', () => { it('returns undefined if there is no alias', async () => {
note.aliases[0].primary = false; (await note.aliases)[0].primary = false;
expect(getPrimaryAlias(note)).toEqual(undefined); expect(await getPrimaryAlias(note)).toEqual(undefined);
}); });
}); });

View file

@ -22,8 +22,8 @@ export function generatePublicId(): string {
* Extract the primary alias from a aliases of a note * Extract the primary alias from a aliases of a note
* @param {Note} note - the note from which the primary alias should be extracted * @param {Note} note - the note from which the primary alias should be extracted
*/ */
export function getPrimaryAlias(note: Note): string | undefined { export async function getPrimaryAlias(note: Note): Promise<string | undefined> {
const listWithPrimaryAlias = note.aliases.filter( const listWithPrimaryAlias = (await note.aliases).filter(
(alias: Alias) => alias.primary, (alias: Alias) => alias.primary,
); );
if (listWithPrimaryAlias.length !== 1) { if (listWithPrimaryAlias.length !== 1) {

View file

@ -30,6 +30,7 @@ import { GuestPermission, PermissionsService } from './permissions.service';
describe('PermissionsService', () => { describe('PermissionsService', () => {
let permissionsService: PermissionsService; let permissionsService: PermissionsService;
let notes: Note[];
beforeAll(async () => { beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
@ -73,6 +74,7 @@ describe('PermissionsService', () => {
.useValue({}) .useValue({})
.compile(); .compile();
permissionsService = module.get<PermissionsService>(PermissionsService); permissionsService = module.get<PermissionsService>(PermissionsService);
notes = await createNoteUserPermissionNotes();
}); });
// The two users we test with: // The two users we test with:
@ -87,16 +89,16 @@ describe('PermissionsService', () => {
function createNote(owner: User): Note { function createNote(owner: User): Note {
const note = {} as Note; const note = {} as Note;
note.userPermissions = []; note.userPermissions = Promise.resolve([]);
note.groupPermissions = []; note.groupPermissions = Promise.resolve([]);
note.owner = owner; note.owner = Promise.resolve(owner);
return note; return note;
} }
/* /*
* Creates the permission objects for UserPermission for two users with write and with out write permission * Creates the permission objects for UserPermission for two users with write and with out write permission
*/ */
function createNoteUserPermissionNotes(): Note[] { async function createNoteUserPermissionNotes(): Promise<Note[]> {
const note0 = createNote(user1); const note0 = createNote(user1);
const note1 = createNote(user2); const note1 = createNote(user2);
const note2 = createNote(user2); const note2 = createNote(user2);
@ -116,23 +118,23 @@ describe('PermissionsService', () => {
noteUserPermission4.user = user2; noteUserPermission4.user = user2;
noteUserPermission4.canEdit = true; noteUserPermission4.canEdit = true;
note1.userPermissions.push(noteUserPermission1); (await note1.userPermissions).push(noteUserPermission1);
note2.userPermissions.push(noteUserPermission1); (await note2.userPermissions).push(noteUserPermission1);
note2.userPermissions.push(noteUserPermission2); (await note2.userPermissions).push(noteUserPermission2);
note3.userPermissions.push(noteUserPermission2); (await note3.userPermissions).push(noteUserPermission2);
note3.userPermissions.push(noteUserPermission1); (await note3.userPermissions).push(noteUserPermission1);
note4.userPermissions.push(noteUserPermission3); (await note4.userPermissions).push(noteUserPermission3);
note5.userPermissions.push(noteUserPermission3); (await note5.userPermissions).push(noteUserPermission3);
note5.userPermissions.push(noteUserPermission4); (await note5.userPermissions).push(noteUserPermission4);
note6.userPermissions.push(noteUserPermission4); (await note6.userPermissions).push(noteUserPermission4);
note6.userPermissions.push(noteUserPermission3); (await note6.userPermissions).push(noteUserPermission3);
note7.userPermissions.push(noteUserPermission2); (await note7.userPermissions).push(noteUserPermission2);
const everybody = {} as Group; const everybody = {} as Group;
everybody.name = SpecialGroup.EVERYONE; everybody.name = SpecialGroup.EVERYONE;
@ -143,7 +145,9 @@ describe('PermissionsService', () => {
noteGroupPermissionRead.group = everybody; noteGroupPermissionRead.group = everybody;
noteGroupPermissionRead.canEdit = false; noteGroupPermissionRead.canEdit = false;
noteGroupPermissionRead.note = noteEverybodyRead; noteGroupPermissionRead.note = noteEverybodyRead;
noteEverybodyRead.groupPermissions = [noteGroupPermissionRead]; noteEverybodyRead.groupPermissions = Promise.resolve([
noteGroupPermissionRead,
]);
const noteEverybodyWrite = createNote(user1); const noteEverybodyWrite = createNote(user1);
@ -151,7 +155,9 @@ describe('PermissionsService', () => {
noteGroupPermissionWrite.group = everybody; noteGroupPermissionWrite.group = everybody;
noteGroupPermissionWrite.canEdit = true; noteGroupPermissionWrite.canEdit = true;
noteGroupPermissionWrite.note = noteEverybodyWrite; noteGroupPermissionWrite.note = noteEverybodyWrite;
noteEverybodyWrite.groupPermissions = [noteGroupPermissionWrite]; noteEverybodyWrite.groupPermissions = Promise.resolve([
noteGroupPermissionWrite,
]);
return [ return [
note0, note0,
@ -167,8 +173,6 @@ describe('PermissionsService', () => {
]; ];
} }
const notes = createNoteUserPermissionNotes();
describe('mayRead works with', () => { describe('mayRead works with', () => {
it('Owner', async () => { it('Owner', async () => {
permissionsService.guestPermission = GuestPermission.DENY; permissionsService.guestPermission = GuestPermission.DENY;
@ -501,7 +505,7 @@ describe('PermissionsService', () => {
let i = 0; let i = 0;
for (const permission of permissions) { for (const permission of permissions) {
const note = createNote(user2); const note = createNote(user2);
note.groupPermissions = permission.permissions; note.groupPermissions = Promise.resolve(permission.permissions);
let permissionString = ''; let permissionString = '';
for (const perm of permission.permissions) { for (const perm of permission.permissions) {
permissionString += ` ${perm.group.name}:${String(perm.canEdit)}`; permissionString += ` ${perm.group.name}:${String(perm.canEdit)}`;
@ -550,13 +554,13 @@ describe('PermissionsService', () => {
}); });
describe('isOwner works', () => { describe('isOwner works', () => {
it('for positive case', () => { it('for positive case', async () => {
permissionsService.guestPermission = GuestPermission.DENY; permissionsService.guestPermission = GuestPermission.DENY;
expect(permissionsService.isOwner(user1, notes[0])).toBeTruthy(); expect(await permissionsService.isOwner(user1, notes[0])).toBeTruthy();
}); });
it('for negative case', () => { it('for negative case', async () => {
permissionsService.guestPermission = GuestPermission.DENY; permissionsService.guestPermission = GuestPermission.DENY;
expect(permissionsService.isOwner(user1, notes[1])).toBeFalsy(); expect(await permissionsService.isOwner(user1, notes[1])).toBeFalsy();
}); });
}); });
}); });

View file

@ -22,9 +22,9 @@ export enum GuestPermission {
export class PermissionsService { export class PermissionsService {
public guestPermission: GuestPermission; // TODO change to configOption public guestPermission: GuestPermission; // TODO change to configOption
async mayRead(user: User | null, note: Note): Promise<boolean> { async mayRead(user: User | null, note: Note): Promise<boolean> {
if (this.isOwner(user, note)) return true; if (await this.isOwner(user, note)) return true;
if (this.hasPermissionUser(user, note, false)) return true; if (await this.hasPermissionUser(user, note, false)) return true;
// noinspection RedundantIfStatementJS // noinspection RedundantIfStatementJS
if (await this.hasPermissionGroup(user, note, false)) return true; if (await this.hasPermissionGroup(user, note, false)) return true;
@ -33,9 +33,9 @@ export class PermissionsService {
} }
async mayWrite(user: User | null, note: Note): Promise<boolean> { async mayWrite(user: User | null, note: Note): Promise<boolean> {
if (this.isOwner(user, note)) return true; if (await this.isOwner(user, note)) return true;
if (this.hasPermissionUser(user, note, true)) return true; if (await this.hasPermissionUser(user, note, true)) return true;
// noinspection RedundantIfStatementJS // noinspection RedundantIfStatementJS
if (await this.hasPermissionGroup(user, note, true)) return true; if (await this.hasPermissionGroup(user, note, true)) return true;
@ -58,21 +58,22 @@ export class PermissionsService {
return false; return false;
} }
isOwner(user: User | null, note: Note): boolean { async isOwner(user: User | null, note: Note): Promise<boolean> {
if (!user) return false; if (!user) return false;
if (!note.owner) return false; const owner = await note.owner;
return note.owner.id === user.id; if (!owner) return false;
return owner.id === user.id;
} }
private hasPermissionUser( private async hasPermissionUser(
user: User | null, user: User | null,
note: Note, note: Note,
wantEdit: boolean, wantEdit: boolean,
): boolean { ): Promise<boolean> {
if (!user) { if (!user) {
return false; return false;
} }
for (const userPermission of note.userPermissions) { for (const userPermission of await note.userPermissions) {
if ( if (
userPermission.user.id === user.id && userPermission.user.id === user.id &&
(userPermission.canEdit || !wantEdit) (userPermission.canEdit || !wantEdit)
@ -99,7 +100,7 @@ export class PermissionsService {
case GuestPermission.READ: case GuestPermission.READ:
guestsAllowed = !wantEdit; guestsAllowed = !wantEdit;
} }
for (const groupPermission of note.groupPermissions) { for (const groupPermission of await note.groupPermissions) {
if (groupPermission.canEdit || !wantEdit) { if (groupPermission.canEdit || !wantEdit) {
// Handle special groups // Handle special groups
if (groupPermission.group.special) { if (groupPermission.group.special) {

View file

@ -76,8 +76,8 @@ createConnection({
const edit = Edit.create(author, 1, 42) as Edit; const edit = Edit.create(author, 1, 42) as Edit;
revision.edits = [edit]; revision.edits = [edit];
notes[i].revisions = Promise.all([revision]); notes[i].revisions = Promise.all([revision]);
notes[i].userPermissions = []; notes[i].userPermissions = Promise.resolve([]);
notes[i].groupPermissions = []; notes[i].groupPermissions = Promise.resolve([]);
user.ownedNotes = [notes[i]]; user.ownedNotes = [notes[i]];
await connection.manager.save([ await connection.manager.save([
notes[i], notes[i],
@ -99,7 +99,7 @@ createConnection({
throw new Error('Could not find freshly seeded notes. Aborting.'); throw new Error('Could not find freshly seeded notes. Aborting.');
} }
for (const note of foundNotes) { for (const note of foundNotes) {
if (!note.aliases[0]) { if (!(await note.aliases)[0]) {
throw new Error( throw new Error(
'Could not find alias of freshly seeded notes. Aborting.', 'Could not find alias of freshly seeded notes. Aborting.',
); );
@ -111,7 +111,7 @@ createConnection({
); );
} }
for (const note of foundNotes) { for (const note of foundNotes) {
console.log(`Created Note '${note.aliases[0].name ?? ''}'`); console.log(`Created Note '${(await note.aliases)[0].name ?? ''}'`);
} }
for (const user of foundUsers) { for (const user of foundUsers) {
for (const note of foundNotes) { for (const note of foundNotes) {
@ -119,7 +119,7 @@ createConnection({
await connection.manager.save(historyEntry); await connection.manager.save(historyEntry);
console.log( console.log(
`Created HistoryEntry for user '${user.username}' and note '${ `Created HistoryEntry for user '${user.username}' and note '${
note.aliases[0].name ?? '' (await note.aliases)[0].name ?? ''
}'`, }'`,
); );
} }

View file

@ -69,7 +69,7 @@ describe('History', () => {
note, note,
user, user,
); );
const entryDto = testSetup.historyService.toHistoryEntryDto(entry); const entryDto = await testSetup.historyService.toHistoryEntryDto(entry);
const response = await agent const response = await agent
.get('/api/private/me/history') .get('/api/private/me/history')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -92,7 +92,7 @@ describe('History', () => {
const pinStatus = true; const pinStatus = true;
const lastVisited = new Date('2020-12-01 12:23:34'); const lastVisited = new Date('2020-12-01 12:23:34');
const postEntryDto = new HistoryEntryImportDto(); const postEntryDto = new HistoryEntryImportDto();
postEntryDto.note = note2.aliases.filter( postEntryDto.note = (await note2.aliases).filter(
(alias) => alias.primary, (alias) => alias.primary,
)[0].name; )[0].name;
postEntryDto.pinStatus = pinStatus; postEntryDto.pinStatus = pinStatus;
@ -104,13 +104,15 @@ describe('History', () => {
.expect(201); .expect(201);
const userEntries = await testSetup.historyService.getEntriesByUser(user); const userEntries = await testSetup.historyService.getEntriesByUser(user);
expect(userEntries.length).toEqual(1); expect(userEntries.length).toEqual(1);
expect(userEntries[0].note.aliases[0].name).toEqual( expect((await userEntries[0].note.aliases)[0].name).toEqual(
note2.aliases[0].name, (await note2.aliases)[0].name,
); );
expect(userEntries[0].note.aliases[0].primary).toEqual( expect((await userEntries[0].note.aliases)[0].primary).toEqual(
note2.aliases[0].primary, (await note2.aliases)[0].primary,
);
expect((await userEntries[0].note.aliases)[0].id).toEqual(
(await note2.aliases)[0].id,
); );
expect(userEntries[0].note.aliases[0].id).toEqual(note2.aliases[0].id);
expect(userEntries[0].user.username).toEqual(user.username); expect(userEntries[0].user.username).toEqual(user.username);
expect(userEntries[0].pinStatus).toEqual(pinStatus); expect(userEntries[0].pinStatus).toEqual(pinStatus);
expect(userEntries[0].updatedAt).toEqual(lastVisited); expect(userEntries[0].updatedAt).toEqual(lastVisited);
@ -129,7 +131,7 @@ describe('History', () => {
pinStatus = !previousHistory[0].pinStatus; pinStatus = !previousHistory[0].pinStatus;
lastVisited = new Date('2020-12-01 23:34:45'); lastVisited = new Date('2020-12-01 23:34:45');
postEntryDto = new HistoryEntryImportDto(); postEntryDto = new HistoryEntryImportDto();
postEntryDto.note = note2.aliases.filter( postEntryDto.note = (await note2.aliases).filter(
(alias) => alias.primary, (alias) => alias.primary,
)[0].name; )[0].name;
postEntryDto.pinStatus = pinStatus; postEntryDto.pinStatus = pinStatus;
@ -188,7 +190,8 @@ describe('History', () => {
user, user,
); );
expect(entry.pinStatus).toBeFalsy(); expect(entry.pinStatus).toBeFalsy();
const alias = entry.note.aliases.filter((alias) => alias.primary)[0].name; const alias = (await entry.note.aliases).filter((alias) => alias.primary)[0]
.name;
await agent await agent
.put(`/api/private/me/history/${alias || 'undefined'}`) .put(`/api/private/me/history/${alias || 'undefined'}`)
.send({ pinStatus: true }) .send({ pinStatus: true })
@ -201,15 +204,16 @@ describe('History', () => {
it('DELETE /me/history/:note', async () => { it('DELETE /me/history/:note', async () => {
const entry = await historyService.updateHistoryEntryTimestamp(note2, user); const entry = await historyService.updateHistoryEntryTimestamp(note2, user);
const alias = entry.note.aliases.filter((alias) => alias.primary)[0].name; const alias = (await entry.note.aliases).filter((alias) => alias.primary)[0]
.name;
const entry2 = await historyService.updateHistoryEntryTimestamp(note, user); const entry2 = await historyService.updateHistoryEntryTimestamp(note, user);
const entryDto = historyService.toHistoryEntryDto(entry2); const entryDto = await historyService.toHistoryEntryDto(entry2);
await agent await agent
.delete(`/api/private/me/history/${alias || 'undefined'}`) .delete(`/api/private/me/history/${alias || 'undefined'}`)
.expect(200); .expect(200);
const userEntries = await historyService.getEntriesByUser(user); const userEntries = await historyService.getEntriesByUser(user);
expect(userEntries.length).toEqual(1); expect(userEntries.length).toEqual(1);
const userEntryDto = historyService.toHistoryEntryDto(userEntries[0]); const userEntryDto = await historyService.toHistoryEntryDto(userEntries[0]);
expect(userEntryDto.identifier).toEqual(entryDto.identifier); expect(userEntryDto.identifier).toEqual(entryDto.identifier);
expect(userEntryDto.title).toEqual(entryDto.title); expect(userEntryDto.title).toEqual(entryDto.title);
expect(userEntryDto.tags).toEqual(entryDto.tags); expect(userEntryDto.tags).toEqual(entryDto.tags);

View file

@ -51,8 +51,9 @@ describe('Me', () => {
.expect(200); .expect(200);
const history: HistoryEntryDto[] = response.body; const history: HistoryEntryDto[] = response.body;
expect(history.length).toEqual(1); expect(history.length).toEqual(1);
const historyDto = const historyDto = await testSetup.historyService.toHistoryEntryDto(
testSetup.historyService.toHistoryEntryDto(createdHistoryEntry); createdHistoryEntry,
);
for (const historyEntry of history) { for (const historyEntry of history) {
expect(historyEntry.identifier).toEqual(historyDto.identifier); expect(historyEntry.identifier).toEqual(historyDto.identifier);
expect(historyEntry.title).toEqual(historyDto.title); expect(historyEntry.title).toEqual(historyDto.title);
@ -75,8 +76,9 @@ describe('Me', () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
const historyEntry: HistoryEntryDto = response.body; const historyEntry: HistoryEntryDto = response.body;
const historyEntryDto = const historyEntryDto = await testSetup.historyService.toHistoryEntryDto(
testSetup.historyService.toHistoryEntryDto(createdHistoryEntry); createdHistoryEntry,
);
expect(historyEntry.identifier).toEqual(historyEntryDto.identifier); expect(historyEntry.identifier).toEqual(historyEntryDto.identifier);
expect(historyEntry.title).toEqual(historyEntryDto.title); expect(historyEntry.title).toEqual(historyEntryDto.title);
expect(historyEntry.tags).toEqual(historyEntryDto.tags); expect(historyEntry.tags).toEqual(historyEntryDto.tags);
@ -109,8 +111,12 @@ describe('Me', () => {
expect(historyEntry.pinStatus).toEqual(true); expect(historyEntry.pinStatus).toEqual(true);
let theEntry: HistoryEntryDto; let theEntry: HistoryEntryDto;
for (const entry of history) { for (const entry of history) {
if (entry.note.aliases.find((element) => element.name === noteName)) { if (
theEntry = testSetup.historyService.toHistoryEntryDto(entry); (await entry.note.aliases).find(
(element) => element.name === noteName,
)
) {
theEntry = await testSetup.historyService.toHistoryEntryDto(entry);
} }
} }
expect(theEntry.pinStatus).toEqual(true); expect(theEntry.pinStatus).toEqual(true);
@ -134,7 +140,11 @@ describe('Me', () => {
expect(response.body).toEqual({}); expect(response.body).toEqual({});
const history = await testSetup.historyService.getEntriesByUser(user); const history = await testSetup.historyService.getEntriesByUser(user);
for (const entry of history) { for (const entry of history) {
if (entry.note.aliases.find((element) => element.name === noteName)) { if (
(await entry.note.aliases).find(
(element) => element.name === noteName,
)
) {
throw new Error('Deleted history entry still in history'); throw new Error('Deleted history entry still in history');
} }
} }

View file

@ -200,16 +200,16 @@ describe('Notes', () => {
updateNotePermission, updateNotePermission,
); );
const updatedNote = await testSetup.notesService.getNoteByIdOrAlias( const updatedNote = await testSetup.notesService.getNoteByIdOrAlias(
note.aliases.filter((alias) => alias.primary)[0].name, (await note.aliases).filter((alias) => alias.primary)[0].name,
); );
expect(updatedNote.userPermissions).toHaveLength(1); expect(await updatedNote.userPermissions).toHaveLength(1);
expect(updatedNote.userPermissions[0].canEdit).toEqual( expect((await updatedNote.userPermissions)[0].canEdit).toEqual(
updateNotePermission.sharedToUsers[0].canEdit, updateNotePermission.sharedToUsers[0].canEdit,
); );
expect(updatedNote.userPermissions[0].user.username).toEqual( expect((await updatedNote.userPermissions)[0].user.username).toEqual(
user.username, user.username,
); );
expect(updatedNote.groupPermissions).toHaveLength(0); expect(await updatedNote.groupPermissions).toHaveLength(0);
await request(testSetup.app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete('/api/v2/notes/test3') .delete('/api/v2/notes/test3')
.expect(204); .expect(204);