def test_unsubscribe_sends_goodbye_email(self):
        # Create user and response
        user, request = self.create_user_and_request('*****@*****.**')

        # Confirm user
        user.is_active = True
        user.save()

        # Create an unsubscribe link
        unsubscribe_link = create_secure_link(request=request,
                                              user=user,
                                              viewname='unsubscribe_user')

        # Follow unsubscribe link
        response_unsub = self.client.get(unsubscribe_link)

        # Get the most recent email message
        msg = mail.outbox[0]

        # Check if msg has correct details
        self.assertSequenceEqual(msg.recipients(), [user.email])
        self.assertEqual(msg.subject, msg_comp['goodbye']['subject'])
        self.assertIn(user.email, msg.body)
        self.assertEqual(msg.from_email, from_address['notifications'])
        self.assertIn(user.email, msg.alternatives[0][0])
Exemple #2
0
    def create_email_alerts(self):
        """Creates email alerts for each active user.

        The resulting collection of email alerts will then be 
        assigned to this instance's email_alerts attribute and returned
        as a list.
        """
        alerts = []
        for user in OutageNotice.get_all_active_users():
            if self.is_unsent(user):
                alert = self.email_alerts.create(recipient=user)
                alert.set_alert_subject_line(notice=self)
                alert.render_email_body(template='email_alerts/alert.html',
                                        context={
                                            'recipient':
                                            user,
                                            'notice':
                                            self,
                                            'unsubscribe_link':
                                            create_secure_link(
                                                user=user,
                                                viewname='unsubscribe_user')
                                        })
                alert.save()
                alerts.append(alert)

        return alerts
    def test_invalid_confirmation_links(self):
        address_1 = '*****@*****.**'
        address_2 = '*****@*****.**'

        response_1 = self.client.post(reverse('register_new_email'),
                                      data={'email': address_1})

        new_user_1 = Subscriber.objects.get(email=address_1)
        new_user_2 = Subscriber.objects.create(email=address_2)

        request_1 = response_1.wsgi_request

        confirmation_link_1 = create_secure_link(request=request_1,
                                                 user=new_user_2,
                                                 viewname='verify_email',
                                                 external=False)

        # Replace last 5 alphanumeric chars in URL with 'xxxxx'
        fake_link = re.sub(r'[a-z0-9]{5}$', 'xxxxx', confirmation_link_1)

        verification_response = self.client.get(fake_link)
        this_user = Subscriber.objects.get(email=address_1)
        self.assertIs(this_user.is_active, False)
        self.assertRedirects(
            verification_response,
            reverse('verification_results', kwargs={'results': 'failed'}))
Exemple #4
0
    def create_email_link(self, user, mode=''):
        """Helper method that generates full absolute URL.

        Args:
            user (Subscriber object): user to process
            mode (str): type of transaction ('confirm', 'unsubscribe')
        """
        path_url = ''

        if mode == 'confirm':
            path_url = create_secure_link(user=user,
                                          viewname='verify_email',
                                          external=False)
        elif mode == 'unsubscribe':
            path_url = create_secure_link(user=user,
                                          viewname='unsubscribe_user',
                                          external=False)

        return urljoin(self.live_server_url, path_url)
Exemple #5
0
    def send_transactional_email(self, request=None, message_type=''):
        """Creates transactional message of given type and sends it to user.

        Args:
            request (Request object): the request passed to the view 
                that called this function.
            message_type (str): the type of transactional message to 
                create and send; can be one of:
                * 'confirm'
                * 'welcome'
                * 'optout'
                * 'goodbye'

        Returns:
            TransactionalEmail object
        """
        if message_type not in message_components:
            raise TypeError(f'{message_type} is not a valid message type.')

        # Create and initialize the transactional email object
        components = message_components[message_type]
        email_obj = self.transactionalemail_set.create(
            subject_line=components['subject'], message_type=message_type)

        # Define the template context
        context = {}
        context['message_title'] = email_obj.subject_line
        if message_type == 'goodbye':
            context['recipient'] = self
        else:
            secure_link = create_secure_link(
                request=request,
                user=self,
                viewname=components['target_view'],
                external=True)
            if message_type == 'confirm':
                context['confirmation_link'] = secure_link
            elif message_type == 'welcome':
                context['recipient'] = self
                context['unsubscribe_link'] = secure_link
                context['vcard_link'] = get_external_link_for_static_file(
                    'others/dvoalerts.vcf')
            elif message_type == 'optout':
                context['recipient'] = self
                context['optout_link'] = secure_link

        # Render the email body and HTML content
        email_obj.render_email_body(template=components['template'],
                                    context=context if context else None)

        # Send the email
        process_and_send_email.delay(email_id=email_obj.pk)
        return email_obj
    def test_successful_confirm_triggers_sendout(self, send_welcome_email):
        # Create a user and request object
        new_user, request = self.create_user_and_request(
            email='*****@*****.**')

        # Create a confirmation link
        confirmation_link = create_secure_link(request=request,
                                               user=new_user,
                                               viewname='verify_email')

        # Follow the confirmation link
        response = self.client.get(confirmation_link)
        self.assertEqual(send_welcome_email.called, True)
    def test_successful_optout_triggers_sendout(self, send_goodbye_email):
        # Create a user and request object
        new_user, request = self.create_user_and_request(
            email='*****@*****.**')

        # Activate the new user
        new_user.is_active = True
        new_user.save()

        # Create an optout link for the new user
        unsubscribe_link = create_secure_link(request=request,
                                              user=new_user,
                                              viewname='unsubscribe_user')

        # Follow the unsubscribe link
        response = self.client.get(unsubscribe_link)
        self.assertEqual(send_goodbye_email.called, True)
    def test_activation_uid_and_token(self):
        test_email_address = '*****@*****.**'
        response = self.client.post(reverse('register_new_email'),
                                    data={'email': test_email_address})
        new_user = Subscriber.objects.get(email=test_email_address)
        request = response.wsgi_request

        confirmation_link = create_secure_link(request=request,
                                               user=new_user,
                                               viewname='verify_email',
                                               external=True)

        verification_response = self.client.get(confirmation_link)
        this_user = Subscriber.objects.get(email=test_email_address)
        active_users = Subscriber.get_all_active_subscribers()

        self.assertIs(this_user.is_active, True)
        self.assertRedirects(
            verification_response,
            reverse('verification_results', kwargs={'results': 'success'}))
        self.assertEqual(active_users.count(), 1)
    def test_successful_confirmation_sends_welcome_email(self):
        # Create user and response
        user, request = self.create_user_and_request('*****@*****.**')

        # Create a confirmation link
        confirmation_link = create_secure_link(request=request,
                                               user=user,
                                               viewname='verify_email')

        # Activate confirmation link
        response_conf = self.client.get(confirmation_link)

        # Get the most recent email message
        msg = mail.outbox[0]

        # Check if msg has correct details
        self.assertSequenceEqual(msg.recipients(), [user.email])
        self.assertEqual(msg.subject, msg_comp['welcome']['subject'])
        self.assertIn(user.email, msg.body)
        self.assertEqual(msg.from_email, from_address['notifications'])
        self.assertIn(user.email, msg.alternatives[0][0])
        self.assertIn(
            get_external_link_for_static_file('others/dvoalerts.vcf'),
            msg.alternatives[0][0])
    def test_user_opt_out_flow(self):
        # Create confirmed user
        test_email_address = '*****@*****.**'
        user = Subscriber.objects.create(email=test_email_address)
        user.is_active = True
        user.save()

        # Get a request object
        response = self.client.get(reverse('homepage'))  # Could be any view
        request = response.wsgi_request

        # Create an opt out URL
        unsubscribe_link = create_secure_link(request=request,
                                              user=user,
                                              viewname='unsubscribe_user',
                                              external=True)

        # Unsubscribe the user
        response_unsub = self.client.get(unsubscribe_link)

        # Check the user object
        this_user = Subscriber.objects.get(email=test_email_address)
        active_users = Subscriber.get_all_active_subscribers()

        self.assertIs(this_user.is_active, False)
        self.assertEqual(active_users.count(), 0)

        # Redirect to successful opt out page
        self.assertRedirects(
            response_unsub,
            reverse('unsubscribe_results', kwargs={'results': 'success'}))

        # Check if prev_vew is deleted
        session = self.client.session
        with self.assertRaises(KeyError):
            session['prev_view']