Fix various ESLint errors in unit tests

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-02-24 20:20:04 +01:00
parent c5fb87de05
commit 8c3bf66469
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
7 changed files with 53 additions and 21 deletions

View file

@ -4,6 +4,12 @@
* SPDX-License-Identifier: AGPL-3.0-only * 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 { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { PassportModule } from '@nestjs/passport'; import { PassportModule } from '@nestjs/passport';
@ -64,17 +70,17 @@ describe('AuthService', () => {
it('works', async () => { it('works', async () => {
const testPassword = 'thisIsATestPassword'; const testPassword = 'thisIsATestPassword';
const hash = await service.hashPassword(testPassword); const hash = await service.hashPassword(testPassword);
service void service
.checkPassword(testPassword, hash) .checkPassword(testPassword, hash)
.then((result) => expect(result).toBeTruthy()); .then((result) => expect(result).toBeTruthy());
}); });
it('fails, if secret is too short', async () => { 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); const hash = await service.hashPassword(secret);
service void service
.checkPassword(secret, hash) .checkPassword(secret, hash)
.then((result) => expect(result).toBeTruthy()); .then((result) => expect(result).toBeTruthy());
service void service
.checkPassword(secret.substr(0, secret.length - 1), hash) .checkPassword(secret.substr(0, secret.length - 1), hash)
.then((result) => expect(result).toBeFalsy()); .then((result) => expect(result).toBeFalsy());
}); });
@ -194,12 +200,12 @@ describe('AuthService', () => {
}); });
describe('fails:', () => { describe('fails:', () => {
it('the secret is missing', () => { it('the secret is missing', () => {
expect( void expect(
service.validateToken(`${authToken.keyId}`), service.validateToken(`${authToken.keyId}`),
).rejects.toBeInstanceOf(TokenNotValidError); ).rejects.toBeInstanceOf(TokenNotValidError);
}); });
it('the secret is too long', () => { it('the secret is too long', () => {
expect( void expect(
service.validateToken(`${authToken.keyId}.${'a'.repeat(73)}`), service.validateToken(`${authToken.keyId}.${'a'.repeat(73)}`),
).rejects.toBeInstanceOf(TokenNotValidError); ).rejects.toBeInstanceOf(TokenNotValidError);
}); });
@ -293,7 +299,7 @@ describe('AuthService', () => {
authToken.keyId = 'testKeyId'; authToken.keyId = 'testKeyId';
authToken.label = 'testLabel'; authToken.label = 'testLabel';
authToken.createdAt = new Date(); authToken.createdAt = new Date();
const tokenDto = await service.toAuthTokenDto(authToken); const tokenDto = service.toAuthTokenDto(authToken);
expect(tokenDto.keyId).toEqual(authToken.keyId); expect(tokenDto.keyId).toEqual(authToken.keyId);
expect(tokenDto.lastUsed).toBeNull(); expect(tokenDto.lastUsed).toBeNull();
expect(tokenDto.label).toEqual(authToken.label); expect(tokenDto.label).toEqual(authToken.label);

View file

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

View file

@ -4,6 +4,12 @@
* SPDX-License-Identifier: AGPL-3.0-only * 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 { Test, TestingModule } from '@nestjs/testing';
import { LoggerModule } from '../logger/logger.module'; import { LoggerModule } from '../logger/logger.module';
import { HistoryService } from './history.service'; import { HistoryService } from './history.service';

View file

@ -4,6 +4,12 @@
* SPDX-License-Identifier: AGPL-3.0-only * 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 { ConfigModule } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm'; import { getRepositoryToken } from '@nestjs/typeorm';
@ -96,7 +102,7 @@ describe('MediaService', () => {
}); });
describe('saveFile', () => { describe('saveFile', () => {
beforeEach(async () => { beforeEach(() => {
const user = User.create('hardcoded', 'Testy') as User; const user = User.create('hardcoded', 'Testy') as User;
const alias = 'alias'; const alias = 'alias';
const note = Note.create(user, alias); const note = Note.create(user, alias);

View file

@ -22,6 +22,8 @@ async function getServerVersionFromPackageJson() {
joinPath(__dirname, '../../package.json'), joinPath(__dirname, '../../package.json'),
{ encoding: 'utf8' }, { 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 packageInfo: { version: string } = JSON.parse(rawFileContent);
const versionParts: number[] = packageInfo.version const versionParts: number[] = packageInfo.version
.split('.') .split('.')

View file

@ -4,6 +4,12 @@
* SPDX-License-Identifier: AGPL-3.0-only * 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 { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm'; import { getRepositoryToken } from '@nestjs/typeorm';
import { LoggerModule } from '../logger/logger.module'; import { LoggerModule } from '../logger/logger.module';
@ -189,7 +195,7 @@ describe('NotesService', () => {
const newNote = await service.createNote(content); const newNote = await service.createNote(content);
const revisions = await newNote.revisions; const revisions = await newNote.revisions;
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]); jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
service.getNoteContent(newNote).then((result) => { void service.getNoteContent(newNote).then((result) => {
expect(result).toEqual(content); expect(result).toEqual(content);
}); });
}); });
@ -204,7 +210,7 @@ describe('NotesService', () => {
const newNote = await service.createNote(content); const newNote = await service.createNote(content);
const revisions = await newNote.revisions; const revisions = await newNote.revisions;
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]); jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
service.getLatestRevision(newNote).then((result) => { void service.getLatestRevision(newNote).then((result) => {
expect(result).toEqual(revisions[0]); expect(result).toEqual(revisions[0]);
}); });
}); });
@ -221,7 +227,7 @@ describe('NotesService', () => {
const newNote = await service.createNote(content); const newNote = await service.createNote(content);
const revisions = await newNote.revisions; const revisions = await newNote.revisions;
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]); jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
service.getLatestRevision(newNote).then((result) => { void service.getLatestRevision(newNote).then((result) => {
expect(result).toEqual(revisions[0]); expect(result).toEqual(revisions[0]);
}); });
}); });
@ -594,7 +600,7 @@ describe('NotesService', () => {
describe('toNotePermissionsDto', () => { describe('toNotePermissionsDto', () => {
it('works', async () => { it('works', async () => {
const user = User.create('hardcoded', 'Testy') as User; 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); const note = Note.create(user);
note.userPermissions = [ note.userPermissions = [
{ {
@ -610,7 +616,7 @@ describe('NotesService', () => {
canEdit: true, canEdit: true,
}, },
]; ];
const permissions = await service.toNotePermissionsDto(note); const permissions = service.toNotePermissionsDto(note);
expect(permissions.owner.userName).toEqual(user.userName); expect(permissions.owner.userName).toEqual(user.userName);
expect(permissions.sharedToUsers).toHaveLength(1); expect(permissions.sharedToUsers).toHaveLength(1);
expect(permissions.sharedToUsers[0].user.userName).toEqual(user.userName); expect(permissions.sharedToUsers[0].user.userName).toEqual(user.userName);
@ -627,7 +633,7 @@ describe('NotesService', () => {
it('works', async () => { it('works', async () => {
const user = User.create('hardcoded', 'Testy') as User; const user = User.create('hardcoded', 'Testy') as User;
const otherUser = User.create('other hardcoded', 'Testy2') 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'; const content = 'testContent';
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
@ -718,7 +724,7 @@ describe('NotesService', () => {
const user = User.create('hardcoded', 'Testy') as User; const user = User.create('hardcoded', 'Testy') as User;
const otherUser = User.create('other hardcoded', 'Testy2') as User; const otherUser = User.create('other hardcoded', 'Testy2') as User;
otherUser.userName = 'other hardcoded user'; otherUser.userName = 'other hardcoded user';
const group = Group.create('testGroup', 'testGroup') as Group; const group = Group.create('testGroup', 'testGroup');
const content = 'testContent'; const content = 'testContent';
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')

View file

@ -4,6 +4,12 @@
* SPDX-License-Identifier: AGPL-3.0-only * 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 { Test, TestingModule } from '@nestjs/testing';
import { LoggerModule } from '../logger/logger.module'; import { LoggerModule } from '../logger/logger.module';
import { GuestPermission, PermissionsService } from './permissions.service'; import { GuestPermission, PermissionsService } from './permissions.service';
@ -350,7 +356,7 @@ describe('PermissionsService', () => {
for (const group2 of noteGroupPermissions[2]) { for (const group2 of noteGroupPermissions[2]) {
for (const group3 of noteGroupPermissions[3]) { for (const group3 of noteGroupPermissions[3]) {
for (const group4 of noteGroupPermissions[4]) { for (const group4 of noteGroupPermissions[4]) {
const insert = []; const insert: NoteGroupPermission[] = [];
let readPermission = false; let readPermission = false;
let writePermission = false; let writePermission = false;
if (group0 !== null) { if (group0 !== null) {
@ -408,7 +414,7 @@ describe('PermissionsService', () => {
): NoteGroupPermission[][] { ): NoteGroupPermission[][] {
const results = []; const results = [];
function permute(arr, memo) { function permute(arr: NoteGroupPermission[], memo: NoteGroupPermission[]) {
let cur; let cur;
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
@ -456,15 +462,15 @@ describe('PermissionsService', () => {
note.groupPermissions = permission.permissions; note.groupPermissions = permission.permissions;
let permissionString = ''; let permissionString = '';
for (const perm of permission.permissions) { 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; permissionsService.guestPermission = guestPermission;
expect(permissionsService.mayWrite(user1, note)).toEqual( expect(permissionsService.mayWrite(user1, note)).toEqual(
permission.allowsWrite, permission.allowsWrite,
); );
}); });
it('mayRead - test #' + i + ':' + permissionString, () => { it(`mayRead - test #${i}:${permissionString}`, () => {
permissionsService.guestPermission = guestPermission; permissionsService.guestPermission = guestPermission;
expect(permissionsService.mayRead(user1, note)).toEqual( expect(permissionsService.mayRead(user1, note)).toEqual(
permission.allowsRead, permission.allowsRead,