Tests: Fix eslint errors

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-04-12 18:05:27 +02:00
parent 434bc55bab
commit 51f1da7083
8 changed files with 108 additions and 141 deletions

View file

@ -64,17 +64,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);
void service await 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(service.randomString(54)); const secret = service.bufferToBase64Url(service.randomString(54));
const hash = await service.hashPassword(secret); const hash = await service.hashPassword(secret);
void service await service
.checkPassword(secret, hash) .checkPassword(secret, hash)
.then((result) => expect(result).toBeTruthy()); .then((result) => expect(result).toBeTruthy());
void service await 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());
}); });
@ -113,11 +113,9 @@ describe('AuthService', () => {
describe('fails:', () => { describe('fails:', () => {
it('AuthToken could not be found', async () => { it('AuthToken could not be found', async () => {
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce(undefined);
try { await expect(
await service.getAuthTokenAndValidate(authToken.keyId, token); service.getAuthTokenAndValidate(authToken.keyId, token),
} catch (e) { ).rejects.toThrow(NotInDBError);
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
it('AuthToken has wrong hash', async () => { it('AuthToken has wrong hash', async () => {
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
@ -125,11 +123,9 @@ describe('AuthService', () => {
user: user, user: user,
accessTokenHash: 'the wrong hash', accessTokenHash: 'the wrong hash',
}); });
try { await expect(
await service.getAuthTokenAndValidate(authToken.keyId, token); service.getAuthTokenAndValidate(authToken.keyId, token),
} catch (e) { ).rejects.toThrow(TokenNotValidError);
expect(e).toBeInstanceOf(TokenNotValidError);
}
}); });
it('AuthToken has wrong validUntil Date', async () => { it('AuthToken has wrong validUntil Date', async () => {
const accessTokenHash = await service.hashPassword(token); const accessTokenHash = await service.hashPassword(token);
@ -139,11 +135,9 @@ describe('AuthService', () => {
accessTokenHash: accessTokenHash, accessTokenHash: accessTokenHash,
validUntil: new Date(1549312452000), validUntil: new Date(1549312452000),
}); });
try { await expect(
await service.getAuthTokenAndValidate(authToken.keyId, token); service.getAuthTokenAndValidate(authToken.keyId, token),
} catch (e) { ).rejects.toThrow(TokenNotValidError);
expect(e).toBeInstanceOf(TokenNotValidError);
}
}); });
}); });
}); });
@ -155,13 +149,13 @@ describe('AuthService', () => {
user: user, user: user,
lastUsed: new Date(1549312452000), lastUsed: new Date(1549312452000),
}); });
jest jest.spyOn(authTokenRepo, 'save').mockImplementationOnce(
.spyOn(authTokenRepo, 'save') async (authTokenSaved, _): Promise<AuthToken> => {
.mockImplementationOnce(async (authTokenSaved, _) => {
expect(authTokenSaved.keyId).toEqual(authToken.keyId); expect(authTokenSaved.keyId).toEqual(authToken.keyId);
expect(authTokenSaved.lastUsed).not.toEqual(1549312452000); expect(authTokenSaved.lastUsed).not.toEqual(1549312452000);
return authToken; return authToken;
}); },
);
await service.setLastUsedToken(authToken.keyId); await service.setLastUsedToken(authToken.keyId);
}); });
}); });
@ -179,11 +173,11 @@ describe('AuthService', () => {
user: user, user: user,
accessTokenHash: accessTokenHash, accessTokenHash: accessTokenHash,
}); });
jest jest.spyOn(authTokenRepo, 'save').mockImplementationOnce(
.spyOn(authTokenRepo, 'save') async (_, __): Promise<AuthToken> => {
.mockImplementationOnce(async (_, __) => {
return authToken; return authToken;
}); },
);
const userByToken = await service.validateToken( const userByToken = await service.validateToken(
`${authToken.keyId}.${token}`, `${authToken.keyId}.${token}`,
); );
@ -193,15 +187,15 @@ describe('AuthService', () => {
}); });
}); });
describe('fails:', () => { describe('fails:', () => {
it('the secret is missing', () => { it('the secret is missing', async () => {
void expect( await expect(
service.validateToken(`${authToken.keyId}`), service.validateToken(`${authToken.keyId}`),
).rejects.toBeInstanceOf(TokenNotValidError); ).rejects.toThrow(TokenNotValidError);
}); });
it('the secret is too long', async () => { it('the secret is too long', async () => {
await expect( await expect(
service.validateToken(`${authToken.keyId}.${'a'.repeat(73)}`), service.validateToken(`${authToken.keyId}.${'a'.repeat(73)}`),
).rejects.toBeInstanceOf(TokenNotValidError); ).rejects.toThrow(TokenNotValidError);
}); });
}); });
}); });
@ -212,15 +206,15 @@ describe('AuthService', () => {
...authToken, ...authToken,
user: user, user: user,
}); });
jest jest.spyOn(authTokenRepo, 'remove').mockImplementationOnce(
.spyOn(authTokenRepo, 'remove') async (token, __): Promise<AuthToken> => {
.mockImplementationOnce(async (token, __) => {
expect(token).toEqual({ expect(token).toEqual({
...authToken, ...authToken,
user: user, user: user,
}); });
return authToken; return authToken;
}); },
);
await service.removeToken(authToken.keyId); await service.removeToken(authToken.keyId);
}); });
}); });
@ -233,12 +227,12 @@ describe('AuthService', () => {
...user, ...user,
authTokens: [authToken], authTokens: [authToken],
}); });
jest jest.spyOn(authTokenRepo, 'save').mockImplementationOnce(
.spyOn(authTokenRepo, 'save') async (authTokenSaved: AuthToken, _): Promise<AuthToken> => {
.mockImplementationOnce(async (authTokenSaved: AuthToken, _) => {
expect(authTokenSaved.lastUsed).toBeUndefined(); expect(authTokenSaved.lastUsed).toBeUndefined();
return authTokenSaved; return authTokenSaved;
}); },
);
const token = await service.createTokenForUser( const token = await service.createTokenForUser(
user.userName, user.userName,
identifier, identifier,
@ -257,12 +251,12 @@ describe('AuthService', () => {
...user, ...user,
authTokens: [authToken], authTokens: [authToken],
}); });
jest jest.spyOn(authTokenRepo, 'save').mockImplementationOnce(
.spyOn(authTokenRepo, 'save') async (authTokenSaved: AuthToken, _): Promise<AuthToken> => {
.mockImplementationOnce(async (authTokenSaved: AuthToken, _) => {
expect(authTokenSaved.lastUsed).toBeUndefined(); expect(authTokenSaved.lastUsed).toBeUndefined();
return authTokenSaved; return authTokenSaved;
}); },
);
const validUntil = new Date().getTime() + 30000; const validUntil = new Date().getTime() + 30000;
const token = await service.createTokenForUser( const token = await service.createTokenForUser(
user.userName, user.userName,
@ -288,7 +282,7 @@ describe('AuthService', () => {
}); });
describe('toAuthTokenDto', () => { describe('toAuthTokenDto', () => {
it('works', async () => { it('works', () => {
const authToken = new AuthToken(); const authToken = new AuthToken();
authToken.keyId = 'testKeyId'; authToken.keyId = 'testKeyId';
authToken.label = 'testLabel'; authToken.label = 'testLabel';

View file

@ -28,7 +28,7 @@ describe('config utils', () => {
]); ]);
}); });
}); });
describe('toArrayConfig', () => { describe('replaceAuthErrorsWithEnvironmentVariables', () => {
it('"gitlab[0].scope', () => { it('"gitlab[0].scope', () => {
expect( expect(
replaceAuthErrorsWithEnvironmentVariables( replaceAuthErrorsWithEnvironmentVariables(

View file

@ -16,6 +16,10 @@ import { AppConfig } from '../config/app.config';
import { ExternalServicesConfig } from '../config/external-services.config'; import { ExternalServicesConfig } from '../config/external-services.config';
import { Loglevel } from '../config/loglevel.enum'; import { Loglevel } from '../config/loglevel.enum';
/* eslint-disable
jest/no-conditional-expect
*/
describe('FrontendConfigService', () => { describe('FrontendConfigService', () => {
const emptyAuthConfig: AuthConfig = { const emptyAuthConfig: AuthConfig = {
email: { email: {

View file

@ -48,11 +48,9 @@ describe('GroupsService', () => {
}); });
it('fails with non-existing group', async () => { it('fails with non-existing group', async () => {
jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(undefined);
try { await expect(service.getGroupByName('i_dont_exist')).rejects.toThrow(
await service.getGroupByName('i_dont_exist'); NotInDBError,
} catch (e) { );
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
}); });

View file

@ -132,11 +132,9 @@ describe('HistoryService', () => {
describe('fails', () => { describe('fails', () => {
it('with an non-existing note', async () => { it('with an non-existing note', async () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
try { await expect(
await service.getEntryByNoteIdOrAlias(alias, {} as User); service.getEntryByNoteIdOrAlias(alias, {} as User),
} catch (e) { ).rejects.toThrow(NotInDBError);
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
}); });
}); });
@ -223,13 +221,11 @@ describe('HistoryService', () => {
const note = Note.create(user, alias); const note = Note.create(user, alias);
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note);
try { await expect(
await service.updateHistoryEntry(alias, user, { service.updateHistoryEntry(alias, user, {
pinStatus: true, pinStatus: true,
}); }),
} catch (e) { ).rejects.toThrow(NotInDBError);
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
}); });
}); });
@ -276,6 +272,7 @@ describe('HistoryService', () => {
it('without an entry', async () => { it('without an entry', async () => {
jest.spyOn(historyRepo, 'find').mockResolvedValueOnce([]); jest.spyOn(historyRepo, 'find').mockResolvedValueOnce([]);
await service.deleteHistory(user); await service.deleteHistory(user);
expect(true).toBeTruthy();
}); });
}); });
}); });
@ -305,19 +302,15 @@ describe('HistoryService', () => {
const note = Note.create(user, alias); const note = Note.create(user, alias);
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note);
try { await expect(service.deleteHistoryEntry(alias, user)).rejects.toThrow(
await service.deleteHistoryEntry(alias, user); NotInDBError,
} catch (e) { );
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
it('without a note', async () => { it('without a note', async () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
try { await expect(
await service.getEntryByNoteIdOrAlias(alias, {} as User); service.getEntryByNoteIdOrAlias(alias, {} as User),
} catch (e) { ).rejects.toThrow(NotInDBError);
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
}); });
}); });

View file

@ -129,24 +129,16 @@ describe('MediaService', () => {
describe('fails:', () => { describe('fails:', () => {
it('MIME type not identifiable', async () => { it('MIME type not identifiable', async () => {
try { await expect(
await service.saveFile(Buffer.alloc(1), 'hardcoded', 'test'); service.saveFile(Buffer.alloc(1), 'hardcoded', 'test'),
} catch (e) { ).rejects.toThrow(ClientError);
expect(e).toBeInstanceOf(ClientError);
expect(e.message).toContain('detect');
}
}); });
it('MIME type not supported', async () => { it('MIME type not supported', async () => {
try { const testText = await fs.readFile('test/public-api/fixtures/test.zip');
const testText = await fs.readFile( await expect(
'test/public-api/fixtures/test.zip', service.saveFile(testText, 'hardcoded', 'test'),
); ).rejects.toThrow(ClientError);
await service.saveFile(testText, 'hardcoded', 'test');
} catch (e) {
expect(e).toBeInstanceOf(ClientError);
expect(e.message).not.toContain('detect');
}
}); });
}); });
}); });
@ -191,36 +183,36 @@ describe('MediaService', () => {
jest jest
.spyOn(mediaRepo, 'findOne') .spyOn(mediaRepo, 'findOne')
.mockResolvedValueOnce(mockMediaUploadEntry); .mockResolvedValueOnce(mockMediaUploadEntry);
try { await expect(
await service.deleteFile(testFileName, 'hardcoded'); service.deleteFile(testFileName, 'hardcoded'),
} catch (e) { ).rejects.toThrow(PermissionError);
expect(e).toBeInstanceOf(PermissionError);
}
}); });
}); });
describe('findUploadByFilename', () => { describe('findUploadByFilename', () => {
it('works', async () => { it('works', async () => {
const testFileName = 'testFilename'; const testFileName = 'testFilename';
const userName = 'hardcoded';
const backendData = 'testBackendData';
const mockMediaUploadEntry = { const mockMediaUploadEntry = {
id: 'testMediaUpload', id: 'testMediaUpload',
backendData: 'testBackendData', backendData: backendData,
user: { user: {
userName: 'hardcoded', userName: userName,
} as User, } as User,
} as MediaUpload; } as MediaUpload;
jest jest
.spyOn(mediaRepo, 'findOne') .spyOn(mediaRepo, 'findOne')
.mockResolvedValueOnce(mockMediaUploadEntry); .mockResolvedValueOnce(mockMediaUploadEntry);
await service.findUploadByFilename(testFileName); const mediaUpload = await service.findUploadByFilename(testFileName);
expect(mediaUpload.user.userName).toEqual(userName);
expect(mediaUpload.backendData).toEqual(backendData);
}); });
it("fails: can't find mediaUpload", async () => { it("fails: can't find mediaUpload", async () => {
const testFileName = 'testFilename'; const testFileName = 'testFilename';
jest.spyOn(mediaRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(mediaRepo, 'findOne').mockResolvedValueOnce(undefined);
try { await expect(service.findUploadByFilename(testFileName)).rejects.toThrow(
await service.findUploadByFilename(testFileName); NotInDBError,
} catch (e) { );
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
}); });

View file

@ -196,22 +196,18 @@ describe('NotesService', () => {
}); });
describe('fails:', () => { describe('fails:', () => {
it('alias is forbidden', async () => { it('alias is forbidden', async () => {
try { await expect(
await service.createNote(content, forbiddenNoteId); service.createNote(content, forbiddenNoteId),
} catch (e) { ).rejects.toThrow(ForbiddenIdError);
expect(e).toBeInstanceOf(ForbiddenIdError);
}
}); });
it('alias is already used', async () => { it('alias is already used', async () => {
jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => { jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => {
throw new Error(); throw new Error();
}); });
try { await expect(service.createNote(content, alias)).rejects.toThrow(
await service.createNote(content, alias); AlreadyInDBError,
} catch (e) { );
expect(e).toBeInstanceOf(AlreadyInDBError);
}
}); });
}); });
}); });
@ -225,7 +221,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]);
void service.getNoteContent(newNote).then((result) => { await service.getNoteContent(newNote).then((result) => {
expect(result).toEqual(content); expect(result).toEqual(content);
}); });
}); });
@ -240,7 +236,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]);
void service.getLatestRevision(newNote).then((result) => { await service.getLatestRevision(newNote).then((result) => {
expect(result).toEqual(revisions[0]); expect(result).toEqual(revisions[0]);
}); });
}); });
@ -257,7 +253,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]);
void service.getLatestRevision(newNote).then((result) => { await service.getLatestRevision(newNote).then((result) => {
expect(result).toEqual(revisions[0]); expect(result).toEqual(revisions[0]);
}); });
}); });
@ -274,19 +270,15 @@ describe('NotesService', () => {
describe('fails:', () => { describe('fails:', () => {
it('no note found', async () => { it('no note found', async () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
try { await expect(
await service.getNoteByIdOrAlias('noteThatDoesNoteExist'); service.getNoteByIdOrAlias('noteThatDoesNoteExist'),
} catch (e) { ).rejects.toThrow(NotInDBError);
expect(e).toBeInstanceOf(NotInDBError);
}
}); });
it('id is forbidden', async () => { it('id is forbidden', async () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
try { await expect(
await service.getNoteByIdOrAlias(forbiddenNoteId); service.getNoteByIdOrAlias(forbiddenNoteId),
} catch (e) { ).rejects.toThrow(ForbiddenIdError);
expect(e).toBeInstanceOf(ForbiddenIdError);
}
}); });
}); });
}); });
@ -587,36 +579,30 @@ describe('NotesService', () => {
}); });
describe('fails:', () => { describe('fails:', () => {
it('userPermissions has duplicate entries', async () => { it('userPermissions has duplicate entries', async () => {
try { await expect(
await service.updateNotePermissions(note, { service.updateNotePermissions(note, {
sharedToUsers: [userPermissionUpdate, userPermissionUpdate], sharedToUsers: [userPermissionUpdate, userPermissionUpdate],
sharedToGroups: [], sharedToGroups: [],
}); }),
} catch (e) { ).rejects.toThrow(PermissionsUpdateInconsistentError);
expect(e).toBeInstanceOf(PermissionsUpdateInconsistentError);
}
}); });
it('groupPermissions has duplicate entries', async () => { it('groupPermissions has duplicate entries', async () => {
try { await expect(
await service.updateNotePermissions(note, { service.updateNotePermissions(note, {
sharedToUsers: [], sharedToUsers: [],
sharedToGroups: [groupPermissionUpate, groupPermissionUpate], sharedToGroups: [groupPermissionUpate, groupPermissionUpate],
}); }),
} catch (e) { ).rejects.toThrow(PermissionsUpdateInconsistentError);
expect(e).toBeInstanceOf(PermissionsUpdateInconsistentError);
}
}); });
it('userPermissions and groupPermissions have duplicate entries', async () => { it('userPermissions and groupPermissions have duplicate entries', async () => {
try { await expect(
await service.updateNotePermissions(note, { service.updateNotePermissions(note, {
sharedToUsers: [userPermissionUpdate, userPermissionUpdate], sharedToUsers: [userPermissionUpdate, userPermissionUpdate],
sharedToGroups: [groupPermissionUpate, groupPermissionUpate], sharedToGroups: [groupPermissionUpate, groupPermissionUpate],
}); }),
} catch (e) { ).rejects.toThrow(PermissionsUpdateInconsistentError);
expect(e).toBeInstanceOf(PermissionsUpdateInconsistentError);
}
}); });
}); });
}); });

View file

@ -419,13 +419,13 @@ describe('PermissionsService', () => {
function permutator( function permutator(
inputArr: NoteGroupPermission[], inputArr: NoteGroupPermission[],
): NoteGroupPermission[][] { ): NoteGroupPermission[][] {
const results = []; const results: NoteGroupPermission[][] = [];
function permute( function permute(
arr: NoteGroupPermission[], arr: NoteGroupPermission[],
memo: NoteGroupPermission[], memo: NoteGroupPermission[],
): NoteGroupPermission[][] { ): NoteGroupPermission[][] {
let cur; let cur: NoteGroupPermission[];
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
cur = arr.splice(i, 1); cur = arr.splice(i, 1);