Exemple #1
0
    def test_no_first_name_subscribe_adds_person_and_subscription(self):
        email = Factory.rand_email()
        response = self.get(
            reverse(
                'inkmail:transfer_subscription',
                kwargs={
                    "transfer_code": self.newsletter.hashid,
                },
            ),
            {
                'e': email,
                # 'newsletter': self.newsletter.internal_name,
            },
        )

        self.assertEquals(response.status_code, 200)

        self.assertEquals(Person.objects.count(), 1)
        p = Person.objects.all()[0]
        self.assertEquals(p.email, email)

        self.assertEquals(Subscription.objects.count(), 1)
        s = Subscription.objects.all()[0]
        self.assertEquals(s.person, p)
        self.assertEquals(s.subscription_url, "transfer-subscription")
        self.assertEquals(s.newsletter.name, self.newsletter.name)
        self.assertEquals(s.subscribed_from_ip, self._source_ip)
Exemple #2
0
    def test_invalid_opt_in_click(self):
        self.assertEquals(Subscription.objects.count(), 0)
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse(
                'inkmail:subscribe',
            ),
            {
                'first_name': name,
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            },
        )
        self.assertEquals(response.status_code, 200)
        process_outgoing_message_queue()

        self.assertEquals(len(mail.outbox), 1)
        self.assertEquals(Subscription.objects.count(), 1)
        s = Subscription.objects.all()[0]
        self.assertIn(s.opt_in_link, mail.outbox[0].body)

        self.get("%s%s" % (s.opt_in_link, Factory.rand_str(length=2)))
        s = Subscription.objects.get(pk=s.pk)
        self.assertEquals(s.double_opted_in, False)
        self.assertEquals(s.double_opted_in_at, None)
Exemple #3
0
    def test_get_transfer_subscribe_200(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        response = self.get(
            reverse(
                'inkmail:transfer_subscription',
                kwargs={
                    "transfer_code": self.newsletter.hashid,
                },
            ),
            {
                'f': name,
                'e': email,
                # 'newsletter': self.newsletter.internal_name,
            },
        )
        self.assertEquals(response.status_code, 200)
        process_outgoing_message_queue()

        self.assertEquals(len(mail.outbox), 1)
        self.assertEquals(mail.outbox[0].subject,
                          self.newsletter.welcome_message.subject)
        om = OutgoingMessage.objects.all()[0]
        self.assertIn(
            om.render_email_string(
                self.newsletter.welcome_message.body_text_unrendered),
            mail.outbox[0].alternatives[0][0])
        self.assertIn(
            om.render_email_string(
                self.newsletter.welcome_message.body_text_unrendered,
                plain_text=True), mail.outbox[0].body)
        self.assertEquals(len(mail.outbox[0].to), 1)
        self.assertEquals(mail.outbox[0].to[0], email)
        self.assertEquals(mail.outbox[0].from_email,
                          self.newsletter.full_from_email)
Exemple #4
0
    def test_post_subscribe_adds_person_and_subscription(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            {
                'first_name': name,
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            },
        )

        self.assertEquals(response.status_code, 200)

        self.assertEquals(Person.objects.count(), 1)
        p = Person.objects.all()[0]
        self.assertEquals(p.first_name, name)
        self.assertEquals(p.email, email)

        self.assertEquals(Subscription.objects.count(), 1)
        s = Subscription.objects.all()[0]
        self.assertEquals(s.person, p)
        self.assertEquals(s.newsletter.name, self.newsletter.name)
        self.assertEquals(s.subscription_url, subscription_url)
        self.assertEquals(s.subscribed_from_ip, self._source_ip)
Exemple #5
0
    def test_post_subscribe_200(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            {
                'first_name': name,
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            },
        )
        self.assertEquals(response.status_code, 200)
        process_outgoing_message_queue()

        self.assertEquals(len(mail.outbox), 1)
        self.assertEquals(mail.outbox[0].subject,
                          self.newsletter.confirm_message.subject)
        self.assertEquals(OutgoingMessage.objects.count(), 1)
        om = OutgoingMessage.objects.all()[0]
        self.assertIn(
            om.render_email_string(
                self.newsletter.confirm_message.body_text_unrendered),
            mail.outbox[0].alternatives[0][0])
        self.assertIn(
            om.render_email_string(
                self.newsletter.confirm_message.body_text_unrendered,
                plain_text=True), mail.outbox[0].body)
        self.assertEquals(len(mail.outbox[0].to), 1)
        self.assertEquals(mail.outbox[0].to[0], email)
        self.assertEquals(mail.outbox[0].from_email,
                          self.newsletter.full_from_email)
Exemple #6
0
 def test_post_subscribe_200(self):
     email = Factory.rand_email()
     name = Factory.rand_name()
     subscription_url = Factory.rand_url()
     response = self.post(
         reverse('inkmail:subscribe', ),
         {
             'first_name': name,
             'email': email,
             'newsletter': self.newsletter.internal_name,
             'subscription_url': subscription_url,
         },
     )
     self.assertEquals(response.status_code, 200)
     self.assertEquals(HistoricalEvent.objects.count(), 1)
     self.assertEquals(Person.objects.count(), 1)
     self.assertEquals(Subscription.objects.count(), 1)
     he = HistoricalEvent.objects.all()[0]
     p = Person.objects.all()[0]
     s = Subscription.objects.all()[0]
     self.assertEquals(he.event_type, "subscribed")
     self.assertEquals(he.event_creator_type, "person")
     self.assertEquals(he.event_creator_pk, p.pk)
     self.assertHistoricalEventDataEquality(
         he,
         person=p,
         event_type="subscribed",
         newsletter=self.newsletter,
         subscription=s,
     )
Exemple #7
0
    def test_clicked_confirm_a_second_time(self):
        self.assertEquals(Subscription.objects.count(), 0)
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse(
                'inkmail:subscribe',
            ),
            {
                'first_name': name,
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            },
        )
        self.assertEquals(response.status_code, 200)
        process_outgoing_message_queue()

        self.assertEquals(len(mail.outbox), 1)
        self.assertEquals(Subscription.objects.count(), 1)
        s = Subscription.objects.all()[0]
        self.assertIn(s.opt_in_link, mail.outbox[0].body)

        self.get(s.opt_in_link)
        s = Subscription.objects.get(pk=s.pk)
        self.assertEquals(s.double_opted_in, True)
        self.assertBasicallyEqualTimes(s.double_opted_in_at, self.now())
        first_time = s.double_opted_in_at

        # Click it again
        self.get(s.opt_in_link)
        s = Subscription.objects.get(pk=s.pk)
        self.assertEquals(s.double_opted_in, True)
        self.assertBasicallyEqualTimes(s.double_opted_in_at, first_time)
Exemple #8
0
    def test_basic_encryption(self):
        e = Factory.rand_str()
        historical_event = HistoricalEvent.objects.create()
        historical_event.event_data = e
        historical_event.save()
        historical_event = None
        historical_event = HistoricalEvent.objects.all()[0]

        self.assertEquals(historical_event.event_data, e)

        e = "😀💌❤️"
        historical_event = HistoricalEvent.objects.create()
        historical_event.event_data = e
        historical_event.save()
        historical_event = None
        historical_event = HistoricalEvent.objects.all()[0]
        self.assertEquals(historical_event.event_data, e)

        e = Factory.rand_text()
        historical_event = HistoricalEvent.objects.create()
        historical_event.event_data = e
        historical_event.save()
        historical_event = None
        historical_event = HistoricalEvent.objects.all()[0]
        self.assertEquals(historical_event.event_data, e)

        e = Factory.rand_email()
        historical_event = HistoricalEvent.objects.create()
        historical_event.event_data = e
        historical_event.save()
        historical_event = None
        historical_event = HistoricalEvent.objects.all()[0]
        self.assertEquals(historical_event.event_data, e)
Exemple #9
0
    def test_no_first_name_subscribe_adds_person_and_subscription(self):
        email = Factory.rand_email()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            json.dumps({
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            }),
            'json',
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
        )

        json_string = response.content.decode('utf-8')
        response_data = json.loads(json_string)
        self.assertEquals(response_data["success"], True)
        self.assertEquals(response.status_code, 200)

        self.assertEquals(Person.objects.count(), 1)
        p = Person.objects.all()[0]
        self.assertEquals(p.email, email)

        self.assertEquals(Subscription.objects.count(), 1)
        s = Subscription.objects.all()[0]
        self.assertEquals(s.person, p)
        self.assertEquals(s.newsletter.name, self.newsletter.name)
        self.assertEquals(s.subscription_url, subscription_url)
        self.assertEquals(s.subscribed_from_ip, self._source_ip)
Exemple #10
0
 def test_get_transfer_subscribe_200(self):
     email = Factory.rand_email()
     name = Factory.rand_name()
     response = self.get(
         reverse(
             'inkmail:transfer_subscription',
             kwargs={
                 "transfer_code": self.newsletter.hashid,
             },
         ),
         {
             'f': name,
             'e': email,
             # 'newsletter': self.newsletter.internal_name,
         },
     )
     self.assertEquals(response.status_code, 200)
     self.assertEquals(HistoricalEvent.objects.count(), 1)
     self.assertEquals(Person.objects.count(), 1)
     self.assertEquals(Subscription.objects.count(), 1)
     he = HistoricalEvent.objects.all()[0]
     p = Person.objects.all()[0]
     s = Subscription.objects.all()[0]
     self.assertEquals(he.event_type, "transfer-subscription")
     self.assertEquals(he.event_creator_type, "person")
     self.assertEquals(he.event_creator_pk, p.pk)
     self.assertHistoricalEventDataEquality(
         he,
         person=p,
         event_type="transfer-subscription",
         newsletter=self.newsletter,
         subscription=s,
     )
Exemple #11
0
    def test_basic_encryption(self):
        e = Factory.rand_str(include_emoji=False)
        self.assertEquals(e, decrypt(encrypt(e)))

        e = "😀💌❤️"
        self.assertEquals(e, decrypt(encrypt(e)))

        e = Factory.rand_text()
        self.assertEquals(e, decrypt(encrypt(e)))

        e = Factory.rand_email()
        self.assertEquals(e, decrypt(encrypt(e)))
Exemple #12
0
    def test_post_subscribe_200(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            {
                'first_name': name,
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            },
        )

        self.assertEquals(response.status_code, 200)
Exemple #13
0
    def test_newsletter_required(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            {
                'first_name': name,
                'email': email,
                'subscription_url': subscription_url,
            },
        )

        self.assertEquals(response.status_code, 422)

        self.assertEquals(Person.objects.count(), 0)
        self.assertEquals(Subscription.objects.count(), 0)
Exemple #14
0
    def test_get_transfer_subscribe_200(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        response = self.get(
            reverse(
                'inkmail:transfer_subscription',
                kwargs={
                    "transfer_code": self.newsletter.hashid,
                },
            ),
            {
                'f': name,
                'e': email,
                # 'newsletter': self.newsletter.internal_name,
            },
        )

        self.assertEquals(response.status_code, 200)
Exemple #15
0
    def test_post_subscribe_200(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            json.dumps({
                'first_name': name,
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            }),
            'json',
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
        )

        json_string = response.content.decode('utf-8')
        response_data = json.loads(json_string)
        self.assertEquals(response_data["success"], True)
        self.assertEquals(response.status_code, 200)
Exemple #16
0
    def test_import_with_no_newsletter_creates_person_but_not_subscription(
            self):
        self.assertEquals(Person.objects.count(), 0)
        self.assertEquals(Subscription.objects.count(), 0)

        import_source = Factory.rand_str()
        email = Factory.rand_email()
        subscribed_at = Factory.rand_datetime()
        subscription_url = Factory.rand_url()
        double_opted_in = Factory.rand_bool()

        self.newsletter.import_subscriber(
            import_source=import_source,
            email=email,
            subscribed_at=subscribed_at,
            subscription_url=subscription_url,
            double_opted_in=double_opted_in,
        )

        self.assertEquals(Person.objects.count(), 1)
        self.assertEquals(Newsletter.objects.count(), 1)
        self.assertEquals(Subscription.objects.count(), 0)
Exemple #17
0
    def test_newsletter_required(self):
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            json.dumps({
                'first_name': name,
                'email': email,
                'subscription_url': subscription_url,
            }),
            'json',
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
        )

        json_string = response.content.decode('utf-8')
        response_data = json.loads(json_string)
        self.assertEquals(response_data["success"], False)
        self.assertEquals(response.status_code, 422)

        self.assertEquals(Person.objects.count(), 0)
        self.assertEquals(Subscription.objects.count(), 0)
Exemple #18
0
    def test_unsubscribe_resubscribe_has_records_of_all_actions(self):
        self.newsletter = Factory.newsletter()
        self.assertEquals(HistoricalEvent.objects.count(), 0)
        email = Factory.rand_email()
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            {
                'first_name': name,
                'email': email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            },
        )

        self.assertEquals(response.status_code, 200)

        self.assertEquals(Person.objects.count(), 1)
        self.assertEquals(Subscription.objects.count(), 1)
        p = Person.objects.all()[0]
        s = Subscription.objects.all()[0]

        self.assertEquals(HistoricalEvent.objects.count(), 1)
        he = HistoricalEvent.objects.order_by("-created_at").all()[0]
        self.assertEquals(he.event_type, "subscribed")
        self.assertEquals(he.event_creator_type, "person")
        self.assertEquals(he.event_creator_pk, p.pk)
        self.assertHistoricalEventDataEquality(
            he,
            person=p,
            event_type="subscribed",
            newsletter=self.newsletter,
            subscription=s,
        )

        # Unsubscribe
        om = OutgoingMessage.objects.all()[0]
        self.get(om.unsubscribe_link)

        s = Subscription.objects.get(pk=s.pk)
        self.assertEquals(HistoricalEvent.objects.count(), 2)
        he = HistoricalEvent.objects.order_by("-created_at").all()[0]
        self.assertEquals(he.event_type, "unsubscribe")
        self.assertEquals(he.event_creator_type, "person")
        self.assertEquals(he.event_creator_pk, p.pk)
        self.assertHistoricalEventDataEquality(
            he,
            person=p,
            event_type="unsubscribe",
            subscription=s,
            outgoingmessage=om,
        )

        # Fetch updated subscription
        s = Subscription.objects.get(pk=s.pk)

        # Re-subscribe
        name = Factory.rand_name()
        subscription_url = Factory.rand_url()
        response = self.post(
            reverse('inkmail:subscribe', ),
            {
                'first_name': name,
                'email': p.email,
                'newsletter': self.newsletter.internal_name,
                'subscription_url': subscription_url,
            },
        )
        self.assertEquals(response.status_code, 200)
        s = Subscription.objects.get(pk=s.pk)
        p = Person.objects.get(pk=p.pk)

        self.assertEquals(HistoricalEvent.objects.count(), 3)
        he = HistoricalEvent.objects.order_by("-created_at").all()[0]
        self.assertEquals(he.event_type, "subscribed")
        self.assertEquals(he.event_creator_type, "person")
        self.assertEquals(he.event_creator_pk, p.pk)
        self.assertHistoricalEventDataEquality(
            he,
            person=p,
            event_type="subscribed",
            newsletter=self.newsletter,
            subscription=s,
        )

        process_outgoing_message_queue()

        # Re-double-opt-in
        self.get(s.opt_in_link)

        s = Subscription.objects.get(pk=s.pk)
        p = Person.objects.get(pk=p.pk)

        self.assertEquals(HistoricalEvent.objects.count(), 4)
        he = HistoricalEvent.objects.order_by("-created_at").all()[0]
        self.assertEquals(he.event_type, "double-opt-in")
        self.assertEquals(he.event_creator_type, "person")
        self.assertEquals(he.event_creator_pk, p.pk)
        self.assertHistoricalEventDataEquality(
            he,
            person=p,
            event_type="double-opt-in",
            newsletter=self.newsletter,
            subscription=s,
        )
Exemple #19
0
    def test_import_adds_person_to_newsletter(self):
        s = None
        p = None
        self.assertEquals(Person.objects.count(), 0)
        self.assertEquals(Subscription.objects.count(), 0)

        import_source = Factory.rand_str()
        email = Factory.rand_email()
        subscribed_at = Factory.rand_datetime()
        subscription_url = Factory.rand_url()
        double_opted_in = Factory.rand_bool()
        double_opted_in_at = Factory.rand_datetime()
        first_name = Factory.rand_name()
        last_name = Factory.rand_name()
        subscription_ip = Factory.rand_ip()
        time_zone = Factory.rand_timezone()
        import_time = timezone.now()

        self.newsletter.import_subscriber(
            import_source=import_source,
            email=email,
            subscribed_at=subscribed_at,
            subscription_url=subscription_url,
            double_opted_in=double_opted_in,
            double_opted_in_at=double_opted_in_at,
            first_name=first_name,
            last_name=last_name,
            subscription_ip=subscription_ip,
            time_zone=time_zone,
            newsletter_name=self.newsletter.internal_name,
        )

        self.assertEquals(Person.objects.count(), 1)
        self.assertEquals(Newsletter.objects.count(), 1)
        self.assertEquals(Subscription.objects.count(), 1)
        p = Person.objects.all()[0]
        s = Subscription.objects.all()[0]

        # Check person
        self.assertEquals(p.first_name, first_name)
        self.assertEquals(p.last_name, last_name)
        self.assertEquals(p.email, email)
        self.assertEquals(p.hashed_first_name, lookup_hash(first_name))
        self.assertEquals(p.hashed_last_name, lookup_hash(last_name))
        self.assertEquals(p.hashed_email, lookup_hash(email))
        self.assertEquals(p.email_verified, double_opted_in)
        self.assertEquals(p.time_zone, time_zone)
        self.assertEquals(p.was_imported, True)
        self.assertBasicallyEqualTimes(p.was_imported_at, import_time)
        self.assertEquals(p.import_source, import_source)
        self.assertEquals(p.marked_troll, False)
        self.assertEquals(p.marked_troll_at, None)
        self.assertEquals(p.banned, False)
        self.assertEquals(p.banned_at, None)
        self.assertEquals(p.hard_bounced, False)
        self.assertEquals(p.hard_bounced_at, None)
        self.assertEquals(p.hard_bounced_reason, None)
        self.assertEquals(p.hard_bounced_message, None)
        self.assertEquals(p.never_contact_set, False)
        self.assertEquals(p.never_contact_set_at, None)
        self.assertEquals(p.personal_contact, False)

        # Check subscription
        self.assertEquals(s.person, p)
        self.assertEquals(s.newsletter, self.newsletter)
        self.assertBasicallyEqualTimes(s.subscribed_at, import_time)
        self.assertEquals(s.subscription_url, subscription_url)
        self.assertEquals(s.subscribed_from_ip, subscription_ip)
        self.assertEquals(s.was_imported, True)
        self.assertBasicallyEqualTimes(s.was_imported_at, import_time)
        self.assertEquals(s.import_source, import_source)
        self.assertEquals(s.double_opted_in, double_opted_in)
        self.assertBasicallyEqualTimes(s.double_opted_in_at,
                                       double_opted_in_at)
        self.assertEquals(s.has_set_never_unsubscribe, False)
        self.assertEquals(s.unsubscribed, False)
        self.assertEquals(s.unsubscribed_at, None)