Vaihe 1/2: asiakkaat, yhteyshenkilöt ja laskutusyksiköt
- customers-moduuli Clean Architecture -kerroksin: asiakas (yritys/kuluttaja), Y-tunnuksen tarkiste ja yksilöllisyys, yhteyshenkilöt, laskutusyksiköt (verkkolaskuosoite vaatii välittäjätunnuksen) - henkilötunnus salattuna (Fernet) ja HMAC-tiiviste päällekkäisyyksiin; pääkäyttäjä ja johto näkevät ja poistavat, muut näkevät peitettynä - sisäinen asiakas data-migraatiolla, ei poistettavissa - yleinen poistonesto käänteisten relaatioiden perusteella (tulevat PROTECT-viiteavaimet estävät poiston automaattisesti) - asiakkaan sivu: lisäys ja muokkaus dialogissa HTMX:llä, fokuksen hallinta ja ruudunlukijailmoitukset; HTMX näyttää nyt 422-validointivirheet - testit: domain, application, integraatio ja selaintestit (axe); yhteiset fixturet apps/conftest.py:hin, FakeUnitOfWork shared-moduuliin - import-linter: moduulien riippumattomuussäännöt - dokumentit päivitetty Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
from dataclasses import replace
|
||||
|
||||
import pytest
|
||||
|
||||
from apps.customers.application.dto import BillingUnitData, ContactData, CustomerDetailsData
|
||||
from apps.customers.application.use_cases import (
|
||||
AddBillingUnit,
|
||||
AddContact,
|
||||
DeleteContact,
|
||||
DeleteCustomer,
|
||||
RegisterCustomer,
|
||||
UpdateBillingUnit,
|
||||
UpdateContact,
|
||||
UpdateCustomer,
|
||||
)
|
||||
from apps.customers.domain.enums import CustomerType
|
||||
from apps.customers.domain.exceptions import (
|
||||
BillingUnitNotFound,
|
||||
BusinessIdTaken,
|
||||
CompanyCannotHavePersonalId,
|
||||
ContactNotFound,
|
||||
CustomerInUse,
|
||||
CustomerNotFound,
|
||||
InternalCustomerCannotBeDeleted,
|
||||
PersonalIdRemovalNotAllowed,
|
||||
PersonalIdTaken,
|
||||
)
|
||||
from apps.customers.tests.fakes import (
|
||||
InMemoryBillingUnitRepository,
|
||||
InMemoryContactRepository,
|
||||
InMemoryCustomerRepository,
|
||||
)
|
||||
from apps.shared.tests.fakes import FakeUnitOfWork
|
||||
|
||||
PIC = "131052-308T"
|
||||
PIC2 = "010594Y9032"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repo() -> InMemoryCustomerRepository:
|
||||
return InMemoryCustomerRepository()
|
||||
|
||||
|
||||
def company(**kw: object) -> CustomerDetailsData:
|
||||
return CustomerDetailsData(**{"name": "Oy Esimerkki Ab", "type": CustomerType.COMPANY, **kw}) # type: ignore[arg-type]
|
||||
|
||||
|
||||
def consumer(**kw: object) -> CustomerDetailsData:
|
||||
return CustomerDetailsData(**{"name": "Kalle Kuluttaja", "type": CustomerType.CONSUMER, **kw}) # type: ignore[arg-type]
|
||||
|
||||
|
||||
def register(repo: InMemoryCustomerRepository, data: CustomerDetailsData) -> int:
|
||||
return RegisterCustomer(repo, FakeUnitOfWork()).execute(data)
|
||||
|
||||
|
||||
class TestRegister:
|
||||
def test_business_id_is_unique(self, repo: InMemoryCustomerRepository) -> None:
|
||||
register(repo, company(business_id="0112038-9"))
|
||||
with pytest.raises(BusinessIdTaken):
|
||||
register(repo, company(name="Toinen", business_id="01120389"))
|
||||
|
||||
def test_personal_id_is_stored_and_unique(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, consumer(personal_identity_code=PIC))
|
||||
assert repo.personal_ids[cid] == PIC
|
||||
assert repo.get(cid).has_personal_id # type: ignore[union-attr]
|
||||
with pytest.raises(PersonalIdTaken):
|
||||
register(repo, consumer(name="Toinen", personal_identity_code=PIC.lower()))
|
||||
|
||||
def test_company_cannot_get_personal_id(self, repo: InMemoryCustomerRepository) -> None:
|
||||
with pytest.raises(CompanyCannotHavePersonalId):
|
||||
register(repo, company(personal_identity_code=PIC))
|
||||
|
||||
|
||||
class TestUpdatePersonalId:
|
||||
def update(
|
||||
self,
|
||||
repo: InMemoryCustomerRepository,
|
||||
cid: int,
|
||||
data: CustomerDetailsData,
|
||||
manage: bool = False,
|
||||
) -> None:
|
||||
UpdateCustomer(repo, FakeUnitOfWork()).execute(cid, data, can_manage_personal_id=manage)
|
||||
|
||||
def test_empty_field_keeps_code(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, consumer(personal_identity_code=PIC))
|
||||
self.update(repo, cid, consumer(name="Kalle K."))
|
||||
assert repo.personal_ids[cid] == PIC
|
||||
|
||||
def test_new_value_replaces_code(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, consumer(personal_identity_code=PIC))
|
||||
self.update(repo, cid, consumer(personal_identity_code=PIC2))
|
||||
assert repo.personal_ids[cid] == PIC2
|
||||
|
||||
def test_only_admin_or_management_may_remove(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, consumer(personal_identity_code=PIC))
|
||||
with pytest.raises(PersonalIdRemovalNotAllowed):
|
||||
self.update(repo, cid, consumer(remove_personal_id=True))
|
||||
self.update(repo, cid, consumer(remove_personal_id=True), manage=True)
|
||||
assert cid not in repo.personal_ids
|
||||
|
||||
def test_becoming_company_removes_code(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, consumer(personal_identity_code=PIC))
|
||||
self.update(repo, cid, company(name="Kalle Oy"))
|
||||
assert cid not in repo.personal_ids
|
||||
assert repo.get(cid).type is CustomerType.COMPANY # type: ignore[union-attr]
|
||||
|
||||
def test_unknown_customer(self, repo: InMemoryCustomerRepository) -> None:
|
||||
with pytest.raises(CustomerNotFound):
|
||||
self.update(repo, 99, company())
|
||||
|
||||
|
||||
class TestDelete:
|
||||
def test_internal_customer_cannot_be_deleted(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, company())
|
||||
repo.customers[cid] = replace(repo.customers[cid], is_internal=True)
|
||||
with pytest.raises(InternalCustomerCannotBeDeleted):
|
||||
DeleteCustomer(repo, FakeUnitOfWork()).execute(cid)
|
||||
|
||||
def test_referenced_customer_cannot_be_deleted(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, company())
|
||||
repo.references.add(cid)
|
||||
with pytest.raises(CustomerInUse):
|
||||
DeleteCustomer(repo, FakeUnitOfWork()).execute(cid)
|
||||
|
||||
def test_delete(self, repo: InMemoryCustomerRepository) -> None:
|
||||
cid = register(repo, company())
|
||||
DeleteCustomer(repo, FakeUnitOfWork()).execute(cid)
|
||||
assert repo.get(cid) is None
|
||||
|
||||
|
||||
class TestContactsAndBillingUnits:
|
||||
def test_contact_lifecycle_and_ownership(self, repo: InMemoryCustomerRepository) -> None:
|
||||
contacts = InMemoryContactRepository()
|
||||
a = register(repo, company())
|
||||
b = register(repo, company(name="Toinen"))
|
||||
contact_id = AddContact(repo, contacts, FakeUnitOfWork()).execute(
|
||||
a, ContactData(name="Maija", email="[email protected]")
|
||||
)
|
||||
assert contacts.items[contact_id].email.value == "[email protected]" # type: ignore[union-attr]
|
||||
with pytest.raises(ContactNotFound): # belongs to customer a, not b
|
||||
UpdateContact(repo, contacts, FakeUnitOfWork()).execute(
|
||||
b, contact_id, ContactData(name="X")
|
||||
)
|
||||
with pytest.raises(ContactNotFound):
|
||||
DeleteContact(repo, contacts, FakeUnitOfWork()).execute(b, contact_id)
|
||||
DeleteContact(repo, contacts, FakeUnitOfWork()).execute(a, contact_id)
|
||||
assert contacts.items == {}
|
||||
|
||||
def test_contact_for_unknown_customer(self, repo: InMemoryCustomerRepository) -> None:
|
||||
with pytest.raises(CustomerNotFound):
|
||||
AddContact(repo, InMemoryContactRepository(), FakeUnitOfWork()).execute(
|
||||
5, ContactData(name="Maija")
|
||||
)
|
||||
|
||||
def test_billing_unit(self, repo: InMemoryCustomerRepository) -> None:
|
||||
units = InMemoryBillingUnitRepository()
|
||||
a = register(repo, company())
|
||||
unit_id = AddBillingUnit(repo, units, FakeUnitOfWork()).execute(
|
||||
a,
|
||||
BillingUnitData(
|
||||
name="Pääkonttori",
|
||||
e_invoice_address="0037 0112 0389",
|
||||
e_invoice_operator="003721291126",
|
||||
),
|
||||
)
|
||||
assert units.items[unit_id].e_invoice_address.value == "003701120389" # type: ignore[union-attr]
|
||||
with pytest.raises(BillingUnitNotFound):
|
||||
UpdateBillingUnit(repo, units, FakeUnitOfWork()).execute(
|
||||
a + 1, unit_id, BillingUnitData(name="X")
|
||||
)
|
||||
@@ -0,0 +1,153 @@
|
||||
import pytest
|
||||
|
||||
from apps.customers.domain.entities import BillingUnit, Contact, Customer
|
||||
from apps.customers.domain.enums import CustomerType
|
||||
from apps.customers.domain.exceptions import (
|
||||
ConsumerCannotHaveBusinessId,
|
||||
EInvoiceOperatorRequired,
|
||||
InternalCustomerMustBeCompany,
|
||||
InvalidBusinessId,
|
||||
InvalidContactName,
|
||||
InvalidCustomerName,
|
||||
InvalidEInvoiceAddress,
|
||||
InvalidPersonalIdentityCode,
|
||||
)
|
||||
from apps.customers.domain.value_objects import (
|
||||
BusinessId,
|
||||
CustomerName,
|
||||
EInvoiceAddress,
|
||||
PersonalIdentityCode,
|
||||
)
|
||||
|
||||
|
||||
class TestBusinessId:
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
[
|
||||
("0112038-9", "0112038-9"), # Nokia
|
||||
("01120389", "0112038-9"),
|
||||
("112038-9", "0112038-9"), # leading zero missing
|
||||
(" 1572860-0 ", "1572860-0"), # remainder 0 → check digit 0
|
||||
],
|
||||
)
|
||||
def test_valid(self, value: str, expected: str) -> None:
|
||||
assert BusinessId(value).value == expected
|
||||
|
||||
@pytest.mark.parametrize("value", ["0112038-8", "1234567", "abcdefg-1", "", "12345678-9"])
|
||||
def test_invalid(self, value: str) -> None:
|
||||
with pytest.raises(InvalidBusinessId):
|
||||
BusinessId(value)
|
||||
|
||||
def test_remainder_one_is_never_valid(self) -> None:
|
||||
weights = (7, 9, 10, 5, 8, 4, 2)
|
||||
body = next(
|
||||
f"{n:07d}"
|
||||
for n in range(1, 10_000)
|
||||
if sum(int(d) * w for d, w in zip(f"{n:07d}", weights, strict=True)) % 11 == 1
|
||||
)
|
||||
for check in range(10):
|
||||
with pytest.raises(InvalidBusinessId):
|
||||
BusinessId(f"{body}-{check}")
|
||||
|
||||
|
||||
class TestPersonalIdentityCode:
|
||||
@pytest.mark.parametrize(
|
||||
"value", ["131052-308T", "131052-308t", "010594Y9032", "010594Y9021", "020300A123N"]
|
||||
)
|
||||
def test_valid(self, value: str) -> None:
|
||||
assert PersonalIdentityCode(value).value == value.upper()
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
[
|
||||
"131052-308X", # wrong check character
|
||||
"310252-308T", # 31 February
|
||||
"131052Z308T", # unknown century sign
|
||||
"131052-001T", # individual number below 002
|
||||
"13105-308T",
|
||||
"",
|
||||
],
|
||||
)
|
||||
def test_invalid(self, value: str) -> None:
|
||||
with pytest.raises(InvalidPersonalIdentityCode):
|
||||
PersonalIdentityCode(value)
|
||||
|
||||
def test_masked_and_repr_hide_birth_date(self) -> None:
|
||||
code = PersonalIdentityCode("131052-308T")
|
||||
assert code.masked == "••••••-308T"
|
||||
assert "131052" not in repr(code)
|
||||
assert "131052" not in str(code)
|
||||
|
||||
|
||||
class TestOtherValueObjects:
|
||||
def test_customer_name(self) -> None:
|
||||
assert CustomerName(" Oy Esimerkki Ab ").value == "Oy Esimerkki Ab"
|
||||
with pytest.raises(InvalidCustomerName):
|
||||
CustomerName(" ")
|
||||
|
||||
def test_e_invoice_address(self) -> None:
|
||||
assert EInvoiceAddress("0037 1234 5678").value == "003712345678"
|
||||
for bad in ["12345678901", "x" * 36, "FI12-3456-7890"]:
|
||||
with pytest.raises(InvalidEInvoiceAddress):
|
||||
EInvoiceAddress(bad)
|
||||
|
||||
|
||||
class TestCustomer:
|
||||
def test_consumer_cannot_have_business_id(self) -> None:
|
||||
with pytest.raises(ConsumerCannotHaveBusinessId):
|
||||
Customer.register(
|
||||
name=CustomerName("Kalle"),
|
||||
type=CustomerType.CONSUMER,
|
||||
business_id=BusinessId("0112038-9"),
|
||||
)
|
||||
|
||||
def test_becoming_company_drops_personal_id(self) -> None:
|
||||
consumer = Customer.reconstitute(
|
||||
id=1,
|
||||
name=CustomerName("Kalle"),
|
||||
type=CustomerType.CONSUMER,
|
||||
business_id=None,
|
||||
is_internal=False,
|
||||
has_personal_id=True,
|
||||
)
|
||||
company = consumer.change_details(
|
||||
name=CustomerName("Kalle Oy"), type=CustomerType.COMPANY, business_id=None
|
||||
)
|
||||
assert not company.has_personal_id
|
||||
|
||||
def test_internal_customer_stays_company(self) -> None:
|
||||
internal = Customer.reconstitute(
|
||||
id=1,
|
||||
name=CustomerName("Oma yritys"),
|
||||
type=CustomerType.COMPANY,
|
||||
business_id=None,
|
||||
is_internal=True,
|
||||
has_personal_id=False,
|
||||
)
|
||||
with pytest.raises(InternalCustomerMustBeCompany):
|
||||
internal.change_details(
|
||||
name=CustomerName("Oma"), type=CustomerType.CONSUMER, business_id=None
|
||||
)
|
||||
|
||||
|
||||
class TestChildren:
|
||||
def test_contact_requires_name(self) -> None:
|
||||
with pytest.raises(InvalidContactName):
|
||||
Contact(id=None, customer_id=1, name=" ")
|
||||
|
||||
def test_e_invoice_needs_operator(self) -> None:
|
||||
with pytest.raises(EInvoiceOperatorRequired):
|
||||
BillingUnit(
|
||||
id=None,
|
||||
customer_id=1,
|
||||
name="Laskutus",
|
||||
e_invoice_address=EInvoiceAddress("003712345678"),
|
||||
)
|
||||
unit = BillingUnit(
|
||||
id=None,
|
||||
customer_id=1,
|
||||
name="Laskutus",
|
||||
e_invoice_address=EInvoiceAddress("003712345678"),
|
||||
e_invoice_operator=" 003721291126 ",
|
||||
)
|
||||
assert unit.e_invoice_operator == "003721291126"
|
||||
@@ -0,0 +1,89 @@
|
||||
"""In-memory test doubles for the customers application layer (no database)."""
|
||||
|
||||
from dataclasses import replace
|
||||
|
||||
from apps.customers.domain.entities import BillingUnit, Contact, Customer
|
||||
from apps.customers.domain.exceptions import CustomerInUse
|
||||
from apps.customers.domain.value_objects import BusinessId, PersonalIdentityCode
|
||||
|
||||
|
||||
class InMemoryCustomerRepository:
|
||||
def __init__(self) -> None:
|
||||
self.customers: dict[int, Customer] = {}
|
||||
self.personal_ids: dict[int, str] = {}
|
||||
self.references: set[int] = set() # customer ids that something refers to
|
||||
self._next = 1
|
||||
|
||||
def get(self, customer_id: int) -> Customer | None:
|
||||
return self.customers.get(customer_id)
|
||||
|
||||
def save(self, customer: Customer) -> Customer:
|
||||
if customer.id is None:
|
||||
customer = customer.with_id(self._next)
|
||||
self._next += 1
|
||||
assert customer.id is not None
|
||||
if not customer.has_personal_id:
|
||||
self.personal_ids.pop(customer.id, None)
|
||||
self.customers[customer.id] = customer
|
||||
return customer
|
||||
|
||||
def set_personal_id(self, customer_id: int, code: PersonalIdentityCode) -> None:
|
||||
self.personal_ids[customer_id] = code.value
|
||||
self.customers[customer_id] = replace(self.customers[customer_id], has_personal_id=True)
|
||||
|
||||
def is_business_id_taken(
|
||||
self, business_id: BusinessId, *, exclude_id: int | None = None
|
||||
) -> bool:
|
||||
return any(
|
||||
c.business_id == business_id and c.id != exclude_id for c in self.customers.values()
|
||||
)
|
||||
|
||||
def is_personal_id_taken(
|
||||
self, code: PersonalIdentityCode, *, exclude_id: int | None = None
|
||||
) -> bool:
|
||||
return any(v == code.value and k != exclude_id for k, v in self.personal_ids.items())
|
||||
|
||||
def delete(self, customer_id: int) -> None:
|
||||
if customer_id in self.references:
|
||||
raise CustomerInUse()
|
||||
del self.customers[customer_id]
|
||||
|
||||
|
||||
class InMemoryContactRepository:
|
||||
def __init__(self) -> None:
|
||||
self.items: dict[int, Contact] = {}
|
||||
self._next = 1
|
||||
|
||||
def get(self, contact_id: int) -> Contact | None:
|
||||
return self.items.get(contact_id)
|
||||
|
||||
def save(self, contact: Contact) -> Contact:
|
||||
if contact.id is None:
|
||||
contact = contact.with_id(self._next)
|
||||
self._next += 1
|
||||
assert contact.id is not None
|
||||
self.items[contact.id] = contact
|
||||
return contact
|
||||
|
||||
def delete(self, contact_id: int) -> None:
|
||||
del self.items[contact_id]
|
||||
|
||||
|
||||
class InMemoryBillingUnitRepository:
|
||||
def __init__(self) -> None:
|
||||
self.items: dict[int, BillingUnit] = {}
|
||||
self._next = 1
|
||||
|
||||
def get(self, billing_unit_id: int) -> BillingUnit | None:
|
||||
return self.items.get(billing_unit_id)
|
||||
|
||||
def save(self, billing_unit: BillingUnit) -> BillingUnit:
|
||||
if billing_unit.id is None:
|
||||
billing_unit = billing_unit.with_id(self._next)
|
||||
self._next += 1
|
||||
assert billing_unit.id is not None
|
||||
self.items[billing_unit.id] = billing_unit
|
||||
return billing_unit
|
||||
|
||||
def delete(self, billing_unit_id: int) -> None:
|
||||
del self.items[billing_unit_id]
|
||||
@@ -0,0 +1,223 @@
|
||||
from collections.abc import Callable
|
||||
from datetime import date
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from django.test import Client
|
||||
|
||||
from apps.customers.infrastructure import vault
|
||||
from apps.customers.infrastructure.orm import BillingUnitModel, ContactModel, CustomerModel
|
||||
from apps.customers.infrastructure.queries import DjangoCustomerQueries
|
||||
from apps.users.infrastructure.orm import User
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
PIC = "131052-308T"
|
||||
HX = {"HX-Request": "true"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def employee_client(client: Client, make_user: Callable[..., User]) -> Client:
|
||||
client.force_login(make_user(role="employee"))
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def management_client(make_user: Callable[..., User]) -> Client:
|
||||
c = Client()
|
||||
c.force_login(make_user(role="management"))
|
||||
return c
|
||||
|
||||
|
||||
def create(client: Client, **data: str) -> Any:
|
||||
return client.post("/customers/new/", {"name": "Oy Esimerkki Ab", "type": "company", **data})
|
||||
|
||||
|
||||
class TestInternalCustomer:
|
||||
def test_migration_created_internal_customer(self) -> None:
|
||||
internal = CustomerModel.objects.get(is_internal=True)
|
||||
assert internal.name == "Oma yritys (sisäinen)"
|
||||
assert internal.type == "company"
|
||||
assert not DjangoCustomerQueries().is_deletable(internal.pk)
|
||||
|
||||
def test_internal_customer_cannot_be_deleted(self, employee_client: Client) -> None:
|
||||
internal = CustomerModel.objects.get(is_internal=True)
|
||||
employee_client.post(f"/customers/{internal.pk}/delete/")
|
||||
assert CustomerModel.objects.filter(pk=internal.pk).exists()
|
||||
|
||||
|
||||
class TestPermissions:
|
||||
def test_subcontractor_gets_403(self, client: Client, make_user: Callable[..., User]) -> None:
|
||||
client.force_login(make_user(role="subcontractor"))
|
||||
assert client.get("/customers/").status_code == 403
|
||||
assert client.get("/customers/new/").status_code == 403
|
||||
|
||||
def test_intern_can_use_customers(self, client: Client, make_user: Callable[..., User]) -> None:
|
||||
boss = make_user()
|
||||
client.force_login(
|
||||
make_user(role="intern", supervisor=boss, employment_ends_on=date(2099, 1, 1))
|
||||
)
|
||||
assert client.get("/customers/").status_code == 200
|
||||
|
||||
|
||||
class TestPersonalIdentityCode:
|
||||
def test_stored_encrypted_with_fingerprint(self, employee_client: Client) -> None:
|
||||
response = create(
|
||||
employee_client, name="Kalle", type="consumer", personal_identity_code=PIC
|
||||
)
|
||||
assert response.status_code == 302
|
||||
row = CustomerModel.objects.get(name="Kalle")
|
||||
assert row.personal_identity_code and PIC not in row.personal_identity_code
|
||||
assert vault.decrypt(row.personal_identity_code) == PIC
|
||||
assert row.personal_identity_code_hash == vault.fingerprint(PIC)
|
||||
|
||||
def test_duplicate_is_rejected(self, employee_client: Client) -> None:
|
||||
create(employee_client, name="Kalle", type="consumer", personal_identity_code=PIC)
|
||||
response = create(
|
||||
employee_client, name="Kalle 2", type="consumer", personal_identity_code=PIC
|
||||
)
|
||||
assert response.status_code == 422
|
||||
assert "Henkilötunnus on jo toisella asiakkaalla." in response.content.decode()
|
||||
|
||||
def test_employee_sees_masked_only(self, employee_client: Client) -> None:
|
||||
create(employee_client, name="Kalle", type="consumer", personal_identity_code=PIC)
|
||||
pk = CustomerModel.objects.get(name="Kalle").pk
|
||||
html = employee_client.get(f"/customers/{pk}/").content.decode()
|
||||
assert "••••••-308T" in html
|
||||
assert "131052" not in html
|
||||
assert "Näytä henkilötunnus" not in html
|
||||
assert employee_client.get(f"/customers/{pk}/personal-id/").status_code == 403
|
||||
|
||||
def test_management_can_reveal(
|
||||
self, employee_client: Client, management_client: Client
|
||||
) -> None:
|
||||
create(employee_client, name="Kalle", type="consumer", personal_identity_code=PIC)
|
||||
pk = CustomerModel.objects.get(name="Kalle").pk
|
||||
response = management_client.get(f"/customers/{pk}/personal-id/", headers=HX)
|
||||
assert PIC in response.content.decode()
|
||||
assert response["Cache-Control"] == "no-store"
|
||||
|
||||
def test_form_never_shows_stored_code(self, management_client: Client) -> None:
|
||||
create(management_client, name="Kalle", type="consumer", personal_identity_code=PIC)
|
||||
pk = CustomerModel.objects.get(name="Kalle").pk
|
||||
html = management_client.get(f"/customers/{pk}/edit/").content.decode()
|
||||
assert "131052" not in html
|
||||
assert "••••••-308T" in html
|
||||
assert 'name="remove_personal_id"' in html
|
||||
|
||||
def test_employee_has_no_remove_option(self, employee_client: Client) -> None:
|
||||
create(employee_client, name="Kalle", type="consumer", personal_identity_code=PIC)
|
||||
pk = CustomerModel.objects.get(name="Kalle").pk
|
||||
assert (
|
||||
'name="remove_personal_id"'
|
||||
not in employee_client.get(f"/customers/{pk}/edit/").content.decode()
|
||||
)
|
||||
|
||||
def test_management_removes_code(self, management_client: Client) -> None:
|
||||
create(management_client, name="Kalle", type="consumer", personal_identity_code=PIC)
|
||||
pk = CustomerModel.objects.get(name="Kalle").pk
|
||||
management_client.post(
|
||||
f"/customers/{pk}/edit/",
|
||||
{"name": "Kalle", "type": "consumer", "remove_personal_id": "on"},
|
||||
)
|
||||
row = CustomerModel.objects.get(pk=pk)
|
||||
assert row.personal_identity_code is None
|
||||
assert row.personal_identity_code_hash is None
|
||||
|
||||
def test_list_search_does_not_match_code(self, employee_client: Client) -> None:
|
||||
create(employee_client, name="Kalle", type="consumer", personal_identity_code=PIC)
|
||||
html = employee_client.get("/customers/?q=131052", headers=HX).content.decode()
|
||||
assert "Kalle" not in html
|
||||
|
||||
|
||||
class TestCreateAndList:
|
||||
def test_create_company_redirects_to_customer_page(self, employee_client: Client) -> None:
|
||||
response = create(employee_client, business_id="01120389")
|
||||
pk = CustomerModel.objects.get(name="Oy Esimerkki Ab").pk
|
||||
assert response["Location"] == f"/customers/{pk}/"
|
||||
assert CustomerModel.objects.get(pk=pk).business_id == "0112038-9"
|
||||
|
||||
def test_invalid_business_id(self, employee_client: Client) -> None:
|
||||
response = create(employee_client, business_id="0112038-8")
|
||||
assert response.status_code == 422
|
||||
assert "Anna kelvollinen Y-tunnus" in response.content.decode()
|
||||
|
||||
def test_search_by_business_id_and_internal_first(self, employee_client: Client) -> None:
|
||||
create(employee_client, business_id="0112038-9")
|
||||
html = employee_client.get("/customers/?q=0112038", headers=HX).content.decode()
|
||||
assert "Oy Esimerkki Ab" in html
|
||||
names = [c.name for c in DjangoCustomerQueries().list_customers()]
|
||||
assert names[0] == "Oma yritys (sisäinen)"
|
||||
|
||||
|
||||
class TestContactsAndBillingUnits:
|
||||
def test_add_contact_via_htmx_updates_section(self, employee_client: Client) -> None:
|
||||
create(employee_client)
|
||||
pk = CustomerModel.objects.get(name="Oy Esimerkki Ab").pk
|
||||
form = employee_client.get(f"/customers/{pk}/contacts/new/", headers=HX).content.decode()
|
||||
assert "<html" not in form
|
||||
response = employee_client.post(
|
||||
f"/customers/{pk}/contacts/new/",
|
||||
{"name": "Maija", "email": "[email protected]"},
|
||||
headers=HX,
|
||||
)
|
||||
assert response["HX-Retarget"] == "#contacts-section"
|
||||
assert "itemSaved" in response["HX-Trigger-After-Settle"]
|
||||
html = response.content.decode()
|
||||
assert "Maija" in html
|
||||
assert 'id="customer-actions" class="flex gap-2" hx-swap-oob="true"' in html
|
||||
|
||||
def test_invalid_contact_returns_form_with_422(self, employee_client: Client) -> None:
|
||||
create(employee_client)
|
||||
pk = CustomerModel.objects.get(name="Oy Esimerkki Ab").pk
|
||||
response = employee_client.post(f"/customers/{pk}/contacts/new/", {"name": ""}, headers=HX)
|
||||
assert response.status_code == 422
|
||||
assert 'aria-invalid="true"' in response.content.decode()
|
||||
|
||||
def test_billing_unit_defaults_to_customer_name(self, employee_client: Client) -> None:
|
||||
create(employee_client)
|
||||
pk = CustomerModel.objects.get(name="Oy Esimerkki Ab").pk
|
||||
html = employee_client.get(
|
||||
f"/customers/{pk}/billing-units/new/", headers=HX
|
||||
).content.decode()
|
||||
assert 'value="Oy Esimerkki Ab"' in html
|
||||
|
||||
def test_e_invoice_requires_operator(self, employee_client: Client) -> None:
|
||||
create(employee_client)
|
||||
pk = CustomerModel.objects.get(name="Oy Esimerkki Ab").pk
|
||||
response = employee_client.post(
|
||||
f"/customers/{pk}/billing-units/new/",
|
||||
{"name": "Lasku", "e_invoice_address": "003701120389"},
|
||||
headers=HX,
|
||||
)
|
||||
assert response.status_code == 422
|
||||
assert "Anna välittäjätunnus" in response.content.decode()
|
||||
|
||||
def test_cannot_edit_other_customers_contact(self, employee_client: Client) -> None:
|
||||
create(employee_client)
|
||||
create(employee_client, name="Toinen Oy")
|
||||
a = CustomerModel.objects.get(name="Oy Esimerkki Ab")
|
||||
b = CustomerModel.objects.get(name="Toinen Oy")
|
||||
contact = ContactModel.objects.create(customer=a, name="Maija")
|
||||
response = employee_client.post(f"/customers/{b.pk}/contacts/{contact.pk}/delete/")
|
||||
assert ContactModel.objects.filter(pk=contact.pk).exists()
|
||||
assert response.status_code == 302
|
||||
|
||||
|
||||
class TestDeleteCustomer:
|
||||
def test_delete_blocked_while_children_exist(self, employee_client: Client) -> None:
|
||||
create(employee_client)
|
||||
customer = CustomerModel.objects.get(name="Oy Esimerkki Ab")
|
||||
unit = BillingUnitModel.objects.create(customer=customer, name="Lasku")
|
||||
html = employee_client.get(f"/customers/{customer.pk}/").content.decode()
|
||||
assert "Poista asiakas" not in html
|
||||
employee_client.post(f"/customers/{customer.pk}/delete/")
|
||||
assert CustomerModel.objects.filter(pk=customer.pk).exists()
|
||||
|
||||
unit.delete()
|
||||
assert (
|
||||
"Poista asiakas" in employee_client.get(f"/customers/{customer.pk}/").content.decode()
|
||||
)
|
||||
response = employee_client.post(f"/customers/{customer.pk}/delete/")
|
||||
assert response["Location"] == "/customers/"
|
||||
assert not CustomerModel.objects.filter(pk=customer.pk).exists()
|
||||
Reference in New Issue
Block a user