Introduction
Imagine you have a building with many rooms, but only certain people can enter specific rooms. Authorization testing solves the problem of making sure that only the right people can access the right parts of a system or application.
Think of a hotel where guests have key cards that open only their rooms and some common areas. Authorization testing is like checking that guests cannot open other guests' rooms or restricted staff areas.
┌───────────────────────────────┐
│ User Login │
└──────────────┬────────────────┘
│ Authentication
↓
┌─────────────────────┐
│ Authorization Check │
└─────────┬───────────┘
│
┌────────────┴─────────────┐
│ │
Allowed Access Denied Access
│ │
↓ ↓
Access granted Access blocked
import unittest class AuthorizationTest(unittest.TestCase): def setUp(self): # Simulate user roles and permissions self.permissions = { 'admin': ['read', 'write', 'delete'], 'user': ['read'], 'guest': [] } def can_access(self, role, action): return action in self.permissions.get(role, []) def test_admin_access(self): self.assertTrue(self.can_access('admin', 'delete')) def test_user_no_delete(self): self.assertFalse(self.can_access('user', 'delete')) def test_guest_no_read(self): self.assertFalse(self.can_access('guest', 'read')) if __name__ == '__main__': unittest.main()