def test_import_ignores_banned_people(self): self.assertEquals(Person.objects.count(), 0) self.assertEquals(Subscription.objects.count(), 0) banned_person = Factory.person() banned_person.ban() self.assertEquals(Person.objects.count(), 1) self.assertEquals(Subscription.objects.count(), 0) import_source = Factory.rand_str() email = banned_person.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, newsletter_name=self.newsletter.internal_name, overwrite=True, ) self.assertEquals(Person.objects.count(), 1) self.assertEquals(Newsletter.objects.count(), 1) self.assertEquals(Subscription.objects.count(), 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)
def test_email_is_skipped_if_you_come_back_first(self): self.seven_product = Factory.product(number_of_days=8, has_epilogue=True) self.purchase = Factory.productpurchase(person=self.person, product=self.seven_product) journey = Journey.objects.create(productpurchase=self.purchase, start_date=timezone.now()) day = journey.days[0] day.completed = True day.completed_at = timezone.now() day.save() self.assertEquals(len(mail.outbox), 0) send_pre_day_emails() self.assertEquals(len(mail.outbox), 0) # Day 2 with freeze_time(self.now() + datetime.timedelta(hours=12 + 0)): send_pre_day_emails() # I came back early for day 2, no email sent. self.assertEquals(len(mail.outbox), 0) day = journey.days[1] day.completed = True day.completed_at = self.now() day.save() send_pre_day_emails() # Day 3 with freeze_time(self.now() + datetime.timedelta(hours=23 + 24 * 1)): send_pre_day_emails() # I forgot day 3, email sent. self.assertEquals(len(mail.outbox), 1)
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)
def test_if_scheduled_for_future_does_not_send_until_future(self): people = [] num_subscribers = Factory.rand_int(end=20) for i in range(0, num_subscribers): s = Factory.subscription(newsletter=self.newsletter) s.double_opt_in() people.append(s) future_date = self.now() + datetime.timedelta(hours=1) self.scheduled_newsletter_message = Factory.scheduled_newsletter_message( newsletter=self.newsletter, send_at_date=future_date, send_at_hour=future_date.hour, send_at_minute=future_date.minute, use_local_time=False, ) self.subject = self.scheduled_newsletter_message.message.subject self.body = self.scheduled_newsletter_message.message.body_text_unrendered queue_newsletter_message(self.scheduled_newsletter_message.hashid) process_outgoing_message_queue() self.assertEquals(len(mail.outbox), 0) with freeze_time(self.now() + datetime.timedelta(hours=1)): process_outgoing_message_queue() self.assertEquals(len(mail.outbox), num_subscribers)
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)
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)
def test_extended_types_encryption(self): e = Factory.rand_phone() 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_name() 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.temp_password() 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_url() 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)
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, )
def test_get_transfer_subscribe_adds_person_and_subscription(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(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.subscription_url, "transfer-subscription") self.assertEquals(s.newsletter.name, self.newsletter.name) self.assertEquals(s.subscribed_from_ip, self._source_ip)
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)
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, )
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)
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)
def test_normal_page_renders(self): t = Factory.template(content='{{rendered_page_html|safe}}') self.page = Factory.page( template=t, ) url = reverse('website:page_or_post', kwargs={"page_slug": self.page.slug, },) response = self.get(url) self.assertEquals(response.status_code, 200)
def test_unsubscribe_resubscribe_updates_all_fields(self): self.assertEquals(len(mail.outbox), 0) self.create_subscribed_person() self.assertEquals(self.subscription.unsubscribed, False) self.assertEquals(self.subscription.unsubscribed_at, None) self.send_newsletter_message() self.assertEquals(len(mail.outbox), 1) m = mail.outbox[0] self.assertEquals(OutgoingMessage.objects.count(), 1) om = OutgoingMessage.objects.all()[0] # Unsubscribe self.assertIn(om.unsubscribe_link, m.body) self.get(om.unsubscribe_link) # Fetch updated subscription self.subscription = Subscription.objects.get(pk=self.subscription.pk) self.assertEquals(self.subscription.unsubscribed, True) self.assertBasicallyEqualTimes(self.subscription.unsubscribed_at, self.now()) # Re-subscribe name = Factory.rand_name() subscription_url = Factory.rand_url() response = self.post( reverse( 'inkmail:subscribe', ), { 'first_name': name, 'email': self.person.email, 'newsletter': self.newsletter.internal_name, 'subscription_url': subscription_url, }, ) self.assertEquals(response.status_code, 200) self.assertEquals(Subscription.objects.count(), 1) self.subscription = Subscription.objects.get(pk=self.subscription.pk) self.assertEquals(self.subscription.unsubscribed, False) self.assertEquals(self.subscription.unsubscribed_at, None) process_outgoing_message_queue() self.assertEquals(len(mail.outbox), 2) self.assertEquals(Subscription.objects.count(), 1) s = Subscription.objects.all()[0] self.assertIn(s.opt_in_link, mail.outbox[1].body) # Re-double-opt-in self.get(s.opt_in_link) self.subscription = Subscription.objects.get(pk=self.subscription.pk) self.assertEquals(self.subscription.unsubscribed, False) self.assertEquals(self.subscription.unsubscribed_at, None) process_outgoing_message_queue() # Check fields self.person = Person.objects.get(pk=self.person.pk) self.assertEquals(self.person.first_name, name) self.assertEquals(self.subscription.subscription_url, subscription_url)
def test_basic_body_override_page_renders(self): t = Factory.template(body_override=Factory.rand_text(), ) self.page = Factory.page(template=t, ) rendered = self.page.rendered self.assertEquals( rendered, """<!doctype html > <html itemscope itemtype="http://schema.org/CreativeWork" class="%(html_extra_classes)s" lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>%(title)s</title> <meta name="description" content="%(description)s"> <!-- Open Graph data --> <meta property="og:title" content="%(title)s" /> <meta property="og:type" content="article" /> <meta property="og:url" content="" /> <meta property="og:image" content="" /> <meta property="og:description" content="%(description)s" /> <meta property="og:site_name" content="" /> <link rel="canonical" href="" /> <meta property="article:published_time" content="" /> <meta property="article:modified_time" content="" /> <meta property="article:section" content="Writing" /> <meta property="article:tag" content="Writing" /> <!-- Schema.org markup for Google+ --> <meta itemprop="name" content="%(title)s"> <meta itemprop="description" content="%(description)s"> <meta itemprop="author" content=""> <meta itemprop="provider" content=""> %(css)s <script> window.inkshop = window.inkshop || {}; window.inkshop.site_data; </script> %(inkdots)s </head> <body > %(body_override)s %(js)s </body> </html>""" % { "title": self.page.title, "description": self.page.description, "css": self.page.template.css, "js": self.page.template.js, "html_extra_classes": self.page.template.html_extra_classes, "body_override": self.page.template.body_override, "inkdots": inkdots_template, })
def test_basic_no_frills_page_renders(self): t = Factory.template( everything_override="This is as basic as it gets.", ) self.page = Factory.page( template=t, ) rendered = self.page.rendered self.assertEquals(rendered, """This is as basic as it gets.""")
def test_template_body_override_works(self): self.template = Factory.template(body_override=Factory.rand_text()) t = get_template(self.template.name) rendered = t.render({}) self.assertIn(self.template.body_override, rendered) self.assertNotIn(self.template.nav, rendered) # self.assertNotIn("{{ content }}", rendered) self.assertNotIn(self.template.footer, rendered)
def test_root_page_renders(self): t = Factory.template(content='{{rendered_page_html|safe}}') self.page = Factory.page( template=t, root_page=True, slug='', ) url = reverse('website:root_page_or_post') response = self.get(url) self.assertEquals(response.status_code, 200)
def test_if_unsubscriber_link_is_removed_by_trickery_message_refuses_to_send(self): self.assertEquals(len(mail.outbox), 0) self.create_subscribed_person() Factory.message() n = self.newsletter n.unsubscribe_footer = "" n.save() self.send_newsletter_message() self.assertEquals(len(mail.outbox), 0)
def test_sends_to_everyone_in_list(self): people = [] num_subscribers = Factory.rand_int(end=20) for i in range(0, num_subscribers): s = Factory.subscription(newsletter=self.newsletter) s.double_opt_in() people.append(s) self.send_newsletter_message() self.assertEquals(len(mail.outbox), num_subscribers)
def setUp(self, *args, **kwargs): self.person = Factory.person() self.product = Factory.product(has_epilogue=True) self.purchase = Factory.productpurchase(person=self.person, product=self.product) self.journey = Journey.objects.create(productpurchase=self.purchase, start_date=timezone.now()) self.day1 = self.journey.days[0] # self.pw = Factory.rand_str(include_emoji=False) # self.person.set_password(self.pw) super(TestDailyEmails, self).setUp(*args, **kwargs)
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)))
def test_extended_types_encryption(self): e = Factory.rand_phone() self.assertEquals(e, decrypt(encrypt(e))) e = Factory.rand_name() self.assertEquals(e, decrypt(encrypt(e))) e = Factory.temp_password() self.assertEquals(e, decrypt(encrypt(e))) e = Factory.rand_url() self.assertEquals(e, decrypt(encrypt(e)))
def test_hard_bounce_marks_hard_bounced(self): s = Factory.subscription() p = s.person self.assertEquals(p.hard_bounced, False) self.assertEquals(p.hard_bounced_at, None) self.assertEquals(p.hard_bounced_message, None) bounce_reason = Factory.rand_text() p.hard_bounce(bounce_reason) self.assertEquals(p.hard_bounced, True) self.assertEquals(p.hard_bounced_at, None) self.assertEquals(p.hard_bounce_reason, bounce_reason)
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)
def test_existing_users_crazy_missing_days_handled(self): self.seven_product = Factory.product(number_of_days=8, has_epilogue=True) self.purchase = Factory.productpurchase(person=self.person, product=self.seven_product) journey = Journey.objects.create(productpurchase=self.purchase, start_date=timezone.now()) day = journey.days[0] day.completed = True day.completed_at = timezone.now() day.save() self.assertEquals(len(mail.outbox), 0) # Day 2 with freeze_time(self.now() + datetime.timedelta(hours=12 + 0)): day = journey.days[1] day.completed = True day.completed_at = self.now() day.save() # Day 3 with freeze_time(self.now() + datetime.timedelta(hours=12 + 24)): day = journey.days[2] day.completed = True day.completed_at = self.now() day.save() # Day 4 with freeze_time(self.now() + datetime.timedelta(hours=12 + 45)): day = journey.days[3] day.completed = True day.completed_at = self.now() day.save() # Somehow skip completing day 5, do day 6 though. with freeze_time(self.now() + datetime.timedelta(hours=12 + 24 * 3)): day = journey.days[5] day.completed = True day.completed_at = self.now() day.save() # Then, they stopped last week. # Should be for day 7, I guess. with freeze_time(self.now() + datetime.timedelta(hours=12 + 24 * 12)): send_pre_day_emails() self.assertEquals(len(mail.outbox), 1) self.assertIn(ProductDay.objects.get( day_number=7, product=self.seven_product ).pre_day_message.body_text_unrendered, mail.outbox[0].body)
def bestimator_experiment(request, slug): if "bestid" not in request.GET: now = timezone.now() bestid = "%s%s" % (now.time(), Factory.rand_str(length=10, include_emoji=False)) bestid = bestid.encode('utf-8').hex() return redirect("%s?bestid=%s" % ( reverse("products:bestimator_experiment", args=(slug, )), bestid, )) else: bestid = request.GET["bestid"] date = datetime.datetime.today() experiment = BestimatorExperiment.objects.get(slug=slug) answer_objs = BestimatorAnswer.objects.filter( experiment_choice__experiment=experiment, session_hash=bestid, ) if answer_objs.count() > 0: answers = {} for a in answer_objs: answers[a.experiment_choice.slug] = a return locals()
def test_stores_event_in_archive(self): s = Factory.subscription() p = s.person self.assertEquals(HistoricalEvent.objects.count(), 0) reason = Factory.rand_str() p.ban(reason) self.assertEquals(HistoricalEvent.objects.count(), 1) h = HistoricalEvent.objects.all()[0] self.assertEquals(h.event_type, "ban") self.assertEquals(h.event_creator_type, "test") self.assertEquals(h.event_creator_pk, 1) self.assertEquals(h.event_creator_data, { "person": p.get_data_dict(), "reason": reason, })
def handle(self, *args, **options): cache.delete(NEXT_SURVEY_PATH_KEY) for i in range(100): Factory.survey_path(order=i)