Include complete API and frontend sources for unified deployment
This commit is contained in:
1 parent
627b0ae60e
commit
d8fcae32b0
93 files changed
+53270
-3
No files matched your search
@@ -0,0 +1,219 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import admin from '../config/admin.js';
|
||||
import { models, sequelize } from '../models/index.js';
|
||||
import setupAssociations from '../models/setupAssociations.js';
|
||||
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 suffix = Date.now().toString(36);
|
||||
const created = { cases: [], users: [], teams: [] };
|
||||
let token;
|
||||
async function req(path, body, status = 200, auth = token, method = body ? 'POST' : 'GET') {
|
||||
const res = await fetch(base + path, {
|
||||
method,
|
||||
headers: { Authorization: 'Bearer ' + auth, 'Content-Type': 'application/json' },
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
assert.equal(res.status, status, `${method} ${path}: ${await res.clone().text()}`);
|
||||
const text = await res.text();
|
||||
return text ? JSON.parse(text) : null;
|
||||
}
|
||||
async function expert(firstName, role, caseIds) {
|
||||
const { user } = await req('/admin/users', { firstName, lastName: 'Technical test ' + suffix, role, caseIds }, 201);
|
||||
created.users.push(user.id);
|
||||
return models.User.findByPk(user.id);
|
||||
}
|
||||
async function login(user) {
|
||||
return req('/auth/login', { login: user.login, password: user.passwordForAdmin });
|
||||
}
|
||||
try {
|
||||
token = (await req('/auth/login', { login: admin.login, password: admin.password })).token;
|
||||
for (const name of ['A', 'B']) {
|
||||
const { case: task } = await req('/admin/cases', { name: 'Technical ' + name + ' ' + suffix }, 201);
|
||||
created.cases.push(task.id);
|
||||
}
|
||||
const [caseId, otherCaseId] = created.cases;
|
||||
for (const name of ['A', 'B']) {
|
||||
const { team } = await req(`/admin/cases/${caseId}/teams`, { name }, 201);
|
||||
created.teams.push(team.id);
|
||||
}
|
||||
const [teamId, secondTeamId] = created.teams;
|
||||
const regular = await expert('Regular', 'expert', [caseId]);
|
||||
const technical = await expert('Technical', 'technical', [caseId]);
|
||||
const other = await expert('Other technical', 'technical', [otherCaseId]);
|
||||
await req('/admin/users', { firstName: 'Conflict', lastName: suffix, role: 'technical', caseIds: [caseId] }, 409);
|
||||
assert.equal(await models.User.count({ where: { firstName: 'Conflict', lastName: suffix }, paranoid: false }), 0);
|
||||
await req('/admin/users', { firstName: 'Invalid', lastName: suffix, role: 'admin' }, 400);
|
||||
await req(`/admin/users/${other.id}/cases`, { caseIds: [caseId, otherCaseId] }, 409, token, 'PUT');
|
||||
assert.deepEqual(
|
||||
(await other.getCases()).map(task => task.id),
|
||||
[otherCaseId]
|
||||
);
|
||||
await req(
|
||||
`/admin/users/${regular.id}`,
|
||||
{ firstName: regular.firstName, lastName: regular.lastName, login: regular.login, role: 'technical' },
|
||||
409,
|
||||
token,
|
||||
'PUT'
|
||||
);
|
||||
assert.equal((await regular.reload()).role, 'expert');
|
||||
const techLogin = await login(technical),
|
||||
regularLogin = await login(regular);
|
||||
assert.equal(techLogin.user.role, 'technical');
|
||||
const techToken = techLogin.token,
|
||||
regularToken = regularLogin.token;
|
||||
assert.equal((await req('/auth/user', null, 200, techToken)).user.role, 'technical');
|
||||
const save = (team, marks, auth = techToken, status = 200) =>
|
||||
req(`/cases/${caseId}/teams/${team}/marks`, { marks }, status, auth, 'PUT');
|
||||
await req('/admin/users', null, 403, techToken);
|
||||
await save(teamId, [{ markType: 10, mark: 5 }], techToken, 403);
|
||||
await req(`/cases/${caseId}/teams/${teamId}/marks`, { markType: 50, mark: 5 }, 403, techToken);
|
||||
await save(teamId, [{ markType: 20, mark: 5 }], regularToken, 403);
|
||||
await save(teamId, [{ markType: 20, mark: 6 }], techToken, 400);
|
||||
await save(
|
||||
teamId,
|
||||
[
|
||||
{ markType: 20, mark: 3 },
|
||||
{ markType: 10, mark: 5 },
|
||||
],
|
||||
techToken,
|
||||
403
|
||||
);
|
||||
assert.equal(await models.Mark.count({ where: { caseId } }), 0);
|
||||
const commentPath = `/cases/${caseId}/teams/${teamId}/marks`;
|
||||
const comment = 'Проверить обработку ошибок\nДемо работает локально. <b>Текст, не HTML</b>';
|
||||
await req(commentPath, { technicalComment: ' ' + comment + ' ' }, 200, techToken, 'PUT');
|
||||
assert.equal(await models.Mark.count({ where: { caseId } }), 0);
|
||||
assert.equal((await req(commentPath, null, 200, regularToken)).technicalComment, comment);
|
||||
assert.equal(
|
||||
(await req('/cases', null, 200, regularToken)).cases.find(c => c.id === caseId).teams.find(t => t.id === teamId)
|
||||
.technicalComment,
|
||||
comment
|
||||
);
|
||||
assert.equal(
|
||||
(await req('/admin/cases')).cases.find(c => c.id === caseId).teams.find(t => t.id === teamId).TeamInCase
|
||||
.technicalComment,
|
||||
comment
|
||||
);
|
||||
assert.equal(
|
||||
(await req(`/cases/${caseId}/teams/${secondTeamId}/marks`, null, 200, regularToken)).technicalComment,
|
||||
''
|
||||
);
|
||||
await req(
|
||||
commentPath,
|
||||
{ marks: [{ markType: 10, mark: 3 }], technicalComment: 'Forbidden' },
|
||||
403,
|
||||
regularToken,
|
||||
'PUT'
|
||||
);
|
||||
await req(commentPath, { markType: 10, mark: 3, technicalComment: 'Forbidden' }, 403, regularToken);
|
||||
assert.equal(await models.Mark.count({ where: { caseId } }), 0);
|
||||
for (const invalid of [null, 123, 'x'.repeat(5001)])
|
||||
await req(commentPath, { technicalComment: invalid }, 400, techToken, 'PUT');
|
||||
await req(commentPath, { technicalComment: 'Wrong', marks: [{ markType: 10, mark: 3 }] }, 403, techToken, 'PUT');
|
||||
assert.equal((await req(commentPath, null, 200, techToken)).technicalComment, comment);
|
||||
await req(commentPath, { technicalComment: 'x'.repeat(5000) }, 200, techToken, 'PUT');
|
||||
await req(commentPath, { technicalComment: '', marks: [] }, 200, techToken, 'PUT');
|
||||
assert.equal((await req(commentPath, null, 200, regularToken)).technicalComment, '');
|
||||
await req(commentPath, { technicalComment: comment }, 200, techToken, 'PUT');
|
||||
await req(
|
||||
`/cases/${otherCaseId}/teams/${teamId}/marks`,
|
||||
{ marks: [{ markType: 20, mark: 5 }] },
|
||||
403,
|
||||
techToken,
|
||||
'PUT'
|
||||
);
|
||||
await save(teamId, [
|
||||
{ markType: 20, mark: 0 },
|
||||
{ markType: 30, mark: 3 },
|
||||
]);
|
||||
await save(secondTeamId, [
|
||||
{ markType: 20, mark: 5 },
|
||||
{ markType: 30, mark: 5 },
|
||||
]);
|
||||
const visible = (await req('/cases', null, 200, regularToken)).cases
|
||||
.find(c => c.id === caseId)
|
||||
.teams.find(t => t.id === teamId).marks;
|
||||
assert.equal(visible.find(m => m.markType === 20).mark, 0);
|
||||
assert.equal(visible.find(m => m.markType === 30).mark, 3);
|
||||
const row = (await req('/admin/marks/details')).details
|
||||
.find(c => c.id === caseId)
|
||||
.rows.find(r => r.userId === technical.id && r.teamId === teamId);
|
||||
assert.equal(row.role, 'technical');
|
||||
assert.equal(row.total, 3);
|
||||
assert.equal(row.marks[10], null);
|
||||
let missing = (await req('/admin/marks')).noMarks;
|
||||
assert.equal(missing[technical.firstName + ' ' + technical.lastName]?.['Technical A ' + suffix], undefined);
|
||||
assert.ok(missing[regular.firstName + ' ' + regular.lastName]['Technical A ' + suffix]);
|
||||
await save(
|
||||
teamId,
|
||||
[
|
||||
{ markType: 10, mark: 5 },
|
||||
{ markType: 40, mark: 4 },
|
||||
{ markType: 50, mark: 3 },
|
||||
],
|
||||
regularToken
|
||||
);
|
||||
await save(
|
||||
secondTeamId,
|
||||
[10, 40, 50].map(markType => ({ markType, mark: 0 })),
|
||||
regularToken
|
||||
);
|
||||
missing = (await req('/admin/marks')).noMarks;
|
||||
assert.equal(missing[regular.firstName + ' ' + regular.lastName]?.['Technical A ' + suffix], undefined);
|
||||
const ranking = (await req('/admin/positions')).positions[caseId];
|
||||
assert.equal(ranking[teamId], 1);
|
||||
assert.equal(ranking[secondTeamId], 2);
|
||||
const late = await expert('Late', 'expert', [caseId]);
|
||||
assert.equal(await models.Mark.count({ where: { userId: late.id, caseId } }), 4);
|
||||
assert.equal((await req(commentPath, null, 200, (await login(late)).token)).technicalComment, comment);
|
||||
await req(`/cases/${caseId}/teams/${teamId}/marks`, { markType: 20, mark: 4 }, 200, techToken);
|
||||
assert.equal((await models.Mark.findOne({ where: { userId: late.id, caseId, teamId, markType: 20 } })).mark, 4);
|
||||
assert.equal((await models.Mark.findOne({ where: { userId: regular.id, caseId, teamId, markType: 10 } })).mark, 5);
|
||||
await req(`/admin/users/${technical.id}`, null, 204, token, 'DELETE');
|
||||
await req(`/admin/users/${other.id}/cases`, { caseIds: [caseId, otherCaseId] }, 200, token, 'PUT');
|
||||
await req(`/admin/users/${technical.id}/restore`, {}, 409);
|
||||
assert.ok((await models.User.findByPk(technical.id, { paranoid: false })).deletedAt);
|
||||
await req(`/admin/users/${other.id}/cases`, { caseIds: [otherCaseId] }, 200, token, 'PUT');
|
||||
await req(`/admin/users/${technical.id}/restore`, {});
|
||||
await req('/auth/user', null, 401, techToken);
|
||||
await req(`/admin/users/${technical.id}/cases`, { caseIds: [] }, 200, token, 'PUT');
|
||||
const race = await Promise.all(
|
||||
[technical.id, other.id].map(id =>
|
||||
fetch(base + `/admin/users/${id}/cases`, {
|
||||
method: 'PUT',
|
||||
headers: { Authorization: 'Bearer ' + token, 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ caseIds: [caseId] }),
|
||||
})
|
||||
)
|
||||
);
|
||||
assert.deepEqual(race.map(r => r.status).sort(), [200, 409]);
|
||||
for (const id of [technical.id, other.id]) await req(`/admin/users/${id}/cases`, { caseIds: [] }, 200, token, 'PUT');
|
||||
await req(
|
||||
`/admin/users/${regular.id}`,
|
||||
{ firstName: regular.firstName, lastName: regular.lastName, login: regular.login, role: 'technical' },
|
||||
200,
|
||||
token,
|
||||
'PUT'
|
||||
);
|
||||
await req('/auth/user', null, 401, regularToken);
|
||||
const converted = await login(regular);
|
||||
assert.equal(converted.user.role, 'technical');
|
||||
await save(teamId, [{ markType: 20, mark: 2 }], converted.token);
|
||||
await save(teamId, [{ markType: 10, mark: 2 }], converted.token, 403);
|
||||
const convertedRow = (await req('/admin/marks/details')).details
|
||||
.find(c => c.id === caseId)
|
||||
.rows.find(row => row.userId === regular.id && row.teamId === teamId);
|
||||
assert.equal(convertedRow.marks[10], null);
|
||||
console.log(
|
||||
'PASS technical role, two criteria only, case access, one technical per case including concurrent assignment/restore/role change, shared and late scores, missing reports, totals, no extra ranking vote and revoked session.'
|
||||
);
|
||||
} finally {
|
||||
await models.Mark.destroy({ where: { caseId: created.cases }, force: true });
|
||||
await models.UserInCase.destroy({ where: { caseId: created.cases }, force: true });
|
||||
await models.TeamInCase.destroy({ where: { caseId: created.cases }, force: true });
|
||||
await models.Team.destroy({ where: { id: created.teams }, force: true });
|
||||
await models.User.destroy({ where: { id: created.users }, force: true });
|
||||
await models.Case.destroy({ where: { id: created.cases }, force: true });
|
||||
await sequelize.close();
|
||||
}
|
||||
Reference in new issue
Block a user