T.ME/BIBIL_0DAY
CasperSecurity


Server : Apache/2
System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64
User : gositeme ( 1004)
PHP Version : 8.2.29
Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Directory :  /home/gositeme/domains/lavocat.ca/private_html/scripts/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/gositeme/domains/lavocat.ca/private_html/scripts/comprehensive-comment-test.js
const { PrismaClient } = require('@prisma/client');
const fetch = require('node-fetch');

const prisma = new PrismaClient();

// Mock session for testing
const MOCK_SESSION = {
  user: {
    id: 'test-user-id',
    name: 'Test User',
    email: 'test@example.com',
    role: 'ADMIN'
  }
};

async function testCommentDeletion() {
  console.log('\n=== TESTING COMMENT DELETION ===');
  
  try {
    // Find a comment to delete
    const comment = await prisma.caseComment.findFirst({
      where: { isDeleted: false },
      include: { user: true, case: true }
    });

    if (!comment) {
      console.log('❌ No comments found to test deletion');
      return false;
    }

    console.log('Found comment to delete:', {
      id: comment.id,
      content: comment.content.substring(0, 50) + '...',
      user: comment.user.name,
      caseId: comment.caseId
    });

    // Test direct database deletion
    console.log('\n1. Testing direct database deletion...');
    const deleteResult = await prisma.caseComment.update({
      where: { id: comment.id },
      data: { isDeleted: true }
    });
    console.log('✅ Direct deletion result:', deleteResult);

    // Test cascade deletion
    console.log('\n2. Testing cascade deletion...');
    const commentWithReplies = await prisma.caseComment.findFirst({
      where: { isDeleted: false },
      include: { 
        replies: { where: { isDeleted: false } },
        case: true 
      }
    });

    if (commentWithReplies && commentWithReplies.replies.length > 0) {
      console.log('Found comment with replies:', {
        id: commentWithReplies.id,
        replies: commentWithReplies.replies.length
      });

      const cascadeResult = await prisma.caseComment.updateMany({
        where: {
          OR: [
            { id: commentWithReplies.id },
            { parentId: commentWithReplies.id }
          ]
        },
        data: { isDeleted: true }
      });
      console.log('✅ Cascade deletion result:', cascadeResult);
    } else {
      console.log('ℹ️ No comments with replies found for cascade test');
    }

    return true;
  } catch (error) {
    console.error('❌ Error testing comment deletion:', error);
    return false;
  }
}

async function testReactions() {
  console.log('\n=== TESTING REACTIONS ===');
  
  try {
    // Find a comment to test reactions
    const comment = await prisma.caseComment.findFirst({
      where: { isDeleted: false },
      include: { reactions: { include: { user: true } } }
    });

    if (!comment) {
      console.log('❌ No comments found to test reactions');
      return false;
    }

    // Find a real user to use for testing
    const testUser = await prisma.user.findFirst({
      where: { role: { in: ['CLIENT', 'LAWYER', 'ADMIN'] } },
      select: { id: true, name: true, role: true }
    });

    if (!testUser) {
      console.log('❌ No test users found for reaction testing');
      return false;
    }

    console.log('Found comment for reaction test:', {
      id: comment.id,
      content: comment.content.substring(0, 30) + '...',
      existingReactions: comment.reactions.length
    });

    console.log('Using test user for reactions:', testUser.name, `(${testUser.role})`);

    // Test all reaction types
    const reactionTypes = ['like', 'love', 'laugh', 'wow', 'sad', 'angry'];
    
    for (const reactionType of reactionTypes) {
      console.log(`\nTesting reaction: ${reactionType}`);
      
      // Add reaction
      const addReaction = await prisma.commentReaction.create({
        data: {
          commentId: comment.id,
          userId: testUser.id,
          reactionType: reactionType
        }
      });
      console.log(`✅ Added ${reactionType} reaction:`, addReaction.id);

      // Verify reaction exists
      const verifyReaction = await prisma.commentReaction.findUnique({
        where: {
          commentId_userId_reactionType: {
            commentId: comment.id,
            userId: testUser.id,
            reactionType: reactionType
          }
        }
      });
      console.log(`✅ Verified ${reactionType} reaction exists:`, !!verifyReaction);

      // Remove reaction
      const removeReaction = await prisma.commentReaction.delete({
        where: {
          commentId_userId_reactionType: {
            commentId: comment.id,
            userId: testUser.id,
            reactionType: reactionType
          }
        }
      });
      console.log(`✅ Removed ${reactionType} reaction:`, removeReaction.id);
    }

    return true;
  } catch (error) {
    console.error('❌ Error testing reactions:', error);
    return false;
  }
}

async function testAttachments() {
  console.log('\n=== TESTING ATTACHMENTS ===');
  
  try {
    // Find a comment to test attachments
    const comment = await prisma.caseComment.findFirst({
      where: { isDeleted: false },
      include: { attachments: true }
    });

    if (!comment) {
      console.log('❌ No comments found to test attachments');
      return false;
    }

    console.log('Found comment for attachment test:', {
      id: comment.id,
      content: comment.content.substring(0, 30) + '...',
      existingAttachments: comment.attachments.length
    });

    // Test creating attachment
    const testAttachment = await prisma.commentAttachment.create({
      data: {
        commentId: comment.id,
        name: 'test-image.jpg',
        url: '/uploads/comments/test-image.jpg',
        type: 'image/jpeg',
        size: 1024
      }
    });
    console.log('✅ Created test attachment:', testAttachment);

    // Verify attachment exists
    const verifyAttachment = await prisma.commentAttachment.findUnique({
      where: { id: testAttachment.id }
    });
    console.log('✅ Verified attachment exists:', !!verifyAttachment);

    // Delete attachment
    const deleteAttachment = await prisma.commentAttachment.delete({
      where: { id: testAttachment.id }
    });
    console.log('✅ Deleted test attachment:', deleteAttachment.id);

    return true;
  } catch (error) {
    console.error('❌ Error testing attachments:', error);
    return false;
  }
}

async function testPermissions() {
  console.log('\n=== TESTING PERMISSIONS ===');
  
  try {
    // Find a comment to test permissions
    const comment = await prisma.caseComment.findFirst({
      where: { isDeleted: false },
      include: { user: true }
    });

    if (!comment) {
      console.log('❌ No comments found to test permissions');
      return false;
    }

    console.log('Found comment for permission test:', {
      id: comment.id,
      owner: comment.user.name,
      ownerRole: comment.user.role
    });

    // Test different user roles
    const testUsers = [
      { id: 'owner', name: 'Owner', role: 'CLIENT' },
      { id: 'admin', name: 'Admin', role: 'ADMIN' },
      { id: 'superadmin', name: 'SuperAdmin', role: 'SUPERADMIN' },
      { id: 'other', name: 'Other User', role: 'LAWYER' }
    ];

    for (const testUser of testUsers) {
      const isOwner = testUser.id === 'owner';
      const isAdmin = testUser.role === 'ADMIN';
      const isSuperAdmin = testUser.role === 'SUPERADMIN';
      
      const canDelete = isOwner || isAdmin || isSuperAdmin;
      
      console.log(`\nTesting ${testUser.name} (${testUser.role}):`);
      console.log(`  - Is owner: ${isOwner}`);
      console.log(`  - Is admin: ${isAdmin}`);
      console.log(`  - Is superadmin: ${isSuperAdmin}`);
      console.log(`  - Can delete: ${canDelete}`);
    }

    return true;
  } catch (error) {
    console.error('❌ Error testing permissions:', error);
    return false;
  }
}

async function testReactionIcons() {
  console.log('\n=== TESTING REACTION ICONS ===');
  
  try {
    // Check what reaction icons are available
    const reactionTypes = ['like', 'love', 'laugh', 'wow', 'sad', 'angry'];
    
    console.log('Available reaction types:', reactionTypes);
    console.log('Expected reaction icons:');
    
    const iconMap = {
      'like': '👍',
      'love': '❤️',
      'laugh': '😂',
      'wow': '😮',
      'sad': '😢',
      'angry': '😠'
    };

    for (const [type, icon] of Object.entries(iconMap)) {
      console.log(`  ${type}: ${icon}`);
    }

    // Check if ReactionPicker component exists
    const fs = require('fs');
    const reactionPickerPath = 'src/components/ReactionPicker.tsx';
    
    if (fs.existsSync(reactionPickerPath)) {
      console.log('✅ ReactionPicker component exists');
      const content = fs.readFileSync(reactionPickerPath, 'utf8');
      
      // Check if all reaction types are handled
      for (const reactionType of reactionTypes) {
        if (content.includes(reactionType)) {
          console.log(`✅ ${reactionType} reaction is handled`);
        } else {
          console.log(`❌ ${reactionType} reaction is NOT handled`);
        }
      }
    } else {
      console.log('❌ ReactionPicker component not found');
    }

    return true;
  } catch (error) {
    console.error('❌ Error testing reaction icons:', error);
    return false;
  }
}

async function testCommentCreation() {
  console.log('\n=== TESTING COMMENT CREATION ===');
  
  try {
    // Find a public case
    const publicCase = await prisma.legalCase.findFirst({
      where: { isPublic: true },
      select: { id: true, title: true }
    });

    if (!publicCase) {
      console.log('❌ No public cases found');
      return false;
    }

    console.log('Found public case:', publicCase.title);

    // Find a test user
    const testUser = await prisma.user.findFirst({
      where: { role: { in: ['CLIENT', 'LAWYER', 'ADMIN'] } },
      select: { id: true, name: true, role: true }
    });

    if (!testUser) {
      console.log('❌ No test users found');
      return false;
    }

    console.log('Using test user:', testUser.name, `(${testUser.role})`);

    // Create a test comment
    const testComment = await prisma.caseComment.create({
      data: {
        caseId: publicCase.id,
        userId: testUser.id,
        content: 'This is a comprehensive test comment created by the test script.',
        isDeleted: false,
        isEdited: false
      },
      include: {
        user: { select: { name: true, role: true } },
        case: { select: { title: true } }
      }
    });

    console.log('✅ Created test comment:', {
      id: testComment.id,
      content: testComment.content,
      user: testComment.user.name,
      case: testComment.case.title
    });

    // Create a reply
    const testReply = await prisma.caseComment.create({
      data: {
        caseId: publicCase.id,
        userId: testUser.id,
        parentId: testComment.id,
        content: 'This is a test reply to the test comment.',
        isDeleted: false,
        isEdited: false
      },
      include: {
        user: { select: { name: true, role: true } }
      }
    });

    console.log('✅ Created test reply:', {
      id: testReply.id,
      content: testReply.content,
      parentId: testReply.parentId,
      user: testReply.user.name
    });

    return { commentId: testComment.id, replyId: testReply.id };
  } catch (error) {
    console.error('❌ Error testing comment creation:', error);
    return false;
  }
}

async function comprehensiveTest() {
  console.log('=== COMPREHENSIVE COMMENT SYSTEM TEST ===\n');

  try {
    // Test 1: Comment Creation
    const creationResult = await testCommentCreation();
    if (!creationResult) {
      console.log('❌ Comment creation test failed');
      return;
    }

    // Test 2: Reactions
    const reactionsResult = await testReactions();
    if (!reactionsResult) {
      console.log('❌ Reactions test failed');
    }

    // Test 3: Attachments
    const attachmentsResult = await testAttachments();
    if (!attachmentsResult) {
      console.log('❌ Attachments test failed');
    }

    // Test 4: Permissions
    const permissionsResult = await testPermissions();
    if (!permissionsResult) {
      console.log('❌ Permissions test failed');
    }

    // Test 5: Reaction Icons
    const iconsResult = await testReactionIcons();
    if (!iconsResult) {
      console.log('❌ Reaction icons test failed');
    }

    // Test 6: Comment Deletion
    const deletionResult = await testCommentDeletion();
    if (!deletionResult) {
      console.log('❌ Comment deletion test failed');
    }

    // Clean up test data
    console.log('\n=== CLEANING UP TEST DATA ===');
    if (creationResult && creationResult.commentId) {
      await prisma.caseComment.updateMany({
        where: { id: { in: [creationResult.commentId, creationResult.replyId] } },
        data: { isDeleted: true }
      });
      console.log('✅ Cleaned up test comments');
    }

    console.log('\n=== COMPREHENSIVE TEST COMPLETE ===');
    console.log('✅ All tests completed. Check results above for any issues.');

  } catch (error) {
    console.error('❌ Comprehensive test failed:', error);
  } finally {
    await prisma.$disconnect();
  }
}

// Run the comprehensive test
comprehensiveTest(); 

CasperSecurity Mini