Skip to content

Commit

Permalink
remove jest deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
janishar committed Jan 5, 2024
1 parent 5f70eb2 commit db15c41
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 203 deletions.
10 changes: 5 additions & 5 deletions tests/auth/apikey/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('apikey validation', () => {
it('Should response with 400 if x-api-key header is not passed', async () => {
const response = await request.get(endpoint).timeout(2000);
expect(response.status).toBe(400);
expect(mockFindApiKey).not.toBeCalled();
expect(mockFindApiKey).not.toHaveBeenCalled();
});

it('Should response with 403 if wrong x-api-key header is passed', async () => {
Expand All @@ -25,8 +25,8 @@ describe('apikey validation', () => {
.set('x-api-key', wrongApiKey)
.timeout(2000);
expect(response.status).toBe(403);
expect(mockFindApiKey).toBeCalledTimes(1);
expect(mockFindApiKey).toBeCalledWith(wrongApiKey);
expect(mockFindApiKey).toHaveBeenCalledTimes(1);
expect(mockFindApiKey).toHaveBeenCalledWith(wrongApiKey);
});

it('Should response with 404 if correct x-api-key header is passed and when route is not handelled', async () => {
Expand All @@ -35,7 +35,7 @@ describe('apikey validation', () => {
.set('x-api-key', API_KEY)
.timeout(2000);
expect(response.status).toBe(404);
expect(mockFindApiKey).toBeCalledTimes(1);
expect(mockFindApiKey).toBeCalledWith(API_KEY);
expect(mockFindApiKey).toHaveBeenCalledTimes(1);
expect(mockFindApiKey).toHaveBeenCalledWith(API_KEY);
});
});
30 changes: 15 additions & 15 deletions tests/auth/authentication/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('authentication validation', () => {
const response = await addHeaders(request.get(endpoint));
expect(response.status).toBe(400);
expect(response.body.message).toMatch(/authorization/);
expect(getAccessTokenSpy).not.toBeCalled();
expect(getAccessTokenSpy).not.toHaveBeenCalled();
});

it('Should response with 400 if Authorization header do not have Bearer', async () => {
Expand All @@ -39,7 +39,7 @@ describe('authentication validation', () => {
);
expect(response.status).toBe(400);
expect(response.body.message).toMatch(/authorization/);
expect(getAccessTokenSpy).not.toBeCalled();
expect(getAccessTokenSpy).not.toHaveBeenCalled();
});

it('Should response with 401 if wrong Authorization header is provided', async () => {
Expand All @@ -49,25 +49,25 @@ describe('authentication validation', () => {
);
expect(response.status).toBe(401);
expect(response.body.message).toMatch(/token/i);
expect(getAccessTokenSpy).toBeCalledTimes(1);
expect(getAccessTokenSpy).toBeCalledWith('Bearer 123');
expect(getAccessTokenSpy).toReturnWith('123');
expect(mockJwtValidate).toBeCalledTimes(1);
expect(mockJwtValidate).toBeCalledWith('123');
expect(mockUserFindById).not.toBeCalled();
expect(getAccessTokenSpy).toHaveBeenCalledTimes(1);
expect(getAccessTokenSpy).toHaveBeenCalledWith('Bearer 123');
expect(getAccessTokenSpy).toHaveReturnedWith('123');
expect(mockJwtValidate).toHaveBeenCalledTimes(1);
expect(mockJwtValidate).toHaveBeenCalledWith('123');
expect(mockUserFindById).not.toHaveBeenCalled();
});

it('Should response with 404 if correct Authorization header is provided', async () => {
const response = await addAuthHeaders(request.get(endpoint));
expect(response.body.message).not.toMatch(/not registered/);
expect(response.body.message).not.toMatch(/token/i);
expect(response.status).toBe(404);
expect(getAccessTokenSpy).toBeCalledTimes(1);
expect(getAccessTokenSpy).toBeCalledWith(`Bearer ${ACCESS_TOKEN}`);
expect(getAccessTokenSpy).toReturnWith(ACCESS_TOKEN);
expect(mockJwtValidate).toBeCalledTimes(1);
expect(mockJwtValidate).toBeCalledWith(ACCESS_TOKEN);
expect(mockUserFindById).toBeCalledTimes(1);
expect(mockKeystoreFindForKey).toBeCalledTimes(1);
expect(getAccessTokenSpy).toHaveBeenCalledTimes(1);
expect(getAccessTokenSpy).toHaveBeenCalledWith(`Bearer ${ACCESS_TOKEN}`);
expect(getAccessTokenSpy).toHaveReturnedWith(ACCESS_TOKEN);
expect(mockJwtValidate).toHaveBeenCalledTimes(1);
expect(mockJwtValidate).toHaveBeenCalledWith(ACCESS_TOKEN);
expect(mockUserFindById).toHaveBeenCalledTimes(1);
expect(mockKeystoreFindForKey).toHaveBeenCalledTimes(1);
});
});
12 changes: 6 additions & 6 deletions tests/auth/authorization/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ describe('authentication validation for editor', () => {
const response = await addAuthHeaders(request.get(endpoint));
expect(response.status).toBe(401);
expect(response.body.message).toMatch(/denied/);
expect(mockRoleRepoFindByCodes).toBeCalledTimes(1);
expect(mockUserFindById).toBeCalledTimes(1);
expect(mockRoleRepoFindByCodes).toBeCalledWith([
expect(mockRoleRepoFindByCodes).toHaveBeenCalledTimes(1);
expect(mockUserFindById).toHaveBeenCalledTimes(1);
expect(mockRoleRepoFindByCodes).toHaveBeenCalledWith([
RoleCode.ADMIN,
RoleCode.EDITOR,
]);
Expand All @@ -51,8 +51,8 @@ describe('authentication validation for writer', () => {
EDITOR_ACCESS_TOKEN,
);
expect(response.status).toBe(404);
expect(mockRoleRepoFindByCodes).toBeCalledTimes(1);
expect(mockUserFindById).toBeCalledTimes(1);
expect(mockRoleRepoFindByCodes).toBeCalledWith([RoleCode.WRITER]);
expect(mockRoleRepoFindByCodes).toHaveBeenCalledTimes(1);
expect(mockUserFindById).toHaveBeenCalledTimes(1);
expect(mockRoleRepoFindByCodes).toHaveBeenCalledWith([RoleCode.WRITER]);
});
});
14 changes: 7 additions & 7 deletions tests/core/jwt/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ describe('JWT class tests', () => {
expect(e).toBeInstanceOf(BadTokenError);
}

expect(readFileSpy).toBeCalledTimes(1);
expect(readFileSpy).toHaveBeenCalledTimes(1);
});

it('Should generate a token for JWT.encode', async () => {
const payload = new JwtPayload(issuer, audience, subject, param, validity);
const token = await JWT.encode(payload);

expect(typeof token).toBe('string');
expect(readFileSpy).toBeCalledTimes(1);
expect(readFileSpy).toHaveBeenCalledTimes(1);
});

it('Should decode a valid token for JWT.decode', async () => {
Expand All @@ -37,7 +37,7 @@ describe('JWT class tests', () => {
const decoded = await JWT.decode(token);

expect(decoded).toMatchObject(payload);
expect(readFileSpy).toBeCalledTimes(2);
expect(readFileSpy).toHaveBeenCalledTimes(2);
});

it('Should parse an expired token for JWT.decode', async () => {
Expand All @@ -55,7 +55,7 @@ describe('JWT class tests', () => {
const decoded = await JWT.decode(token);

expect(decoded).toMatchObject(payload);
expect(readFileSpy).toBeCalledTimes(2);
expect(readFileSpy).toHaveBeenCalledTimes(2);
});

it('Should throw error for invalid token in JWT.validate', async () => {
Expand All @@ -65,7 +65,7 @@ describe('JWT class tests', () => {
expect(e).toBeInstanceOf(BadTokenError);
}

expect(readFileSpy).toBeCalledTimes(1);
expect(readFileSpy).toHaveBeenCalledTimes(1);
});

it('Should validate a valid token for JWT.validate', async () => {
Expand All @@ -74,7 +74,7 @@ describe('JWT class tests', () => {
const decoded = await JWT.validate(token);

expect(decoded).toMatchObject(payload);
expect(readFileSpy).toBeCalledTimes(2);
expect(readFileSpy).toHaveBeenCalledTimes(2);
});

it('Should validate a token expiry for JWT.validate', async () => {
Expand All @@ -94,6 +94,6 @@ describe('JWT class tests', () => {
} catch (e) {
expect(e).toBeInstanceOf(TokenExpiredError);
}
expect(readFileSpy).toBeCalledTimes(2);
expect(readFileSpy).toHaveBeenCalledTimes(2);
});
});
66 changes: 33 additions & 33 deletions tests/routes/access/login/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ describe('Login basic route', () => {
it('Should send error when empty body is sent', async () => {
const response = await addHeaders(request.post(endpoint), apikey);
expect(response.status).toBe(400);
expect(userFindByEmailSpy).not.toBeCalled();
expect(bcryptCompareSpy).not.toBeCalled();
expect(keystoreCreateSpy).not.toBeCalled();
expect(createTokensSpy).not.toBeCalled();
expect(userFindByEmailSpy).not.toHaveBeenCalled();
expect(bcryptCompareSpy).not.toHaveBeenCalled();
expect(keystoreCreateSpy).not.toHaveBeenCalled();
expect(createTokensSpy).not.toHaveBeenCalled();
});

it('Should send error when email is only sent', async () => {
Expand All @@ -70,10 +70,10 @@ describe('Login basic route', () => {
);
expect(response.status).toBe(400);
expect(response.body.message).toMatch(/password/);
expect(userFindByEmailSpy).not.toBeCalled();
expect(bcryptCompareSpy).not.toBeCalled();
expect(keystoreCreateSpy).not.toBeCalled();
expect(createTokensSpy).not.toBeCalled();
expect(userFindByEmailSpy).not.toHaveBeenCalled();
expect(bcryptCompareSpy).not.toHaveBeenCalled();
expect(keystoreCreateSpy).not.toHaveBeenCalled();
expect(createTokensSpy).not.toHaveBeenCalled();
});

it('Should send error when password is only sent', async () => {
Expand All @@ -83,10 +83,10 @@ describe('Login basic route', () => {
);
expect(response.status).toBe(400);
expect(response.body.message).toMatch(/email/);
expect(userFindByEmailSpy).not.toBeCalled();
expect(bcryptCompareSpy).not.toBeCalled();
expect(keystoreCreateSpy).not.toBeCalled();
expect(createTokensSpy).not.toBeCalled();
expect(userFindByEmailSpy).not.toHaveBeenCalled();
expect(bcryptCompareSpy).not.toHaveBeenCalled();
expect(keystoreCreateSpy).not.toHaveBeenCalled();
expect(createTokensSpy).not.toHaveBeenCalled();
});

it('Should send error when email is not valid format', async () => {
Expand All @@ -96,10 +96,10 @@ describe('Login basic route', () => {
);
expect(response.status).toBe(400);
expect(response.body.message).toMatch(/valid email/);
expect(userFindByEmailSpy).not.toBeCalled();
expect(bcryptCompareSpy).not.toBeCalled();
expect(keystoreCreateSpy).not.toBeCalled();
expect(createTokensSpy).not.toBeCalled();
expect(userFindByEmailSpy).not.toHaveBeenCalled();
expect(bcryptCompareSpy).not.toHaveBeenCalled();
expect(keystoreCreateSpy).not.toHaveBeenCalled();
expect(createTokensSpy).not.toHaveBeenCalled();
});

it('Should send error when password is not valid format', async () => {
Expand All @@ -113,10 +113,10 @@ describe('Login basic route', () => {
expect(response.status).toBe(400);
expect(response.body.message).toMatch(/password length/);
expect(response.body.message).toMatch(/6 char/);
expect(userFindByEmailSpy).not.toBeCalled();
expect(bcryptCompareSpy).not.toBeCalled();
expect(keystoreCreateSpy).not.toBeCalled();
expect(createTokensSpy).not.toBeCalled();
expect(userFindByEmailSpy).not.toHaveBeenCalled();
expect(bcryptCompareSpy).not.toHaveBeenCalled();
expect(keystoreCreateSpy).not.toHaveBeenCalled();
expect(createTokensSpy).not.toHaveBeenCalled();
});

it('Should send error when user not registered for email', async () => {
Expand All @@ -129,10 +129,10 @@ describe('Login basic route', () => {
);
expect(response.status).toBe(400);
expect(response.body.message).toMatch(/not registered/);
expect(userFindByEmailSpy).toBeCalledTimes(1);
expect(bcryptCompareSpy).not.toBeCalled();
expect(keystoreCreateSpy).not.toBeCalled();
expect(createTokensSpy).not.toBeCalled();
expect(userFindByEmailSpy).toHaveBeenCalledTimes(1);
expect(bcryptCompareSpy).not.toHaveBeenCalled();
expect(keystoreCreateSpy).not.toHaveBeenCalled();
expect(createTokensSpy).not.toHaveBeenCalled();
});

it('Should send error for wrong password', async () => {
Expand All @@ -145,10 +145,10 @@ describe('Login basic route', () => {
);
expect(response.status).toBe(401);
expect(response.body.message).toMatch(/authentication failure/i);
expect(userFindByEmailSpy).toBeCalledTimes(1);
expect(bcryptCompareSpy).toBeCalledTimes(1);
expect(keystoreCreateSpy).not.toBeCalled();
expect(createTokensSpy).not.toBeCalled();
expect(userFindByEmailSpy).toHaveBeenCalledTimes(1);
expect(bcryptCompareSpy).toHaveBeenCalledTimes(1);
expect(keystoreCreateSpy).not.toHaveBeenCalled();
expect(createTokensSpy).not.toHaveBeenCalled();
});

it('Should send success response for correct credentials', async () => {
Expand All @@ -172,12 +172,12 @@ describe('Login basic route', () => {
expect(response.body.data.tokens).toHaveProperty('accessToken');
expect(response.body.data.tokens).toHaveProperty('refreshToken');

expect(userFindByEmailSpy).toBeCalledTimes(1);
expect(keystoreCreateSpy).toBeCalledTimes(1);
expect(bcryptCompareSpy).toBeCalledTimes(1);
expect(createTokensSpy).toBeCalledTimes(1);
expect(userFindByEmailSpy).toHaveBeenCalledTimes(1);
expect(keystoreCreateSpy).toHaveBeenCalledTimes(1);
expect(bcryptCompareSpy).toHaveBeenCalledTimes(1);
expect(createTokensSpy).toHaveBeenCalledTimes(1);

expect(bcryptCompareSpy).toBeCalledWith(password, user.password);
expect(bcryptCompareSpy).toHaveBeenCalledWith(password, user.password);
});
});

Expand Down
Loading

0 comments on commit db15c41

Please sign in to comment.