openvibers/openvibe.coupons Commits

agent.json: what a code's and a shop's id is (AGT-4) (#215)

OP OpenVibers committed 10 days ago · de190dc6ec · 2 files, +25 −0

policy.stableIds: /c/{id} is cpn_ + a ULID minted once, never reused (a code is taken down, never
deleted); a taken-down code or shop answers 410, an unknown id 404. The test ties each claim to the code.

Changes

ddiff --git a/server/agent-descriptor.js b/server/agent-descriptor.js
iindex 8e2fc80..e7dd64e 100644
---- a/server/agent-descriptor.js
++++ b/server/agent-descriptor.js
@@@ -13,5 +13,11 @@ module.exports = {
openapi: '/openapi.json',
policy: {
auth: ['none', 'Network OIDC'],
++ // What an id in a page's address is, and that it never changes meaning (AGT-4): server/domain/coupons.js mints
++ // the code ids, and server/http/public.js answers a taken-down code or shop with 410.
++ stableIds: [
++ { kind: 'code', url: 'https://openvibe.coupons/c/{id}', id: 'cpn_ and a ULID, minted once when the code is first reported: never reused, since a code is taken down but never deleted', gone: 'a code taken down answers 410; an id that never existed answers 404' },
++ { kind: 'shop', url: 'https://openvibe.coupons/m/{slug}', id: 'the shop\'s slug', gone: 'a shop no longer listed answers 410' },
++ ],
},
};
ddiff --git a/test/agent-stable-ids.test.js b/test/agent-stable-ids.test.js
nnew file mode 100644
iindex 0000000..58251f7
---- /dev/null
++++ b/test/agent-stable-ids.test.js
@@@ -0,0 +1,19 @@
++'use strict';
++// AGT-4: agent.json says what an id in a Coupons address is, and the code behaves as it says.
++const assert = require('assert');
++const fs = require('fs');
++const path = require('path');
++const { agentManifest } = require('openvibe-shared/agent-layer');
++
++const ids = agentManifest(require('../server/agent-descriptor')).policy.stableIds;
++assert.deepStrictEqual(ids.map((x) => [x.kind, x.url]), [['code', 'https://openvibe.coupons/c/{id}'], ['shop', 'https://openvibe.coupons/m/{slug}']]);
++const read = (f) => fs.readFileSync(path.join(__dirname, '..', 'server', f), 'utf8');
++// A code id is a ULID minted once, and no code row is ever deleted (so no id is freed to be minted again).
++assert.match(read('domain/coupons.js'), /const id = `cpn_\$\{ids\.ulid\(now\)\}`;/);
++const all = fs.readdirSync(path.join(__dirname, '..', 'server'), { recursive: true }).filter((f) => f.endsWith('.js')).map((f) => read(f)).join('\n');
++assert.doesNotMatch(all, /DELETE FROM coupons\b/i, 'a code is taken down, never deleted');
++// Taken down: 410, both a code and a shop.
++const pub = read('http/public.js');
++assert.match(pub, /messagePage\(req, res, 410, 'Gone', 'This code was taken down\.'/);
++assert.match(pub, /messagePage\(req, res, 410, 'Gone', 'This shop is no longer listed\.'/);
++console.log('agent stable ids: ok');