Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
test_permissions.py · 8.6 KB · 227 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """Test ownership-based permissions system""" |
| 3 | |
| 4 | import asyncio |
| 5 | import aiohttp |
| 6 | import json |
| 7 | import sys |
| 8 | |
| 9 | BASE_URL = "http://localhost:8005" |
| 10 | |
| 11 | async def test_public_read(): |
| 12 | """Test that public read permission works without authentication""" |
| 13 | print("\n🧪 Testing public read (no auth required)...") |
| 14 | |
| 15 | async with aiohttp.ClientSession() as session: |
| 16 | # Try to read customers without auth (should work - read is public) |
| 17 | async with session.get(f"{BASE_URL}/api/customers") as resp: |
| 18 | if resp.status == 200: |
| 19 | data = await resp.json() |
| 20 | print(f"✅ Public read successful: {data['total']} records") |
| 21 | else: |
| 22 | print(f"❌ Public read failed: {resp.status}") |
| 23 | |
| 24 | async def test_authenticated_create(): |
| 25 | """Test that create requires authentication""" |
| 26 | print("\n🧪 Testing create permission (requires authentication)...") |
| 27 | |
| 28 | async with aiohttp.ClientSession() as session: |
| 29 | # Try to create without auth (should fail) |
| 30 | test_data = { |
| 31 | "name": "Test User", |
| 32 | "email": "test@example.com", |
| 33 | "phone": "555-1234", |
| 34 | "credit_limit": 1000, |
| 35 | "status": "active" |
| 36 | } |
| 37 | |
| 38 | async with session.post(f"{BASE_URL}/api/customers", json=test_data) as resp: |
| 39 | if resp.status == 403: |
| 40 | print("✅ Create without auth correctly denied") |
| 41 | else: |
| 42 | print(f"❌ Unexpected response: {resp.status}") |
| 43 | |
| 44 | # Try to create with auth (should work) |
| 45 | headers = {"X-User": "user123"} |
| 46 | async with session.post(f"{BASE_URL}/api/customers", json=test_data, headers=headers) as resp: |
| 47 | if resp.status == 200: |
| 48 | result = await resp.json() |
| 49 | print(f"✅ Create with auth successful: ID {result['id']}") |
| 50 | return result['id'] |
| 51 | else: |
| 52 | print(f"❌ Create with auth failed: {resp.status}") |
| 53 | return None |
| 54 | |
| 55 | async def test_owner_update(record_id: int): |
| 56 | """Test that owner can update their own record""" |
| 57 | print("\n🧪 Testing owner update permission...") |
| 58 | |
| 59 | if not record_id: |
| 60 | print("⚠️ Skipping - no record ID") |
| 61 | return |
| 62 | |
| 63 | async with aiohttp.ClientSession() as session: |
| 64 | # Try to update as different user (should fail) |
| 65 | headers = {"X-User": "otheruser"} |
| 66 | update_data = {"name": "Updated by Other"} |
| 67 | |
| 68 | async with session.put(f"{BASE_URL}/api/customers/{record_id}", json=update_data, headers=headers) as resp: |
| 69 | if resp.status == 403: |
| 70 | print("✅ Update by non-owner correctly denied") |
| 71 | else: |
| 72 | print(f"❌ Unexpected response: {resp.status}") |
| 73 | |
| 74 | # Try to update as owner (should work) |
| 75 | headers = {"X-User": "user123"} |
| 76 | update_data = {"name": "Updated by Owner"} |
| 77 | |
| 78 | async with session.put(f"{BASE_URL}/api/customers/{record_id}", json=update_data, headers=headers) as resp: |
| 79 | if resp.status == 200: |
| 80 | print("✅ Update by owner successful") |
| 81 | else: |
| 82 | print(f"❌ Update by owner failed: {resp.status}") |
| 83 | |
| 84 | # Try to update as admin (should work) |
| 85 | headers = {"X-User": "admin"} |
| 86 | update_data = {"name": "Updated by Admin"} |
| 87 | |
| 88 | async with session.put(f"{BASE_URL}/api/customers/{record_id}", json=update_data, headers=headers) as resp: |
| 89 | if resp.status == 200: |
| 90 | print("✅ Update by admin successful") |
| 91 | else: |
| 92 | print(f"❌ Update by admin failed: {resp.status}") |
| 93 | |
| 94 | async def test_admin_delete(record_id: int): |
| 95 | """Test that only admin can delete""" |
| 96 | print("\n🧪 Testing admin-only delete permission...") |
| 97 | |
| 98 | if not record_id: |
| 99 | print("⚠️ Skipping - no record ID") |
| 100 | return |
| 101 | |
| 102 | async with aiohttp.ClientSession() as session: |
| 103 | # Create another test record as a different user |
| 104 | headers = {"X-User": "user456"} |
| 105 | test_data = { |
| 106 | "name": "Delete Test User", |
| 107 | "email": "delete@example.com", |
| 108 | "phone": "555-9999", |
| 109 | "credit_limit": 500, |
| 110 | "status": "active" |
| 111 | } |
| 112 | |
| 113 | async with session.post(f"{BASE_URL}/api/customers", json=test_data, headers=headers) as resp: |
| 114 | if resp.status == 200: |
| 115 | result = await resp.json() |
| 116 | delete_id = result['id'] |
| 117 | print(f"✅ Created test record: ID {delete_id}") |
| 118 | else: |
| 119 | print(f"❌ Failed to create test record: {resp.status}") |
| 120 | return |
| 121 | |
| 122 | # Try to delete as owner (should fail - delete is admin only) |
| 123 | headers = {"X-User": "user456"} |
| 124 | async with session.delete(f"{BASE_URL}/api/customers/{delete_id}", headers=headers) as resp: |
| 125 | if resp.status == 403: |
| 126 | print("✅ Delete by owner correctly denied (admin-only)") |
| 127 | else: |
| 128 | print(f"❌ Unexpected response: {resp.status}") |
| 129 | |
| 130 | # Try to delete as different user (should fail) |
| 131 | headers = {"X-User": "otheruser"} |
| 132 | async with session.delete(f"{BASE_URL}/api/customers/{delete_id}", headers=headers) as resp: |
| 133 | if resp.status == 403: |
| 134 | print("✅ Delete by non-admin correctly denied") |
| 135 | else: |
| 136 | print(f"❌ Unexpected response: {resp.status}") |
| 137 | |
| 138 | # Try to delete as admin (should work) |
| 139 | headers = {"X-User": "admin"} |
| 140 | async with session.delete(f"{BASE_URL}/api/customers/{delete_id}", headers=headers) as resp: |
| 141 | if resp.status == 200: |
| 142 | print("✅ Delete by admin successful") |
| 143 | else: |
| 144 | print(f"❌ Delete by admin failed: {resp.status}") |
| 145 | |
| 146 | async def test_ownership_tracking(): |
| 147 | """Test that ownership fields are properly populated""" |
| 148 | print("\n🧪 Testing ownership tracking fields...") |
| 149 | |
| 150 | async with aiohttp.ClientSession() as session: |
| 151 | # Create record as user123 |
| 152 | headers = {"X-User": "user123"} |
| 153 | test_data = { |
| 154 | "name": "Ownership Test", |
| 155 | "email": "ownership@example.com", |
| 156 | "phone": "555-7777", |
| 157 | "credit_limit": 2000, |
| 158 | "status": "active" |
| 159 | } |
| 160 | |
| 161 | async with session.post(f"{BASE_URL}/api/customers", json=test_data, headers=headers) as resp: |
| 162 | if resp.status == 200: |
| 163 | result = await resp.json() |
| 164 | record_id = result['id'] |
| 165 | |
| 166 | # Check if created_by field was set |
| 167 | if result.get('created_by') == 'user123': |
| 168 | print(f"✅ created_by field correctly set to 'user123'") |
| 169 | else: |
| 170 | print(f"❌ created_by field incorrect: {result.get('created_by')}") |
| 171 | |
| 172 | # Update the record as admin |
| 173 | headers = {"X-User": "admin"} |
| 174 | update_data = {"status": "inactive"} |
| 175 | |
| 176 | async with session.put(f"{BASE_URL}/api/customers/{record_id}", json=update_data, headers=headers) as resp2: |
| 177 | if resp2.status == 200: |
| 178 | updated = await resp2.json() |
| 179 | |
| 180 | # Check if updated_by field was set |
| 181 | if updated.get('updated_by') == 'admin': |
| 182 | print(f"✅ updated_by field correctly set to 'admin'") |
| 183 | else: |
| 184 | print(f"❌ updated_by field incorrect: {updated.get('updated_by')}") |
| 185 | |
| 186 | # Verify created_by didn't change |
| 187 | if updated.get('created_by') == 'user123': |
| 188 | print(f"✅ created_by field preserved as 'user123'") |
| 189 | else: |
| 190 | print(f"❌ created_by field changed: {updated.get('created_by')}") |
| 191 | else: |
| 192 | print(f"❌ Failed to create test record: {resp.status}") |
| 193 | |
| 194 | async def main(): |
| 195 | """Run all permission tests""" |
| 196 | print("=" * 60) |
| 197 | print("🚀 Testing Ownership-Based Permissions System") |
| 198 | print("=" * 60) |
| 199 | |
| 200 | try: |
| 201 | # Test public read |
| 202 | await test_public_read() |
| 203 | |
| 204 | # Test authenticated create and get record ID |
| 205 | record_id = await test_authenticated_create() |
| 206 | |
| 207 | # Test owner update permissions |
| 208 | await test_owner_update(record_id) |
| 209 | |
| 210 | # Test admin-only delete |
| 211 | await test_admin_delete(record_id) |
| 212 | |
| 213 | # Test ownership field tracking |
| 214 | await test_ownership_tracking() |
| 215 | |
| 216 | print("\n" + "=" * 60) |
| 217 | print("✅ All permission tests completed!") |
| 218 | print("=" * 60) |
| 219 | |
| 220 | except Exception as e: |
| 221 | print(f"\n❌ Test failed: {e}") |
| 222 | import traceback |
| 223 | traceback.print_exc() |
| 224 | sys.exit(1) |
| 225 | |
| 226 | if __name__ == "__main__": |
| 227 | asyncio.run(main()) |