@opengis/fastify-table 1.0.22 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Changelog.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # fastify-table
2
2
 
3
+ ## 1.0.23 - 07.05.2024
4
+
5
+ - getTemplate funcs
6
+ - dblist api
7
+
3
8
  ## 1.0.22 - 03.05.2024
4
9
 
5
10
  - getFilterSQL funcs
@@ -1,29 +1,29 @@
1
- import dataInsert from '../funcs/dataInsert.js';
2
- import getToken from '../funcs/getToken.js';
3
- import checkXSS from './utils/checkXSS.js';
4
- import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
-
6
- export default async function insert(req) {
7
- const loadTemplate = await getTemplate('table', req.params.table);
8
- const { table } = loadTemplate || req.params || {};
9
- if (!table) return { status: 404, message: 'table is required' };
10
-
11
- const { funcs, session, params } = req;
12
- const tokenDataString = await getToken({
13
- funcs, session, token: params.table, mode: 'a', json: 0,
14
- });
15
-
16
- const { form, add } = JSON.parse(tokenDataString || '{}');
17
-
18
- const formData = form ? await getTemplate('form', form) : {};
19
-
20
- const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
21
-
22
- if (xssCheck.error && formData?.xssCheck !== false) {
23
- req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
24
- return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
25
- }
26
-
27
- const res = await dataInsert({ table: add || table, data: req.body });
28
- return { rows: res.rows };
29
- }
1
+ import dataInsert from '../funcs/dataInsert.js';
2
+ import getToken from '../funcs/getToken.js';
3
+ import checkXSS from './utils/checkXSS.js';
4
+ import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
+
6
+ export default async function insert(req) {
7
+ const loadTemplate = await getTemplate('table', req.params.table);
8
+ const { table } = loadTemplate || req.params || {};
9
+ if (!table) return { status: 404, message: 'table is required' };
10
+
11
+ const { funcs, session, params } = req;
12
+ const tokenDataString = await getToken({
13
+ funcs, session, token: params.table, mode: 'a', json: 0,
14
+ });
15
+
16
+ const { form, add } = JSON.parse(tokenDataString || '{}');
17
+
18
+ const formData = form ? await getTemplate('form', form) : {};
19
+
20
+ const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
21
+
22
+ if (xssCheck.error && formData?.xssCheck !== false) {
23
+ req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
24
+ return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
25
+ }
26
+
27
+ const res = await dataInsert({ table: add || table, data: req.body });
28
+ return { rows: res.rows };
29
+ }
@@ -1,31 +1,31 @@
1
- import dataUpdate from '../funcs/dataUpdate.js';
2
- import getToken from '../funcs/getToken.js';
3
- import checkXSS from './utils/checkXSS.js';
4
- import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
-
6
- export default async function update(req) {
7
- const loadTemplate = await getTemplate('table', req.params.table);
8
- const { table } = loadTemplate || req.params || {};
9
- const { id } = req.params || {};
10
- if (!req.params?.table) return { message: 'table is required', status: 404 };
11
- if (!id) return { message: 'id is required', status: 404 };
12
-
13
- const { funcs, session, params } = req;
14
- const tokenDataString = await getToken({
15
- funcs, session, token: params.table, mode: 'w', json: 0,
16
- });
17
-
18
- const tokenData = JSON.parse(tokenDataString || '{}');
19
-
20
- const formData = tokenData?.form ? await getTemplate('form', tokenData.form) : {};
21
-
22
- const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
23
-
24
- if (xssCheck.error && formData?.xssCheck !== false) {
25
- req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
26
- return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
27
- }
28
-
29
- const res = await dataUpdate({ table: tokenData?.table || table, id: tokenData?.id || id, data: req.body });
30
- return res;
31
- }
1
+ import dataUpdate from '../funcs/dataUpdate.js';
2
+ import getToken from '../funcs/getToken.js';
3
+ import checkXSS from './utils/checkXSS.js';
4
+ import getTemplate from '../../table/controllers/utils/getTemplate.js';
5
+
6
+ export default async function update(req) {
7
+ const loadTemplate = await getTemplate('table', req.params.table);
8
+ const { table } = loadTemplate || req.params || {};
9
+ const { id } = req.params || {};
10
+ if (!req.params?.table) return { message: 'table is required', status: 404 };
11
+ if (!id) return { message: 'id is required', status: 404 };
12
+
13
+ const { funcs, session, params } = req;
14
+ const tokenDataString = await getToken({
15
+ funcs, session, token: params.table, mode: 'w', json: 0,
16
+ });
17
+
18
+ const tokenData = JSON.parse(tokenDataString || '{}');
19
+
20
+ const formData = tokenData?.form ? await getTemplate('form', tokenData.form) : {};
21
+
22
+ const xssCheck = checkXSS({ body: req.body, schema: formData?.schema });
23
+
24
+ if (xssCheck.error && formData?.xssCheck !== false) {
25
+ req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
26
+ return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
27
+ }
28
+
29
+ const res = await dataUpdate({ table: tokenData?.table || table, id: tokenData?.id || id, data: req.body });
30
+ return res;
31
+ }
@@ -1,24 +1,24 @@
1
- import getPG from '../../pg/funcs/getPG.js';
2
- import getMeta from '../../pg/funcs/getMeta.js';
3
-
4
- export default async function dataInsert({ table, data }) {
5
- const pg = getPG({ name: 'client' });
6
- if (!data) return null;
7
- const { columns } = await getMeta(table);
8
- if (!columns) return null;
9
-
10
- const names = columns.map((el) => el.name);
11
- const filterData = Object.keys(data)
12
- .filter((el) => data[el] && names.includes(el)).map((el) => [el, data[el]]);
13
-
14
- const insertQuery = `insert into ${table}
15
-
16
- ( ${filterData?.map((key) => `"${key[0]}"`).join(',')})
17
-
18
- values (${filterData?.map((key, i) => `$${i + 1}`).join(',')})
19
-
20
- returning *`;
21
-
22
- const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' ? JSON.stringify(el[1]) : el[1]))]) || {};
23
- return res;
24
- }
1
+ import getPG from '../../pg/funcs/getPG.js';
2
+ import getMeta from '../../pg/funcs/getMeta.js';
3
+
4
+ export default async function dataInsert({ table, data }) {
5
+ const pg = getPG({ name: 'client' });
6
+ if (!data) return null;
7
+ const { columns } = await getMeta(table);
8
+ if (!columns) return null;
9
+
10
+ const names = columns.map((el) => el.name);
11
+ const filterData = Object.keys(data)
12
+ .filter((el) => data[el] && names.includes(el)).map((el) => [el, data[el]]);
13
+
14
+ const insertQuery = `insert into ${table}
15
+
16
+ ( ${filterData?.map((key) => `"${key[0]}"`).join(',')})
17
+
18
+ values (${filterData?.map((key, i) => `$${i + 1}`).join(',')})
19
+
20
+ returning *`;
21
+
22
+ const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' && !Array.isArray(el[1]) ? JSON.stringify(el[1]) : el[1]))]) || {};
23
+ return res;
24
+ }
@@ -1,20 +1,20 @@
1
- import getPG from '../../pg/funcs/getPG.js';
2
-
3
- import getMeta from '../../pg/funcs/getMeta.js';
4
-
5
- export default async function dataUpdate({
6
- table, id, data,
7
- }) {
8
- const pg = getPG({ name: 'client' });
9
- const { columns, pk } = await getMeta(table);
10
-
11
- const names = columns?.map((el) => el.name);
12
- const filterData = Object.keys(data)
13
- .filter((el) => data[el] && names?.includes(el)).map((el) => [el, data[el]]);
14
-
15
- const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => `"${key[0]}"=$${i + 2}`).join(',')}
16
- WHERE ${pk} = $1 returning *`;
17
- // console.log(updateDataset);
18
- const res = await pg.query(updateQuery, [id, ...filterData.map((el) => (typeof el[1] === 'object' ? JSON.stringify(el[1]) : el[1]))]).then(el => el?.rows?.[0]) || {};
19
- return res;
20
- }
1
+ import getPG from '../../pg/funcs/getPG.js';
2
+
3
+ import getMeta from '../../pg/funcs/getMeta.js';
4
+
5
+ export default async function dataUpdate({
6
+ table, id, data,
7
+ }) {
8
+ const pg = getPG({ name: 'client' });
9
+ const { columns, pk } = await getMeta(table);
10
+
11
+ const names = columns?.map((el) => el.name);
12
+ const filterData = Object.keys(data)
13
+ .filter((el) => data[el] && names?.includes(el)).map((el) => [el, data[el]]);
14
+
15
+ const updateQuery = `UPDATE ${table} SET ${filterData?.map((key, i) => `"${key[0]}"=$${i + 2}`).join(',')}
16
+ WHERE ${pk} = $1 returning *`;
17
+ // console.log(updateDataset);
18
+ const res = await pg.query(updateQuery, [id, ...filterData.map((el) => (typeof el[1] === 'object' && !Array.isArray(el[1]) ? JSON.stringify(el[1]) : el[1]))]).then(el => el?.rows?.[0]) || {};
19
+ return res;
20
+ }
package/crud/index.js CHANGED
@@ -1,29 +1,29 @@
1
- import getOPt from './funcs/getOpt.js';
2
- import setOpt from './funcs/setOpt.js';
3
- import isFileExists from './funcs/isFileExists.js';
4
- import dataUpdate from './funcs/dataUpdate.js';
5
- import dataInsert from './funcs/dataInsert.js';
6
-
7
- import update from './controllers/update.js';
8
- import insert from './controllers/insert.js';
9
- import deleteCrud from './controllers/deleteCrud.js';
10
-
11
- // import config from '../config.js';
12
-
13
- async function plugin(fastify, config = {}) {
14
- const prefix = config.prefix || '/api';
15
- // funcs
16
- fastify.decorate('setOpt', setOpt);
17
- fastify.decorate('getOpt', getOPt);
18
- fastify.decorate('dataUpdate', dataUpdate);
19
- fastify.decorate('dataInsert', dataInsert);
20
-
21
- fastify.decorate('isFileExists', isFileExists);
22
-
23
- // api
24
- fastify.put(`${prefix}/table/:table/:id`, {}, update);
25
- fastify.delete(`${prefix}/table/:table/:id`, {}, deleteCrud);
26
- fastify.post(`${prefix}/table/:table`, {}, insert);
27
- }
28
-
29
- export default plugin;
1
+ import getOPt from './funcs/getOpt.js';
2
+ import setOpt from './funcs/setOpt.js';
3
+ import isFileExists from './funcs/isFileExists.js';
4
+ import dataUpdate from './funcs/dataUpdate.js';
5
+ import dataInsert from './funcs/dataInsert.js';
6
+
7
+ import update from './controllers/update.js';
8
+ import insert from './controllers/insert.js';
9
+ import deleteCrud from './controllers/deleteCrud.js';
10
+
11
+ // import config from '../config.js';
12
+
13
+ async function plugin(fastify, config = {}) {
14
+ const prefix = config.prefix || '/api';
15
+ // funcs
16
+ fastify.decorate('setOpt', setOpt);
17
+ fastify.decorate('getOpt', getOPt);
18
+ fastify.decorate('dataUpdate', dataUpdate);
19
+ fastify.decorate('dataInsert', dataInsert);
20
+
21
+ fastify.decorate('isFileExists', isFileExists);
22
+
23
+ // api
24
+ fastify.put(`${prefix}/table/:table/:id`, {}, update);
25
+ fastify.delete(`${prefix}/table/:table/:id`, {}, deleteCrud);
26
+ fastify.post(`${prefix}/table/:table`, {}, insert);
27
+ }
28
+
29
+ export default plugin;
@@ -1,17 +1,17 @@
1
- import { randomUUID } from 'crypto';
2
- import { existsSync } from 'fs';
3
- import { readFile, writeFile } from 'fs/promises';
4
-
5
- import checkItem from './utils/checkItem.js';
6
-
7
- export default async function insertItem({ body = {} }) {
8
- const check = checkItem(body.data);
9
- if (check?.error) return check;
10
-
11
- Object.assign(body.data, { id: randomUUID() });
12
- const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
13
- data.push(body.data);
14
-
15
- await writeFile('dblist.json', JSON.stringify(data));
16
- return { data };
17
- }
1
+ import { randomUUID } from 'crypto';
2
+ import { existsSync } from 'fs';
3
+ import { readFile, writeFile } from 'fs/promises';
4
+
5
+ import checkItem from './utils/checkItem.js';
6
+
7
+ export default async function insertItem({ body = {} }) {
8
+ const check = checkItem(body.data);
9
+ if (check?.error) return check;
10
+
11
+ Object.assign(body.data, { id: randomUUID() });
12
+ const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
13
+ data.push(body.data);
14
+
15
+ await writeFile('dblist.json', JSON.stringify(data));
16
+ return { data };
17
+ }
@@ -1,7 +1,8 @@
1
- import { existsSync } from 'fs';
2
- import { readFile } from 'fs/promises';
3
-
4
- export default async function readItemList() {
5
- const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
6
- return { data };
7
- }
1
+ import { existsSync } from 'fs';
2
+ import { readFile } from 'fs/promises';
3
+
4
+ export default async function readItemList(req) {
5
+ const data = existsSync('dblist.json') ? JSON.parse(await readFile('dblist.json') || '[]') : [];
6
+ // const { user = {} } = req.session?.passport || {};
7
+ return { data };
8
+ }
package/dblist/index.js CHANGED
@@ -1,40 +1,40 @@
1
- import createItem from './controllers/insertItem.js';
2
- import readItemList from './controllers/readItems.js';
3
- import updateItem from './controllers/updateItem.js';
4
- import deleteItem from './controllers/deleteItem.js';
5
-
6
- export default async function plugin(fastify, config = {}) {
7
- const prefix = config.prefix || '/api';
8
- fastify.route({
9
- method: 'POST',
10
- url: `${prefix}/list`,
11
- config: {
12
- policy: [],
13
- },
14
- handler: createItem,
15
- });
16
- fastify.route({
17
- method: 'GET',
18
- url: `${prefix}/list`,
19
- config: {
20
- policy: [],
21
- },
22
- handler: readItemList,
23
- });
24
- fastify.route({
25
- method: 'PUT',
26
- url: `${prefix}/list/:id`,
27
- config: {
28
- policy: [],
29
- },
30
- handler: updateItem,
31
- });
32
- fastify.route({
33
- method: 'DELETE',
34
- url: `${prefix}/list/:id`,
35
- config: {
36
- policy: [],
37
- },
38
- handler: deleteItem,
39
- });
40
- }
1
+ import createItem from './controllers/createItem.js';
2
+ import readItemList from './controllers/readItems.js';
3
+ import updateItem from './controllers/updateItem.js';
4
+ import deleteItem from './controllers/deleteItem.js';
5
+
6
+ export default async function plugin(fastify, config = {}) {
7
+ const prefix = config.prefix || '/api';
8
+ fastify.route({
9
+ method: 'POST',
10
+ url: `${prefix}/list`,
11
+ config: {
12
+ policy: [],
13
+ },
14
+ handler: createItem,
15
+ });
16
+ fastify.route({
17
+ method: 'GET',
18
+ url: `${prefix}/list`,
19
+ config: {
20
+ policy: [],
21
+ },
22
+ handler: readItemList,
23
+ });
24
+ fastify.route({
25
+ method: 'PUT',
26
+ url: `${prefix}/list/:id`,
27
+ config: {
28
+ policy: [],
29
+ },
30
+ handler: updateItem,
31
+ });
32
+ fastify.route({
33
+ method: 'DELETE',
34
+ url: `${prefix}/list/:id`,
35
+ config: {
36
+ policy: [],
37
+ },
38
+ handler: deleteItem,
39
+ });
40
+ }
package/helper.js CHANGED
@@ -1,28 +1,28 @@
1
- // This file contains code that we reuse
2
- // between our tests.
3
- import Fastify from 'fastify';
4
- import config from './test/config.js';
5
- import appService from './index.js';
6
-
7
- import rclient from './redis/client.js';
8
- import pgClients from './pg/pgClients.js';
9
-
10
- // automatically build and tear down our instance
11
- async function build(t) {
12
- // you can set all the options supported by the fastify CLI command
13
- // const argv = [AppPath]
14
- process.env.NODE_ENV = 'production';
15
- const app = Fastify({ logger: false });
16
- app.register(appService, config);
17
- // close the app after we are done
18
- t.after(() => {
19
- // console.log('close app');
20
- pgClients.client.end();
21
- rclient.quit();
22
- app.close();
23
- });
24
-
25
- return app;
26
- }
27
-
28
- export default build;
1
+ // This file contains code that we reuse
2
+ // between our tests.
3
+ import Fastify from 'fastify';
4
+ import config from './test/config.js';
5
+ import appService from './index.js';
6
+
7
+ import rclient from './redis/client.js';
8
+ import pgClients from './pg/pgClients.js';
9
+
10
+ // automatically build and tear down our instance
11
+ async function build(t) {
12
+ // you can set all the options supported by the fastify CLI command
13
+ // const argv = [AppPath]
14
+ process.env.NODE_ENV = 'production';
15
+ const app = Fastify({ logger: false });
16
+ app.register(appService, config);
17
+ // close the app after we are done
18
+ t.after(() => {
19
+ // console.log('close app');
20
+ pgClients.client.end();
21
+ rclient.quit();
22
+ app.close();
23
+ });
24
+
25
+ return app;
26
+ }
27
+
28
+ export default build;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.22",
3
+ "version": "1.0.23",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
package/table/index.js CHANGED
@@ -5,6 +5,7 @@ import filter from './controllers/filter.js';
5
5
  import form from './controllers/form.js';
6
6
  import metaFormat from './funcs/metaFormat/index.js';
7
7
  import getFilterSQL from './funcs/getFilterSQL/index.js';
8
+ import getTemplate from './controllers/utils/getTemplate.js';
8
9
 
9
10
  const tableSchema = {
10
11
  querystring: {
@@ -29,6 +30,7 @@ async function plugin(fastify, config = {}) {
29
30
  const prefix = config.prefix || '/api';
30
31
  fastify.decorate('metaFormat', metaFormat);
31
32
  fastify.decorate('getFilterSQL', getFilterSQL);
33
+ fastify.decorate('getTemplate', getTemplate);
32
34
 
33
35
  fastify.get(`${prefix}/suggest/:data`, {}, suggest);
34
36
  fastify.get(`${prefix}/data/:table/:id?`, { schema: tableSchema }, data); // vs.crm.data.api с node
@@ -1,50 +1,56 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import build from '../../helper.js';
5
-
6
- import config from '../config.js';
7
-
8
- test('api crud', async (t) => {
9
- const app = await build(t);
10
- const prefix = config.prefix || '/api';
11
-
12
- await t.test('POST /insert', async () => {
13
- const res = await app.inject({
14
- method: 'POST',
15
- url: `${prefix}/crud/gis.dataset`,
16
- body: { dataset_name: '111', dataset_id: '5400000' },
17
- });
18
-
19
- const rep = JSON.parse(res?.body);
20
- // rep.dataset_id
21
- // console.log(rep);
22
- // pgClients.client.query('delete from gis.dataset where dataset_id=$1', [rep.dataset_id]);
23
- assert.ok(rep.dataset_id);
24
- });
25
-
26
- await t.test('PUT /update', async () => {
27
- const res = await app.inject({
28
- method: 'PUT',
29
- url: `${prefix}/crud/gis.dataset/5400000`,
30
- body: { editor_id: '11' },
31
- });
32
-
33
- const rep = JSON.parse(res?.body);
34
- // rep.dataset_id
35
- // console.log(rep);
36
- assert.equal(rep.editor_id, '11');
37
- });
38
-
39
- await t.test('DELETE /delete', async () => {
40
- const res = await app.inject({
41
- method: 'DELETE',
42
- url: `${prefix}/crud/gis.dataset/5400000`,
43
- });
44
-
45
- const rep = JSON.parse(res?.body);
46
- // rep.dataset_id
47
- // console.log(rep);
48
- assert.ok(rep);
49
- });
50
- });
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+
6
+ import config from '../config.js';
7
+
8
+ test('api crud', async (t) => {
9
+ const app = await build(t);
10
+ const prefix = config.prefix || '/api';
11
+
12
+ await t.test('POST /insert', async () => {
13
+ const res = await app.inject({
14
+ method: 'POST',
15
+ url: `${prefix}/table/gis.map`,
16
+ body: {
17
+ alias: 'testMap',
18
+ map_id: '5400000',
19
+ ord: 1,
20
+ enabled: false,
21
+ tags: ['unit', 'test'],
22
+ },
23
+ });
24
+
25
+ const rep = JSON.parse(res?.body);
26
+ assert.ok(rep.rows ? rep.rows[0]?.map_id : rep.map_id);
27
+ });
28
+
29
+ await t.test('PUT /update', async () => {
30
+ const res = await app.inject({
31
+ method: 'PUT',
32
+ url: `${prefix}/table/gis.map/5400000`,
33
+ body: {
34
+ editor_id: '11',
35
+ alias: 'testMapEdit',
36
+ map_id: '5400000',
37
+ ord: 2,
38
+ enabled: true,
39
+ tags: ['unittest'],
40
+ },
41
+ });
42
+
43
+ const rep = JSON.parse(res?.body);
44
+ assert.equal(rep.rows ? rep.rows[0]?.editor_id : rep.editor_id, '11');
45
+ });
46
+
47
+ await t.test('DELETE /delete', async () => {
48
+ const res = await app.inject({
49
+ method: 'DELETE',
50
+ url: `${prefix}/table/gis.map/5400000`,
51
+ });
52
+
53
+ const rep = JSON.parse(res?.body);
54
+ assert.ok(rep);
55
+ });
56
+ });
@@ -1,68 +1,68 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import build from '../../helper.js';
5
-
6
- import setToken from '../../crud/funcs/setToken.js';
7
- import config from '../config.js';
8
-
9
- test('api crud xss', async (t) => {
10
- const app = await build(t);
11
- const session = { passport: { user: { uid: '1' } } };
12
- app.decorateRequest('session', session);
13
-
14
- const prefix = config.prefix || '/api';
15
-
16
- let addTokens;
17
- let editTokens;
18
-
19
- // before
20
- t.test('setToken', async () => {
21
- addTokens = setToken({
22
- ids: [JSON.stringify({ add: 'gis.dataset', form: 'test.dataset.form' })],
23
- mode: 'a',
24
- uid: 1,
25
- array: 1,
26
- });
27
- editTokens = setToken({
28
- ids: [JSON.stringify({ id: '5400000', table: 'gis.dataset', form: 'test.dataset.form' })],
29
- mode: 'w',
30
- uid: 1,
31
- array: 1,
32
- });
33
- assert.ok(addTokens.length === 1 && editTokens.length === 1, 'invalid token');
34
- });
35
-
36
- await t.test('POST /insert', async () => {
37
- const res = await app.inject({
38
- method: 'POST',
39
- url: `${prefix}/crud/${addTokens[0]}`,
40
- body: { dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>', dataset_id: '5400000' },
41
- });
42
-
43
- const rep = JSON.parse(res?.body);
44
-
45
- assert.ok(rep.status, 409);
46
- });
47
-
48
- await t.test('PUT /update', async () => {
49
- const res = await app.inject({
50
- method: 'PUT',
51
- url: `${prefix}/crud/${editTokens[0]}/${editTokens[0]}`,
52
- body: { editor_id: '11', dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>' },
53
- });
54
-
55
- const rep = JSON.parse(res?.body);
56
-
57
- assert.equal(rep.status, 409);
58
- });
59
- await t.test('DELETE /delete', async () => {
60
- const res = await app.inject({
61
- method: 'DELETE',
62
- url: `${prefix}/crud/gis.dataset/5400000`,
63
- });
64
-
65
- const rep = JSON.parse(res?.body);
66
- assert.ok(rep);
67
- });
68
- });
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import build from '../../helper.js';
5
+
6
+ import setToken from '../../crud/funcs/setToken.js';
7
+ import config from '../config.js';
8
+
9
+ test('api crud xss', async (t) => {
10
+ const app = await build(t);
11
+ const session = { passport: { user: { uid: '1' } } };
12
+ app.decorateRequest('session', session);
13
+
14
+ const prefix = config.prefix || '/api';
15
+
16
+ let addTokens;
17
+ let editTokens;
18
+
19
+ // before
20
+ t.test('setToken', async () => {
21
+ addTokens = setToken({
22
+ ids: [JSON.stringify({ add: 'gis.dataset', form: 'test.dataset.form' })],
23
+ mode: 'a',
24
+ uid: 1,
25
+ array: 1,
26
+ });
27
+ editTokens = setToken({
28
+ ids: [JSON.stringify({ id: '5400000', table: 'gis.dataset', form: 'test.dataset.form' })],
29
+ mode: 'w',
30
+ uid: 1,
31
+ array: 1,
32
+ });
33
+ assert.ok(addTokens.length === 1 && editTokens.length === 1, 'invalid token');
34
+ });
35
+
36
+ await t.test('POST /insert', async () => {
37
+ const res = await app.inject({
38
+ method: 'POST',
39
+ url: `${prefix}/table/${addTokens[0]}`,
40
+ body: { dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>', dataset_id: '5400000' },
41
+ });
42
+
43
+ const rep = JSON.parse(res?.body);
44
+
45
+ assert.ok(rep.status, 409);
46
+ });
47
+
48
+ await t.test('PUT /update', async () => {
49
+ const res = await app.inject({
50
+ method: 'PUT',
51
+ url: `${prefix}/table/${editTokens[0]}/${editTokens[0]}`,
52
+ body: { editor_id: '11', dataset_name: '<a onClick="alert("XSS Injection")">xss injection</a>' },
53
+ });
54
+
55
+ const rep = JSON.parse(res?.body);
56
+
57
+ assert.equal(rep.status, 409);
58
+ });
59
+ await t.test('DELETE /delete', async () => {
60
+ const res = await app.inject({
61
+ method: 'DELETE',
62
+ url: `${prefix}/table/gis.dataset/5400000`,
63
+ });
64
+
65
+ const rep = JSON.parse(res?.body);
66
+ assert.ok(rep);
67
+ });
68
+ });