Test Data
client.testdata is available only on Environment.TEST.
It helps create disposable subjects, people, permissions, attachment state, and blocked auth contexts.
Create Subjects and People
Section titled “Create Subjects and People”from ksef2 import Client, Environment
client = Client(Environment.TEST)
client.testdata.create_subject( nip="1234567890", subject_type="enforcement_authority", description="Test organization",)
client.testdata.create_person( nip="1234567890", pesel="12345678901", description="Test person",)SDK endpoints:
POST /testdata/subjectPOST /testdata/person
Grant and Revoke Permissions
Section titled “Grant and Revoke Permissions”from ksef2.domain.models.testdata import Identifier, Permission
client.testdata.grant_permissions( permissions=[ Permission(type="invoice_read", description="Read invoices"), Permission(type="invoice_write", description="Send invoices"), ], grant_to=Identifier(type="nip", value="0987654321"), in_context_of=Identifier(type="nip", value="1234567890"),)
client.testdata.revoke_permissions( revoke_from=Identifier(type="nip", value="0987654321"), in_context_of=Identifier(type="nip", value="1234567890"),)SDK endpoints:
POST /testdata/permissionsPOST /testdata/permissions/revoke
Attachment Permissions
Section titled “Attachment Permissions”from datetime import date, timedelta
client.testdata.enable_attachments(nip="1234567890")client.testdata.revoke_attachments(nip="1234567890")client.testdata.revoke_attachments( nip="1234567890", expected_end_date=date.today() + timedelta(days=30),)SDK endpoints:
POST /testdata/attachmentPOST /testdata/attachment/revoke
Block and Unblock Auth Contexts
Section titled “Block and Unblock Auth Contexts”from ksef2.domain.models.testdata import AuthContextIdentifier
context = AuthContextIdentifier(type="nip", value="1234567890")client.testdata.block_context(context=context)client.testdata.unblock_context(context=context)SDK endpoints:
POST /testdata/context/blockPOST /testdata/context/unblock
Automatic Cleanup with temporal()
Section titled “Automatic Cleanup with temporal()”Use temporal() whenever possible so cleanup happens automatically:
from ksef2.core.tools import generate_nip, generate_peselfrom ksef2.domain.models.testdata import Identifier, Permission
org_nip = generate_nip()person_nip = generate_nip()person_pesel = generate_pesel()
with client.testdata.temporal() as temp: temp.create_subject( nip=org_nip, subject_type="enforcement_authority", description="Example organization", ) temp.create_person( nip=person_nip, pesel=person_pesel, description="Example person", ) temp.grant_permissions( permissions=[ Permission(type="invoice_read", description="Read invoices"), Permission(type="invoice_write", description="Send invoices"), ], grant_to=Identifier(type="nip", value=person_nip), in_context_of=Identifier(type="nip", value=org_nip), )
# tests or demos go hereExample: