39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
appendPublicAssetTokens,
|
|
publicAssetToken,
|
|
verifyPublicAssetToken,
|
|
} from './mindspace-public-asset-token.mjs';
|
|
|
|
const SECRET = 'internal-agent-secret-for-tests';
|
|
|
|
test('public asset token round-trips for the matching asset id', () => {
|
|
const token = publicAssetToken('asset-1', SECRET);
|
|
assert.equal(verifyPublicAssetToken('asset-1', token, SECRET), true);
|
|
assert.equal(verifyPublicAssetToken('asset-2', token, SECRET), false);
|
|
});
|
|
|
|
test('appendPublicAssetTokens signs public asset download urls without clobbering existing query params', () => {
|
|
const html = [
|
|
'<img src="/api/mindspace/v1/assets/asset-1/download?inline=1&v=2">',
|
|
'<img src="https://m.tkmind.cn/api/mindspace/v1/assets/asset-2/download?inline=1&v=3">',
|
|
].join('');
|
|
|
|
const signed = appendPublicAssetTokens(html, SECRET);
|
|
|
|
assert.match(
|
|
signed,
|
|
/asset-1\/download\?inline=1&v=2&public_token=[A-Za-z0-9_-]+/,
|
|
);
|
|
assert.match(
|
|
signed,
|
|
/asset-2\/download\?inline=1&v=3&public_token=[A-Za-z0-9_-]+/,
|
|
);
|
|
});
|
|
|
|
test('appendPublicAssetTokens preserves existing public tokens', () => {
|
|
const html = '<img src="/api/mindspace/v1/assets/asset-1/download?inline=1&public_token=keep-me">';
|
|
assert.equal(appendPublicAssetTokens(html, SECRET), html);
|
|
});
|