From 8c3bf66469478b4d78e00ed46d624c0d44488911 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Wed, 24 Feb 2021 20:20:04 +0100 Subject: [PATCH] Fix various ESLint errors in unit tests Signed-off-by: David Mehren --- src/auth/auth.service.spec.ts | 20 +++++++++++++------- src/groups/groups.service.spec.ts | 2 +- src/history/history.service.spec.ts | 6 ++++++ src/media/media.service.spec.ts | 8 +++++++- src/monitoring/monitoring.service.ts | 2 ++ src/notes/notes.service.spec.ts | 20 +++++++++++++------- src/permissions/permissions.service.spec.ts | 16 +++++++++++----- 7 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index 356570fdd..0aaa41ae3 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -4,6 +4,12 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +/* eslint-disable +@typescript-eslint/no-unsafe-call, +@typescript-eslint/no-unsafe-member-access, +@typescript-eslint/no-unsafe-return, +@typescript-eslint/require-await */ + import { Test, TestingModule } from '@nestjs/testing'; import { AuthService } from './auth.service'; import { PassportModule } from '@nestjs/passport'; @@ -64,17 +70,17 @@ describe('AuthService', () => { it('works', async () => { const testPassword = 'thisIsATestPassword'; const hash = await service.hashPassword(testPassword); - service + void service .checkPassword(testPassword, hash) .then((result) => expect(result).toBeTruthy()); }); it('fails, if secret is too short', async () => { - const secret = service.bufferToBase64Url(await service.randomString(54)); + const secret = service.bufferToBase64Url(service.randomString(54)); const hash = await service.hashPassword(secret); - service + void service .checkPassword(secret, hash) .then((result) => expect(result).toBeTruthy()); - service + void service .checkPassword(secret.substr(0, secret.length - 1), hash) .then((result) => expect(result).toBeFalsy()); }); @@ -194,12 +200,12 @@ describe('AuthService', () => { }); describe('fails:', () => { it('the secret is missing', () => { - expect( + void expect( service.validateToken(`${authToken.keyId}`), ).rejects.toBeInstanceOf(TokenNotValidError); }); it('the secret is too long', () => { - expect( + void expect( service.validateToken(`${authToken.keyId}.${'a'.repeat(73)}`), ).rejects.toBeInstanceOf(TokenNotValidError); }); @@ -293,7 +299,7 @@ describe('AuthService', () => { authToken.keyId = 'testKeyId'; authToken.label = 'testLabel'; authToken.createdAt = new Date(); - const tokenDto = await service.toAuthTokenDto(authToken); + const tokenDto = service.toAuthTokenDto(authToken); expect(tokenDto.keyId).toEqual(authToken.keyId); expect(tokenDto.lastUsed).toBeNull(); expect(tokenDto.label).toEqual(authToken.label); diff --git a/src/groups/groups.service.spec.ts b/src/groups/groups.service.spec.ts index 827d2d1f0..840fd2029 100644 --- a/src/groups/groups.service.spec.ts +++ b/src/groups/groups.service.spec.ts @@ -31,7 +31,7 @@ describe('GroupsService', () => { service = module.get(GroupsService); groupRepo = module.get>(getRepositoryToken(Group)); - group = Group.create('testGroup', 'Superheros') as Group; + group = Group.create('testGroup', 'Superheros'); }); it('should be defined', () => { diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index bea7a67cd..fe104eb1c 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -4,6 +4,12 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +/* eslint-disable +@typescript-eslint/no-unsafe-call, +@typescript-eslint/no-unsafe-member-access, +@typescript-eslint/no-unsafe-return, +@typescript-eslint/require-await */ + import { Test, TestingModule } from '@nestjs/testing'; import { LoggerModule } from '../logger/logger.module'; import { HistoryService } from './history.service'; diff --git a/src/media/media.service.spec.ts b/src/media/media.service.spec.ts index 8773be3da..d84a98dba 100644 --- a/src/media/media.service.spec.ts +++ b/src/media/media.service.spec.ts @@ -4,6 +4,12 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +/* eslint-disable +@typescript-eslint/no-unsafe-call, +@typescript-eslint/no-unsafe-member-access, +@typescript-eslint/no-unsafe-return, +@typescript-eslint/require-await */ + import { ConfigModule } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; @@ -96,7 +102,7 @@ describe('MediaService', () => { }); describe('saveFile', () => { - beforeEach(async () => { + beforeEach(() => { const user = User.create('hardcoded', 'Testy') as User; const alias = 'alias'; const note = Note.create(user, alias); diff --git a/src/monitoring/monitoring.service.ts b/src/monitoring/monitoring.service.ts index 92ef6452d..6c4f18287 100644 --- a/src/monitoring/monitoring.service.ts +++ b/src/monitoring/monitoring.service.ts @@ -22,6 +22,8 @@ async function getServerVersionFromPackageJson() { joinPath(__dirname, '../../package.json'), { encoding: 'utf8' }, ); + // TODO: Should this be validated in more detail? + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const packageInfo: { version: string } = JSON.parse(rawFileContent); const versionParts: number[] = packageInfo.version .split('.') diff --git a/src/notes/notes.service.spec.ts b/src/notes/notes.service.spec.ts index 18a02dba5..f1858f30b 100644 --- a/src/notes/notes.service.spec.ts +++ b/src/notes/notes.service.spec.ts @@ -4,6 +4,12 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +/* eslint-disable @typescript-eslint/require-await */ +/* eslint-disable +@typescript-eslint/no-unsafe-assignment, +@typescript-eslint/no-unsafe-member-access +*/ + import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; import { LoggerModule } from '../logger/logger.module'; @@ -189,7 +195,7 @@ describe('NotesService', () => { const newNote = await service.createNote(content); const revisions = await newNote.revisions; jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]); - service.getNoteContent(newNote).then((result) => { + void service.getNoteContent(newNote).then((result) => { expect(result).toEqual(content); }); }); @@ -204,7 +210,7 @@ describe('NotesService', () => { const newNote = await service.createNote(content); const revisions = await newNote.revisions; jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]); - service.getLatestRevision(newNote).then((result) => { + void service.getLatestRevision(newNote).then((result) => { expect(result).toEqual(revisions[0]); }); }); @@ -221,7 +227,7 @@ describe('NotesService', () => { const newNote = await service.createNote(content); const revisions = await newNote.revisions; jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]); - service.getLatestRevision(newNote).then((result) => { + void service.getLatestRevision(newNote).then((result) => { expect(result).toEqual(revisions[0]); }); }); @@ -594,7 +600,7 @@ describe('NotesService', () => { describe('toNotePermissionsDto', () => { it('works', async () => { const user = User.create('hardcoded', 'Testy') as User; - const group = Group.create('testGroup', 'testGroup') as Group; + const group = Group.create('testGroup', 'testGroup'); const note = Note.create(user); note.userPermissions = [ { @@ -610,7 +616,7 @@ describe('NotesService', () => { canEdit: true, }, ]; - const permissions = await service.toNotePermissionsDto(note); + const permissions = service.toNotePermissionsDto(note); expect(permissions.owner.userName).toEqual(user.userName); expect(permissions.sharedToUsers).toHaveLength(1); expect(permissions.sharedToUsers[0].user.userName).toEqual(user.userName); @@ -627,7 +633,7 @@ describe('NotesService', () => { it('works', async () => { const user = User.create('hardcoded', 'Testy') as User; const otherUser = User.create('other hardcoded', 'Testy2') as User; - const group = Group.create('testGroup', 'testGroup') as Group; + const group = Group.create('testGroup', 'testGroup'); const content = 'testContent'; jest .spyOn(noteRepo, 'save') @@ -718,7 +724,7 @@ describe('NotesService', () => { const user = User.create('hardcoded', 'Testy') as User; const otherUser = User.create('other hardcoded', 'Testy2') as User; otherUser.userName = 'other hardcoded user'; - const group = Group.create('testGroup', 'testGroup') as Group; + const group = Group.create('testGroup', 'testGroup'); const content = 'testContent'; jest .spyOn(noteRepo, 'save') diff --git a/src/permissions/permissions.service.spec.ts b/src/permissions/permissions.service.spec.ts index 6396eb09c..d123cdd2c 100644 --- a/src/permissions/permissions.service.spec.ts +++ b/src/permissions/permissions.service.spec.ts @@ -4,6 +4,12 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +/* eslint-disable +@typescript-eslint/no-unsafe-call, +@typescript-eslint/no-unsafe-member-access, +@typescript-eslint/no-unsafe-return, +@typescript-eslint/require-await */ + import { Test, TestingModule } from '@nestjs/testing'; import { LoggerModule } from '../logger/logger.module'; import { GuestPermission, PermissionsService } from './permissions.service'; @@ -350,7 +356,7 @@ describe('PermissionsService', () => { for (const group2 of noteGroupPermissions[2]) { for (const group3 of noteGroupPermissions[3]) { for (const group4 of noteGroupPermissions[4]) { - const insert = []; + const insert: NoteGroupPermission[] = []; let readPermission = false; let writePermission = false; if (group0 !== null) { @@ -408,7 +414,7 @@ describe('PermissionsService', () => { ): NoteGroupPermission[][] { const results = []; - function permute(arr, memo) { + function permute(arr: NoteGroupPermission[], memo: NoteGroupPermission[]) { let cur; for (let i = 0; i < arr.length; i++) { @@ -456,15 +462,15 @@ describe('PermissionsService', () => { note.groupPermissions = permission.permissions; let permissionString = ''; for (const perm of permission.permissions) { - permissionString += ' ' + perm.group.name + ':' + perm.canEdit; + permissionString += ` ${perm.group.name}:${String(perm.canEdit)}`; } - it('mayWrite - test #' + i + ':' + permissionString, () => { + it(`mayWrite - test #${i}:${permissionString}`, () => { permissionsService.guestPermission = guestPermission; expect(permissionsService.mayWrite(user1, note)).toEqual( permission.allowsWrite, ); }); - it('mayRead - test #' + i + ':' + permissionString, () => { + it(`mayRead - test #${i}:${permissionString}`, () => { permissionsService.guestPermission = guestPermission; expect(permissionsService.mayRead(user1, note)).toEqual( permission.allowsRead,