Skip to content

Test Data

client.testdata is available only on Environment.TEST. It helps create disposable subjects, people, permissions, attachment state, and blocked auth contexts.

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/subject
  • POST /testdata/person
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/permissions
  • POST /testdata/permissions/revoke
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/attachment
  • POST /testdata/attachment/revoke
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/block
  • POST /testdata/context/unblock

Use temporal() whenever possible so cleanup happens automatically:

from ksef2.core.tools import generate_nip, generate_pesel
from 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 here

Example: