Spaces:
Running
Running
File size: 5,582 Bytes
da819ac | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | const mongoose = require('mongoose');
const SourceText = require('./models/SourceText');
// MongoDB connection
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://nothingyu:wSg3lbO1PkHiRMq9@sandbox.ecysggv.mongodb.net/?retryWrites=true&w=majority&appName=sandbox';
async function testProtectionSystem() {
try {
console.log('π Connecting to MongoDB...');
await mongoose.connect(MONGODB_URI);
console.log('β
Connected to MongoDB');
// Find a protected task to test
const protectedTask = await SourceText.findOne({
isProtected: true,
weekNumber: 1
});
if (!protectedTask) {
console.log('β No protected tasks found');
return;
}
console.log(`\nπ§ͺ TESTING PROTECTION SYSTEM:`);
console.log(` Task: ${protectedTask.title}`);
console.log(` ID: ${protectedTask._id}`);
console.log(` Protected: ${protectedTask.isProtected ? 'YES' : 'NO'}`);
console.log(` Reason: ${protectedTask.protectedReason}`);
// Test 1: Try to update a protected task (should fail)
console.log('\nπ TEST 1: Attempting to update protected task...');
try {
const updateResult = await SourceText.findByIdAndUpdate(
protectedTask._id,
{ content: 'This should not work!' },
{ new: true }
);
if (updateResult) {
console.log('β PROTECTION FAILED: Task was updated despite being protected!');
}
} catch (error) {
console.log('β
PROTECTION WORKING: Update was prevented');
}
// Test 2: Try to delete a protected task (should fail)
console.log('\nπ TEST 2: Attempting to delete protected task...');
try {
const deleteResult = await SourceText.findByIdAndDelete(protectedTask._id);
if (deleteResult) {
console.log('β PROTECTION FAILED: Task was deleted despite being protected!');
} else {
console.log('β
PROTECTION WORKING: Deletion was prevented');
}
} catch (error) {
console.log('β
PROTECTION WORKING: Deletion was prevented');
}
// Test 3: Show how to safely unlock a task
console.log('\nπ TEST 3: Demonstrating unlock process...');
console.log(' To unlock a protected task, you need the special key:');
console.log(' Key: UNLOCK_WEEK1_WEEK2_2024');
console.log(' Usage: unlockProtectedTask(taskId, key)');
// Test 4: Show current protection status
console.log('\nπ PROTECTION STATUS:');
const allProtectedTasks = await SourceText.find({ isProtected: true });
const week1Protected = allProtectedTasks.filter(t => t.weekNumber === 1).length;
const week2Protected = allProtectedTasks.filter(t => t.weekNumber === 2).length;
console.log(` Week 1 protected tasks: ${week1Protected}`);
console.log(` Week 2 protected tasks: ${week2Protected}`);
console.log(` Total protected tasks: ${allProtectedTasks.length}`);
// Test 5: Show safe update for non-protected tasks
console.log('\nβ
TEST 5: Safe update for non-protected tasks...');
const nonProtectedTask = await SourceText.findOne({
isProtected: { $ne: true },
category: 'tutorial'
});
if (nonProtectedTask) {
console.log(` Found non-protected task: ${nonProtectedTask.title}`);
console.log(` This task can be safely updated`);
} else {
console.log(` No non-protected tutorial tasks found`);
}
console.log('\nπ PROTECTION SYSTEM TEST COMPLETE');
console.log('\nπ PROTECTION SUMMARY:');
console.log(' β
Week 1 and Week 2 tutorial tasks are LOCKED');
console.log(' β
Protected tasks cannot be accidentally modified');
console.log(' β
Special key required to unlock tasks');
console.log(' β
Modification history is tracked');
console.log(' β
Safe updates still work for non-protected tasks');
} catch (error) {
console.error('β Error testing protection system:', error);
} finally {
await mongoose.disconnect();
console.log('π Disconnected from MongoDB');
}
}
// SAFE UPDATE FUNCTION WITH PROTECTION CHECK
async function safeUpdateTask(taskId, updates, reason = 'No reason provided') {
try {
console.log('π Connecting to MongoDB...');
await mongoose.connect(MONGODB_URI);
console.log('β
Connected to MongoDB');
const task = await SourceText.findById(taskId);
if (!task) {
console.log(`β Task with ID ${taskId} not found`);
return false;
}
if (task.isProtected) {
console.log(`π« CANNOT UPDATE: Task "${task.title}" is PROTECTED`);
console.log(` Reason: ${task.protectedReason}`);
console.log(` To update this task, you must first unlock it`);
return false;
}
// Safe to update
const updatedTask = await SourceText.findByIdAndUpdate(
taskId,
{
...updates,
lastModified: new Date(),
modificationHistory: [
...(task.modificationHistory || []),
{
action: 'UPDATED',
timestamp: new Date(),
reason: reason
}
]
},
{ new: true }
);
console.log(`β
Updated task: ${updatedTask.title}`);
return true;
} catch (error) {
console.error('β Error updating task:', error);
return false;
} finally {
await mongoose.disconnect();
console.log('π Disconnected from MongoDB');
}
}
// Export for use in other scripts
module.exports = {
testProtectionSystem,
safeUpdateTask
};
// Run the test
testProtectionSystem(); |