@@ -476,16 +476,170 @@ def test_draft_message_unauthorized(self):
476
476
"senderId" : uuid .uuid4 (),
477
477
"subject" : "test" ,
478
478
"draftBody" : "<p>test</p> or test" ,
479
- "to" : ["pierre@example .com" ],
479
+ "to" : ["pierre@external .com" ],
480
480
},
481
481
format = "json" ,
482
482
)
483
483
# Assert the response is unauthorized
484
484
assert response .status_code == status .HTTP_401_UNAUTHORIZED
485
485
486
- # TODO: implement this test
487
- # def test_send_message_unauthorized(self, mailbox, authenticated_user, send_url):
488
- # """Test send message unauthorized."""
486
+ def test_draft_message_with_empty_subject (self , mailbox , authenticated_user ):
487
+ """Test create draft message with empty subject."""
488
+ factories .MailboxAccessFactory (
489
+ mailbox = mailbox ,
490
+ user = authenticated_user ,
491
+ role = enums .MailboxRoleChoices .EDITOR ,
492
+ )
493
+
494
+ client = APIClient ()
495
+ client .force_authenticate (user = authenticated_user )
496
+
497
+ draft_response = client .post (
498
+ reverse ("draft-message" ),
499
+ {
500
+ "senderId" : mailbox .id ,
501
+ "subject" : "" , # Empty subject should be allowed
502
+ "draftBody" : "Test content" ,
503
+
504
+ },
505
+ format = "json" ,
506
+ )
507
+
508
+ assert draft_response .status_code == status .HTTP_201_CREATED
509
+ draft_message_id = draft_response .data ["id" ]
510
+
511
+ # Verify the message was created with empty subject
512
+ draft_message = models .Message .objects .get (id = draft_message_id )
513
+ assert draft_message .subject == ""
514
+ assert draft_message .is_draft is True
515
+ assert str (draft_message ) == "(no subject)"
516
+
517
+ # Verify the thread also has empty subject
518
+ assert draft_message .thread .subject == ""
519
+ assert str (draft_message .thread ) == "(no subject)"
520
+
521
+ def test_draft_message_without_subject (self , mailbox , authenticated_user ):
522
+ """Test create draft message without subject field."""
523
+ factories .MailboxAccessFactory (
524
+ mailbox = mailbox ,
525
+ user = authenticated_user ,
526
+ role = enums .MailboxRoleChoices .EDITOR ,
527
+ )
528
+
529
+ client = APIClient ()
530
+ client .force_authenticate (user = authenticated_user )
531
+
532
+ draft_response = client .post (
533
+ reverse ("draft-message" ),
534
+ {
535
+ "senderId" : mailbox .id ,
536
+ # No subject field - should default to None
537
+ "draftBody" : "Test content" ,
538
+
539
+ },
540
+ format = "json" ,
541
+ )
542
+
543
+ assert draft_response .status_code == status .HTTP_201_CREATED
544
+ draft_message_id = draft_response .data ["id" ]
545
+
546
+ # Verify the message was created with None subject
547
+ draft_message = models .Message .objects .get (id = draft_message_id )
548
+ assert draft_message .subject is None
549
+ assert draft_message .is_draft is True
550
+ assert str (draft_message ) == "(no subject)"
551
+
552
+ # Verify the thread also has None subject
553
+ assert draft_message .thread .subject is None
554
+ assert str (draft_message .thread ) == "(no subject)"
555
+
556
+ @patch ("core.mda.outbound.send_outbound_message" )
557
+ def test_send_message_with_empty_subject (
558
+ self , mock_send_outbound_message , mailbox , authenticated_user , send_url
559
+ ):
560
+ """Test sending a message with empty subject (migration 0018)."""
561
+ mock_send_outbound_message .side_effect = lambda recipient_emails , message : {
562
+ recipient_email : {
563
+ "delivered" : True ,
564
+ "error" : None ,
565
+ }
566
+ for recipient_email in recipient_emails
567
+ }
568
+
569
+ factories .MailboxAccessFactory (
570
+ mailbox = mailbox ,
571
+ user = authenticated_user ,
572
+ role = enums .MailboxRoleChoices .EDITOR ,
573
+ )
574
+
575
+ client = APIClient ()
576
+ client .force_authenticate (user = authenticated_user )
577
+
578
+ # Create draft with empty subject
579
+ draft_response = client .post (
580
+ reverse ("draft-message" ),
581
+ {
582
+ "senderId" : mailbox .id ,
583
+ "subject" : "" , # Empty subject
584
+ "draftBody" : "Test content" ,
585
+
586
+ },
587
+ format = "json" ,
588
+ )
589
+
590
+ assert draft_response .status_code == status .HTTP_201_CREATED
591
+ draft_message_id = draft_response .data ["id" ]
592
+
593
+ # Send the message
594
+ send_response = client .post (
595
+ send_url ,
596
+ {
597
+ "messageId" : draft_message_id ,
598
+ "senderId" : mailbox .id ,
599
+ },
600
+ format = "json" ,
601
+ )
602
+
603
+ assert send_response .status_code == status .HTTP_200_OK
604
+
605
+ # Verify the message was sent with empty subject
606
+ sent_message = models .Message .objects .get (id = draft_message_id )
607
+ assert sent_message .subject == ""
608
+ assert sent_message .is_draft is False
609
+ assert sent_message .sent_at is not None
610
+ assert str (sent_message ) == "(no subject)"
611
+
612
+ # Verify the thread also has empty subject
613
+ assert sent_message .thread .subject == ""
614
+ assert str (sent_message .thread ) == "(no subject)"
615
+
616
+ def test_draft_message_with_very_long_subject (self , mailbox , authenticated_user ):
617
+ """Test create draft message with subject exceeding max_length."""
618
+ factories .MailboxAccessFactory (
619
+ mailbox = mailbox ,
620
+ user = authenticated_user ,
621
+ role = enums .MailboxRoleChoices .EDITOR ,
622
+ )
623
+
624
+ client = APIClient ()
625
+ client .force_authenticate (user = authenticated_user )
626
+
627
+ # Create a subject that exceeds max_length (255)
628
+ long_subject = "A" * 256
629
+
630
+ draft_response = client .post (
631
+ reverse ("draft-message" ),
632
+ {
633
+ "senderId" : mailbox .id ,
634
+ "subject" : long_subject ,
635
+ "draftBody" : "Test content" ,
636
+
637
+ },
638
+ format = "json" ,
639
+ )
640
+
641
+ # Should fail due to max_length constraint
642
+ assert draft_response .status_code == status .HTTP_400_BAD_REQUEST
489
643
490
644
def test_send_nonexistent_message (self , mailbox , authenticated_user , send_url ):
491
645
"""Test sending a message that does not exist."""
0 commit comments