Spaces:
Sleeping
Sleeping
File size: 8,130 Bytes
caf8059 | 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | import json
import unittest
from pathlib import Path
from src.modules.skill.domain.models import SkillError, digest, Draft
from src.modules.context.service import ContextService
from src.modules.project.service import ProjectAccess
from src.modules.artifact_catalog.service import ArtifactCatalog
from src.adapters.persistence.artifact_sqlite import SqliteArtifactRepository
from src.modules.governance.local_validation import LocalValidation
from src.adapters.llm.workos_credentials import workos_provider
from src.tests.runtime.fixtures import setup, workspace, AUTHOR, USER, OTHER, REVIEWER, through_execution
class KeyModulesTests(unittest.TestCase):
def stage(self, runtime, authorization):
doc = runtime.skills.read_draft(AUTHOR, 'erp-java-development')
body = {k: v for k, v in doc['body'].items() if k not in {'owner_id', 'last_editor_id'}}
draft = Draft.model_validate(body).model_copy(update={'project_id': 'fixture-project'})
runtime.skills.save_draft(AUTHOR, draft, doc['revision'])
return runtime.skills.stage_local_validation(AUTHOR, draft.skill_id, authorization)
def test_context_is_detached_and_binds_project_source_release(self):
doc = {'requirement': 'x'}
service = ContextService()
a = service.snapshot(doc, [], project_id='a', source_digest='1' * 64, release_digest='2' * 64)
b = service.snapshot(doc, [], project_id='b', source_digest='1' * 64, release_digest='2' * 64)
doc['requirement'] = 'changed'
self.assertEqual(a['document']['requirement'], 'x')
self.assertNotEqual(a['digest'], b['digest'])
with self.assertRaises(SkillError):
service.snapshot({'requirement': 'x' * 18000}, [], project_id='a', source_digest='1', release_digest='2')
def test_project_admin_does_not_imply_membership(self):
access = ProjectAccess({(USER.tenant_id, 'p'): [USER.user_id]})
access(USER, 'p')
for actor in [AUTHOR, OTHER]:
with self.assertRaises(SkillError):
access(actor, 'p')
def test_artifact_idempotence_tenant_boundary_and_corruption(self):
with workspace() as folder:
catalog = ArtifactCatalog(SqliteArtifactRepository(Path(folder) / 'evidence.sqlite'))
run_id = 'run-' + 'a' * 32
ref = catalog.record('tenant-a', run_id, {'source_digest': '1' * 64})
self.assertEqual(ref, catalog.record('tenant-a', run_id, {'source_digest': '1' * 64}))
with self.assertRaises(SkillError):
catalog.read('tenant-b', run_id, ref['digest'])
import sqlite3
with sqlite3.connect(catalog.repository.path) as db:
db.execute("UPDATE artifacts SET body='{}'")
with self.assertRaises(SkillError) as err:
catalog.read('tenant-a', run_id, ref['digest'])
self.assertEqual(err.exception.code, 'ARTIFACT_DIGEST_MISMATCH')
def test_fixed_tool_dispatch_and_source_validation(self):
runtime, request, _, sandbox = setup(self.enterContext(workspace()))
run = runtime.create(USER, request, 'tool-guard-key')
with self.assertRaises(SkillError):
runtime.tools.invoke('shell', run, {'command': 'untrusted'}, lambda: False)
with self.assertRaises(SkillError):
runtime.tools.invoke('AO08', run, {'source': {'files': {'../bad.java': 'x'}, 'source_digest': 'x'}}, lambda: False)
self.assertEqual(sandbox.calls, [])
def test_local_version_does_not_publish_or_allow_other_user(self):
runtime, request, _, _ = setup(self.enterContext(workspace()))
with self.assertRaises(SkillError) as err:
runtime.skills.stage_local_validation(AUTHOR, 'erp-java-development', 'user-delegation')
self.assertEqual(err.exception.code, 'VALIDATION_PROJECT_REQUIRED')
ref = self.stage(runtime, 'user-delegation')
self.assertIsNone(ref['body']['approval_id'])
self.assertFalse(ref['body']['human_review'])
self.assertNotIn(ref['key'], [x['key'] for x in runtime.skills.releases(AUTHOR)])
with self.assertRaises(SkillError):
runtime.create(USER, dict(request, release_ref=ref['key']), 'local-private-key')
with self.assertRaises(SkillError):
runtime.skills.stage_local_validation(USER, 'erp-java-development', 'user-delegation')
def test_delegation_stops_at_acceptance_and_archives_without_self_approval(self):
folder = self.enterContext(workspace())
runtime, request, _, _ = setup(folder)
runtime.artifacts = ArtifactCatalog(SqliteArtifactRepository(Path(folder) / 'artifacts.sqlite'))
ref = self.stage(runtime, 'user-delegation')
run = runtime.create(AUTHOR, dict(request, release_ref=ref['key']), 'delegation-key')
driver = LocalValidation(runtime, AUTHOR, 'user-delegation')
for _ in range(5):
run = driver.step(run['id'])
self.assertEqual(run['pending_approval']['scope'], 'acceptance')
self.assertEqual(driver.step(run['id'])['status'], 'waiting_approval')
evidence = runtime.validation_evidence(AUTHOR, run['id'])
self.assertEqual(evidence['manifest']['status'], 'verified_pending_user_acceptance')
self.assertFalse(evidence['manifest']['human_acceptance'])
self.assertEqual(len(evidence['manifest']['approvals']), 2)
self.assertTrue(all(x['mechanism'] == 'delegated_local_validation' for x in evidence['manifest']['approvals']))
with self.assertRaises(SkillError):
runtime.approve(AUTHOR, run['id'], run['pending_approval']['digest'], delegation='user-delegation')
def test_wrong_delegation_and_premature_evidence_denied(self):
runtime, request, _, _ = setup(self.enterContext(workspace()))
ref = self.stage(runtime, 'scope-a')
run = runtime.create(AUTHOR, dict(request, release_ref=ref['key']), 'delegation-key')
with self.assertRaises(SkillError):
runtime.validation_evidence(AUTHOR, run['id'])
runtime.advance(AUTHOR, run['id'])
with self.assertRaises(SkillError):
LocalValidation(runtime, AUTHOR, 'scope-b').step(run['id'])
def test_readiness_requires_admin_and_does_not_claim_live_health(self):
runtime, _, _, _ = setup(self.enterContext(workspace()))
with self.assertRaises(SkillError):
runtime.readiness(USER)
self.assertFalse(runtime.readiness(AUTHOR)['production_accepted'])
self.assertEqual(runtime.readiness(AUTHOR)['live_provider_health'], 'not_probed')
class CredentialTests(unittest.TestCase):
def test_named_non_sk_token_wins_over_comment_example(self):
with workspace() as folder:
path = Path(folder) / 'fixture.env'
path.write_text('DASHSCOPE_API_KEY=synthetic-provider-token\n# example: sk-example-not-real\n')
provider = workos_provider(path)
self.assertEqual(provider._token, 'synthetic-provider-token')
self.assertFalse(provider.enable_thinking)
self.assertEqual(path.read_text(), 'DASHSCOPE_API_KEY=synthetic-provider-token\n# example: sk-example-not-real\n')
def test_ambiguous_keys_and_foreign_host_rejected_without_secret(self):
with workspace() as folder:
path = Path(folder) / 'fixture.env'
for content in ['sk-oneexample sk-twoexample',
'DASHSCOPE_API_KEY=synthetic-secret\nDASHSCOPE_BASE_URL=https://evil.example/compatible-mode/v1']:
path.write_text(content)
with self.assertRaises(ValueError) as err:
workos_provider(path)
self.assertEqual(str(err.exception), 'WORKOS_CREDENTIAL_CONFIGURATION_INVALID')
def test_named_token_is_not_replaced_when_invalid(self):
with workspace() as folder:
path = Path(folder) / 'fixture.env'
path.write_text('DASHSCOPE_API_KEY=not a valid token\n# sk-example')
with self.assertRaises(ValueError):
workos_provider(path)
|