Skip to content

Latest commit

 

History

History

testing

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

@tupik/testing

In-process TestKit for Tupik apps: inject requests through app.fetch without opening a socket, and assert route contracts (schema.response) and RFC 9457 problems. Web Standards only.

Install

npm install -D @tupik/testing

Usage

import { createApp, createModule } from '@tupik/core';
import { createTestApp } from '@tupik/testing';
import { z } from 'zod';

const TodoInput = z.object({ title: z.string().min(1) });

const app = createApp().register(
  createModule('/todos').post('/', {
    access: 'public', // demo only: real writes use 'authenticated' or an owner rule
    schema: { body: TodoInput },
    handler: (ctx) => ctx.status(201).json({ saved: ctx.body }),
  }),
);

const t = createTestApp(app);
const res = await t.post('/todos', { title: 'write docs' });
console.log(res.status, res.json()); // 201 { saved: { title: 'write docs' } }
await t.close();

Key API

Export Purpose
createTestApp(app, { baseUrl? }) inject/get/post/put/patch/delete/head/close
expectContract(app, { method, path, request?, expect? }) Sends the request and validates the body against schema.response[status]
expectProblem(response, { status?, type? }) Asserts an application/problem+json response; takes a Response or the InjectResponse of createTestApp
expectValid(schema, value) / expectType<T>(value) Runtime and compile-time assertions
generateAccessTests(app, { principals: { owner, other, anonymous? }, fixtures }) Cases "no principal gets 401" and, for owner routes, "the owner is served" / "another principal gets 403/404"
fuzz(app, { principals?, fixtures?, foreignIds?, seed?, runs? }) / formatFuzzReport Seeded hostile inputs per route: asserts no 5xx, no secret (or, to non-owners, health) field, no cross access, no prototype pollution
tupik-fuzz <module> (also tupik fuzz) The fuzzer as a CLI (--principals, --fixtures, --foreign-ids, --seed, --format json)
TestPrincipals { owner?, other?, anonymous? }, each { headers?, cookies? }

Authorization tests and fuzzing

import { fuzz, generateAccessTests } from '@tupik/testing';

const principals = {
  owner: { headers: { authorization: 'Bearer ana' } },
  other: { headers: { authorization: 'Bearer bia' } },
  // What a request without a principal still carries, so it reaches the 401 of access. Here: a CSRF token that
  // csrf() issued with no session, sent as cookie and header.
  anonymous: { headers: { 'x-csrf-token': anonCsrf }, cookies: { '__Host-tupik_csrf': anonCsrf } },
};
const fixtures = { 'GET /notes/:id': { params: { id: 'note-of-ana' } } };

for (const c of generateAccessTests(app, { principals, fixtures })) (c.skip ? it.skip : it)(c.name, c.run);

it('survives hostile input', async () => {
  const report = await fuzz(app, { principals, fixtures, foreignIds: ['note-of-bia'], seed: 1 });
  expect(report.failures).toEqual([]);
});

A fixture names a valid request and, for routes with params, a record owned by principals.owner. Inputs come from the JSON Schema of each route (own jsonSchema, Standard JSON Schema or schemaConverter), with generic inputs otherwise; body-shape and prototype-pollution attacks always run, the rest is sampled (runs per route) from the seed.

principals.anonymous is optional. Without it, the anonymous case sends no headers or cookies, so an app with csrf answers 403 on writes before access gives its 401. With it, the generated tests, the fuzzer and tupik-fuzz --principals ({ owner, other, anonymous? } in the JSON file) send those values and still check for the 401. A principal's values win over a fixture's.

License

MIT