Example #1
0
    def test_create_first_sends_email(self):
        """If, on POST, user is captain of one ep team, they get an email."""

        captain_first = User.create(name='foo', email='*****@*****.**')
        captain_first.put()

        # Demo program doesn't trigger any emails.
        team_params = {'name': 'Demo 1', 'program_id': self.demo_program.uid}
        self.testapp.post_json(
            '/api/teams',
            team_params,
            headers=self.login_headers(captain_first),
        )
        self.assertEqual(Email.count(), 0)

        # First team created for EP sends email (even though they have a team
        # on another program).
        team_params = {'name': 'EP 1', 'program_id': self.ep_program.uid}
        result1 = self.testapp.post_json(
            '/api/teams',
            team_params,
            headers=self.login_headers(captain_first),
        )
        self.assertEqual(Email.count(), 1)
        email1 = Email.get()[0]
        self.assertEqual(email1.template, 'ep19/welcome.html')
        self.assertEqual(email1.to_address, captain_first.email)

        # User who already is on another team, but isn't captain, then creates
        # a team, gets the email.
        team1_id = json.loads(result1.body)['uid']
        member_first = User.create(name="Member", email="*****@*****.**",
                                   owned_teams=[team1_id])
        member_first.put()
        team_params = {'name': 'EP 2', 'program_id': self.ep_program.uid}
        result2 = self.testapp.post_json(
            '/api/teams',
            team_params,
            headers=self.login_headers(member_first),
        )
        email2 = next(e for e in Email.get()
                     if e.to_address == member_first.email)
        self.assertEqual(email2.template, 'ep19/welcome.html')

        # User who creates a second team doesn't get the email.
        team_params = {'name': 'EP 3', 'program_id': self.ep_program.uid}
        result3 = self.testapp.post_json(
            '/api/teams',
            team_params,
            headers=self.login_headers(captain_first),
        )
        captain_first_emails = [e for e in Email.get()
                                if e.to_address == captain_first.email]

        # Only the first email is there, no second email.
        self.assertEqual(len(captain_first_emails), 1)
Example #2
0
    def test_put_artifact_url_sends_email(self):
        """PUT response has special jwt giving permission on Neptune."""
        user = User.create(name='foo', email='*****@*****.**')
        team = Team.create(name="Foo Team",
                           captain_id=user.uid,
                           program_id="Program_foo")
        survey = Survey.create(team_id=team.uid)
        user.owned_teams = [team.uid]

        team.put()
        survey.put()
        user.put()

        artifact_url = 'https://www.example.com/artifact'

        # Not changing the artifact does NOT trigger an email.
        self.testapp.put_json(
            '/api/surveys/{}'.format(survey.uid),
            {'meta': {
                'artifact_url': ''
            }},
            headers=self.login_headers(user),
        )
        self.assertEqual(Email.count(), 0)

        # Changing it triggers an email.
        self.testapp.put_json(
            '/api/surveys/{}'.format(survey.uid),
            {'meta': {
                'artifact_use': 'true',
                'artifact_url': artifact_url
            }},
            headers=self.login_headers(user),
        )

        emails = Email.get()
        self.assertEqual(len(emails), 1)
        email = emails[0]
        self.assertIn(artifact_url, email.html)
        self.assertIn(team.name, email.html)

        # Sending a PUT **without** changing it does NOT trigger an email.
        self.testapp.put_json(
            '/api/surveys/{}'.format(survey.uid),
            {'meta': {
                'artifact_use': 'true',
                'artifact_url': artifact_url
            }},
            headers=self.login_headers(user),
        )

        self.assertEqual(Email.count(), 1)  # same as before
Example #3
0
    def test_sends_correct_email_for_ep(self, *args):
        # Note that what this handler should do if there isn't a current cycle
        # is tested in test_surveys.TestSurveys.test_should_notify
        program, captain, team, classrooms, cycle = self.create('ep19')

        # Mock Neptune's participation API.

        class MockFetchResult(object):
            status_code = 200
            content = r'{}'

        with patch.object(urlfetch, 'fetch',
                          return_value=MockFetchResult()) as _:
            handler = task_handlers.TeamCycleEmails()
            # Run the task.
            handler.post(team.uid)

        emails = Email.get()
        self.assertEqual(len(emails), 1)
        self.assertIn('ep19-cycle-progress-email', emails[0].html)
Example #4
0
    def test_create_first_sends_email(self):
        """If, on POST, user is admin of one Elevate org, they get an email."""

        admin_first = User.create(name='foo', email='*****@*****.**')
        admin_first.put()

        # Creating an org in EP doesn't trigger any emails.
        org_params = {'name': 'EP 1', 'program_id': self.program.uid}
        self.testapp.post_json(
            '/api/organizations',
            org_params,
            headers=self.login_headers(admin_first),
        )
        self.assertEqual(Email.count(), 0)

        # First org created for BELESET sends email (even though they have an
        # org in another program).
        org_params1 = {'name': 'Org 1', 'program_id': self.beleset_program.uid}
        result1 = self.testapp.post_json(
            '/api/organizations',
            org_params1,
            headers=self.login_headers(admin_first),
        )
        self.assertEqual(Email.count(), 1)
        email1 = Email.get()[0]
        self.assertEqual(email1.template, 'beleset19/welcome.html')
        self.assertIn(admin_first.email, email1.to_address)

        # User who creates a second org doesn't get the email.
        org_params2 = {'name': 'Org 2', 'program_id': self.beleset_program.uid}
        result2 = self.testapp.post_json(
            '/api/organizations',
            org_params2,
            headers=self.login_headers(admin_first),
        )

        # Only the first email is there, no second email.
        self.assertEqual(Email.count(), 1)