fix: the tests use the new typing from create methods

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-09-25 11:55:35 +02:00 committed by David Mehren
parent 8cda5f99fb
commit b1d4696895
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
11 changed files with 77 additions and 65 deletions

View file

@ -62,9 +62,9 @@ describe('AuthService', () => {
user = User.create('hardcoded', 'Testy') as User;
authToken = AuthToken.create(
'testKeyId',
user,
'testToken',
'testKeyId',
'abc',
new Date(new Date().getTime() + 60000), // make this AuthToken valid for 1min
) as AuthToken;

View file

@ -39,7 +39,7 @@ describe('GroupsService', () => {
service = module.get<GroupsService>(GroupsService);
groupRepo = module.get<Repository<Group>>(getRepositoryToken(Group));
group = Group.create('testGroup', 'Superheros');
group = Group.create('testGroup', 'Superheros') as Group;
});
it('should be defined', () => {

View file

@ -144,7 +144,10 @@ describe('HistoryService', () => {
describe('works', () => {
const user = {} as User;
const alias = 'alias';
const historyEntry = HistoryEntry.create(user, Note.create(user, alias));
const historyEntry = HistoryEntry.create(
user,
Note.create(user, alias) as Note,
) as HistoryEntry;
it('without an preexisting entry', async () => {
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest
@ -153,7 +156,7 @@ describe('HistoryService', () => {
async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
);
const createHistoryEntry = await service.updateHistoryEntryTimestamp(
Note.create(user, alias),
Note.create(user, alias) as Note,
user,
);
expect(createHistoryEntry.note.aliases).toHaveLength(1);
@ -171,7 +174,7 @@ describe('HistoryService', () => {
async (entry: HistoryEntry): Promise<HistoryEntry> => entry,
);
const createHistoryEntry = await service.updateHistoryEntryTimestamp(
Note.create(user, alias),
Note.create(user, alias) as Note,
user,
);
expect(createHistoryEntry.note.aliases).toHaveLength(1);
@ -189,7 +192,7 @@ describe('HistoryService', () => {
describe('updateHistoryEntry', () => {
const user = {} as User;
const alias = 'alias';
const note = Note.create(user, alias);
const note = Note.create(user, alias) as Note;
beforeEach(() => {
const createQueryBuilder = {
leftJoinAndSelect: () => createQueryBuilder,
@ -206,7 +209,7 @@ describe('HistoryService', () => {
});
describe('works', () => {
it('with an entry', async () => {
const historyEntry = HistoryEntry.create(user, note);
const historyEntry = HistoryEntry.create(user, note) as HistoryEntry;
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry);
jest
.spyOn(historyRepo, 'save')
@ -242,8 +245,8 @@ describe('HistoryService', () => {
describe('works', () => {
const user = {} as User;
const alias = 'alias';
const note = Note.create(user, alias);
const historyEntry = HistoryEntry.create(user, note);
const note = Note.create(user, alias) as Note;
const historyEntry = HistoryEntry.create(user, note) as HistoryEntry;
it('with an entry', async () => {
jest.spyOn(historyRepo, 'find').mockResolvedValueOnce([historyEntry]);
jest
@ -258,8 +261,8 @@ describe('HistoryService', () => {
});
it('with multiple entries', async () => {
const alias2 = 'alias2';
const note2 = Note.create(user, alias2);
const historyEntry2 = HistoryEntry.create(user, note2);
const note2 = Note.create(user, alias2) as Note;
const historyEntry2 = HistoryEntry.create(user, note2) as HistoryEntry;
jest
.spyOn(historyRepo, 'find')
.mockResolvedValueOnce([historyEntry, historyEntry2]);
@ -292,8 +295,8 @@ describe('HistoryService', () => {
it('with an entry', async () => {
const user = {} as User;
const alias = 'alias';
const note = Note.create(user, alias);
const historyEntry = HistoryEntry.create(user, note);
const note = Note.create(user, alias) as Note;
const historyEntry = HistoryEntry.create(user, note) as HistoryEntry;
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(historyEntry);
const createQueryBuilder = {
leftJoinAndSelect: () => createQueryBuilder,
@ -322,7 +325,7 @@ describe('HistoryService', () => {
const user = {} as User;
const alias = 'alias';
it('without an entry', async () => {
const note = Note.create(user, alias);
const note = Note.create(user, alias) as Note;
const createQueryBuilder = {
leftJoinAndSelect: () => createQueryBuilder,
where: () => createQueryBuilder,
@ -347,7 +350,7 @@ describe('HistoryService', () => {
it('works', async () => {
const user = {} as User;
const alias = 'alias';
const note = Note.create(user, alias);
const note = Note.create(user, alias) as Note;
const historyEntry = HistoryEntry.create(user, note);
const historyEntryImport: HistoryEntryImportDto = {
lastVisited: new Date('2020-12-01 12:23:34'),
@ -397,14 +400,14 @@ describe('HistoryService', () => {
const alias = 'alias';
const title = 'title';
const tags = ['tag1', 'tag2'];
const note = Note.create(user, alias);
const note = Note.create(user, alias) as Note;
note.title = title;
note.tags = tags.map((tag) => {
const newTag = new Tag();
newTag.name = tag;
return newTag;
});
const historyEntry = HistoryEntry.create(user, note);
const historyEntry = HistoryEntry.create(user, note) as HistoryEntry;
historyEntry.pinStatus = true;
const createQueryBuilder = {
leftJoinAndSelect: () => createQueryBuilder,

View file

@ -15,8 +15,8 @@ describe('getIdentifier', () => {
let entry: HistoryEntry;
beforeEach(() => {
const user = User.create('hardcoded', 'Testy') as User;
note = Note.create(user, alias);
entry = HistoryEntry.create(user, note);
note = Note.create(user, alias) as Note;
entry = HistoryEntry.create(user, note) as HistoryEntry;
});
it('returns the publicId if there are no aliases', () => {
note.aliases = undefined as unknown as Alias[];

View file

@ -95,7 +95,7 @@ describe('IdentityService', () => {
describe('loginWithLocalIdentity', () => {
it('works', async () => {
const identity = Identity.create(user, ProviderType.LOCAL);
const identity = Identity.create(user, ProviderType.LOCAL) as Identity;
identity.passwordHash = await hashPassword(password);
user.identities = Promise.resolve([identity]);
await expect(

View file

@ -106,7 +106,7 @@ describe('MediaService', () => {
beforeEach(() => {
user = User.create('hardcoded', 'Testy') as User;
const alias = 'alias';
note = Note.create(user, alias);
note = Note.create(user, alias) as Note;
jest.spyOn(userRepo, 'findOne').mockResolvedValueOnce(user);
const createQueryBuilder = {
leftJoinAndSelect: () => createQueryBuilder,
@ -289,12 +289,12 @@ describe('MediaService', () => {
describe('removeNoteFromMediaUpload', () => {
it('works', async () => {
const mockNote = {} as Note;
mockNote.aliases = [Alias.create('test', mockNote, true) as Alias];
const mockMediaUploadEntry = {
id: 'testMediaUpload',
backendData: 'testBackendData',
note: {
aliases: [Alias.create('test', true)],
} as Note,
note: mockNote,
user: {
username: 'hardcoded',
} as User,

View file

@ -113,7 +113,7 @@ describe('AliasService', () => {
const user = User.create('hardcoded', 'Testy') as User;
describe('creates', () => {
it('an primary alias if no alias is already present', async () => {
const note = Note.create(user);
const note = Note.create(user) as Note;
jest
.spyOn(noteRepo, 'save')
.mockImplementationOnce(async (note: Note): Promise<Note> => note);
@ -124,7 +124,7 @@ describe('AliasService', () => {
expect(savedAlias.primary).toBeTruthy();
});
it('an non-primary alias if an primary alias is already present', async () => {
const note = Note.create(user, alias);
const note = Note.create(user, alias) as Note;
jest
.spyOn(noteRepo, 'save')
.mockImplementationOnce(async (note: Note): Promise<Note> => note);
@ -136,11 +136,11 @@ describe('AliasService', () => {
});
});
describe('does not create an alias', () => {
const note = Note.create(user, alias2);
const note = Note.create(user, alias2) as Note;
it('with an already used name', async () => {
jest
.spyOn(aliasRepo, 'findOne')
.mockResolvedValueOnce(Alias.create(alias2));
.mockResolvedValueOnce(Alias.create(alias2, note) as Alias);
await expect(service.addAlias(note, alias2)).rejects.toThrow(
AlreadyInDBError,
);
@ -158,8 +158,8 @@ describe('AliasService', () => {
const alias2 = 'testAlias2';
const user = User.create('hardcoded', 'Testy') as User;
describe('removes one alias correctly', () => {
const note = Note.create(user, alias);
note.aliases.push(Alias.create(alias2));
const note = Note.create(user, alias) as Note;
note.aliases.push(Alias.create(alias2, note) as Alias);
it('with two aliases', async () => {
jest
.spyOn(noteRepo, 'save')
@ -188,8 +188,8 @@ describe('AliasService', () => {
});
});
describe('does not remove one alias', () => {
const note = Note.create(user, alias);
note.aliases.push(Alias.create(alias2));
const note = Note.create(user, alias) as Note;
note.aliases.push(Alias.create(alias2, note) as Alias);
it('if the alias is unknown', async () => {
await expect(service.removeAlias(note, 'non existent')).rejects.toThrow(
NotInDBError,
@ -204,10 +204,11 @@ describe('AliasService', () => {
});
describe('makeAliasPrimary', () => {
const alias = Alias.create('testAlias', true);
const alias2 = Alias.create('testAlias2');
const user = User.create('hardcoded', 'Testy') as User;
const note = Note.create(user, alias.name);
const aliasName = 'testAlias';
const note = Note.create(user, aliasName) as Note;
const alias = Alias.create(aliasName, note, true) as Alias;
const alias2 = Alias.create('testAlias2', note) as Alias;
note.aliases.push(alias2);
it('mark the alias as primary', async () => {
jest
@ -256,9 +257,9 @@ describe('AliasService', () => {
it('toAliasDto correctly creates an AliasDto', () => {
const aliasName = 'testAlias';
const alias = Alias.create(aliasName, true);
const user = User.create('hardcoded', 'Testy') as User;
const note = Note.create(user, alias.name);
const note = Note.create(user, aliasName) as Note;
const alias = Alias.create(aliasName, note, true) as Alias;
const aliasDto = service.toAliasDto(alias, note);
expect(aliasDto.name).toEqual(aliasName);
expect(aliasDto.primaryAlias).toBeTruthy();

View file

@ -131,7 +131,7 @@ describe('NotesService', () => {
describe('works', () => {
const user = User.create('hardcoded', 'Testy') as User;
const alias = 'alias';
const note = Note.create(user, alias);
const note = Note.create(user, alias) as Note;
it('with no note', async () => {
jest.spyOn(noteRepo, 'find').mockResolvedValueOnce(undefined);
@ -168,7 +168,7 @@ describe('NotesService', () => {
const revisions = await newNote.revisions;
expect(revisions).toHaveLength(1);
expect(revisions[0].content).toEqual(content);
expect(newNote.historyEntries).toBeUndefined();
expect(newNote.historyEntries).toHaveLength(0);
expect(newNote.userPermissions).toHaveLength(0);
expect(newNote.groupPermissions).toHaveLength(0);
expect(newNote.tags).toHaveLength(0);
@ -193,7 +193,7 @@ describe('NotesService', () => {
const revisions = await newNote.revisions;
expect(revisions).toHaveLength(1);
expect(revisions[0].content).toEqual(content);
expect(newNote.historyEntries).toBeUndefined();
expect(newNote.historyEntries).toHaveLength(0);
expect(newNote.userPermissions).toHaveLength(0);
expect(newNote.groupPermissions).toHaveLength(0);
expect(newNote.tags).toHaveLength(0);
@ -328,7 +328,7 @@ describe('NotesService', () => {
describe('deleteNote', () => {
it('works', async () => {
const user = User.create('hardcoded', 'Testy') as User;
const note = Note.create(user);
const note = Note.create(user) as Note;
jest
.spyOn(noteRepo, 'remove')
.mockImplementationOnce(async (entry, _) => {
@ -342,7 +342,7 @@ describe('NotesService', () => {
describe('updateNote', () => {
it('works', async () => {
const user = User.create('hardcoded', 'Testy') as User;
const note = Note.create(user);
const note = Note.create(user) as Note;
const revisionLength = (await note.revisions).length;
jest
.spyOn(noteRepo, 'save')
@ -365,8 +365,8 @@ describe('NotesService', () => {
const group = Group.create(
groupPermissionUpate.groupname,
groupPermissionUpate.groupname,
);
const note = Note.create(user);
) as Group;
const note = Note.create(user) as Note;
describe('works', () => {
it('with empty GroupPermissions and with empty UserPermissions', async () => {
jest
@ -668,8 +668,8 @@ describe('NotesService', () => {
describe('toNotePermissionsDto', () => {
it('works', async () => {
const user = User.create('hardcoded', 'Testy') as User;
const group = Group.create('testGroup', 'testGroup');
const note = Note.create(user);
const group = Group.create('testGroup', 'testGroup') as Group;
const note = Note.create(user) as Note;
note.userPermissions = [
{
note: note,
@ -685,7 +685,8 @@ describe('NotesService', () => {
},
];
const permissions = service.toNotePermissionsDto(note);
expect(permissions.owner.username).toEqual(user.username);
expect(permissions.owner).not.toEqual(null);
expect(permissions.owner?.username).toEqual(user.username);
expect(permissions.sharedToUsers).toHaveLength(1);
expect(permissions.sharedToUsers[0].user.username).toEqual(user.username);
expect(permissions.sharedToUsers[0].canEdit).toEqual(true);
@ -702,7 +703,7 @@ describe('NotesService', () => {
const user = User.create('hardcoded', 'Testy') as User;
const author = Author.create(1);
author.user = user;
const group = Group.create('testGroup', 'testGroup');
const group = Group.create('testGroup', 'testGroup') as Group;
const content = 'testContent';
jest
.spyOn(noteRepo, 'save')
@ -738,7 +739,7 @@ describe('NotesService', () => {
// @ts-ignore
.mockImplementation(() => createQueryBuilder);
note.publicId = 'testId';
note.aliases = [Alias.create('testAlias', true)];
note.aliases = [Alias.create('testAlias', note, true) as Alias];
note.title = 'testTitle';
note.description = 'testDescription';
note.owner = user;
@ -799,7 +800,7 @@ describe('NotesService', () => {
author.user = user;
const otherUser = User.create('other hardcoded', 'Testy2') as User;
otherUser.username = 'other hardcoded user';
const group = Group.create('testGroup', 'testGroup');
const group = Group.create('testGroup', 'testGroup') as Group;
const content = 'testContent';
jest
.spyOn(noteRepo, 'save')
@ -838,7 +839,7 @@ describe('NotesService', () => {
// @ts-ignore
.mockImplementation(() => createQueryBuilder);
note.publicId = 'testId';
note.aliases = [Alias.create('testAlias', true)];
note.aliases = [Alias.create('testAlias', note, true) as Alias];
note.title = 'testTitle';
note.description = 'testDescription';
note.owner = user;

View file

@ -27,10 +27,10 @@ describe('getPrimaryAlias', () => {
let note: Note;
beforeEach(() => {
const user = User.create('hardcoded', 'Testy') as User;
note = Note.create(user, alias);
note = Note.create(user, alias) as Note;
});
it('finds correct primary alias', () => {
note.aliases.push(Alias.create('annother', false));
note.aliases.push(Alias.create('annother', note, false) as Alias);
expect(getPrimaryAlias(note)).toEqual(alias);
});
it('returns undefined if there is no alias', () => {

View file

@ -265,30 +265,36 @@ describe('PermissionsService', () => {
const everybody: Group = Group.create(
SpecialGroup.EVERYONE,
SpecialGroup.EVERYONE,
);
) as Group;
everybody.special = true;
result[SpecialGroup.EVERYONE] = everybody;
const loggedIn = Group.create(
SpecialGroup.LOGGED_IN,
SpecialGroup.LOGGED_IN,
);
) as Group;
loggedIn.special = true;
result[SpecialGroup.LOGGED_IN] = loggedIn;
const user1group = Group.create('user1group', 'user1group');
const user1group = Group.create('user1group', 'user1group') as Group;
user1group.members = [user1];
result['user1group'] = user1group;
const user2group = Group.create('user2group', 'user2group');
const user2group = Group.create('user2group', 'user2group') as Group;
user2group.members = [user2];
result['user2group'] = user2group;
const user1and2group = Group.create('user1and2group', 'user1and2group');
const user1and2group = Group.create(
'user1and2group',
'user1and2group',
) as Group;
user1and2group.members = [user1, user2];
result['user1and2group'] = user1and2group;
const user2and1group = Group.create('user2and1group', 'user2and1group');
const user2and1group = Group.create(
'user2and1group',
'user2and1group',
) as Group;
user2and1group.members = [user2, user1];
result['user2and1group'] = user2and1group;
@ -308,7 +314,7 @@ describe('PermissionsService', () => {
group: Group,
write: boolean,
): NoteGroupPermission {
return NoteGroupPermission.create(group, write);
return NoteGroupPermission.create(group, {} as Note, write);
}
const everybodyRead = createNoteGroupPermission(

View file

@ -89,7 +89,8 @@ describe('RevisionsService', () => {
describe('getRevision', () => {
it('returns a revision', async () => {
const revision = Revision.create('', '');
const note = {} as Note;
const revision = Revision.create('', '', note) as Revision;
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revision);
expect(await service.getRevision({} as Note, 1)).toEqual(revision);
});
@ -106,11 +107,11 @@ describe('RevisionsService', () => {
const note = {} as Note;
note.id = 'test';
let revisions: Revision[] = [];
const revision1 = Revision.create('a', 'a');
const revision1 = Revision.create('a', 'a', note) as Revision;
revision1.id = 1;
const revision2 = Revision.create('b', 'b');
const revision2 = Revision.create('b', 'b', note) as Revision;
revision2.id = 2;
const revision3 = Revision.create('c', 'c');
const revision3 = Revision.create('c', 'c', note) as Revision;
revision3.id = 3;
revisions.push(revision1, revision2, revision3);
note.revisions = Promise.resolve(revisions);
@ -136,7 +137,7 @@ describe('RevisionsService', () => {
const note = {} as Note;
note.id = 'test';
let revisions: Revision[] = [];
const revision1 = Revision.create('a', 'a');
const revision1 = Revision.create('a', 'a', note) as Revision;
revision1.id = 1;
revisions.push(revision1);
note.revisions = Promise.resolve(revisions);