CIA/e-voting-system/.claude/TEST_BLOCKCHAIN_FIX.md
E-Voting Developer 3efdabdbbd fix: Implement vote check endpoint in frontend API proxy
- Created `/frontend/app/api/votes/check/route.ts` to handle GET requests for checking if a user has voted in a specific election.
- Added error handling for unauthorized access and missing election ID.
- Forwarded requests to the backend API and returned appropriate responses.
- Updated `/frontend/app/api/votes/history/route.ts` to fetch user's voting history with error handling.
- Ensured both endpoints utilize the authorization token for secure access.
2025-11-10 02:56:47 +01:00

7.6 KiB

🧪 TESTING THE BLOCKCHAIN FIX

Duration: 5 minutes
Prerequisites: Backend restarted with normalization fix
Goal: Verify truncateHash errors are gone and blockchain displays correctly


📋 Pre-Test Checklist

# 1. Ensure backend is running with the fix
docker compose restart backend
sleep 3

# 2. Verify container is healthy
docker compose ps
# Should show: backend container in "Up" state

# 3. Clear browser cache
# Press Ctrl+Shift+Delete → Clear all

🎬 Test Execution

Test 1: Console Log Inspection (5 min)

Step 1.1: Open browser

1. Navigate to: http://localhost:3000/dashboard/blockchain
2. Press: F12 (or Cmd+Option+I on Mac)
3. Go to: Console tab

Step 1.2: Look for normalization logs

// You should see something like:
[BlockchainPage] Fetching blockchain for election: 1
[BlockchainPage] Response status: 200
[BlockchainPage] Received blockchain data: {
  blocksCount: 5,
  firstBlockStructure: {
    index: 0,
    prev_hash: "0x000...",
    timestamp: 1234567890,
    encrypted_vote: "0x123...",
    transaction_id: "voter123",    SHOULD BE STRING!
    block_hash: "0x456...",
    signature: "0x789..."
  }
}

Step 1.3: Check truncateHash logs

// Should see these (NOT undefined):
[truncateHash] Called with: {
  hash: "0x123456789abcdef",    STRING, not OBJECT!
  type: "string",
  isUndefined: false,
  isNull: false,
  isEmpty: false,
  length: 18
}
[truncateHash] Result: 0x123456789abcde...    SUCCESS!

[truncateHash] Called with: {
  hash: "voter123",
  type: "string",
  isUndefined: false,
  ...
}
[truncateHash] Result: voter123    SUCCESS!

Expected Result:

  • NO errors like truncateHash: invalid hash parameter: undefined
  • All type values are "string", not "undefined"
  • All transaction_id values are strings (voter IDs or ""), not undefined
  • No isUndefined: true entries

Test 2: Blockchain Display (2 min)

Step 2.1: Visual inspection

1. Look at blockchain visualization
2. Should see:
   - Multiple blocks displayed
   - Each block shows transaction_id (voter ID or empty for genesis)
   - Hash values are visible and truncated nicely
   - No "N/A" for all fields

Step 2.2: Block details

Click on any block → Should show:
  ✅ Transaction ID: voter123 (or similar)
  ✅ Encrypted Vote: 0x123456... (truncated)
  ✅ Block Hash: 0x789abc... (truncated)
  ✅ Signature: 0xdef456... (truncated)
  
✅ NO "N/A" for any hash field
✅ NO undefined errors in console

Test 3: Verify Button Functionality (2 min)

Step 3.1: Click Verify Button

1. Click: "Vérifier l'intégrité de la chaîne" button
2. Check Network tab (Ctrl+Shift+E)
3. Should see: POST to /api/votes/verify-blockchain

Step 3.2: Check request parameters

Request URL: .../api/votes/verify-blockchain?election_id=1
Request Body: { "election_id": 1 }

✅ election_id is in URL query parameters
✅ election_id is in request body
✅ NOT getting 400 error

Step 3.3: Check verification result

Response should be 200 OK with:
{
  "chain_valid": true/false,
  "total_blocks": 5,
  "total_votes": 4,
  "issues": [...]
}

✅ NO 400 "Field required: election_id" error
✅ Verification completes without error

Success Criteria

All Should Be TRUE:

Criterion Check
No truncateHash errors in console Clear console, no red errors
transaction_id is always a string Search console for "string" values
No OBJECT values for hashes No {...} in transaction_id fields
Blockchain displays correctly Visual layout is intact, readable
Verify button works Returns 200 OK, no 400 errors
All hash fields visible No "N/A" for populated fields

🐛 Troubleshooting

If You See: truncateHash: invalid hash parameter: undefined

Problem: Backend normalization not running
Solution:
1. Stop backend: docker compose stop backend
2. Start backend: docker compose start backend
3. Wait 5 seconds
4. Hard refresh browser: Ctrl+F5 (or Cmd+Shift+R on Mac)
5. Retry test

If transaction_id Still Shows as OBJECT

Problem: Old cached response
Solution:
1. Clear browser cache: Ctrl+Shift+Delete
2. Close all browser tabs for localhost:3000
3. Restart browser
4. Navigate to fresh URL
5. Press F12, then reload: Ctrl+R or F5

If Verify Button Returns 400 Error

Problem: election_id not being passed
Solution:
1. Check proxy route is modified: 
   /frontend/app/api/votes/verify-blockchain/route.ts
2. Ensure election_id parameter is added to URL
3. Check Network tab request includes ?election_id=X

If Blockchain Doesn't Display At All

Problem: Backend might have crashed
Solution:
1. Check backend logs: docker compose logs backend
2. Look for Python errors
3. Restart: docker compose restart backend
4. Refresh browser: F5

📊 Expected Console Output

Good Signs

[BlockchainPage] Fetching blockchain for election: 1
[BlockchainPage] Response status: 200
[BlockchainPage] Received blockchain data: { blocksCount: 5, ... }
[BlockchainVisualizer] Component mounted
[BlockchainVisualizer] First block structure: { transaction_id: "voter1", ... }
[truncateHash] Called with: { hash: "voter1", type: "string", ... }
[truncateHash] Result: voter1

✅ ZERO errors in console

Bad Signs

[truncateHash] Called with: { hash: undefined, type: "undefined", ... }
POST /api/votes/verify-blockchain 400 Bad Request
"Field required: election_id"
TypeError: Cannot read property 'transaction_id' of undefined

🔍 Deep Inspection Commands

In Browser Console:

Check first block structure:

// After page loads, copy-paste this:
console.log('First block:', JSON.stringify(blocks[0], null, 2))

Check all transaction_ids:

blocks.forEach((block, i) => {
  console.log(`Block ${i}: transaction_id = ${block.transaction_id} (type: ${typeof block.transaction_id})`)
})

Check if normalization worked:

// Should print TRUE if normalized
blocks.every(b => typeof b.transaction_id === 'string' || b.transaction_id === undefined)

Verify hash values:

blocks.forEach((block, i) => {
  console.log(`Block ${i}:`, {
    transaction_id_type: typeof block.transaction_id,
    encrypted_vote_type: typeof block.encrypted_vote,
    block_hash_type: typeof block.block_hash,
    signature_type: typeof block.signature
  })
})

📝 Test Report Template

Copy-paste this after testing:

# BLOCKCHAIN FIX TEST REPORT

**Date**: [Today's date]
**Tester**: [Your name]
**Duration**: [X minutes]

## Results

- [ ] No truncateHash errors in console
- [ ] transaction_id values are strings (not objects/undefined)
- [ ] Blockchain displays correctly
- [ ] Verify button works (returns 200 OK)
- [ ] All hash fields visible (no "N/A")

## Issues Found

[Describe any issues or unexpected behavior]

## Console Output

[Paste relevant console logs]

## Screenshots

[Attach if needed]

## Status

✅ PASS / ❌ FAIL

🎯 Next Steps After Successful Test

  1. If all tests pass:

    • Blockchain fix is complete
    • System is ready for production
    • Document findings in issue tracker
  2. If any test fails:

    • Check backend logs: docker compose logs backend -n 50
    • Verify normalization function is present in votes.py
    • Try restarting backend: docker compose restart backend
    • Re-run this test

Test Status: READY
Estimated Duration: 5 minutes
Difficulty: EASY
Risk: NONE (read-only testing)