Certificates / admin /tests /test_browser_core.mjs
sahar-millis-runi's picture
Adopt the final academic SVG certificate template across viewer and generators
f8d6e7b verified
Raw History Blame
3.25 kB
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {webcrypto} from 'node:crypto';
import {parseCSV,generateBatch,zipCertificates,validateSettings,LABELS} from '../certificate-core.js';
globalThis.crypto ??= webcrypto;
const settings={term:'2027A',lecturer:'Example Lecturer',date:'2027-06-30'};
const input='\uFEFFemail,name,grade\r\none@example.com,"Example, ืื—ื“",89.999\r\ntwo@example.com,Example Two,90\r\nthree@example.com,Example Three,94.999\r\nfour@example.com,Example Four,95\r\n';
const decode=s=>Buffer.from(s,'base64url');
test('quoted Unicode CSV, exact distinction boundaries and validation',()=>{
const students=parseCSV(input);assert.equal(students[0].name,'Example, ืื—ื“');
assert.deepEqual(students.map(s=>s.distinction),[LABELS[0],LABELS[1],LABELS[1],LABELS[2]]);
for(const text of ['email,name,grade\na@example.com,Example,NaN','email,name,grade\na@example.com,Example,101','email,name,grade\na@example.com,Example,90\nA@example.com,Other,95','email,name,grade\na@example.com,"unclosed,90','email,name,grade\na@example.com,,90','email,name,grade\na@example.com,Example,100.0000000000000000001'])assert.throws(()=>parseCSV(text));
assert.throws(()=>validateSettings({...settings,date:'2027-02-30'}));assert.throws(()=>validateSettings({...settings,term:'../2027A'}));
});
test('independent keys, compatible decryption and ZIP with ciphertext only',async()=>{
const students=parseCSV(input),batch=await generateBatch(settings,students),rows=batch.recipients.trim().split('\r\n');
assert.equal(rows[0],'email,name,grade,link');
const keys=new Set();
for(let i=0;i<batch.files.length;i++){
const match=rows[i+1].match(/#2027A\.(\d{3})\.([A-Za-z0-9_-]{22})/);assert.ok(match);keys.add(match[2]);
const encrypted=JSON.parse(new TextDecoder().decode(batch.files[i].data));
const key=await crypto.subtle.importKey('raw',decode(match[2]),'AES-GCM',false,['decrypt']);
const data=await crypto.subtle.decrypt({name:'AES-GCM',iv:decode(encrypted.iv)},key,decode(encrypted.data));
const record=JSON.parse(new TextDecoder().decode(data));assert.equal(record.studentName,students[i].name);assert.equal(record.distinction,students[i].distinction);assert.equal(record.courseCode,'2686');assert.equal(record.school,'Adelson School of Entrepreneurship');assert.equal(record.credits,4);assert.ok(!('email' in record));
assert.ok(!new TextDecoder().decode(batch.files[i].data).includes(match[2]));
}
assert.equal(keys.size,4);
const zip=Buffer.from(await zipCertificates(batch.files).arrayBuffer());
assert.equal(zip.readUInt32LE(0),0x04034b50);
assert.equal(zip.readUInt32LE(zip.length-22),0x06054b50);
assert.equal(zip.readUInt16LE(zip.length-12),4);
});
test('private spreadsheet-safe recipient export and abort',async()=>{
const batch=await generateBatch(settings,parseCSV('email,name,grade\n+fictional@example.com,=1+1,95'));
assert.ok(batch.recipients.includes('"\'+fictional@example.com"'));assert.ok(batch.recipients.includes('"\'=1+1"'));
const abort=new AbortController();abort.abort();await assert.rejects(generateBatch(settings,parseCSV(input),()=>{},abort.signal),{name:'AbortError'});
assert.throws(()=>zipCertificates([{name:'../secrets',data:new Uint8Array()}]));
});