Skip to content

Commit e8c0935

Browse files
committed
test(gdpr): cover deletePad authorisation matrix via REST
1 parent 83957ed commit e8c0935

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
import {strict as assert} from 'assert';
4+
5+
const common = require('../../common');
6+
import settings from '../../../../node/utils/Settings';
7+
8+
let agent: any;
9+
let apiVersion = 1;
10+
11+
const endPoint = (p: string) => `/api/${apiVersion}/${p}`;
12+
13+
const makeId = () => `gdprdel_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
14+
15+
const callApi = async (point: string, query: Record<string, string> = {}) => {
16+
const qs = new URLSearchParams(query).toString();
17+
const path = qs ? `${endPoint(point)}?${qs}` : endPoint(point);
18+
return await agent.get(path)
19+
.set('authorization', await common.generateJWTToken())
20+
.expect(200)
21+
.expect('Content-Type', /json/);
22+
};
23+
24+
describe(__filename, function () {
25+
before(async function () {
26+
this.timeout(60000);
27+
agent = await common.init();
28+
const res = await agent.get('/api/').expect(200);
29+
apiVersion = res.body.currentVersion;
30+
});
31+
32+
afterEach(function () { settings.allowPadDeletionByAllUsers = false; });
33+
34+
it('createPad returns a plaintext deletionToken the first time', async function () {
35+
const padId = makeId();
36+
const res = await callApi('createPad', {padID: padId});
37+
assert.equal(res.body.code, 0, JSON.stringify(res.body));
38+
assert.equal(typeof res.body.data.deletionToken, 'string');
39+
assert.ok(res.body.data.deletionToken.length >= 32);
40+
await callApi('deletePad', {padID: padId, deletionToken: res.body.data.deletionToken});
41+
});
42+
43+
it('deletePad with a valid deletionToken succeeds', async function () {
44+
const padId = makeId();
45+
const create = await callApi('createPad', {padID: padId});
46+
const token = create.body.data.deletionToken;
47+
const del = await callApi('deletePad', {padID: padId, deletionToken: token});
48+
assert.equal(del.body.code, 0, JSON.stringify(del.body));
49+
const check = await callApi('getText', {padID: padId});
50+
assert.equal(check.body.code, 1); // "padID does not exist"
51+
});
52+
53+
it('deletePad with a wrong deletionToken is refused', async function () {
54+
const padId = makeId();
55+
await callApi('createPad', {padID: padId});
56+
const del = await callApi('deletePad', {padID: padId, deletionToken: 'not-the-real-token'});
57+
assert.equal(del.body.code, 1);
58+
assert.match(del.body.message, /invalid deletionToken/);
59+
// cleanup — JWT-authenticated caller is trusted when no token is supplied
60+
await callApi('deletePad', {padID: padId});
61+
});
62+
63+
it('deletePad with allowPadDeletionByAllUsers=true bypasses the token check', async function () {
64+
const padId = makeId();
65+
await callApi('createPad', {padID: padId});
66+
settings.allowPadDeletionByAllUsers = true;
67+
const del = await callApi('deletePad', {padID: padId, deletionToken: 'bogus'});
68+
assert.equal(del.body.code, 0);
69+
});
70+
71+
it('JWT admin call (no deletionToken) still works — admins stay trusted', async function () {
72+
const padId = makeId();
73+
await callApi('createPad', {padID: padId});
74+
const del = await callApi('deletePad', {padID: padId});
75+
assert.equal(del.body.code, 0);
76+
});
77+
});

0 commit comments

Comments
 (0)