fix: assign member hanya intern yg gk ada active project

This commit is contained in:
Rafi Athallah
2026-06-27 18:12:42 +07:00
parent 8145b0afc0
commit ccf192a14d
5 changed files with 201 additions and 85 deletions
+181
View File
@@ -278,6 +278,187 @@ async function runTests() {
}
console.log(' PASSED: Intern successfully retrieved project detail containing certificate template URL.\n');
// 10. Claim Certificate Tests
console.log('10. Claim Certificate Tests...');
// Get Intern User ID
const internUser = await prisma.user.findUnique({
where: { email: internEmail }
});
const internUserId = internUser.id;
console.log(` Intern User ID: ${internUserId}`);
// A. Claim before tasks are created (Should fail - 400)
console.log(' A. Trying to claim certificate before any tasks are created...');
try {
await axios.post(
`${BASE_URL}/certificate-api/claim`,
{ id_project: project1Id },
getHeader(internToken)
);
throw new Error('TEST FAILED: Intern claimed certificate with no tasks!');
} catch (err) {
if (err.response && err.response.status === 400) {
console.log(' PASSED: Claim failed with 400 Bad Request as expected.');
console.log(' Error Message:', err.response.data.message);
} else {
throw err;
}
}
// B. Create a task for Project 1
console.log(' B. Creating a task for Project 1...');
const task = await prisma.task.create({
data: {
id_project: project1Id,
slug: `final-assignment-${Date.now()}`,
title: 'Final Assignment',
description: 'Submit your final report.',
deadline_at: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days from now
submission_type: 'url_link'
}
});
console.log(` Task created successfully! ID: ${task.id}`);
// C. Claim before submitting tasks (Should fail - 400)
console.log(' C. Trying to claim certificate before task submission...');
try {
await axios.post(
`${BASE_URL}/certificate-api/claim`,
{ id_project: project1Id },
getHeader(internToken)
);
throw new Error('TEST FAILED: Intern claimed certificate without submitting tasks!');
} catch (err) {
if (err.response && err.response.status === 400) {
console.log(' PASSED: Claim failed with 400 Bad Request as expected.');
console.log(' Error Message:', err.response.data.message);
} else {
throw err;
}
}
// D. Submit the task
console.log(' D. Creating submission for the task...');
const submission = await prisma.taskSubmission.create({
data: {
id_task: task.id,
id_user: internUserId,
url_link: 'https://github.com/internify/report'
}
});
console.log(` Submission created successfully! ID: ${submission.id}`);
// E. Claim certificate (Should succeed - 201)
console.log(' E. Claiming certificate after submitting tasks...');
const claimRes = await axios.post(
`${BASE_URL}/certificate-api/claim`,
{ id_project: project1Id },
getHeader(internToken)
);
console.log(' Response status:', claimRes.status);
console.log(' Claimed Certificate No:', claimRes.data.data.certificate_no);
if (claimRes.status !== 201) {
throw new Error(`TEST FAILED: Expected status 201, got ${claimRes.status}`);
}
console.log(' PASSED: Certificate claimed successfully!\n');
// F. Claim certificate again (Should fail - 409)
console.log(' F. Trying to claim certificate again...');
try {
await axios.post(
`${BASE_URL}/certificate-api/claim`,
{ id_project: project1Id },
getHeader(internToken)
);
throw new Error('TEST FAILED: Intern claimed the same certificate twice!');
} catch (err) {
if (err.response && err.response.status === 409) {
console.log(' PASSED: Claim failed with 409 Conflict as expected.');
console.log(' Error Message:', err.response.data.message);
} else {
throw err;
}
}
// G. Retrieve my certificates (Should include the claimed certificate)
console.log(' G. Verifying certificate is in my certificates list...');
const myCertsRes = await axios.get(
`${BASE_URL}/certificate-api/my-certificates`,
getHeader(internToken)
);
console.log(' Certificates list length:', myCertsRes.data.data.length);
const foundCert = myCertsRes.data.data.find(c => c.id_project === project1Id);
if (!foundCert) {
throw new Error('TEST FAILED: Claimed certificate not found in my certificates list!');
}
console.log(' PASSED: Claimed certificate verified in list!\n');
// 11. Assign Member Restriction Tests
console.log('11. Assign Member Restriction Tests...');
// A. Assign Intern (who is active in Project 1) to Project 2 (Should fail - 409)
console.log(' A. Trying to assign intern (already active in Project 1) to Project 2...');
try {
await axios.post(
`${BASE_URL}/project-api/assign-member`,
{ id_project: project2Id, id_user: internUserId },
getHeader(mentor2Token)
);
throw new Error('TEST FAILED: Intern was assigned to two active projects!');
} catch (err) {
if (err.response && err.response.status === 409) {
console.log(' PASSED: Assign failed with 409 Conflict as expected.');
console.log(' Error Message:', err.response.data.message);
} else {
throw err;
}
}
// B. Remove intern from Project 1
console.log(' B. Removing intern from Project 1...');
const removeRes = await axios.post(
`${BASE_URL}/project-api/remove-member`,
{ id_project: project1Id, id_user: internUserId },
getHeader(mentor1Token)
);
console.log(' Response status:', removeRes.status);
if (removeRes.status !== 200) {
throw new Error(`TEST FAILED: Failed to remove member, got status ${removeRes.status}`);
}
console.log(' PASSED: Intern successfully removed from Project 1.');
// C. Assign intern to Project 2 (Should succeed now)
console.log(' C. Assigning intern to Project 2...');
const assignRes = await axios.post(
`${BASE_URL}/project-api/assign-member`,
{ id_project: project2Id, id_user: internUserId },
getHeader(mentor2Token)
);
console.log(' Response status:', assignRes.status);
if (assignRes.status !== 200) {
throw new Error(`TEST FAILED: Failed to assign member, got status ${assignRes.status}`);
}
console.log(' PASSED: Intern successfully assigned to Project 2.');
// D. Assign intern to Project 2 again (Should fail - 409)
console.log(' D. Trying to assign intern to Project 2 again...');
try {
await axios.post(
`${BASE_URL}/project-api/assign-member`,
{ id_project: project2Id, id_user: internUserId },
getHeader(mentor2Token)
);
throw new Error('TEST FAILED: Intern assigned to Project 2 twice!');
} catch (err) {
if (err.response && err.response.status === 409) {
console.log(' PASSED: Assign failed with 409 Conflict as expected.');
console.log(' Error Message:', err.response.data.message);
} else {
throw err;
}
}
console.log('=== ALL INTEGRATION TESTS PASSED SUCCESSFULLY! ===');
await prisma.$disconnect();
process.exit(0);