|
5 | 5 | for E2E testing across different browsers (chromium, firefox, webkit). |
6 | 6 | """ |
7 | 7 |
|
| 8 | +from email.mime.text import MIMEText |
| 9 | +from email.utils import format_datetime |
| 10 | + |
| 11 | +from django.conf import settings |
8 | 12 | from django.core.management.base import BaseCommand |
9 | 13 | from django.db import transaction |
10 | 14 | from django.utils import timezone |
|
21 | 25 | BROWSERS = ["chromium", "firefox", "webkit"] |
22 | 26 | DOMAIN_NAME = "example.local" |
23 | 27 | SHARED_MAILBOX_LOCAL_PART = "shared.e2e" |
| 28 | +CLIENTBRIDGE_APP_PASSWORD = "e2e-client-bridge-password" # noqa: S105 |
24 | 29 |
|
25 | 30 |
|
26 | 31 | class Command(BaseCommand): |
@@ -122,6 +127,18 @@ def handle(self, *args, **options): |
122 | 127 | for browser in BROWSERS: |
123 | 128 | self._create_outbox_test_data(domain, browser) |
124 | 129 |
|
| 130 | + # Step 7: Create client-bridge channels and IMAP test data |
| 131 | + if settings.FEATURE_CLIENTBRIDGE: |
| 132 | + self.stdout.write( |
| 133 | + "\n-- 6/6 📧 Creating client-bridge channels and IMAP test data" |
| 134 | + ) |
| 135 | + for _user, mailbox in regular_users: |
| 136 | + self._create_clientbridge_channel(mailbox) |
| 137 | + self._create_clientbridge_channel(shared_mailbox) |
| 138 | + # Create IMAP test messages on the first regular user's mailbox |
| 139 | + _first_user, first_mailbox = regular_users[0] |
| 140 | + self._create_imap_test_messages(first_mailbox, domain) |
| 141 | + |
125 | 142 | def _create_user_with_mailbox( |
126 | 143 | self, email, domain, is_domain_admin=False, is_superuser=False |
127 | 144 | ): |
@@ -387,3 +404,122 @@ def _create_thread_with_message(self, mailbox, sender_contact, subject, recipien |
387 | 404 | thread.update_stats() |
388 | 405 |
|
389 | 406 | return thread |
| 407 | + |
| 408 | + def _create_clientbridge_channel(self, mailbox): |
| 409 | + """Create a client-bridge channel with a known password for e2e testing.""" |
| 410 | + # Use the first user with admin access to this mailbox |
| 411 | + access = models.MailboxAccess.objects.filter( |
| 412 | + mailbox=mailbox, role=MailboxRoleChoices.ADMIN |
| 413 | + ).first() |
| 414 | + if not access: |
| 415 | + self.stdout.write( |
| 416 | + self.style.WARNING( |
| 417 | + f" No admin user found for {mailbox}, skipping channel" |
| 418 | + ) |
| 419 | + ) |
| 420 | + return |
| 421 | + |
| 422 | + _channel, created = models.Channel.objects.get_or_create( |
| 423 | + mailbox=mailbox, |
| 424 | + type="client-bridge", |
| 425 | + defaults={ |
| 426 | + "name": f"E2E client-bridge ({mailbox})", |
| 427 | + "user": access.user, |
| 428 | + "settings": {"role": "sender"}, |
| 429 | + "encrypted_settings": {"password": CLIENTBRIDGE_APP_PASSWORD}, |
| 430 | + }, |
| 431 | + ) |
| 432 | + if created: |
| 433 | + self.stdout.write( |
| 434 | + self.style.SUCCESS(f" Created client-bridge channel for {mailbox}") |
| 435 | + ) |
| 436 | + else: |
| 437 | + self.stdout.write( |
| 438 | + self.style.SUCCESS( |
| 439 | + f" Client-bridge channel already exists for {mailbox}" |
| 440 | + ) |
| 441 | + ) |
| 442 | + |
| 443 | + @staticmethod |
| 444 | + def _make_eml(subject, sender_email, recipient_email, body, sent_at): |
| 445 | + """Build a minimal RFC 5322 message and return raw bytes.""" |
| 446 | + msg = MIMEText(body, "plain") |
| 447 | + msg["Subject"] = subject |
| 448 | + msg["From"] = sender_email |
| 449 | + msg["To"] = recipient_email |
| 450 | + msg["Date"] = format_datetime(sent_at) |
| 451 | + return msg.as_bytes() |
| 452 | + |
| 453 | + def _create_imap_test_messages(self, mailbox, domain): |
| 454 | + """Create messages for IMAP read/unread e2e testing. |
| 455 | +
|
| 456 | + Creates two threads with proper EML blobs so the client-bridge |
| 457 | + can serve envelope data (subject, from, date) over IMAP. |
| 458 | + """ |
| 459 | + sender_email = f"imap-sender@{domain.name}" |
| 460 | + recipient_email = str(mailbox) |
| 461 | + sender_contact, _ = models.Contact.objects.get_or_create( |
| 462 | + email=sender_email, |
| 463 | + mailbox=mailbox, |
| 464 | + defaults={"name": "IMAP Test Sender"}, |
| 465 | + ) |
| 466 | + |
| 467 | + # Thread 1: an unread message |
| 468 | + now = timezone.now() |
| 469 | + thread1 = models.Thread.objects.create(subject="IMAP unread test") |
| 470 | + models.ThreadAccess.objects.create( |
| 471 | + thread=thread1, |
| 472 | + mailbox=mailbox, |
| 473 | + role=ThreadAccessRoleChoices.EDITOR, |
| 474 | + read_at=None, # No read_at → all messages unread |
| 475 | + ) |
| 476 | + eml1 = self._make_eml( |
| 477 | + "IMAP unread test", |
| 478 | + sender_email, |
| 479 | + recipient_email, |
| 480 | + "This message should appear as unread in IMAP.", |
| 481 | + now, |
| 482 | + ) |
| 483 | + blob1 = mailbox.create_blob(content=eml1, content_type="message/rfc822") |
| 484 | + models.Message.objects.create( |
| 485 | + thread=thread1, |
| 486 | + sender=sender_contact, |
| 487 | + subject="IMAP unread test", |
| 488 | + is_sender=False, |
| 489 | + is_draft=False, |
| 490 | + sent_at=now, |
| 491 | + blob=blob1, |
| 492 | + ) |
| 493 | + thread1.update_stats() |
| 494 | + |
| 495 | + # Thread 2: a read message |
| 496 | + sent_at2 = now - timezone.timedelta(minutes=5) |
| 497 | + thread2 = models.Thread.objects.create(subject="IMAP read test") |
| 498 | + models.ThreadAccess.objects.create( |
| 499 | + thread=thread2, |
| 500 | + mailbox=mailbox, |
| 501 | + role=ThreadAccessRoleChoices.EDITOR, |
| 502 | + read_at=now, # read_at set → messages before this are read |
| 503 | + ) |
| 504 | + eml2 = self._make_eml( |
| 505 | + "IMAP read test", |
| 506 | + sender_email, |
| 507 | + recipient_email, |
| 508 | + "This message should appear as read in IMAP.", |
| 509 | + sent_at2, |
| 510 | + ) |
| 511 | + blob2 = mailbox.create_blob(content=eml2, content_type="message/rfc822") |
| 512 | + models.Message.objects.create( |
| 513 | + thread=thread2, |
| 514 | + sender=sender_contact, |
| 515 | + subject="IMAP read test", |
| 516 | + is_sender=False, |
| 517 | + is_draft=False, |
| 518 | + sent_at=sent_at2, |
| 519 | + blob=blob2, |
| 520 | + ) |
| 521 | + thread2.update_stats() |
| 522 | + |
| 523 | + self.stdout.write( |
| 524 | + self.style.SUCCESS(f" Created IMAP test messages for {mailbox}") |
| 525 | + ) |
0 commit comments