industry-api.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { createServer } from 'node:http';
  4. import { createIndustryApi } from '../server/industry-api.mjs';
  5. async function withServer(config, fn) {
  6. const server = createServer(createIndustryApi(config));
  7. await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
  8. try { await fn(`http://127.0.0.1:${server.address().port}/api/industry`); }
  9. finally { await new Promise(resolve => server.close(resolve)); }
  10. }
  11. test('industry API requires server-side authorization outside local preview', async () => {
  12. await withServer({ token: 'test-only-value', snapshot: 'not-read' }, async url => {
  13. for (const headers of [{}, { authorization: 'Bearer invalid' }]) {
  14. const response = await fetch(url, { method: 'POST', headers, body: '{}' });
  15. assert.equal(response.status, 401);
  16. assert.equal((await response.json()).error.code, 'AUTH_REQUIRED');
  17. }
  18. });
  19. });
  20. test('unconfigured library is unavailable, not an empty successful dataset', async () => {
  21. await withServer({ localPreview: true }, async url => {
  22. const response = await fetch(url, { method: 'POST', body: '{"op":"overview"}' });
  23. assert.equal(response.status, 503);
  24. assert.equal((await response.json()).error.code, 'DATABASE_NOT_INSTALLED');
  25. });
  26. });
  27. test('industry API rejects unbounded input and non-read transport methods before starting Python', async () => {
  28. await withServer({ localPreview: true, snapshot: 'not-read' }, async url => {
  29. const get = await fetch(url);
  30. assert.equal(get.status, 405);
  31. const large = await fetch(url, { method: 'POST', body: 'x'.repeat(13000) });
  32. assert.equal((await large.json()).error.code, 'REQUEST_TOO_LARGE');
  33. const array = await fetch(url, { method: 'POST', body: '[]' });
  34. assert.equal((await array.json()).error.code, 'INVALID_REQUEST');
  35. });
  36. });