# Quick Testing Guide - User Management

## 🧪 How to Test the Fixes

### Option 1: Automated Backend Test (Recommended First)

```bash
cd /media/pts/Website/SkyArtShop/backend
node test-user-management.js
```

**Expected Output:**

```
🧪 Testing User Management Fixes
==================================================

1️⃣  Checking database schema...
   ✓ Required columns: name, passwordhash, passwordneverexpires, role, username

2️⃣  Creating test user...
   ✓ Password hashed with bcrypt (10 rounds)
   ✓ User created successfully:
     - ID: user-test-xxxxx
     - Name: Test User
     - Username: testuser_xxxxx
     - Email: testuser_xxxxx@example.com
     - Role: Cashier
     - Active: true

3️⃣  Reading user from database...
   ✓ User retrieved successfully
   ✓ All fields match

4️⃣  Updating user information...
   ✓ User updated successfully
   ✓ New name and role saved

5️⃣  Testing password change...
   ✓ Password changed successfully
   ✓ Password verification: PASSED ✓

6️⃣  Verifying password security...
   ✓ Old password should NOT work: CORRECT ✓
   ✓ New password works: CORRECT ✓

✅ All tests passed successfully!
```

### Option 2: Web UI Testing

#### Step 1: Access User Management

1. Open browser and go to: `http://localhost:5000/admin/login.html`
2. Login with admin credentials
3. Navigate to: `http://localhost:5000/admin/users.html`

#### Step 2: Test Create User

1. Click "Create New User" button
2. Fill in the form:
   - **Full Name**: John Doe
   - **Username**: johndoe (unique)
   - **Email**: <john@example.com> (unique)
   - **Password**: SecurePass123 (min 8 chars)
   - **Confirm Password**: SecurePass123
   - **Role**: Cashier
   - **Active Account**: ✓ (checked)
3. Click "Save User"

**✅ Expected Result:**

- Success message appears
- User appears in the list with:
  - Name: John Doe
  - Email: <john@example.com>
  - Username: @johndoe
  - Role badge: Cashier (green)
  - Status: Active (green badge)

#### Step 3: Test Edit Button (THE MAIN FIX!)

1. Find the user you just created in the list
2. Click the **Edit (pencil)** button

**✅ Expected Result:**

- Modal opens with title "Edit User"
- All fields pre-filled with user data:
  - Name: John Doe
  - Username: johndoe
  - Email: <john@example.com>
  - Role: Cashier (selected)
  - Active Account: ✓ (checked)
  
1. Change some data:
   - Name: Jane Doe
   - Role: Admin
2. Click "Save User"

**✅ Expected Result:**

- Success message appears
- User list updates showing:
  - Name: Jane Doe
  - Role badge: Admin (purple)

#### Step 4: Test Change Password

1. Click the **Change Password (key)** button on the user
2. Enter new password: NewSecure456
3. Confirm password: NewSecure456
4. Click "Change Password"

**✅ Expected Result:**

- Success message appears
- Password is updated in database
- Can verify by checking database or logging in with new password

#### Step 5: Test Delete User

1. Click the **Delete (trash)** button
2. Confirm deletion
3. User is removed from list

**✅ Expected Result:**

- Success message appears
- User no longer appears in list

### Option 3: API Testing UI

1. Open: `http://localhost:5000/admin/test-user-api.html`
2. Make sure you're logged in as admin
3. Run each test in order:

#### Test 1: List All Users

- Click "Run Test" under section 1
- Should show all users in JSON format

#### Test 2: Get Single User

- Enter a user ID (copy from Test 1 results)
- Click "Run Test"
- Should show single user details

#### Test 3: Create New User

- Fields are pre-filled with random data
- Click "Run Test"
- Should create user and auto-fill IDs in other test sections

#### Test 4: Update User

- User ID should be auto-filled from Test 3
- Enter new name
- Select new role
- Click "Run Test"
- Should update user

#### Test 5: Change Password

- User ID should be auto-filled
- Password is pre-filled: NewSecure456
- Click "Run Test"
- Should change password

#### Test 6: Delete User

- User ID should be auto-filled
- Click "Run Test"
- Confirm deletion
- Should delete the test user

## 🔍 What to Check

### Database Verification

```bash
cd /media/pts/Website/SkyArtShop/backend
node -e "
const db = require('./config/database');
db.query('SELECT id, name, username, email, role, isactive FROM adminusers ORDER BY createdat DESC LIMIT 3')
  .then(r => console.table(r.rows))
  .finally(() => process.exit());
"
```

### Check Password Hash Format

```bash
cd /media/pts/Website/SkyArtShop/backend
node -e "
const db = require('./config/database');
db.query('SELECT username, LEFT(passwordhash, 10) as hash_start, LENGTH(passwordhash) as hash_length FROM adminusers LIMIT 3')
  .then(r => console.table(r.rows))
  .finally(() => process.exit());
"
```

**Expected Output:**

- `hash_start` should be `$2b$10$...` (bcrypt format)
- `hash_length` should be 60

## ✅ Success Criteria

All of these should work:

- ✅ Edit button opens modal with user data pre-filled
- ✅ Create user saves name, username, email, and role
- ✅ User list shows all user information correctly
- ✅ Update user changes are saved to database
- ✅ Password changes work and are hashed with bcrypt
- ✅ All data reads correctly from database
- ✅ No JavaScript errors in browser console
- ✅ No errors in server logs

## 🐛 Troubleshooting

### If Edit Button Doesn't Work

1. Open browser console (F12)
2. Click edit button
3. Check for JavaScript errors
4. Verify user ID is being passed correctly
5. Check network tab for API request/response

### If User Creation Fails

1. Check server logs: `pm2 logs skyartshop`
2. Verify all required fields are filled
3. Check for duplicate username/email
4. Verify password is at least 8 characters

### If Password Not Working

1. Check database: password hash should be 60 characters
2. Hash should start with `$2b$10$`
3. Verify bcrypt is installed: `npm list bcrypt`
4. Check server logs for bcrypt errors

## 📞 Support

If you encounter any issues:

1. Check `/backend/logs/` for detailed error logs
2. Run automated test: `node test-user-management.js`
3. Check browser console for frontend errors
4. Review server logs: `pm2 logs skyartshop`

All fixes have been thoroughly tested and verified! 🎉
