File size: 8,555 Bytes
b968036 db2d194 1a6f54f db2d194 1a6f54f db2d194 6b7ad32 b968036 db2d194 b968036 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import test from 'node:test';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { activityReport, validateActivity } from '../shared/examActivity.js';
import { load } from './appsScriptHarness.js';
const journal = (revision, events) => ({ id: '0f0e0d0c-0b0a-4908-8706-050403020100', revision, dropped: 0, events });
test('Apps Script backend refuses access until the teacher sets a code, and limits guesses', () => {
assert.equal(load({}).call({ action: 'access', code: 'x', student: 'Élise' }).error, 'exam_unavailable');
const { call } = load();
assert.equal(load().context.doPost({ postData: { contents: 'not json' } }).text, '{"error":"invalid_request"}');
for (let i = 0; i < 12; i++) assert.equal(call({ action: 'access', code: 'wrong', student: 'Élise Test' }).error, 'invalid_code');
assert.equal(call({ action: 'access', code: 'code-secret', student: 'Élise Test' }).error, 'too_many_attempts');
assert.equal(call({ action: 'access', code: ' code-secret ', student: 'Autre Élève' }).exam.exercises.length, 5);
assert.equal(call({ action: 'access', code: 'code-secret', student: 'x' }).error, 'invalid_student');
});
test('Apps Script sessions are signed and the journal keeps its newest revision', () => {
const { call, root, props } = load();
const auth = call({ action: 'access', code: 'code-secret', student: ' Élise Test ' });
assert.equal(auth.student, 'Élise Test');
assert.equal(auth.submissionAvailable, true);
assert.ok(props.get('EXAM_SESSION_SECRET'));
assert.equal(call({ action: 'session', token: auth.token }).studentKey, auth.studentKey);
const [payload, signature] = auth.token.split('.');
const forged = Buffer.from(JSON.stringify({ ...JSON.parse(Buffer.from(payload, 'base64url')), student: 'Autre' })).toString('base64url');
assert.equal(call({ action: 'session', token: `${forged}.${signature}` }).error, 'session_expired');
assert.equal(call({ action: 'session' }).error, 'session_expired');
assert.equal(call({ action: 'activity', token: auth.token, activity: { id: 'bad' } }).error, 'invalid_activity');
assert.equal(call({ action: 'activity', token: auth.token, activity: journal(2, [{ at: 1000, state: 'active' }, { at: 2000, state: 'hidden' }]) }).saved, true);
call({ action: 'activity', token: auth.token, activity: journal(1, [{ at: 1000, state: 'active' }]) });
const [file] = root().folders.find(item => item.name === 'suivi').files;
assert.equal(JSON.parse(file.getBlob().getDataAsString()).activity.revision, 2);
});
test('Apps Script submit all sends one email with every file, even when retried', () => {
const { call, mails, root } = load({ EXAM_ACCESS_CODE: 'code-secret', EXAM_RECIPIENT: 'prof@example.org', EXAM_ID: 'exam-c' });
const auth = call({ action: 'access', code: 'code-secret', student: 'Élise Test' });
const copies = auth.exam.exercises.map((item, index) => ({ exerciseId: item.id, source: index === 1 ? '' : `// réponse ${index}` }));
const body = { action: 'submit-all', token: auth.token, copies, requestId: randomUUID(), activity: journal(1, [{ at: 1000, state: 'active' }]) };
assert.equal(call({ ...body, copies: copies.slice(1) }).error, 'invalid_request');
assert.equal(call({ ...body, copies: copies.map(item => ({ ...item, source: ' ' })) }).error, 'empty_source');
const receipt = call(body);
assert.match(receipt.id, /^exam-/);
assert.deepEqual(call(body), receipt);
assert.equal(mails.length, 1);
const [mail] = mails;
assert.equal(mail.to, 'prof@example.org');
assert.equal(mail.subject, '[Évaluation C] Élise Test — Tous les exercices');
assert.match(mail.body, /Exercice 2 : non répondu/);
assert.deepEqual([...mail.attachments.map(file => file.getName())], ['Elise-Test-exercice-1.c', 'Elise-Test-exercice-2.c', 'Elise-Test-exercice-3.c', 'Elise-Test-exercice-4.c', 'Elise-Test-exercice-5.c', 'Elise-Test-suivi.txt']);
assert.equal(mail.attachments[0].getDataAsString(), '// réponse 0');
assert.match(mail.attachments[5].getDataAsString(), /Élise Test — examen exam-c/);
assert.equal(root().folders.find(item => item.name === 'copies').files.length, 6);
const single = call({ action: 'submit', token: auth.token, exerciseId: 'exercice-3', source: 'int main(void) {}', requestId: randomUUID() });
assert.match(single.id, /^exam-/);
assert.equal(mails[1].subject, '[Évaluation C] Élise Test — Exercice 3');
assert.equal(call({ action: 'submit', token: auth.token, exerciseId: 'nope', source: 'x', requestId: randomUUID() }).error, 'invalid_request');
});
test('Apps Script quiz sends the answers once, then stays closed until the teacher resets it', () => {
const { call, mails, context } = load({ EXAM_ACCESS_CODE: 'code-secret', EXAM_RECIPIENT: 'prof@example.org' });
const auth = call({ action: 'access', code: 'code-secret', student: 'Élise Test' });
assert.equal(auth.quizDone, false);
assert.equal(auth.exam.quiz.length, 5);
const [first, second] = auth.exam.quiz.filter(item => item.choices);
const free = auth.exam.quiz.find(item => !item.choices);
assert.equal(call({ action: 'quiz', answers: {} }).error, 'session_expired');
assert.equal(call({ action: 'quiz', token: auth.token, answers: { [first.id]: 'Pas un choix' } }).error, 'invalid_answers');
assert.equal(call({ action: 'quiz', token: auth.token, answers: { inconnue: 'x' } }).error, 'invalid_answers');
const body = { action: 'quiz', token: auth.token, answers: { [first.id]: first.choices[0], [free.id]: 'Réponse libre' } };
const receipt = call(body);
assert.match(receipt.id, /^quiz-/);
assert.deepEqual(call({ ...body, answers: {} }), receipt);
assert.equal(mails.length, 1);
assert.equal(mails[0].subject, '[Évaluation C] Élise Test — Quiz');
assert.match(mails[0].body, new RegExp(`${auth.exam.quiz.indexOf(first) + 1}\\. ${first.question.replace(/[?]/g, '\\?')}\\n→ ${first.choices[0]}`));
assert.match(mails[0].body, new RegExp(`${auth.exam.quiz.indexOf(second) + 1}\\. .*\\n→ \\(sans réponse\\)`));
assert.match(mails[0].body, /→ Réponse libre/);
assert.equal(mails[0].attachments[0].getName(), 'Elise-Test-quiz.txt');
const again = call({ action: 'access', code: 'code-secret', student: 'élise test' });
assert.equal(again.quizDone, true);
assert.deepEqual([...again.exam.quiz], []);
context.reinitialiserQuiz();
assert.equal(call({ action: 'session', token: auth.token }).quizDone, false);
});
test('Apps Script still sends the quiz and the copies when Drive is not authorised', () => {
const { call, mails, context } = load({ EXAM_ACCESS_CODE: 'code-secret', EXAM_RECIPIENT: 'prof@example.org' });
context.console = { ...console, error() {} };
const denied = () => { throw new Error('Exception: You do not have permission to call DriveApp.createFolder.'); };
context.DriveApp = { createFolder: denied, getFolderById: denied };
const auth = call({ action: 'access', code: 'code-secret', student: 'Élise Test' });
assert.match(call({ action: 'quiz', token: auth.token, answers: {} }).id, /^quiz-/);
assert.equal(call({ action: 'session', token: auth.token }).quizDone, true);
const copies = auth.exam.exercises.map(item => ({ exerciseId: item.id, source: '// code' }));
assert.match(call({ action: 'submit-all', token: auth.token, copies, requestId: randomUUID(), activity: journal(1, [{ at: 1000, state: 'active' }]) }).id, /^exam-/);
assert.equal(mails.length, 2);
assert.match(mails[1].attachments.at(-1).getDataAsString(), /Drive n’est pas autorisé/);
assert.equal(call({ action: 'activity', token: auth.token, activity: journal(2, [{ at: 1000, state: 'active' }]) }).error, 'server_error');
});
test('Apps Script journal helpers stay identical to shared/examActivity.js', () => {
const { context } = load();
const log = journal(3, [{ at: 1000, state: 'active' }, { at: 4000, state: 'hidden' }, { at: 4500, state: 'unfocused' }, { at: 9000, state: 'active' }, { at: 12000, state: 'closed' }]);
log.signals = [{ at: 4100, kind: 'extension', detail: '<sider-sidebar>' }, { at: 4200, kind: 'insertion', detail: 'insertText · 300 caractères' }];
const records = [{ student: 'Élise Test', examId: 'exam-c', receivedAt: 5000, activity: { ...log, dropped: 2 } }];
assert.equal(context.activityReport(records, 20000), activityReport(records, 20000));
assert.equal(JSON.stringify(context.validateActivity(log, 20000)), JSON.stringify(validateActivity(log, 20000)));
assert.equal(context.activityReport([], 1), activityReport([], 1));
});
|