def test_default_sender(self):
        email_addr = '*****@*****.**'
        sender = config.from_server_email_address

        response = self.testapp.post_json(
            '/api/register',
            {
                'domain': 'https://neptune.perts.net',
                # no platform, defaults to Neptune
                'email': email_addr,
                'program_label': self.program_label,
            },
            status=204,
        )

        # Email was created with from/reply properties.
        emails = Email.get(to_address=email_addr)
        email = emails[0]
        self.assertEqual(
            email.mandrill_template_content['contact_email_address'],
            sender,
        )
        self.assertEqual(email.from_address, sender)
        self.assertEqual(email.reply_to, sender)
        # Should be hard-coded in api_helper
        self.assertEqual(email.from_name, 'PERTS')
    def test_register_no_cohort(self):
        email = '*****@*****.**'

        response = self.testapp.post_json(
            '/api/register',
            {
                'email': email,
                'password': '******',
                'role': 'principal',
                'platform': 'neptune',
                'domain': 'http://localhost:8888',
                'program_label': self.program_label,
                # no cohort
            },
            status=204,
        )

        # User now exists
        user = User.get_by_auth('email', email)
        self.assertIsNotNone(user)

        # Email was created
        emails = Email.get(to_address=email)
        self.assertEqual(len(emails), 1)
        self.assertEqual(emails[0].mandrill_template, 'neptune-register-new')
    def test_register_new(self):
        email_addr = '*****@*****.**'
        role = 'principal'
        utm_source = 'foo source'

        response = self.testapp.post_json(
            '/api/register',
            {
                'email': email_addr,
                'role': role,
                'program_label': self.program_label,
                'cohort': self.cohort_label,
                'utm_source': utm_source,
            },
            status=204,
        )

        # User now exists
        user = User.get_by_auth('email', email_addr)
        self.assertIsNotNone(user)
        self.assertEqual(user.role, role)

        # Email was created
        emails = Email.get(to_address=email_addr)
        self.assertEqual(len(emails), 1)

        # Email has correct data.
        email = emails[0]
        self.assertEqual(email.mandrill_template, 'neptune-register-new')
        self.assertEqual(
            email.mandrill_template_content['program_label'],
            self.program_label,
        )
        self.assertEqual(
            email.mandrill_template_content['program_name'],
            self.program_name,
        )
        exp_str = email.mandrill_template_content['expiration_datetime']
        self.assertEqual(type(exp_str), unicode)
        self.assertGreater(len(exp_str), 0)

        # Link in email has correct query string.
        parts = urlparse.urlsplit(email.mandrill_template_content['link'])
        scheme, netloc, path, query_string, fragment = parts
        query_params = dict(urlparse.parse_qsl(query_string))

        self.assertTrue('/set_password' in path)
        self.assertEqual(
            query_params, {
                'program': self.program_label,
                'cohort': self.cohort_label,
                'utm_source': utm_source,
            })
예제 #4
0
    def mail_log(self):
        body = self.body + self.get_recent_log()
        subject = self.subject + self.get_error_summary()
        # Ignore the normal email queueing / spam-prevention system because the
        # addressees are devs, and they can customize the deluge themselves.
        email = Email.create(to_address=self.to_addresses,
                             subject=subject,
                             body=body)
        email.put()
        # We want to send this immediately, not in batches.
        email.deliver()

        self.last_email = self.now
        return (subject, body)
    def test_register_triton_pending(self):
        email_addr = '*****@*****.**'
        utm_source = 'foo source'

        user = User.create(email=email_addr)
        user.put()

        response = self.testapp.post_json(
            '/api/register',
            {
                'platform': 'triton',
                'domain': 'https://copilot.perts.net',
                'email': email_addr,
                'program_label': self.program_label,
                'utm_source': utm_source,
            },
            status=204,
        )

        # Email was created
        emails = Email.get(to_address=email_addr)
        self.assertEqual(len(emails), 1)

        # Email has correct data.
        email = emails[0]
        self.assertEqual(email.mandrill_template, 'triton-register-pending')
        self.assertEqual(
            email.mandrill_template_content['program_label'],
            self.program_label,
        )
        self.assertEqual(
            email.mandrill_template_content['program_name'],
            self.program_name,
        )
        exp_str = email.mandrill_template_content['expiration_datetime']
        self.assertEqual(type(exp_str), unicode)
        self.assertGreater(len(exp_str), 0)

        # Link in email is correct.
        parts = urlparse.urlsplit(email.mandrill_template_content['link'])
        scheme, netloc, path, query_string, fragment = parts
        self.assertEqual(netloc, 'copilot.perts.net')
        self.assertTrue('/set_password' in path)
        query_params = dict(urlparse.parse_qsl(query_string))
        self.assertEqual(query_params, {
            'program': self.program_label,
            'utm_source': utm_source,
        })
    def test_register_exists(self):
        email_addr = '*****@*****.**'
        role = 'principal'
        utm_source = 'foo source'

        user = User.create(email=email_addr, hashed_password='******')
        user.put()

        response = self.testapp.post_json(
            '/api/register',
            {
                'email': email_addr,
                'role': role,
                'program_label': self.program_label,
                'cohort': self.cohort_label,
                'utm_source': utm_source,
            },
            status=204,
        )

        # Email was created
        emails = Email.get(to_address=email_addr)
        self.assertEqual(len(emails), 1)

        # Email has correct data.
        email = emails[0]
        self.assertEqual(email.mandrill_template, 'neptune-register-exists')
        self.assertEqual(
            email.mandrill_template_content['program_label'],
            self.program_label,
        )
        self.assertEqual(
            email.mandrill_template_content['program_name'],
            self.program_name,
        )

        # Link in email is correct.
        parts = urlparse.urlsplit(email.mandrill_template_content['link'])
        scheme, netloc, path, query_string, fragment = parts
        self.assertEqual(path, '/login')
        query_params = dict(urlparse.parse_qsl(query_string))
        self.assertEqual(
            query_params, {
                'email': email_addr,
                'program': self.program_label,
                'cohort': self.cohort_label,
                'utm_source': utm_source,
            })
예제 #7
0
    def create_email(self):
        """Create an email to match the content of the notification if user
        has not set their notification preferences to disable emails.
        """
        recipient = self.key.parent().get()

        # The default is to send an email, unless the user has disabled
        if recipient.notification_option.get('email', None) == 'no':
            return None

        return Email.create(
            to_address=recipient.email,
            subject=self.subject,
            template='notification.html',
            template_data={
                'subject': self.subject,
                'body': self.body,
                'link': self.link
            },
        )
    def test_invite_triton_exists(self):
        inviter = User.create(email='*****@*****.**')
        inviter.put()
        invitee = User.create(email='*****@*****.**', hashed_password='******')
        invitee.put()

        response = self.testapp.post_json(
            '/api/invitations',
            {
                'email': invitee.email,
                'platform': 'triton',
                'template_content': {
                    'foo': 'bar'
                },
                'domain': 'https://copilot.perts.net',
                'from_address': '*****@*****.**',
                'from_name': 'Copilot',
                'reply_to': '*****@*****.**',
            },
            headers=jwt_headers(inviter),
        )
        response_user = json.loads(response.body)
        self.assertRegexpMatches(response_user['uid'], r'^User_')

        # One email queued.
        emails = Email.get()
        self.assertEqual(len(emails), 1)
        email = emails[0]

        # To the right person.
        email.to = invitee.email

        # With the right template.
        self.assertEqual(email.mandrill_template, 'triton-invite-exists')

        # And right template params.
        self.assertEqual(type(email.mandrill_template_content['foo']), unicode)
        self.assertGreater(len(email.mandrill_template_content['foo']), 0)

        self.assertIn('/login', email.mandrill_template_content['link'])
    def test_custom_sender(self):
        # Mock a program that defines a special contact email address.
        program_label = 'ep'
        sender = '*****@*****.**'
        Program.mock_program_config(
            program_label,
            {
                'name': 'Engagement Project',
                'contact_email_address': sender,
            },
        )

        email_addr = '*****@*****.**'

        response = self.testapp.post_json(
            '/api/register',
            {
                'domain': 'https://copilot.perts.net',
                'platform': 'triton',
                'email': email_addr,
                'program_label': program_label,
            },
            status=204,
        )

        # Email was created with from/reply properties.
        emails = Email.get(to_address=email_addr)
        email = emails[0]
        self.assertEqual(
            email.mandrill_template_content['contact_email_address'],
            sender,
        )
        self.assertEqual(email.from_address, sender)
        self.assertEqual(email.reply_to, sender)
        # Should be hard-coded in api_helper
        self.assertEqual(email.from_name, 'Copilot')