fix: make client integration kit self-contained and clarify capabilities
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-13 15:22:09 +08:00
parent d42067746c
commit 5353a4a128
15 changed files with 198 additions and 32 deletions

View File

@@ -25,7 +25,7 @@ class SDKTests(unittest.TestCase):
self.public = self.private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()
self.now = __import__('time').time()
stamp = datetime.fromtimestamp(self.now, timezone.utc).isoformat().replace('+00:00', 'Z')
self.payload = {"version":"update-v1","instance_id":"i1","product_id":1,"customer_id":2,"current_version":"1.0","platform":"linux","arch":"x64","channel":"stable","server_time":stamp,"expires_at":datetime.fromtimestamp(self.now+600, timezone.utc).isoformat().replace('+00:00','Z'),"policy_revision":3,"policy_enabled":True,"recommended_version":"2.0","minimum_supported_version":"1.0","enforce_after":None,"mandatory":False,"required_now":False,"update_available":True,"download_allowed":True,"release_id":7,"sha256":hashlib.sha256(b'good').hexdigest(),"size_bytes":4,"message":""}
self.payload = {"version":"update-v1","instance_id":"i1","product_id":1,"customer_id":2,"current_version":"1.0.0","platform":"linux","arch":"x64","channel":"stable","server_time":stamp,"expires_at":datetime.fromtimestamp(self.now+600, timezone.utc).isoformat().replace('+00:00','Z'),"policy_revision":3,"policy_enabled":True,"recommended_version":"2.0.0","minimum_supported_version":"1.0.0","enforce_after":"","mandatory":False,"required_now":False,"update_available":True,"download_allowed":True,"release_id":7,"sha256":hashlib.sha256(b'good').hexdigest(),"size_bytes":4,"message":""}
self.envelope = self.sign(self.payload)
def sign(self, payload):
@@ -34,7 +34,7 @@ class SDKTests(unittest.TestCase):
return {"payload":base64.b64encode(raw).decode().rstrip('='),"signature":base64.b64encode(sig).decode().rstrip('='),"key_id":"k1"}
def test_signature_context_expiry_and_rollback(self):
verify_check(self.envelope, {"k1":self.public}, "i1", now=self.now, expected_platform="linux", expected_arch="x64", expected_channel="stable", expected_current_version="1.0")
verify_check(self.envelope, {"k1":self.public}, "i1", now=self.now, expected_platform="linux", expected_arch="x64", expected_channel="stable", expected_current_version="1.0.0")
tampered = dict(self.envelope); tampered["payload"] = base64.b64encode(b'{"version":"update-v1"}').decode().rstrip('=')
with self.assertRaises(UpdateError): verify_check(tampered, {"k1":self.public}, "i1", now=self.now)
with self.assertRaises(UpdateError): verify_check(self.envelope, {"k1":self.public}, "wrong", now=self.now)
@@ -65,7 +65,7 @@ class SDKTests(unittest.TestCase):
def test_scoped_revision_does_not_leak_between_platforms(self):
state = MemoryState({"scope": '["i1","windows","x64","stable"]', "policy_revision":99})
sdk = self.client(lambda req: httpx.Response(200, json={"data":self.envelope}), state)
result = sdk.check('1.0','linux','x64','stable')
result = sdk.check('1.0.0','linux','x64','stable')
self.assertEqual(result.payload['policy_revision'], 3)