Spaces:
Sleeping
Sleeping
IDAgents Developer
commited on
Commit
Β·
48d481d
1
Parent(s):
d952de8
Add comprehensive session isolation testing suite
Browse files- TEST_SESSION_ISOLATION.md +336 -0
- test_isolation_automated.py +249 -0
TEST_SESSION_ISOLATION.md
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π§ͺ Session Isolation Testing Guide
|
| 2 |
+
|
| 3 |
+
## Test Scenario: Verify Per-User Session Isolation
|
| 4 |
+
|
| 5 |
+
This guide will help you test that different users have completely isolated sessions in your IDWeekAgents app.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## Prerequisites
|
| 10 |
+
|
| 11 |
+
β
App is running at: https://huggingface.co/spaces/John-jero/IDWeekAgents
|
| 12 |
+
β
You have at least 2 test user accounts (from AUTH_CREDENTIALS)
|
| 13 |
+
β
Two browser windows (or one normal + one incognito)
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## Test Accounts Available
|
| 18 |
+
|
| 19 |
+
Based on your AUTH_CREDENTIALS, use any two different accounts:
|
| 20 |
+
```
|
| 21 |
+
Format: username:password
|
| 22 |
+
|
| 23 |
+
Example accounts (if you followed the setup):
|
| 24 |
+
- user1:password1
|
| 25 |
+
- user2:password2
|
| 26 |
+
- doctor1:pass123
|
| 27 |
+
- pharmacist:stewardship
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## π¬ Test Suite
|
| 33 |
+
|
| 34 |
+
### TEST 1: Simple Chat Isolation β¨ (Should Work Now!)
|
| 35 |
+
|
| 36 |
+
**Purpose:** Verify that each user has their own chat history in Simple Chat
|
| 37 |
+
|
| 38 |
+
**Steps:**
|
| 39 |
+
|
| 40 |
+
1. **Browser 1 (User A):**
|
| 41 |
+
- Go to: https://huggingface.co/spaces/John-jero/IDWeekAgents
|
| 42 |
+
- Login with first account (e.g., `user1:password1`)
|
| 43 |
+
- Click on "Simple Chat" tab
|
| 44 |
+
- Send message: `Hello, I'm User 1!`
|
| 45 |
+
- You should see the bot's response
|
| 46 |
+
- Send another message: `This is my private conversation`
|
| 47 |
+
|
| 48 |
+
2. **Browser 2 (User B):**
|
| 49 |
+
- Open incognito window or different browser
|
| 50 |
+
- Go to: https://huggingface.co/spaces/John-jero/IDWeekAgents
|
| 51 |
+
- Login with second account (e.g., `user2:password2`)
|
| 52 |
+
- Click on "Simple Chat" tab
|
| 53 |
+
- **CHECK:** Chat should be EMPTY (you should NOT see User 1's messages)
|
| 54 |
+
- Send message: `Hello, I'm User 2!`
|
| 55 |
+
- You should see the bot's response
|
| 56 |
+
|
| 57 |
+
3. **Verify:**
|
| 58 |
+
- β
Browser 1 should ONLY show User 1's messages
|
| 59 |
+
- β
Browser 2 should ONLY show User 2's messages
|
| 60 |
+
- β
No cross-contamination between users
|
| 61 |
+
|
| 62 |
+
**Expected Result:** β
PASS - Each user sees only their own chat history
|
| 63 |
+
|
| 64 |
+
**If it fails:** β οΈ The simple chat isolation didn't work. Check console logs.
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
### TEST 2: Deployed Agent Chat Isolation β¨ (Should Work Now!)
|
| 69 |
+
|
| 70 |
+
**Purpose:** Verify that deployed agent chats are isolated per user
|
| 71 |
+
|
| 72 |
+
**Prerequisites:** You need at least one deployed agent in the app
|
| 73 |
+
|
| 74 |
+
**Steps:**
|
| 75 |
+
|
| 76 |
+
1. **Browser 1 (User A):**
|
| 77 |
+
- Go to "Agent Builder" tab
|
| 78 |
+
- Build a simple agent (or use existing agent)
|
| 79 |
+
- Deploy it by clicking "Save"
|
| 80 |
+
- Go to "Chat Panel" tab
|
| 81 |
+
- Select your agent from dropdown
|
| 82 |
+
- Chat with it: `Hi, I'm testing User A`
|
| 83 |
+
|
| 84 |
+
2. **Browser 2 (User B):**
|
| 85 |
+
- Login as different user
|
| 86 |
+
- Go to "Chat Panel" tab
|
| 87 |
+
- **CHECK:** The dropdown should show available agents
|
| 88 |
+
- Select an agent
|
| 89 |
+
- **CHECK:** Chat history should be EMPTY (no User A messages)
|
| 90 |
+
- Chat with it: `Hi, I'm testing User B`
|
| 91 |
+
|
| 92 |
+
3. **Verify:**
|
| 93 |
+
- β
Each user has separate chat history with the same agent
|
| 94 |
+
- β
User B doesn't see User A's conversation
|
| 95 |
+
|
| 96 |
+
**Expected Result:** β
PASS - Agent chats are isolated per user
|
| 97 |
+
|
| 98 |
+
**If it fails:** β οΈ Check if chatpanel_handle is being called correctly
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
### TEST 3: Agent Builder Isolation β οΈ (May Not Work Yet)
|
| 103 |
+
|
| 104 |
+
**Purpose:** Verify that agent building is isolated per user
|
| 105 |
+
|
| 106 |
+
**Steps:**
|
| 107 |
+
|
| 108 |
+
1. **Browser 1 (User A):**
|
| 109 |
+
- Go to "Agent Builder" tab
|
| 110 |
+
- Create an agent:
|
| 111 |
+
- Name: `User1 Agent`
|
| 112 |
+
- Mission: `Help User 1 with tasks`
|
| 113 |
+
- Click "Build Agent"
|
| 114 |
+
|
| 115 |
+
2. **Browser 2 (User B):**
|
| 116 |
+
- Login as different user
|
| 117 |
+
- Go to "Agent Builder" tab
|
| 118 |
+
- **CHECK:** The builder should be empty or have User B's agents
|
| 119 |
+
- **CHECK:** "Active Agents" should NOT show "User1 Agent"
|
| 120 |
+
- Create different agent:
|
| 121 |
+
- Name: `User2 Agent`
|
| 122 |
+
- Mission: `Help User 2 with tasks`
|
| 123 |
+
|
| 124 |
+
3. **Verify:**
|
| 125 |
+
- β
User A sees only their agents
|
| 126 |
+
- β
User B sees only their agents
|
| 127 |
+
|
| 128 |
+
**Expected Result:** β οΈ May FAIL - This feature needs more updates (see guide)
|
| 129 |
+
|
| 130 |
+
---
|
| 131 |
+
|
| 132 |
+
### TEST 4: Concurrent User Activity π₯
|
| 133 |
+
|
| 134 |
+
**Purpose:** Verify users can work simultaneously without interference
|
| 135 |
+
|
| 136 |
+
**Steps:**
|
| 137 |
+
|
| 138 |
+
1. **Both browsers simultaneously:**
|
| 139 |
+
- User A: Chat in Simple Chat
|
| 140 |
+
- User B: Chat in Simple Chat at the same time
|
| 141 |
+
- Both users send messages back and forth
|
| 142 |
+
|
| 143 |
+
2. **Verify:**
|
| 144 |
+
- β
Both users can chat simultaneously
|
| 145 |
+
- β
No crashes or errors
|
| 146 |
+
- β
No messages mixed up between users
|
| 147 |
+
- β
Each user's responses are correct and isolated
|
| 148 |
+
|
| 149 |
+
**Expected Result:** β
PASS - Concurrent usage works smoothly
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
### TEST 5: Session Persistence
|
| 154 |
+
|
| 155 |
+
**Purpose:** Verify that data persists during the session
|
| 156 |
+
|
| 157 |
+
**Steps:**
|
| 158 |
+
|
| 159 |
+
1. **Browser 1 (User A):**
|
| 160 |
+
- Chat in Simple Chat: `Message 1`
|
| 161 |
+
- Navigate to "Agent Builder" tab
|
| 162 |
+
- Navigate back to "Simple Chat" tab
|
| 163 |
+
- **CHECK:** Your chat history should still be there
|
| 164 |
+
|
| 165 |
+
2. **Refresh the page:**
|
| 166 |
+
- Refresh the browser (F5)
|
| 167 |
+
- Login again
|
| 168 |
+
- Go to "Simple Chat"
|
| 169 |
+
- **CHECK:** Chat history is cleared (expected - sessions are in-memory)
|
| 170 |
+
|
| 171 |
+
**Expected Result:**
|
| 172 |
+
- β
Session persists during navigation
|
| 173 |
+
- β
Session clears after refresh (expected behavior)
|
| 174 |
+
|
| 175 |
+
---
|
| 176 |
+
|
| 177 |
+
### TEST 6: Clear Chat Isolation
|
| 178 |
+
|
| 179 |
+
**Purpose:** Verify that clearing chat only affects the current user
|
| 180 |
+
|
| 181 |
+
**Steps:**
|
| 182 |
+
|
| 183 |
+
1. **Browser 1 (User A):**
|
| 184 |
+
- Chat in Simple Chat with several messages
|
| 185 |
+
|
| 186 |
+
2. **Browser 2 (User B):**
|
| 187 |
+
- Chat in Simple Chat with several messages
|
| 188 |
+
|
| 189 |
+
3. **Browser 1 (User A):**
|
| 190 |
+
- Click "Clear" button (if available)
|
| 191 |
+
- **CHECK:** Only User A's chat is cleared
|
| 192 |
+
|
| 193 |
+
4. **Browser 2 (User B):**
|
| 194 |
+
- **CHECK:** User B's chat is still there (unchanged)
|
| 195 |
+
|
| 196 |
+
**Expected Result:** β
PASS - Clear only affects current user
|
| 197 |
+
|
| 198 |
+
---
|
| 199 |
+
|
| 200 |
+
## π Test Results Template
|
| 201 |
+
|
| 202 |
+
Copy and fill this out:
|
| 203 |
+
|
| 204 |
+
```
|
| 205 |
+
π§ͺ SESSION ISOLATION TEST RESULTS
|
| 206 |
+
Date: ___________
|
| 207 |
+
Tester: ___________
|
| 208 |
+
|
| 209 |
+
[ ] TEST 1: Simple Chat Isolation
|
| 210 |
+
Status: PASS / FAIL
|
| 211 |
+
Notes: ___________
|
| 212 |
+
|
| 213 |
+
[ ] TEST 2: Deployed Agent Chat Isolation
|
| 214 |
+
Status: PASS / FAIL
|
| 215 |
+
Notes: ___________
|
| 216 |
+
|
| 217 |
+
[ ] TEST 3: Agent Builder Isolation
|
| 218 |
+
Status: PASS / FAIL / SKIPPED
|
| 219 |
+
Notes: ___________
|
| 220 |
+
|
| 221 |
+
[ ] TEST 4: Concurrent User Activity
|
| 222 |
+
Status: PASS / FAIL
|
| 223 |
+
Notes: ___________
|
| 224 |
+
|
| 225 |
+
[ ] TEST 5: Session Persistence
|
| 226 |
+
Status: PASS / FAIL
|
| 227 |
+
Notes: ___________
|
| 228 |
+
|
| 229 |
+
[ ] TEST 6: Clear Chat Isolation
|
| 230 |
+
Status: PASS / FAIL
|
| 231 |
+
Notes: ___________
|
| 232 |
+
|
| 233 |
+
Overall Status: ___________
|
| 234 |
+
Issues Found: ___________
|
| 235 |
+
```
|
| 236 |
+
|
| 237 |
+
---
|
| 238 |
+
|
| 239 |
+
## π Troubleshooting
|
| 240 |
+
|
| 241 |
+
### Issue: Both users see the same chat
|
| 242 |
+
**Cause:** Session isolation not working
|
| 243 |
+
**Solution:**
|
| 244 |
+
1. Check browser console for errors
|
| 245 |
+
2. Verify AUTH_CREDENTIALS is set in HF Spaces
|
| 246 |
+
3. Check that you're using DIFFERENT user accounts
|
| 247 |
+
|
| 248 |
+
### Issue: "request has no attribute username"
|
| 249 |
+
**Cause:** Authentication not enabled or user not logged in
|
| 250 |
+
**Solution:**
|
| 251 |
+
1. Ensure you're logged in with valid credentials
|
| 252 |
+
2. Check AUTH_CREDENTIALS secret in HF Spaces settings
|
| 253 |
+
|
| 254 |
+
### Issue: App crashes with multiple users
|
| 255 |
+
**Cause:** Race condition or thread safety issue
|
| 256 |
+
**Solution:**
|
| 257 |
+
1. Check server logs in HF Spaces
|
| 258 |
+
2. Report the error stack trace
|
| 259 |
+
|
| 260 |
+
---
|
| 261 |
+
|
| 262 |
+
## π What to Expect
|
| 263 |
+
|
| 264 |
+
### β
SHOULD WORK (Implemented):
|
| 265 |
+
- Simple Chat isolation
|
| 266 |
+
- Deployed Agent Chat isolation
|
| 267 |
+
- Concurrent user access
|
| 268 |
+
- Thread-safe operations
|
| 269 |
+
|
| 270 |
+
### β οΈ MAY NOT WORK YET (Needs more updates):
|
| 271 |
+
- Agent Builder isolation
|
| 272 |
+
- Patient data isolation
|
| 273 |
+
- Other advanced features
|
| 274 |
+
|
| 275 |
+
---
|
| 276 |
+
|
| 277 |
+
## π― Success Criteria
|
| 278 |
+
|
| 279 |
+
**Minimum Requirements:**
|
| 280 |
+
- β
TEST 1 (Simple Chat): MUST PASS
|
| 281 |
+
- β
TEST 2 (Agent Chat): MUST PASS
|
| 282 |
+
- β
TEST 4 (Concurrent): MUST PASS
|
| 283 |
+
|
| 284 |
+
**Nice to Have:**
|
| 285 |
+
- β
TEST 3, 5, 6: Would be great but may need more work
|
| 286 |
+
|
| 287 |
+
---
|
| 288 |
+
|
| 289 |
+
## π Quick Test Script
|
| 290 |
+
|
| 291 |
+
**Super Quick 2-Minute Test:**
|
| 292 |
+
|
| 293 |
+
1. Open app in 2 browsers
|
| 294 |
+
2. Login as user1 in browser 1
|
| 295 |
+
3. Login as user2 in browser 2
|
| 296 |
+
4. Both go to "Simple Chat"
|
| 297 |
+
5. Both send: "Hello from [username]"
|
| 298 |
+
6. Each should see ONLY their own message
|
| 299 |
+
|
| 300 |
+
**If this works:** β
Session isolation is working!
|
| 301 |
+
**If this fails:** β Need to debug
|
| 302 |
+
|
| 303 |
+
---
|
| 304 |
+
|
| 305 |
+
## π Need Help?
|
| 306 |
+
|
| 307 |
+
If tests fail or you see issues:
|
| 308 |
+
|
| 309 |
+
1. **Check console logs:**
|
| 310 |
+
- Open browser DevTools (F12)
|
| 311 |
+
- Look for errors in Console tab
|
| 312 |
+
|
| 313 |
+
2. **Check HF Spaces logs:**
|
| 314 |
+
- Go to your space page
|
| 315 |
+
- Click "Logs" tab
|
| 316 |
+
- Look for Python errors
|
| 317 |
+
|
| 318 |
+
3. **Verify setup:**
|
| 319 |
+
- AUTH_CREDENTIALS secret is set
|
| 320 |
+
- Using different user accounts
|
| 321 |
+
- Both logged in successfully
|
| 322 |
+
|
| 323 |
+
---
|
| 324 |
+
|
| 325 |
+
## π Expected Outcome
|
| 326 |
+
|
| 327 |
+
After running these tests:
|
| 328 |
+
- You'll know exactly what's working
|
| 329 |
+
- You'll know what still needs work
|
| 330 |
+
- You'll have concrete examples for debugging
|
| 331 |
+
|
| 332 |
+
**Goal:** Tests 1, 2, and 4 should PASS β
|
| 333 |
+
|
| 334 |
+
---
|
| 335 |
+
|
| 336 |
+
Ready to test? Start with the Quick Test Script above! π
|
test_isolation_automated.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Automated Session Isolation Tester
|
| 3 |
+
===================================
|
| 4 |
+
|
| 5 |
+
This script simulates multiple users and verifies that their sessions
|
| 6 |
+
are properly isolated. Run this before manual testing.
|
| 7 |
+
|
| 8 |
+
Usage:
|
| 9 |
+
python test_isolation_automated.py
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from user_session_manager import session_manager, SessionKeys
|
| 13 |
+
from session_helpers import get_current_username
|
| 14 |
+
import sys
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class MockRequest:
|
| 18 |
+
"""Mock Gradio request object for testing."""
|
| 19 |
+
def __init__(self, username):
|
| 20 |
+
self.username = username
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_basic_isolation():
|
| 24 |
+
"""Test that basic session isolation works."""
|
| 25 |
+
print("\nπ§ͺ TEST 1: Basic Session Isolation")
|
| 26 |
+
print("-" * 50)
|
| 27 |
+
|
| 28 |
+
# Simulate two users
|
| 29 |
+
user1_req = MockRequest("user1")
|
| 30 |
+
user2_req = MockRequest("user2")
|
| 31 |
+
|
| 32 |
+
# User 1 stores data
|
| 33 |
+
session_manager.set_user_data("user1", SessionKeys.SIMPLE_CHAT_HISTORY, [
|
| 34 |
+
["Hi", "Hello User 1!"]
|
| 35 |
+
])
|
| 36 |
+
|
| 37 |
+
# User 2 stores different data
|
| 38 |
+
session_manager.set_user_data("user2", SessionKeys.SIMPLE_CHAT_HISTORY, [
|
| 39 |
+
["Hey", "Hello User 2!"]
|
| 40 |
+
])
|
| 41 |
+
|
| 42 |
+
# Verify isolation
|
| 43 |
+
user1_history = session_manager.get_user_data("user1", SessionKeys.SIMPLE_CHAT_HISTORY)
|
| 44 |
+
user2_history = session_manager.get_user_data("user2", SessionKeys.SIMPLE_CHAT_HISTORY)
|
| 45 |
+
|
| 46 |
+
print(f"User 1 history: {user1_history}")
|
| 47 |
+
print(f"User 2 history: {user2_history}")
|
| 48 |
+
|
| 49 |
+
if user1_history != user2_history:
|
| 50 |
+
print("β
PASS: Users have different data")
|
| 51 |
+
return True
|
| 52 |
+
else:
|
| 53 |
+
print("β FAIL: Users have same data!")
|
| 54 |
+
return False
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_concurrent_updates():
|
| 58 |
+
"""Test that concurrent updates don't interfere."""
|
| 59 |
+
print("\nπ§ͺ TEST 2: Concurrent Updates")
|
| 60 |
+
print("-" * 50)
|
| 61 |
+
|
| 62 |
+
# Clear previous data
|
| 63 |
+
session_manager.clear_user_data("user1")
|
| 64 |
+
session_manager.clear_user_data("user2")
|
| 65 |
+
|
| 66 |
+
# Simulate rapid concurrent updates
|
| 67 |
+
for i in range(10):
|
| 68 |
+
session_manager.set_user_data("user1", f"key_{i}", f"user1_value_{i}")
|
| 69 |
+
session_manager.set_user_data("user2", f"key_{i}", f"user2_value_{i}")
|
| 70 |
+
|
| 71 |
+
# Verify no cross-contamination
|
| 72 |
+
passed = True
|
| 73 |
+
for i in range(10):
|
| 74 |
+
user1_val = session_manager.get_user_data("user1", f"key_{i}")
|
| 75 |
+
user2_val = session_manager.get_user_data("user2", f"key_{i}")
|
| 76 |
+
|
| 77 |
+
if f"user1_value_{i}" != user1_val:
|
| 78 |
+
print(f"β FAIL: User 1 key_{i} has wrong value: {user1_val}")
|
| 79 |
+
passed = False
|
| 80 |
+
|
| 81 |
+
if f"user2_value_{i}" != user2_val:
|
| 82 |
+
print(f"β FAIL: User 2 key_{i} has wrong value: {user2_val}")
|
| 83 |
+
passed = False
|
| 84 |
+
|
| 85 |
+
if passed:
|
| 86 |
+
print("β
PASS: All concurrent updates isolated correctly")
|
| 87 |
+
|
| 88 |
+
return passed
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_user_cleanup():
|
| 92 |
+
"""Test that clearing one user doesn't affect others."""
|
| 93 |
+
print("\nπ§ͺ TEST 3: User Data Cleanup")
|
| 94 |
+
print("-" * 50)
|
| 95 |
+
|
| 96 |
+
# Set up data for both users
|
| 97 |
+
session_manager.set_user_data("user1", "data", "user1_data")
|
| 98 |
+
session_manager.set_user_data("user2", "data", "user2_data")
|
| 99 |
+
|
| 100 |
+
print("Before cleanup:")
|
| 101 |
+
print(f" User 1 data: {session_manager.get_user_data('user1', 'data')}")
|
| 102 |
+
print(f" User 2 data: {session_manager.get_user_data('user2', 'data')}")
|
| 103 |
+
|
| 104 |
+
# Clear user 1
|
| 105 |
+
session_manager.clear_user_data("user1")
|
| 106 |
+
|
| 107 |
+
print("After clearing user1:")
|
| 108 |
+
print(f" User 1 data: {session_manager.get_user_data('user1', 'data', 'NONE')}")
|
| 109 |
+
print(f" User 2 data: {session_manager.get_user_data('user2', 'data')}")
|
| 110 |
+
|
| 111 |
+
user1_cleared = session_manager.get_user_data("user1", "data", "NONE") == "NONE"
|
| 112 |
+
user2_intact = session_manager.get_user_data("user2", "data") == "user2_data"
|
| 113 |
+
|
| 114 |
+
if user1_cleared and user2_intact:
|
| 115 |
+
print("β
PASS: User 1 cleared, User 2 intact")
|
| 116 |
+
return True
|
| 117 |
+
else:
|
| 118 |
+
print("β FAIL: Cleanup affected wrong user")
|
| 119 |
+
return False
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_chat_history_simulation():
|
| 123 |
+
"""Simulate a real chat scenario with multiple users."""
|
| 124 |
+
print("\nπ§ͺ TEST 4: Chat History Simulation")
|
| 125 |
+
print("-" * 50)
|
| 126 |
+
|
| 127 |
+
# Clear previous data
|
| 128 |
+
session_manager.clear_user_data("alice")
|
| 129 |
+
session_manager.clear_user_data("bob")
|
| 130 |
+
|
| 131 |
+
# Alice's conversation
|
| 132 |
+
alice_history = []
|
| 133 |
+
alice_history.append(["Hi", "Hello Alice!"])
|
| 134 |
+
alice_history.append(["How are you?", "I'm great!"])
|
| 135 |
+
session_manager.set_user_data("alice", SessionKeys.SIMPLE_CHAT_HISTORY, alice_history)
|
| 136 |
+
|
| 137 |
+
# Bob's conversation
|
| 138 |
+
bob_history = []
|
| 139 |
+
bob_history.append(["Hey", "Hey Bob!"])
|
| 140 |
+
bob_history.append(["What's new?", "Not much!"])
|
| 141 |
+
session_manager.set_user_data("bob", SessionKeys.SIMPLE_CHAT_HISTORY, bob_history)
|
| 142 |
+
|
| 143 |
+
# Verify isolation
|
| 144 |
+
retrieved_alice = session_manager.get_user_data("alice", SessionKeys.SIMPLE_CHAT_HISTORY)
|
| 145 |
+
retrieved_bob = session_manager.get_user_data("bob", SessionKeys.SIMPLE_CHAT_HISTORY)
|
| 146 |
+
|
| 147 |
+
print(f"Alice's chat: {len(retrieved_alice)} messages")
|
| 148 |
+
print(f"Bob's chat: {len(retrieved_bob)} messages")
|
| 149 |
+
print(f"Alice sees: {retrieved_alice}")
|
| 150 |
+
print(f"Bob sees: {retrieved_bob}")
|
| 151 |
+
|
| 152 |
+
alice_correct = retrieved_alice == alice_history
|
| 153 |
+
bob_correct = retrieved_bob == bob_history
|
| 154 |
+
no_overlap = retrieved_alice != retrieved_bob
|
| 155 |
+
|
| 156 |
+
if alice_correct and bob_correct and no_overlap:
|
| 157 |
+
print("β
PASS: Chat histories properly isolated")
|
| 158 |
+
return True
|
| 159 |
+
else:
|
| 160 |
+
print("β FAIL: Chat history isolation broken")
|
| 161 |
+
return False
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def test_session_stats():
|
| 165 |
+
"""Test session statistics and tracking."""
|
| 166 |
+
print("\nπ§ͺ TEST 5: Session Statistics")
|
| 167 |
+
print("-" * 50)
|
| 168 |
+
|
| 169 |
+
# Create some test data
|
| 170 |
+
session_manager.set_user_data("testuser1", "key1", "value1")
|
| 171 |
+
session_manager.set_user_data("testuser2", "key1", "value1")
|
| 172 |
+
session_manager.set_user_data("testuser3", "key1", "value1")
|
| 173 |
+
|
| 174 |
+
active_users = session_manager.get_active_users()
|
| 175 |
+
print(f"Active users: {active_users}")
|
| 176 |
+
|
| 177 |
+
has_expected_users = len(active_users) >= 3
|
| 178 |
+
|
| 179 |
+
if has_expected_users:
|
| 180 |
+
print("β
PASS: Session tracking working")
|
| 181 |
+
for user in active_users[:3]:
|
| 182 |
+
stats = session_manager.get_user_stats(user)
|
| 183 |
+
print(f" {user}: {stats}")
|
| 184 |
+
return True
|
| 185 |
+
else:
|
| 186 |
+
print("β FAIL: Session tracking not working")
|
| 187 |
+
return False
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def run_all_tests():
|
| 191 |
+
"""Run all tests and report results."""
|
| 192 |
+
print("\n" + "=" * 60)
|
| 193 |
+
print("π SESSION ISOLATION AUTOMATED TEST SUITE")
|
| 194 |
+
print("=" * 60)
|
| 195 |
+
|
| 196 |
+
tests = [
|
| 197 |
+
("Basic Isolation", test_basic_isolation),
|
| 198 |
+
("Concurrent Updates", test_concurrent_updates),
|
| 199 |
+
("User Cleanup", test_user_cleanup),
|
| 200 |
+
("Chat History Simulation", test_chat_history_simulation),
|
| 201 |
+
("Session Statistics", test_session_stats),
|
| 202 |
+
]
|
| 203 |
+
|
| 204 |
+
results = []
|
| 205 |
+
for name, test_func in tests:
|
| 206 |
+
try:
|
| 207 |
+
passed = test_func()
|
| 208 |
+
results.append((name, passed))
|
| 209 |
+
except Exception as e:
|
| 210 |
+
print(f"β EXCEPTION in {name}: {e}")
|
| 211 |
+
results.append((name, False))
|
| 212 |
+
|
| 213 |
+
# Summary
|
| 214 |
+
print("\n" + "=" * 60)
|
| 215 |
+
print("π TEST SUMMARY")
|
| 216 |
+
print("=" * 60)
|
| 217 |
+
|
| 218 |
+
passed_count = sum(1 for _, passed in results if passed)
|
| 219 |
+
total_count = len(results)
|
| 220 |
+
|
| 221 |
+
for name, passed in results:
|
| 222 |
+
status = "β
PASS" if passed else "β FAIL"
|
| 223 |
+
print(f"{status}: {name}")
|
| 224 |
+
|
| 225 |
+
print(f"\nπ Results: {passed_count}/{total_count} tests passed")
|
| 226 |
+
|
| 227 |
+
if passed_count == total_count:
|
| 228 |
+
print("\nπ ALL TESTS PASSED! Session isolation is working correctly!")
|
| 229 |
+
print("\nβ
You can now proceed to manual testing with real browsers.")
|
| 230 |
+
return 0
|
| 231 |
+
else:
|
| 232 |
+
print(f"\nβ οΈ {total_count - passed_count} test(s) failed.")
|
| 233 |
+
print("\nβ Fix these issues before deploying.")
|
| 234 |
+
return 1
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
if __name__ == "__main__":
|
| 238 |
+
exit_code = run_all_tests()
|
| 239 |
+
|
| 240 |
+
print("\n" + "=" * 60)
|
| 241 |
+
print("π NEXT STEPS:")
|
| 242 |
+
print("=" * 60)
|
| 243 |
+
print("1. If all tests passed, proceed to manual testing:")
|
| 244 |
+
print(" - See TEST_SESSION_ISOLATION.md for instructions")
|
| 245 |
+
print("2. If tests failed, check the error messages above")
|
| 246 |
+
print("3. Run this script again after fixes")
|
| 247 |
+
print("=" * 60 + "\n")
|
| 248 |
+
|
| 249 |
+
sys.exit(exit_code)
|