@@ -430,7 +430,7 @@ class PostmarkWebhookTestCase(TestCase):
430430
431431 def setUp (self ):
432432 self .user = models .User .objects .create (
433- username = "alice" ,
email = "[email protected] " 433+ username = "alice" ,
email = "[email protected] " , is_premium = True 434434 )
435435
436436 def test_postmark_webhook_create_post_success (self ):
@@ -551,3 +551,60 @@ def test_postmark_webhook_cannot_post_to_another_users_blog(self):
551551 self .assertFalse (models .Post .objects .exists ())
552552 # no email sent
553553 self .assertEqual (len (mail .outbox ), 0 )
554+
555+ def test_postmark_webhook_non_premium_user_cannot_post (self ):
556+ """Non-premium users should receive an upgrade email instead of creating a post."""
557+ non_premium_user = models .User .objects .create (
558+ username = "freemium" ,
email = "[email protected] " ,
is_premium = False 559+ )
560+
561+ data = {
562+ 563+ "To" : f"post@{ non_premium_user .username } .{ settings .CANONICAL_HOST } " ,
564+ "Subject" : "My Post Attempt" ,
565+ "TextBody" : "This should not create a post." ,
566+ "Headers" : [],
567+ }
568+
569+ response = self .client .post (
570+ reverse ("postmark_webhook" ),
571+ data = json .dumps (data ),
572+ content_type = "application/json" ,
573+ )
574+
575+ self .assertEqual (response .status_code , 200 )
576+ # no post created
577+ self .assertFalse (models .Post .objects .exists ())
578+ # upgrade email sent
579+ self .assertEqual (len (mail .outbox ), 1 )
580+ self .
assertEqual (
mail .
outbox [
0 ].
to , [
"[email protected] " ])
581+ self .assertIn ("premium feature" , mail .outbox [0 ].body .lower ())
582+ self .assertIn ("https://mataroa.blog/billing/overview/" , mail .outbox [0 ].body )
583+
584+ def test_postmark_webhook_non_premium_user_cannot_create_draft (self ):
585+ """Non-premium users should receive an upgrade email when trying to create a draft."""
586+ non_premium_user = models .User .objects .create (
587+ username = "freemium" ,
email = "[email protected] " ,
is_premium = False 588+ )
589+
590+ data = {
591+ 592+ "To" : f"draft@{ non_premium_user .username } .{ settings .CANONICAL_HOST } " ,
593+ "Subject" : "My Draft Attempt" ,
594+ "TextBody" : "This should not create a draft." ,
595+ "Headers" : [],
596+ }
597+
598+ response = self .client .post (
599+ reverse ("postmark_webhook" ),
600+ data = json .dumps (data ),
601+ content_type = "application/json" ,
602+ )
603+
604+ self .assertEqual (response .status_code , 200 )
605+ # no post created
606+ self .assertFalse (models .Post .objects .exists ())
607+ # upgrade email sent
608+ self .assertEqual (len (mail .outbox ), 1 )
609+ self .
assertEqual (
mail .
outbox [
0 ].
to , [
"[email protected] " ])
610+ self .assertIn ("premium feature" , mail .outbox [0 ].body .lower ())
0 commit comments