123 lines
5.2 KiB
JavaScript
123 lines
5.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { models, sequelize } from '../models/index.js';
|
|
import setupAssociations from '../models/setupAssociations.js';
|
|
import admin from '../config/admin.js';
|
|
|
|
async function main() {
|
|
for (const model of Object.values(models)) model.initialize(sequelize);
|
|
setupAssociations();
|
|
const base = process.env.GRADE_API_URL || 'http://127.0.0.1:3080';
|
|
const created = [];
|
|
let adminToken;
|
|
async function request(path, { method = 'GET', body, token = adminToken, status = 200, pdf = false } = {}) {
|
|
const response = await globalThis.fetch(base + path, {
|
|
method,
|
|
headers: {
|
|
...(token ? { Authorization: 'Bearer ' + token } : {}),
|
|
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
},
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
assert.equal(response.status, status, `${method} ${path}`);
|
|
if (pdf) {
|
|
assert.match(response.headers.get('content-type'), /application\/pdf/);
|
|
assert.match(response.headers.get('content-disposition'), /attachment/);
|
|
assert.match(response.headers.get('cache-control'), /no-store/);
|
|
return Buffer.from(await response.arrayBuffer());
|
|
}
|
|
const text = await response.text();
|
|
return text ? JSON.parse(text) : null;
|
|
}
|
|
try {
|
|
adminToken = (
|
|
await request('/auth/login', { method: 'POST', body: { login: admin.login, password: admin.password } })
|
|
).token;
|
|
const suffix = Date.now().toString(36);
|
|
const { user } = await request('/admin/users', {
|
|
method: 'POST',
|
|
body: { firstName: 'Тест', lastName: 'Экспорта' + suffix, middleName: 'Иванович' },
|
|
status: 201,
|
|
});
|
|
created.push(user.id);
|
|
let stored = await models.User.findByPk(user.id);
|
|
const oldPassword = stored.passwordForAdmin;
|
|
const task = await models.Case.findOne({
|
|
include: { model: models.Team, as: 'teams', through: { attributes: [] } },
|
|
});
|
|
let retainedMark;
|
|
if (task?.teams.length) {
|
|
await task.addUser(stored);
|
|
retainedMark = await models.Mark.create({
|
|
caseId: task.id,
|
|
teamId: task.teams[0].id,
|
|
userId: user.id,
|
|
markType: 10,
|
|
mark: 4,
|
|
});
|
|
}
|
|
|
|
const oldToken = (
|
|
await request('/auth/login', { method: 'POST', body: { login: stored.login, password: oldPassword } })
|
|
).token;
|
|
const edit = {
|
|
firstName: 'Иван',
|
|
lastName: 'Петров',
|
|
middleName: 'Сергеевич',
|
|
login: 'edited-' + suffix,
|
|
password: 'New+&?#2026!',
|
|
};
|
|
await request(`/admin/users/${user.id}`, { method: 'PUT', body: edit });
|
|
await request('/cases', { token: oldToken, status: 401 });
|
|
await request('/auth/login', { method: 'POST', body: { login: user.login, password: oldPassword }, status: 403 });
|
|
const currentToken = (
|
|
await request('/auth/login', { method: 'POST', body: { login: edit.login, password: edit.password } })
|
|
).token;
|
|
await request('/cases', { token: currentToken });
|
|
await request(`/admin/users/${user.id}`, { method: 'PUT', body: edit, token: currentToken, status: 403 });
|
|
await request('/admin/users/qr-pdf', {
|
|
method: 'POST',
|
|
body: { baseUrl: 'https://grading.example.ru/' },
|
|
token: currentToken,
|
|
status: 403,
|
|
});
|
|
await request(`/admin/users/${user.id}`, { method: 'PUT', body: { ...edit, login: 'admin' }, status: 400 });
|
|
await request(`/admin/users/${user.id}`, { method: 'PUT', body: { ...edit, password: 'x' }, status: 400 });
|
|
await request(`/admin/users/${user.id}`, { method: 'PUT', body: { ...edit, password: '' } });
|
|
stored = await models.User.findByPk(user.id);
|
|
assert.equal(stored.middleName, 'Сергеевич');
|
|
assert.ok(stored.validatePassword(edit.password));
|
|
const pdf = await request('/admin/users/qr-pdf', {
|
|
method: 'POST',
|
|
body: { baseUrl: 'https://grading.example.ru/' },
|
|
pdf: true,
|
|
});
|
|
assert.equal(pdf.subarray(0, 5).toString(), '%PDF-');
|
|
for (const baseUrl of ['javascript:alert(1)', 'https://example.ru/#/login', 'https://u:p@example.ru/'])
|
|
await request('/admin/users/qr-pdf', { method: 'POST', body: { baseUrl }, status: 400 });
|
|
await request(`/admin/users/${user.id}`, { method: 'DELETE', status: 204 });
|
|
await request('/cases', { token: currentToken, status: 401 });
|
|
await request('/auth/login', { method: 'POST', body: { login: edit.login, password: edit.password }, status: 403 });
|
|
assert.equal(await models.User.findByPk(user.id), null);
|
|
if (retainedMark) assert.ok(await models.Mark.findByPk(retainedMark.id));
|
|
|
|
await request(`/admin/users/${user.id}/restore`, { method: 'POST' });
|
|
const restored = (
|
|
await request('/auth/login', { method: 'POST', body: { login: edit.login, password: edit.password } })
|
|
).token;
|
|
await request('/cases', { token: restored });
|
|
await request('/cases', { token: currentToken, status: 401 });
|
|
console.log(
|
|
'PASS expert name/login/password editing; unchanged password; admin-only access; deletion, login revocation and restore; old tokens stay invalid after same-second login; PDF endpoint and URL validation.'
|
|
);
|
|
} finally {
|
|
await models.Mark.destroy({ where: { userId: created }, force: true });
|
|
await models.UserInCase.destroy({ where: { userId: created }, force: true });
|
|
await models.User.destroy({ where: { id: created }, force: true });
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
main().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|