Some checks failed
Tests / 🛃 Unit tests (push) Failing after 13s
Tests / 🗄 DB tests (push) Failing after 19s
Tests / 🐍 Lint python scripts (push) Failing after 1s
Tests / ⌨ Golang Lint (push) Failing after 1s
Tests / 📦 Packit config lint (push) Failing after 1s
Tests / 🔍 Check source preparation (push) Failing after 1s
Tests / 🔍 Check for valid snapshot urls (push) Failing after 1s
Tests / 🔍 Check for missing or unused runner repos (push) Failing after 1s
Tests / 🐚 Shellcheck (push) Failing after 1s
Tests / 📦 RPMlint (push) Failing after 1s
Tests / Gitlab CI trigger helper (push) Failing after 1s
Tests / 🎀 kube-linter (push) Failing after 1s
Tests / 🧹 cloud-cleaner-is-enabled (push) Successful in 3s
Tests / 🔍 Check spec file osbuild/images dependencies (push) Failing after 1s
257 lines
8.8 KiB
Python
257 lines
8.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for Debian Forge Simplified Composer Client
|
|
|
|
This script tests the simplified composer client functionality including:
|
|
- User authentication
|
|
- Permission-based access control
|
|
- Simulated API operations
|
|
"""
|
|
|
|
from composer_client_simple import ComposerClientSimple, BuildRequest
|
|
from user_management import UserManager
|
|
import os
|
|
|
|
def test_simplified_composer():
|
|
"""Test the simplified composer client with authentication"""
|
|
|
|
# Use a test database
|
|
test_db = "test_users.db"
|
|
|
|
# Clean up any existing test database
|
|
if os.path.exists(test_db):
|
|
os.remove(test_db)
|
|
|
|
print("🧪 Testing Debian Forge Simplified Composer Client")
|
|
print("=" * 60)
|
|
|
|
# Initialize user manager and create test users
|
|
user_mgr = UserManager(test_db)
|
|
|
|
# Create test users
|
|
user_mgr.create_user("admin", "admin@debian-forge.org", "admin123", "admin")
|
|
user_mgr.create_user("user1", "user1@debian-forge.org", "user123", "user")
|
|
user_mgr.create_user("viewer1", "viewer1@debian-forge.org", "viewer123", "viewer")
|
|
|
|
print("✅ Test users created")
|
|
|
|
# Verify users were created
|
|
users = user_mgr.list_users()
|
|
print(f"✅ Created {len(users)} users: {[u.username for u in users]}")
|
|
|
|
# Test authentication directly
|
|
admin_user = user_mgr.authenticate_user("admin", "admin123")
|
|
print(f"✅ Direct admin auth test: {'PASSED' if admin_user else 'FAILED'}")
|
|
|
|
# Test 1: Composer client without authentication
|
|
print("\n1. Testing composer client without authentication...")
|
|
|
|
client_no_auth = ComposerClientSimple()
|
|
|
|
# Should not have any permissions
|
|
if not client_no_auth.check_permission("read"):
|
|
print(" ✅ Unauthenticated client correctly has no permissions")
|
|
else:
|
|
print(" ❌ Unauthenticated client incorrectly has permissions")
|
|
return False
|
|
|
|
# Test 2: Composer client with admin authentication
|
|
print("\n2. Testing composer client with admin authentication...")
|
|
|
|
client_admin = ComposerClientSimple(username="admin", password="admin123")
|
|
|
|
# Admin should have all permissions
|
|
if client_admin.check_permission("read"):
|
|
print(" ✅ Admin client can read")
|
|
else:
|
|
print(" ❌ Admin client cannot read")
|
|
return False
|
|
|
|
if client_admin.check_permission("build"):
|
|
print(" ✅ Admin client can build")
|
|
else:
|
|
print(" ❌ Admin client cannot build")
|
|
return False
|
|
|
|
if client_admin.check_permission("admin"):
|
|
print(" ✅ Admin client can admin")
|
|
else:
|
|
print(" ❌ Admin client cannot admin")
|
|
return False
|
|
|
|
# Test 3: Composer client with user authentication
|
|
print("\n3. Testing composer client with user authentication...")
|
|
|
|
client_user = ComposerClientSimple(username="user1", password="user123")
|
|
|
|
# User should have read and build permissions
|
|
if client_user.check_permission("read"):
|
|
print(" ✅ User client can read")
|
|
else:
|
|
print(" ❌ User client cannot read")
|
|
return False
|
|
|
|
if client_user.check_permission("build"):
|
|
print(" ✅ User client can build")
|
|
else:
|
|
print(" ❌ User client cannot build")
|
|
return False
|
|
|
|
if not client_user.check_permission("admin"):
|
|
print(" ✅ User client correctly cannot admin")
|
|
else:
|
|
print(" ❌ User client incorrectly can admin")
|
|
return False
|
|
|
|
# Test 4: Composer client with viewer authentication
|
|
print("\n4. Testing composer client with viewer authentication...")
|
|
|
|
client_viewer = ComposerClientSimple(username="viewer1", password="viewer123")
|
|
|
|
# Viewer should only have read permissions
|
|
if client_viewer.check_permission("read"):
|
|
print(" ✅ Viewer client can read")
|
|
else:
|
|
print(" ❌ Viewer client cannot read")
|
|
return False
|
|
|
|
if not client_viewer.check_permission("build"):
|
|
print(" ✅ Viewer client correctly cannot build")
|
|
else:
|
|
print(" ❌ Viewer client incorrectly can build")
|
|
return False
|
|
|
|
# Test 5: API operations with permissions
|
|
print("\n5. Testing API operations with permissions...")
|
|
|
|
# Create a test blueprint file
|
|
test_blueprint = "test-blueprint.json"
|
|
with open(test_blueprint, 'w') as f:
|
|
f.write('{"name": "test-blueprint", "version": "0.0.1"}')
|
|
|
|
# Admin should be able to submit blueprint
|
|
try:
|
|
result = client_admin.submit_blueprint(test_blueprint)
|
|
print(f" ✅ Admin can submit blueprint: {result['message']}")
|
|
except PermissionError:
|
|
print(" ❌ Admin cannot submit blueprint (permission check failed)")
|
|
return False
|
|
|
|
# Admin should be able to list blueprints
|
|
try:
|
|
blueprints = client_admin.list_blueprints()
|
|
print(f" ✅ Admin can list blueprints: {len(blueprints)} found")
|
|
except PermissionError:
|
|
print(" ❌ Admin cannot list blueprints (permission check failed)")
|
|
return False
|
|
|
|
# User should be able to submit blueprint
|
|
try:
|
|
result = client_user.submit_blueprint(test_blueprint)
|
|
print(f" ✅ User can submit blueprint: {result['message']}")
|
|
except PermissionError:
|
|
print(" ❌ User cannot submit blueprint (permission check failed)")
|
|
return False
|
|
|
|
# Viewer should not be able to submit blueprint
|
|
try:
|
|
client_viewer.submit_blueprint(test_blueprint)
|
|
print(" ❌ Viewer incorrectly can submit blueprint")
|
|
return False
|
|
except PermissionError:
|
|
print(" ✅ Viewer correctly cannot submit blueprint")
|
|
|
|
# Test 6: Build operations
|
|
print("\n6. Testing build operations...")
|
|
|
|
# Create build request
|
|
build_req = BuildRequest(
|
|
blueprint="debian-atomic-base",
|
|
target="qcow2",
|
|
architecture="amd64"
|
|
)
|
|
|
|
# Admin should be able to start compose
|
|
try:
|
|
compose_id = client_admin.start_compose(build_req)
|
|
print(f" ✅ Admin can start compose: {compose_id}")
|
|
except PermissionError:
|
|
print(" ❌ Admin cannot start compose (permission check failed)")
|
|
return False
|
|
|
|
# User should be able to start compose
|
|
try:
|
|
compose_id = client_user.start_compose(build_req)
|
|
print(f" ✅ User can start compose: {compose_id}")
|
|
except PermissionError:
|
|
print(" ❌ User cannot start compose (permission check failed)")
|
|
return False
|
|
|
|
# Viewer should not be able to start compose
|
|
try:
|
|
client_viewer.start_compose(build_req)
|
|
print(" ❌ Viewer incorrectly can start compose")
|
|
return False
|
|
except PermissionError:
|
|
print(" ✅ Viewer correctly cannot start compose")
|
|
|
|
# Test 7: System status and monitoring
|
|
print("\n7. Testing system status and monitoring...")
|
|
|
|
# All authenticated users should be able to read system status
|
|
for client_name, client in [("Admin", client_admin), ("User", client_user), ("Viewer", client_viewer)]:
|
|
try:
|
|
status = client.get_system_status()
|
|
print(f" ✅ {client_name} can read system status: {status['status']}")
|
|
except PermissionError:
|
|
print(f" ❌ {client_name} cannot read system status")
|
|
return False
|
|
|
|
# Test 8: Dynamic authentication
|
|
print("\n8. Testing dynamic authentication...")
|
|
|
|
client_dynamic = ComposerClientSimple()
|
|
|
|
# Initially no permissions
|
|
if not client_dynamic.check_permission("read"):
|
|
print(" ✅ Dynamic client initially has no permissions")
|
|
else:
|
|
print(" ❌ Dynamic client initially has permissions")
|
|
return False
|
|
|
|
# Authenticate as admin
|
|
client_dynamic.authenticate("admin", "admin123")
|
|
|
|
# Now should have admin permissions
|
|
if client_dynamic.check_permission("admin"):
|
|
print(" ✅ Dynamic client can admin after authentication")
|
|
else:
|
|
print(" ❌ Dynamic client cannot admin after authentication")
|
|
return False
|
|
|
|
# Clean up test files
|
|
if os.path.exists(test_blueprint):
|
|
os.remove(test_blueprint)
|
|
|
|
print("\n" + "=" * 60)
|
|
print("🎉 All simplified composer client tests passed successfully!")
|
|
|
|
# Clean up test database
|
|
if os.path.exists(test_db):
|
|
os.remove(test_db)
|
|
print("🧹 Test database cleaned up")
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
success = test_simplified_composer()
|
|
if success:
|
|
print("\n✅ Simplified composer client is working correctly")
|
|
exit(0)
|
|
else:
|
|
print("\n❌ Simplified composer client has issues")
|
|
exit(1)
|
|
except Exception as e:
|
|
print(f"\n💥 Test failed with exception: {e}")
|
|
exit(1)
|