Create unit test.

This commit is contained in:
Brian Cao 2023-09-25 13:31:25 -07:00
parent ce2a83a09f
commit e6eb9a487e
4 changed files with 21 additions and 19 deletions

View File

@ -30,6 +30,8 @@ export const FILTER_RANGE = 'filter-range';
export const FILTER_REFERRERS = 'filter-referrers'; export const FILTER_REFERRERS = 'filter-referrers';
export const FILTER_PAGES = 'filter-pages'; export const FILTER_PAGES = 'filter-pages';
export const UNIT_TYPES = ['year', 'month', 'hour', 'day'];
export const USER_FILTER_TYPES = { export const USER_FILTER_TYPES = {
all: 'All', all: 'All',
username: 'Username', username: 'Username',

View File

@ -1,5 +1,6 @@
import moment from 'moment'; import moment from 'moment';
import * as yup from 'yup'; import * as yup from 'yup';
import { UNIT_TYPES } from './constants';
export const DateRangeValidation = { export const DateRangeValidation = {
startAt: yup.number().integer().required(), startAt: yup.number().integer().required(),
@ -20,5 +21,11 @@ export function getFilterValidation(matchRegex) {
export const TimezoneTest = yup.string().test( export const TimezoneTest = yup.string().test(
'timezone', 'timezone',
() => `Invalid timezone`, () => `Invalid timezone`,
value => !moment.tz.zone(value), value => !value || !moment.tz.zone(value),
);
export const UnitTypeTest = yup.string().test(
'unit',
() => `Invalid unit`,
value => !value || !UNIT_TYPES.includes(value),
); );

View File

@ -1,22 +1,19 @@
import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware'; import { useAuth, useCors, useValidate } from 'lib/middleware';
import moment from 'moment-timezone';
import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getEventMetrics } from 'queries';
import { parseDateRangeQuery } from 'lib/query'; import { parseDateRangeQuery } from 'lib/query';
import { NextApiRequestQueryBody, WebsiteMetric } from 'lib/types';
import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getEventMetrics } from 'queries';
import * as yup from 'yup'; import * as yup from 'yup';
import { TimezoneTest } from 'lib/yup';
const unitTypes = ['year', 'month', 'hour', 'day'];
export interface WebsiteEventsRequestQuery { export interface WebsiteEventsRequestQuery {
id: string; id: string;
startAt: string; startAt: string;
endAt: string; endAt: string;
unit: string; unit?: string;
timezone: string; timezone?: string;
url: string; url: string;
} }
@ -25,8 +22,8 @@ const schema = {
id: yup.string().uuid().required(), id: yup.string().uuid().required(),
startAt: yup.number().integer().required(), startAt: yup.number().integer().required(),
endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(), endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),
unit: yup.string().required(), unit: UnitTypeTest,
timezone: TimezoneTest.required(), timezone: TimezoneTest,
url: yup.string(), url: yup.string(),
}), }),
}; };
@ -49,10 +46,6 @@ export default async (
return unauthorized(res); return unauthorized(res);
} }
if (!moment.tz.zone(timezone) || !unitTypes.includes(unit)) {
return badRequest(res);
}
const events = await getEventMetrics(websiteId, { const events = await getEventMetrics(websiteId, {
startDate, startDate,
endDate, endDate,

View File

@ -23,14 +23,14 @@ export interface WebsitePageviewRequestQuery {
city?: string; city?: string;
} }
import { TimezoneTest } from 'lib/yup'; import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import * as yup from 'yup'; import * as yup from 'yup';
const schema = { const schema = {
GET: yup.object().shape({ GET: yup.object().shape({
id: yup.string().uuid().required(), id: yup.string().uuid().required(),
startAt: yup.number().required(), startAt: yup.number().required(),
endAt: yup.number().required(), endAt: yup.number().required(),
unit: yup.string(), unit: UnitTypeTest,
timezone: TimezoneTest, timezone: TimezoneTest,
url: yup.string(), url: yup.string(),
referrer: yup.string(), referrer: yup.string(),