askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_composition.py · 5.2 KB · 173 lines
| 1 | """ |
| 2 | Test Phase 5: Object Composition |
| 3 | |
| 4 | Tests for objects calling other objects (_runtime injection). |
| 5 | """ |
| 6 | |
| 7 | import requests |
| 8 | import json |
| 9 | |
| 10 | BASE_URL = 'http://localhost:8001' |
| 11 | |
| 12 | |
| 13 | def test_blog_auth_integration(): |
| 14 | """Test that blog can call auth object for token validation""" |
| 15 | print('\n=== Test 1: Blog-Auth Integration ===') |
| 16 | |
| 17 | # First, create a user in auth system |
| 18 | response = requests.post( |
| 19 | f'{BASE_URL}/objects/advanced_auth', |
| 20 | json={ |
| 21 | 'action': 'register', |
| 22 | 'username': 'testuser', |
| 23 | 'password': 'testpass123', |
| 24 | 'email': 'test@example.com', |
| 25 | } |
| 26 | ) |
| 27 | auth_data = response.json() |
| 28 | print(f"User registered: {auth_data['status']}") |
| 29 | assert auth_data['status'] == 'ok' |
| 30 | token = auth_data['token'] |
| 31 | print(f"Token received: {token[:20]}...") |
| 32 | |
| 33 | # Now try to create a blog post with this token |
| 34 | response = requests.post( |
| 35 | f'{BASE_URL}/objects/advanced_blog', |
| 36 | json={ |
| 37 | 'token': token, |
| 38 | 'title': 'Test Post', |
| 39 | 'content': 'This post was created with real auth integration!', |
| 40 | 'tags': ['test', 'integration'], |
| 41 | } |
| 42 | ) |
| 43 | blog_data = response.json() |
| 44 | |
| 45 | print(f"\nBlog post creation:") |
| 46 | print(f" Status: {blog_data.get('status')}") |
| 47 | print(f" Post ID: {blog_data.get('post_id', 'N/A')}") |
| 48 | |
| 49 | assert blog_data['status'] == 'ok' |
| 50 | assert 'post_id' in blog_data |
| 51 | |
| 52 | # Verify the post was created by the right user |
| 53 | post_id = blog_data['post_id'] |
| 54 | response = requests.get( |
| 55 | f'{BASE_URL}/objects/advanced_blog', |
| 56 | params={'id': post_id} |
| 57 | ) |
| 58 | post_data = response.json() |
| 59 | |
| 60 | print(f"\nPost verification:") |
| 61 | print(f" Author: {post_data['post']['author']}") |
| 62 | print(f" Title: {post_data['post']['title']}") |
| 63 | |
| 64 | assert post_data['post']['author'] == 'testuser' # Should be real username, not demo_user! |
| 65 | |
| 66 | print('\n✅ Blog-Auth integration works (object composition successful!)') |
| 67 | |
| 68 | |
| 69 | def test_invalid_token(): |
| 70 | """Test that blog rejects invalid tokens""" |
| 71 | print('\n=== Test 2: Invalid Token Rejection ===') |
| 72 | |
| 73 | # Try to create post with invalid token |
| 74 | response = requests.post( |
| 75 | f'{BASE_URL}/objects/advanced_blog', |
| 76 | json={ |
| 77 | 'token': 'invalid_token_xyz', |
| 78 | 'title': 'Should Fail', |
| 79 | 'content': 'This should not work', |
| 80 | } |
| 81 | ) |
| 82 | data = response.json() |
| 83 | |
| 84 | print(f"Invalid token response:") |
| 85 | print(f" Status: {data.get('status')}") |
| 86 | print(f" Message: {data.get('message', 'N/A')}") |
| 87 | |
| 88 | # Should be rejected OR use fallback demo_user |
| 89 | # If using fallback, author will be demo_user |
| 90 | # If rejected, status will be error |
| 91 | if data['status'] == 'error': |
| 92 | print(' ✓ Token rejected (strict mode)') |
| 93 | else: |
| 94 | # Fallback to demo_user |
| 95 | post_id = data.get('post_id') |
| 96 | if post_id: |
| 97 | response = requests.get( |
| 98 | f'{BASE_URL}/objects/advanced_blog', |
| 99 | params={'id': post_id} |
| 100 | ) |
| 101 | post_data = response.json() |
| 102 | print(f" ✓ Fallback used (author: {post_data['post']['author']})") |
| 103 | |
| 104 | print('\n✅ Token validation works') |
| 105 | |
| 106 | |
| 107 | def test_cross_object_state(): |
| 108 | """Test that auth and blog maintain separate state""" |
| 109 | print('\n=== Test 3: Separate Object State ===') |
| 110 | |
| 111 | # Check auth state (users, tokens) |
| 112 | response = requests.get( |
| 113 | f'{BASE_URL}/objects/advanced_auth', |
| 114 | params={'metadata': 'true'} |
| 115 | ) |
| 116 | auth_meta = response.json() |
| 117 | |
| 118 | print(f"Auth object state keys: {auth_meta['metadata'].get('state_keys', [])}") |
| 119 | assert 'users' in auth_meta['metadata'].get('state_keys', []) |
| 120 | assert 'tokens' in auth_meta['metadata'].get('state_keys', []) |
| 121 | |
| 122 | # Check blog state (posts) |
| 123 | response = requests.get( |
| 124 | f'{BASE_URL}/objects/advanced_blog', |
| 125 | params={'metadata': 'true'} |
| 126 | ) |
| 127 | blog_meta = response.json() |
| 128 | |
| 129 | print(f"Blog object state keys: {blog_meta['metadata'].get('state_keys', [])}") |
| 130 | assert 'posts' in blog_meta['metadata'].get('state_keys', []) |
| 131 | |
| 132 | # Verify they're separate (no overlap) |
| 133 | auth_keys = set(auth_meta['metadata'].get('state_keys', [])) |
| 134 | blog_keys = set(blog_meta['metadata'].get('state_keys', [])) |
| 135 | |
| 136 | print(f"\nState separation:") |
| 137 | print(f" Auth-only keys: {auth_keys - blog_keys}") |
| 138 | print(f" Blog-only keys: {blog_keys - auth_keys}") |
| 139 | |
| 140 | assert len(auth_keys & blog_keys) == 0 # No overlap |
| 141 | |
| 142 | print('\n✅ Objects maintain separate state') |
| 143 | |
| 144 | |
| 145 | if __name__ == '__main__': |
| 146 | print('Testing Phase 5: Object Composition') |
| 147 | print('=' * 50) |
| 148 | |
| 149 | try: |
| 150 | # Test auth-blog integration |
| 151 | test_blog_auth_integration() |
| 152 | |
| 153 | # Test invalid token handling |
| 154 | test_invalid_token() |
| 155 | |
| 156 | # Test state separation |
| 157 | test_cross_object_state() |
| 158 | |
| 159 | print('\n' + '=' * 50) |
| 160 | print('✅ All composition tests passed!') |
| 161 | print('=' * 50) |
| 162 | |
| 163 | except AssertionError as e: |
| 164 | print(f'\n❌ Test failed: {e}') |
| 165 | raise |
| 166 | except requests.exceptions.ConnectionError: |
| 167 | print('\n❌ Error: Server not running') |
| 168 | print('Start server with: ./server.sh start') |
| 169 | except Exception as e: |
| 170 | print(f'\n❌ Unexpected error: {e}') |
| 171 | import traceback |
| 172 | traceback.print_exc() |
| 173 | raise |