-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_user.py
More file actions
114 lines (103 loc) · 3.47 KB
/
test_api_user.py
File metadata and controls
114 lines (103 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
Test script simulating a first-time user of the CompZ SDK.
"""
import json
# Test 1: Import the SDK
print("=" * 60)
print("TEST 1: Importing CompZ SDK")
print("=" * 60)
try:
from compz import CompZClient, ComplianceResult, ControlEvaluation
print("✅ Successfully imported CompZ SDK")
print(f" - CompZClient: {CompZClient}")
print(f" - ComplianceResult: {ComplianceResult}")
print(f" - ControlEvaluation: {ControlEvaluation}")
except Exception as e:
print(f"❌ Failed to import: {e}")
exit(1)
# Test 2: Create a simple compliance result
print("\n" + "=" * 60)
print("TEST 2: Creating ComplianceResult")
print("=" * 60)
try:
test_data = {
"repo_id": "test/repo",
"commit_hash": "abc123",
"frameworks": ["PCI-DSS"],
"control_evaluations": [
{
"control_id": "PCI-1.1.1",
"framework": "PCI-DSS",
"passed": True,
"reason": "Test control passed",
"severity": "high"
}
],
"risk_score": 25.0,
"timestamp": "2024-11-28T00:00:00Z"
}
result = ComplianceResult(**test_data)
print("✅ Successfully created ComplianceResult")
print(f" - repo_id: {result.repo_id}")
print(f" - frameworks: {result.frameworks}")
print(f" - risk_score: {result.risk_score}")
except Exception as e:
print(f"❌ Failed to create ComplianceResult: {e}")
exit(1)
# Test 3: Initialize CompZClient
print("\n" + "=" * 60)
print("TEST 3: Initializing CompZClient (default mode)")
print("=" * 60)
try:
client = CompZClient()
print("✅ Successfully initialized CompZClient")
status = client.get_status()
print(f" - Mode: {status['mode']}")
print(f" - Version: {status['version']}")
except Exception as e:
print(f"❌ Failed to initialize client: {e}")
exit(1)
# Test 4: Test hashing functionality
print("\n" + "=" * 60)
print("TEST 4: Testing hash functionality")
print("=" * 60)
try:
from compz.normalize import normalize_json
from compz.hash import hash_compliance
normalized = normalize_json(test_data)
print(f"✅ Normalized JSON ({len(normalized)} bytes)")
comp_hash = hash_compliance(normalized)
print(f"✅ Generated hash: {comp_hash[:50]}...")
except Exception as e:
print(f"❌ Hash test failed: {e}")
exit(1)
# Test 5: Try anchor (should fail gracefully without blockchain)
print("\n" + "=" * 60)
print("TEST 5: Testing anchor (expected to fail without RPC)")
print("=" * 60)
try:
anchor_result = client.anchor(test_data)
print(f"❌ Unexpected success: {anchor_result}")
except RuntimeError as e:
print(f"✅ Got expected error with helpful message:")
print(f" {str(e)[:100]}...")
except Exception as e:
print(f"⚠️ Got different error: {e}")
# Test 6: Try verify (should fail gracefully)
print("\n" + "=" * 60)
print("TEST 6: Testing verify (expected to fail without blockchain)")
print("=" * 60)
try:
verify_result = client.verify(test_data, "fake_txid_123")
print(f"❌ Unexpected success: {verify_result}")
except RuntimeError as e:
print(f"✅ Got expected error with helpful message:")
print(f" {str(e)[:100]}...")
except Exception as e:
print(f"⚠️ Got different error: {e}")
print("\n" + "=" * 60)
print("SUMMARY: All API tests completed")
print("=" * 60)
print("✅ SDK is importable and functional")
print("✅ Error messages are helpful")
print("✅ Core hashing works without blockchain")