Skip to content

Commit

Permalink
test logging cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sspenst committed Mar 21, 2024
1 parent d86cd3b commit 9d430c0
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pages/api/signup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default apiWrapper({ POST: {
const recaptchaData = await recaptchaResponse.json();

if (!recaptchaResponse.ok || !recaptchaData?.success) {
const errorMessage = `Error validating recaptcha [Status: ${recaptchaResponse.status}], [Data: ${recaptchaData}]`;
const errorMessage = `Error validating recaptcha [Status: ${recaptchaResponse.status}], [Data: ${JSON.stringify(recaptchaData)}]`;

logger.error(errorMessage);

Expand Down
2 changes: 2 additions & 0 deletions tests/pages/api/collection/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('pages/api/collection/index.ts', () => {
});
});
test('Doing a POST with no data should error', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down Expand Up @@ -209,6 +210,7 @@ describe('pages/api/collection/index.ts', () => {
});

test('Doing a POST but invalid/missing fields should fail', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down
5 changes: 5 additions & 0 deletions tests/pages/api/level/[id]/checkpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Role from '@root/constants/role';
import TestId from '@root/constants/testId';
import { directionsToGameState, isValidDirections } from '@root/helpers/checkpointHelpers';
import { areEqualGameStates } from '@root/helpers/gameStateHelpers';
import { logger } from '@root/helpers/logger';
import { BEST_CHECKPOINT_INDEX } from '@root/hooks/useCheckpoints';
import dbConnect, { dbDisconnect } from '@root/lib/dbConnect';
import { getTokenCookieValue } from '@root/lib/getTokenCookie';
Expand All @@ -12,6 +13,7 @@ import handler from '@root/pages/api/level/[id]/checkpoints';
import { enableFetchMocks } from 'jest-fetch-mock';
import mongoose from 'mongoose';
import { testApiHandler } from 'next-test-api-route-handler';
import { Logger } from 'winston';
import gameStateFromDirections from './gameStateFromDirections';

beforeAll(async () => {
Expand Down Expand Up @@ -111,6 +113,7 @@ describe('api/user/[id]/checkpoints', () => {
});
});
test('try to save with invalid directions', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await UserModel.findByIdAndUpdate(TestId.USER, {
$addToSet: {
roles: Role.PRO
Expand All @@ -132,6 +135,7 @@ describe('api/user/[id]/checkpoints', () => {
});
});
test('try to save with invalid directions (object)', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await UserModel.findByIdAndUpdate(TestId.USER, {
$addToSet: {
roles: Role.PRO
Expand Down Expand Up @@ -326,6 +330,7 @@ describe('api/user/[id]/checkpoints', () => {
});
});
test('should not be able to delete BEST_CHECKPOINT_INDEX', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await UserModel.findByIdAndUpdate(TestId.USER, {
$addToSet: {
roles: Role.PRO
Expand Down
3 changes: 3 additions & 0 deletions tests/pages/api/level/[id]/prostats.level.solves.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Role from '@root/constants/role';
import TestId from '@root/constants/testId';
import { logger } from '@root/helpers/logger';
import dbConnect, { dbDisconnect } from '@root/lib/dbConnect';
import { getTokenCookieValue } from '@root/lib/getTokenCookie';
import { NextApiRequestWithAuth } from '@root/lib/withAuth';
import { UserModel } from '@root/models/mongoose';
import handler from '@root/pages/api/level/[id]/prostats/completions';
import { enableFetchMocks } from 'jest-fetch-mock';
import { testApiHandler } from 'next-test-api-route-handler';
import { Logger } from 'winston';

beforeAll(async () => {
await dbConnect();
Expand All @@ -20,6 +22,7 @@ afterEach(() => {
enableFetchMocks();

describe('api/user/[id]/prostats/[type]', () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
test('bad query', async () => {
await testApiHandler({
pagesHandler: async (_, res) => {
Expand Down
1 change: 1 addition & 0 deletions tests/pages/api/level/edit_levels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ describe('Editing levels should work correctly', () => {
});

test('Testing edit level but using malformed data', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down
2 changes: 2 additions & 0 deletions tests/pages/api/level/level.byid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('pages/api/level/index.ts', () => {
});

test('Doing a POST with no level data should error', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand All @@ -97,6 +98,7 @@ describe('pages/api/level/index.ts', () => {
});
});
test('Doing a POST with partial data should NOT be OK', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down
3 changes: 3 additions & 0 deletions tests/pages/api/match/matchCreate.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DEFAULT_GAME_ID } from '@root/constants/GameId';
import { logger } from '@root/helpers/logger';
import { enableFetchMocks } from 'jest-fetch-mock';
import { testApiHandler } from 'next-test-api-route-handler';
import { Logger } from 'winston';
import TestId from '../../../../constants/testId';
import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect';
import { getTokenCookieValue } from '../../../../lib/getTokenCookie';
Expand Down Expand Up @@ -74,6 +76,7 @@ describe('matchCreate', () => {
});
});
test('POST a second time should fail (already in match)', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
await handler({
Expand Down
2 changes: 2 additions & 0 deletions tests/pages/api/notification/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('Notifications', () => {
});
});
test('Trying to put with invalid id', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down Expand Up @@ -160,6 +161,7 @@ describe('Notifications', () => {
});
});
test('Trying to put with correct id but no body', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down
3 changes: 3 additions & 0 deletions tests/pages/api/review/review.errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe('review.test.errors', () => {
});
});
test('Testing POSTing with malformed score should not work', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down Expand Up @@ -192,6 +193,7 @@ describe('review.test.errors', () => {
});
});
test('Testing POSTing with a number that is not a 0.5 increment should fail', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down Expand Up @@ -226,6 +228,7 @@ describe('review.test.errors', () => {
});
});
test('Testing POSTing with a number that is out of bounds should fail', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down
2 changes: 2 additions & 0 deletions tests/pages/api/review/review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ describe('Reviewing levels should work correctly', () => {
});
});
test('Testing editing review but with score that is not 0.5 increment', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down Expand Up @@ -292,6 +293,7 @@ describe('Reviewing levels should work correctly', () => {
});

test('Testing editing review without userId', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand Down
3 changes: 3 additions & 0 deletions tests/pages/api/ses-webhook/ses-webhook.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { NextApiRequestWrapper } from '@root/helpers/apiWrapper';
import { logger } from '@root/helpers/logger';
import { dbDisconnect } from '@root/lib/dbConnect';
import { NextApiRequestWithAuth } from '@root/lib/withAuth';
import { enableFetchMocks } from 'jest-fetch-mock';
import { testApiHandler } from 'next-test-api-route-handler';
import { Logger } from 'winston';
import handler from '../../../../pages/api/ses-webhook/index';

const sampleJSON = {
Expand Down Expand Up @@ -84,6 +86,7 @@ describe('ses-webhook', () => {
});
});
test('Weird payload', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
process.env.SES_WEBHOOK_SECRET = 'test';
await testApiHandler({
pagesHandler: async (_, res) => {
Expand Down
3 changes: 1 addition & 2 deletions tests/pages/api/signup/signup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('pages/api/signup', () => {
// mock fetch failing with 400
fetchMock.mockResponseOnce(JSON.stringify({ 'mock': true }), { status: 408 });

// jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));
await testApiHandler({
pagesHandler: async (_, res) => {
const req: NextApiRequestWithAuth = {
Expand All @@ -103,7 +102,7 @@ describe('pages/api/signup', () => {
const res = await fetch();
const response = await res.json();

expect(response.error).toBe('Error validating recaptcha [Status: 408]');
expect(response.error).toBe('Error validating recaptcha [Status: 408], [Data: {"mock":true}]');
expect(res.status).toBe(400);
},
});
Expand Down

0 comments on commit 9d430c0

Please sign in to comment.