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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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