fix del contact from group and image for ticket

This commit is contained in:
javad committed 2026-09-13 20:11:59 +03:30
1 parent 5fc9a908c5
commit a18a68578f
7 files changed
+168 -9

No files matched your search

+30 -1
View File
@@ -1,3 +1,5 @@
import base64
import pytest
from django.contrib.auth.models import Permission
from django.urls import reverse
@@ -30,7 +32,7 @@ def test_staff_operations_pages_are_authorized_and_visually_styled(live_server,
unit = OrganizationalUnit.objects.create(name="عملیات E2E", slug="operations-e2e")
permissions = {
"customers": {"view_customerprofile", "add_customerprofile", "change_customerprofile", "view_contactgroup", "add_contactgroup", "change_contactgroup"},
"tickets": {"view_ticket", "dispatch_group_tickets"},
"tickets": {"view_ticket", "add_ticket", "dispatch_group_tickets"},
"notifications": {"view_smstemplate", "add_smstemplate", "change_smstemplate"},
"accounts": {"view_ticketmagiclink"},
}
@@ -45,6 +47,7 @@ def test_staff_operations_pages_are_authorized_and_visually_styled(live_server,
customer = User.objects.create_user(username="e2e-carrier", mobile="09120009121", account_type=User.AccountType.CUSTOMER)
profile = CustomerProfile.objects.create(user=customer, first_name="باربری", stakeholder_type=CustomerProfile.StakeholderType.CARRIER)
group = ContactGroup.objects.create(unit=unit, name="باربری‌ها")
secondary_group = ContactGroup.objects.create(unit=unit, name="مخاطبان منتخب")
profile.groups.add(group)
category = TicketCategory.objects.create(title="استعلام E2E", slug="operations-e2e")
ticket = create_ticket(customer=customer, created_by=staff, unit=unit, category=category, subject="استعلام آزمایشی", body="قیمت را اعلام کنید")
@@ -86,3 +89,29 @@ def test_staff_operations_pages_are_authorized_and_visually_styled(live_server,
expect(province.locator("option")).not_to_have_count(1)
province.select_option(label="اصفهان")
expect(city.locator("option", has_text="کاشان")).to_have_count(1)
page.goto(f"{live_server.url}{reverse('staff_tickets:contact_edit', args=[customer.public_id])}", wait_until="domcontentloaded")
first_group = page.locator(f'input[name="groups"][value="{group.pk}"]')
second_group = page.locator(f'input[name="groups"][value="{secondary_group.pk}"]')
expect(first_group).to_be_checked()
expect(second_group).not_to_be_checked()
second_group.check()
first_group.uncheck()
expect(page.locator("[data-group-selection-count]")).to_contain_text("۱")
page.get_by_role("button", name="ذخیره مخاطب").click()
page.wait_for_url(f"**{reverse('staff_tickets:contacts')}")
page.goto(f"{live_server.url}{reverse('staff_tickets:contact_edit', args=[customer.public_id])}", wait_until="domcontentloaded")
expect(page.locator(f'input[name="groups"][value="{group.pk}"]')).not_to_be_checked()
expect(page.locator(f'input[name="groups"][value="{secondary_group.pk}"]')).to_be_checked()
page.goto(f"{live_server.url}{reverse('staff_tickets:create')}", wait_until="domcontentloaded")
tiny_png = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=")
page.locator("#staff-ticket-gallery-input").set_input_files([
{"name": "first.png", "mimeType": "image/png", "buffer": tiny_png},
{"name": "second.png", "mimeType": "image/png", "buffer": tiny_png},
])
expect(page.locator("[data-file-preview] .file-chip")).to_have_count(2)
expect(page.locator("[data-file-preview] .file-chip img")).to_have_count(2)
page.locator("[data-file-preview] .file-chip button").first.click()
expect(page.locator("[data-file-preview] .file-chip")).to_have_count(1)
assert page.locator('input[name="files"]').evaluate("input => input.files.length") == 1
+86
View File
@@ -257,6 +257,92 @@ def test_superuser_without_staff_profile_can_open_all_staff_contacts(client):
assert "customer-without-profile" not in response.content.decode()
@pytest.mark.django_db
def test_staff_can_visually_add_and_remove_contact_group_memberships(client):
unit = OrganizationalUnit.objects.create(name="واحد ویرایش مخاطب", slug="contact-membership-editor")
unit.permissions.add(*Permission.objects.filter(
content_type__app_label="customers",
codename__in=("view_customerprofile", "change_customerprofile"),
))
staff = make_staff("contact-membership-agent", unit)
contact = make_contact("editable-contact", "09120000505")
first = ContactGroup.objects.create(name="گروه اولیه", unit=unit)
second = ContactGroup.objects.create(name="گروه جایگزین", unit=unit)
contact.customer_profile.groups.add(first)
client.force_login(staff)
url = reverse("staff_tickets:contact_edit", args=[contact.public_id])
page = client.get(url)
content = page.content.decode()
assert page.status_code == 200
assert "data-contact-group-selector" in content
assert "data-group-search" in content
assert str(first.pk) in {str(value) for value in page.context["form"]["groups"].value()}
assert f'value="{second.pk}"' in content
response = client.post(url, {
"mobile": contact.local_mobile,
"first_name": "مخاطب",
"last_name": "ویرایش‌شده",
"organization_name": "",
"stakeholder_type": CustomerProfile.StakeholderType.CUSTOMER,
"groups": [second.pk],
"province": "",
"city": "",
})
assert response.status_code == 302
assert set(contact.customer_profile.groups.values_list("pk", flat=True)) == {second.pk}
@pytest.mark.django_db
def test_staff_ticket_create_supports_multiple_previewable_attachments(client, monkeypatch, settings, tmp_path):
settings.MEDIA_ROOT = tmp_path
unit = OrganizationalUnit.objects.create(name="واحد پیوست کارشناس", slug="staff-attachment-unit")
unit.permissions.add(Permission.objects.get(content_type__app_label="tickets", codename="add_ticket"))
staff = make_staff("staff-attachment-agent", unit)
category = TicketCategory.objects.create(unit=unit, title="مدارک", slug="staff-attachment-category")
AttachmentPolicy.objects.create(attachment_type="IMAGE", max_file_size=1024 * 1024, allowed_extensions=[".jpg"])
AttachmentPolicy.objects.create(attachment_type="PDF", max_file_size=1024 * 1024, allowed_extensions=[".pdf"])
monkeypatch.setattr(
"apps.attachments.services.magic.from_buffer",
lambda content, mime=True: "image/jpeg" if content.startswith(b"\xff\xd8") else "application/pdf",
)
client.force_login(staff)
url = reverse("staff_tickets:create")
page = client.get(url)
content = page.content.decode()
assert page.status_code == 200
assert "data-ticket-composer" in content
assert "data-gallery-input" in content
assert "data-document-input" in content
assert "data-file-preview" in content
assert "attachment-uploader.js" in content
assert "ایجاد پرونده از طرف واحد پیوست کارشناس" in content
response = client.post(url, {
"unit": unit.pk,
"customer_mobile": "09120000506",
"first_name": "مشتری",
"last_name": "پیوست",
"stakeholder_type": CustomerProfile.StakeholderType.CUSTOMER,
"category": category.pk,
"subject": "ارسال همزمان تصویر و سند",
"body": "دو فایل برای این پرونده ثبت می‌شود.",
"files": [
SimpleUploadedFile("photo.jpg", b"\xff\xd8\xff\xe0image", content_type="image/jpeg"),
SimpleUploadedFile("document.pdf", b"%PDF-1.4\n%%EOF", content_type="application/pdf"),
],
})
assert response.status_code == 302
ticket = Ticket.objects.get(subject="ارسال همزمان تصویر و سند")
assert set(ticket.ticket_messages.get().attachments.values_list("original_name", flat=True)) == {
"photo.jpg", "document.pdf",
}
@pytest.mark.django_db
def test_unit_scoped_add_ticket_preserves_contact_ownership_and_stakeholder(client):
unit=OrganizationalUnit.objects.create(name="حسابداری عملیاتی",slug="accounting-ops")