source: Klonkt/test/paid-patron.test.js@ c3d12a6

main
Last change on this file since c3d12a6 was c3d12a6, checked in by Robin <roboburr@…>, 7 weeks ago

Feature: a way to actually become a supporter, and a styled expired page

Bart's test surfaced two gaps in the visitor flow:

  • After "Allow" on Patreon, a non-supporter landed on "Nog geen supporter" with no way to actually pledge. The page now shows a primary "Word supporter op Patreon" button. The site owner sets their public Patreon page in Beheer -> Betaalde posts (new field); it's also linked from the gate itself ("Nog geen supporter? Word het op Patreon").
  • The invalid/expired callback replied with a raw res.send() plain-text line. It now renders the normal paid-result page (reason 'expired').

Changed files:
src/config/database.js

  • paid_patreon.patreon_url column (additive)

src/services/PaidPatreonService.js

  • patreonUrl in config/status/save (undefined keeps, empty clears) + patreonUrl(siteId) helper

src/routes/admin-paid.js

  • save patreon_url from the form

src/views/pages/admin-paid.ejs

  • "Openbare Patreon-pagina" field

src/routes/paid.js

  • expired callback renders paid-result; pass patronUrl to the result pages

src/routes/posts.js

  • pass pgPatronUrl to the gate

src/views/pages/paid-gate.ejs

  • "Word supporter" join line under the unlock button

src/views/pages/paid-result.ejs

  • "Word supporter op Patreon" primary button, 'expired' reason, ghost back button always has a label

test/paid-patron.test.js

  • patreonUrl set/keep/clear semantics

-robo
Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 4.5 KB
Line 
1// Paid posts slice 3 (klonkt-demo-aki): patron verification + pseudonymous
2// passkey entitlements. The WebAuthn ceremony itself needs a browser, so here
3// we cover the pure parsing, the Patreon exchange (mock fetch), the entitlement
4// store, and that registration options carry a challenge.
5import { test } from 'node:test';
6import assert from 'node:assert/strict';
7
8process.env.DATABASE_PATH = ':memory:';
9process.env.PUBLIC_BASE_URL = 'https://test.example';
10process.env.PAID_SECRET = 'a-test-paid-secret-of-sufficient-length';
11
12const dbMod = await import('../src/config/database.js');
13const db = dbMod.default;
14dbMod.initializeDatabase();
15const PP = (await import('../src/services/PaidPatreonService.js')).default;
16const Passkey = (await import('../src/services/PasskeyService.js')).default;
17
18PP.saveOwnerConfig('s1', { clientId: 'cid', clientSecret: 'sec', campaignId: '42', defaultMinCents: 300 });
19
20const identityWith = (campaignId, status, cents) => ({
21 data: { type: 'user', id: 'u', relationships: { memberships: { data: [{ type: 'member', id: 'm1' }] } } },
22 included: [
23 { type: 'member', id: 'm1', attributes: { patron_status: status, currently_entitled_amount_cents: cents },
24 relationships: { campaign: { data: { type: 'campaign', id: campaignId } } } },
25 { type: 'campaign', id: campaignId },
26 ],
27});
28
29test('pickCampaignMembership finds the right campaign, ignores others', async () => {
30 const { pickCampaignMembership } = await import('../src/services/PaidPatreonService.js');
31 const id = identityWith('42', 'active_patron', 500);
32 const m = pickCampaignMembership(id, '42');
33 assert.equal(m.status, 'active_patron');
34 assert.equal(m.cents, 500);
35 assert.equal(pickCampaignMembership(id, '999'), null); // different campaign
36});
37
38test('verifyPatron exchanges the code and reads the membership (mock fetch)', async () => {
39 const calls = [];
40 const fetchMock = async (url, opts) => {
41 calls.push(url);
42 if (url.includes('/token')) return { ok: true, json: async () => ({ access_token: 'patron-tok' }) };
43 return { ok: true, json: async () => identityWith('42', 'active_patron', 800) };
44 };
45 const m = await PP.verifyPatron('s1', 'the-code', 'https://test.example/paid/callback', fetchMock);
46 assert.equal(m.status, 'active_patron');
47 assert.equal(m.cents, 800);
48 assert.ok(calls[0].includes('patreon.com'));
49 assert.ok(calls[1].includes('identity'));
50});
51
52test('registration options carry a challenge and the site host as rpID', async () => {
53 const opts = await Passkey.registrationOptions('https://test.example', 's1');
54 assert.ok(opts.challenge && typeof opts.challenge === 'string');
55 assert.equal(opts.rp.id, 'test.example');
56 assert.equal(opts.authenticatorSelection.residentKey, 'required');
57});
58
59test('entitlement stores, reads, expires, prunes; no patron identity present', () => {
60 Passkey.storeEntitlement({ credentialId: 'cred1', siteId: 's1', publicKey: 'PUBKEY', counter: 0, minCents: 500, ttlDays: 30 });
61 const e = Passkey.getEntitlement('cred1', 's1');
62 assert.ok(e);
63 assert.equal(e.min_cents, 500);
64 // the row has no name/email/patreon id
65 const cols = Object.keys(e);
66 assert.ok(!cols.some((c) => /name|email|patron|user/i.test(c)), 'no identity columns');
67 // expired entitlement is not returned and gets pruned
68 Passkey.storeEntitlement({ credentialId: 'cred2', siteId: 's1', publicKey: 'PK', minCents: 100, ttlDays: 30 });
69 db.prepare('UPDATE paid_entitlements SET expires_at = 1 WHERE credential_id = ?').run('cred2');
70 assert.equal(Passkey.getEntitlement('cred2', 's1'), null);
71 assert.equal(Passkey.pruneExpired() >= 1, true);
72 assert.ok(Passkey.getEntitlement('cred1', 's1')); // the fresh one survives
73});
74
75test('patreonUrl: set, kept on unrelated save, cleared on empty', () => {
76 PP.saveOwnerConfig('s2', { clientId: 'c', clientSecret: 's', campaignId: '7', patreonUrl: 'https://patreon.com/x' });
77 assert.equal(PP.patreonUrl('s2'), 'https://patreon.com/x');
78 // a save that does NOT mention patreonUrl (e.g. token refresh) keeps it
79 PP.saveOwnerConfig('s2', { defaultMinCents: 200 });
80 assert.equal(PP.patreonUrl('s2'), 'https://patreon.com/x');
81 // an explicit empty value clears it
82 PP.saveOwnerConfig('s2', { patreonUrl: '' });
83 assert.equal(PP.patreonUrl('s2'), null);
84});
85
86test('deleteEntitlement removes the row (forget-passkey path)', () => {
87 Passkey.storeEntitlement({ credentialId: 'cred3', siteId: 's1', publicKey: 'PK', minCents: 100 });
88 assert.equal(Passkey.deleteEntitlement('cred3'), true);
89 assert.equal(Passkey.getEntitlement('cred3', 's1'), null);
90});
Note: See TracBrowser for help on using the repository browser.