Skip to content

Commit

Permalink
test(e2e): improve validation fro name query param
Browse files Browse the repository at this point in the history
  • Loading branch information
fityannugroho committed Feb 11, 2024
1 parent 02853ca commit 42b8dc0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
6 changes: 5 additions & 1 deletion test/district.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { District } from '@prisma/client';
import { AppTester } from './helper/app-tester';
import { districtRegex } from './helper/data-regex';
import { getEncodedSymbols } from './helper/utils';

describe('District (e2e)', () => {
const baseUrl = '/districts';
Expand All @@ -27,7 +28,10 @@ describe('District (e2e)', () => {
});

it("should return 400 if the `name` is more than 100 chars, or contains any other symbols besides '()-./", async () => {
const invalidNames = ['x'.repeat(101), 'b@ndung'];
const invalidNames = [
'x'.repeat(101),
...getEncodedSymbols({ exclude: '()-./' }),
];

for (const name of invalidNames) {
await tester.expectBadRequest(`${baseUrl}?name=${name}`);
Expand Down
26 changes: 26 additions & 0 deletions test/helper/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* All symbol characters.
*/
const symbols = '`~!@#$%^&*()_+-=[]{}|;:,.<>?/\\';

type GetEncodedSymbolsOptions = {
/**
* The symbol characters to exclude.
*/
exclude?: string;
};

/**
* Get encoded symbol characters.
* @returns The encoded symbol characters.
*/
export function getEncodedSymbols(
options: GetEncodedSymbolsOptions = {},
): string[] {
const { exclude = '' } = options;

return symbols
.split('')
.filter((char) => !exclude.includes(char))
.map((char) => encodeURIComponent(char));
}
8 changes: 6 additions & 2 deletions test/island.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Island } from '@prisma/client';
import { AppTester } from './helper/app-tester';
import { islandRegex } from './helper/data-regex';
import { getEncodedSymbols } from './helper/utils';

describe('Island (e2e)', () => {
const baseUrl = '/islands';
Expand Down Expand Up @@ -31,8 +32,11 @@ describe('Island (e2e)', () => {
});
});

it('should return 400 if the `name` is more than 100 chars, or contains any symbols', async () => {
const invalidNames = ['x'.repeat(101), 's@bang'];
it("should return 400 if the `name` is more than 100 chars, or contains any symbols except '-/", async () => {
const invalidNames = [
'x'.repeat(101),
...getEncodedSymbols({ exclude: "'-/" }),
];

for (const name of invalidNames) {
await tester.expectBadRequest(`${baseUrl}?name=${name}`);
Expand Down
5 changes: 3 additions & 2 deletions test/province.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Province } from '@prisma/client';
import { AppTester } from './helper/app-tester';
import { provinceRegex } from './helper/data-regex';
import { getEncodedSymbols } from './helper/utils';

describe('Province (e2e)', () => {
const baseUrl = '/provinces';
Expand Down Expand Up @@ -35,7 +36,7 @@ describe('Province (e2e)', () => {

describe(`GET ${baseUrl}?name={name}`, () => {
it('should return 400 if the `name` is more than 100 chars, or contains any symbols', async () => {
const invalidNames = ['x'.repeat(101), 'j@wa'];
const invalidNames = ['x'.repeat(101), ...getEncodedSymbols()];

for (const name of invalidNames) {
await tester.expectBadRequest(`${baseUrl}?name=${name}`);
Expand All @@ -51,7 +52,7 @@ describe('Province (e2e)', () => {
});

it('should return array of provinces that match with the `name`', async () => {
const testName = 'jawa';
const testName = 'jawa barat';
const provinces = await tester.expectData<Province[]>(
`${baseUrl}?name=${testName}`,
);
Expand Down
3 changes: 2 additions & 1 deletion test/regency.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Regency } from '@prisma/client';
import { AppTester } from './helper/app-tester';
import { regencyRegex } from './helper/data-regex';
import { getEncodedSymbols } from './helper/utils';

describe('Regency (e2e)', () => {
const baseUrl = '/regencies';
Expand All @@ -27,7 +28,7 @@ describe('Regency (e2e)', () => {
});

it('should return 400 if the `name` is more than 100 chars, or contains any symbols', async () => {
const invalidNames = ['x'.repeat(101), 'b@ndung'];
const invalidNames = ['x'.repeat(101), ...getEncodedSymbols()];

for (const name of invalidNames) {
await tester.expectBadRequest(`${baseUrl}?name=${name}`);
Expand Down
6 changes: 5 additions & 1 deletion test/village.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Village } from '@prisma/client';
import { AppTester } from './helper/app-tester';
import { villageRegex } from './helper/data-regex';
import { getEncodedSymbols } from './helper/utils';

describe('Village (e2e)', () => {
const baseUrl = '/villages';
Expand All @@ -27,7 +28,10 @@ describe('Village (e2e)', () => {
});

it("should return 400 if the `name` more than 100 chars, or contains any other symbols besides '()-./", async () => {
const invalidNames = ['x'.repeat(101), 'c!nunuk'];
const invalidNames = [
'x'.repeat(101),
...getEncodedSymbols({ exclude: '()-./' }),
];

for (const name of invalidNames) {
await tester.expectBadRequest(`${baseUrl}?name=${name}`);
Expand Down

0 comments on commit 42b8dc0

Please sign in to comment.