Exemplo n.º 1
0
 def test_get_last_report_future(self):
     past_date = now().date() - timedelta(weeks=5)
     future_date = now().date() + timedelta(weeks=2)
     user = UserFactory.create(groups=["Rep"])
     NGReportFactory.create(user=user, report_date=past_date)
     NGReportFactory.create(user=user, report_date=future_date)
     eq_(get_last_report(user).report_date, past_date)
Exemplo n.º 2
0
    def test_longest_streak(self, mocked_date):
        """Update current and longest streak counters when the fourth
        report out of five is deleted.
        """

        mocked_date.return_value = datetime.date(2011, 01, 29)
        user = UserFactory.create()
        up = user.userprofile
        start_date = datetime.date(2011, 01, 01)
        end_date = datetime.date(2011, 01, 29)
        # Create 5 reports
        for i in range(0, 5):
            NGReportFactory.create(
                user=user,
                report_date=end_date - datetime.timedelta(weeks=i))

        eq_(up.current_streak_start, start_date)
        eq_(up.longest_streak_start, start_date)
        eq_(up.longest_streak_end, end_date)

        # Delete the report that the user submitted on 22-01-2012
        NGReport.objects.filter(
            user=user,
            report_date=datetime.date(2011, 01, 22)).delete()

        user = User.objects.get(pk=user.id)
        up = user.userprofile
        eq_(up.current_streak_start, end_date)
        eq_(up.longest_streak_start, start_date)
        eq_(up.longest_streak_end, datetime.date(2011, 01, 15))
Exemplo n.º 3
0
 def test_get_last_ten_weeks_reports(self):
     user = UserFactory.create()
     # Add 4 reports more than a day apart
     for i in range(8, 0, -2):
         NGReportFactory.create(user=user, report_date=(now().date() - timedelta(days=i)))
     # Get the reports added in the last 10 weeks
     eq_(count_user_ng_reports(user, period=10), 4)
Exemplo n.º 4
0
    def test_current_streak_counter_with_past_reports(self):
        past_day = now().date() - datetime.timedelta(days=30)

        user = UserFactory.create()
        for i in range(0, 5):
            NGReportFactory.create(user=user, report_date=past_day - datetime.timedelta(days=i))

        ok_(not user.userprofile.current_streak_start)
Exemplo n.º 5
0
 def test_current_streak(self):
     user = UserFactory.create()
     # Add a report every 22 hours for the last 4 days (5 reports)
     for i in range(0, 4):
         NGReportFactory.create(user=user,
                                report_date=(now().date() -
                                             timedelta(days=i)))
     eq_(count_user_ng_reports(user, current_streak=True), 4)
Exemplo n.º 6
0
    def test_no_current_streak(self):
        user = UserFactory.create(groups=['Rep'])
        for i in range(4):
            report_date = date(2011, 01, 01) + timedelta(weeks=i)
            NGReportFactory.create(user=user, report_date=report_date)

        calculate_longest_streaks()
        eq_(user.userprofile.longest_streak_start, date(2011, 01, 01))
        eq_(user.userprofile.longest_streak_end, date(2011, 01, 22))
Exemplo n.º 7
0
 def test_functional_area_list(self):
     functional_area_1 = FunctionalAreaFactory.create()
     functional_area_2 = FunctionalAreaFactory.create()
     report = NGReportFactory.create(functional_areas=[functional_area_1])
     NGReportFactory.create(functional_areas=[functional_area_2])
     url = reverse('list_ng_reports_functional_area',
                   kwargs={'functional_area_slug': functional_area_1.slug})
     response = Client().get(url)
     eq_(set(response.context['objects'].object_list), set([report]))
Exemplo n.º 8
0
    def test_with_report_filled(self):
        mentor = UserFactory.create(groups=["Mentor"])
        today = now().date()
        rep = UserFactory.create(groups=["Rep"], userprofile__mentor=mentor)
        NGReportFactory.create(user=rep, report_date=today - timedelta(weeks=2))

        with patch("remo.reports.utils.send_remo_mail") as mail_mock:
            send_second_report_notification()
        ok_(not mail_mock.called)
Exemplo n.º 9
0
 def test_mentor_functional_area_list(self):
     mentor = UserFactory.create(groups=['Mentor'])
     functional_area = FunctionalAreaFactory.create()
     report = NGReportFactory.create(mentor=mentor, functional_areas=[functional_area])
     NGReportFactory.create(functional_areas=[functional_area])
     url = reverse('list_ng_reports_mentor_functional_area',
                   kwargs={'functional_area_slug': functional_area.slug,
                           'mentor': mentor.userprofile.display_name})
     response = Client().get(url)
     eq_(set(response.context['objects'].object_list), set([report]))
Exemplo n.º 10
0
    def test_current_longest_streak(self):
        today = now().date()
        user = UserFactory.create()

        for i in range(0, 2):
            NGReportFactory.create(user=user, report_date=today - datetime.timedelta(days=i))

        eq_(user.userprofile.current_streak_start, today - datetime.timedelta(days=1))
        eq_(user.userprofile.longest_streak_start, today - datetime.timedelta(days=1))
        eq_(user.userprofile.longest_streak_end, today)
Exemplo n.º 11
0
    def test_longest_streak(self):
        user = UserFactory.create()
        past_day = now().date() - timedelta(days=30)
        # Add 7 continuous reports somewhere in the past
        for i in range(0, 7):
            NGReportFactory.create(user=user, report_date=(past_day - timedelta(days=i)))

        # Add a report, one each day for the last 4 days (6 reports)
        for i in range(0, 3):
            NGReportFactory.create(user=user, report_date=(now().date() - timedelta(days=i)))
        eq_(count_user_ng_reports(user, longest_streak=True), 7)
Exemplo n.º 12
0
 def test_rep_functional_area_list(self):
     user = UserFactory.create(groups=['Rep'])
     functional_area = FunctionalAreaFactory.create()
     report = NGReportFactory.create(user=user,
                                     functional_areas=[functional_area])
     NGReportFactory.create(functional_areas=[functional_area])
     url = reverse('list_ng_reports_rep_functional_area',
                   kwargs={'functional_area_slug': functional_area.slug,
                           'rep': user.userprofile.display_name})
     response = self.get(url=url)
     eq_(set(response.context['reports'].object_list), set([report]))
Exemplo n.º 13
0
    def test_base(self):
        past_day = get_date(-8)
        user = UserFactory.create(groups=['Rep'])
        NGReportFactory.create(user=user, report_date=past_day)
        user.userprofile.current_streak_start = past_day
        user.userprofile.save()

        zero_current_streak()
        user = User.objects.get(pk=user.id)

        ok_(not user.userprofile.current_streak_start)
Exemplo n.º 14
0
    def test_user_new_future_activity(self):
        report_notification = now().date() - datetime.timedelta(weeks=3)
        new_report_date = now().date() + datetime.timedelta(weeks=6)

        rep = UserFactory.create(
            groups=['Rep'],
            userprofile__first_report_notification=report_notification
        )
        NGReportFactory.create(user=rep, report_date=new_report_date)

        user = User.objects.get(pk=rep.id)
        eq_(user.userprofile.first_report_notification, report_notification)
Exemplo n.º 15
0
 def test_list_rep(self):
     """Test page header context for rep."""
     user = UserFactory.create(groups=['Rep'], first_name='Foo', last_name='Bar')
     name = user.userprofile.display_name
     report = NGReportFactory.create(user=user)
     NGReportFactory.create()
     with self.login(user) as client:
         response = client.get(reverse('list_ng_reports_rep', kwargs={'rep': name}),
                               user=user)
     eq_(response.context['pageheader'], 'Activities for Foo Bar')
     eq_(set(response.context['objects'].object_list),
         set([report]), 'Other Rep reports are listed')
Exemplo n.º 16
0
    def test_with_no_report_filled_and_one_notification(self):
        mentor = UserFactory.create(groups=['Mentor'])
        today = datetime.utcnow().date()
        rep = UserFactory.create(
            groups=['Rep'], userprofile__mentor=mentor,
            userprofile__last_report_notification=today - timedelta(weeks=1))
        NGReportFactory.create(user=rep,
                               report_date=today - timedelta(weeks=2))

        with patch('remo.reports.tasks.send_mail') as send_mail_mock:
            send_ng_report_notification()
        ok_(not send_mail_mock.called)
Exemplo n.º 17
0
    def test_with_user_unavailable(self):
        mentor = UserFactory.create(groups=["Mentor"])
        today = now().date()
        rep = UserFactory.create(
            groups=["Rep"], userprofile__mentor=mentor, userprofile__date_joined_program=get_date(days=-100)
        )
        UserStatusFactory.create(user=rep)
        NGReportFactory.create(user=rep, report_date=today - timedelta(weeks=5))

        with patch("remo.reports.utils.send_remo_mail") as mail_mock:
            send_first_report_notification()

        ok_(not mail_mock.called)
Exemplo n.º 18
0
    def test_with_alumnus_mentor(self):
        mentor = UserFactory.create(groups=["Mentor", "Alumni"])
        today = now().date()
        rep = UserFactory.create(groups=["Rep"], userprofile__date_joined_program=get_date(days=-100))
        NGReportFactory.create(user=rep, mentor=mentor, report_date=today - timedelta(weeks=5))

        rep_subject = "[Reminder] Please share your recent activities"

        with patch("remo.reports.utils.send_remo_mail") as mail_mock:
            send_first_report_notification()

        eq_(mail_mock.call_count, 1)
        expected_call_list = [call(rep_subject, [rep.email], message=mockany)]
        eq_(mail_mock.call_args_list, expected_call_list)
Exemplo n.º 19
0
    def test_list_mentor(self):
        """Test page header context for mentor."""
        mentor = UserFactory.create(groups=['Mentor'], first_name='Foo',
                                    last_name='Bar')
        name = mentor.userprofile.display_name

        report_1 = NGReportFactory.create(mentor=mentor)
        report_2 = NGReportFactory.create(mentor=mentor)
        NGReportFactory.create()
        response = Client().get(reverse('list_ng_reports_mentor',
                                        kwargs={'mentor': name}), user=mentor)
        msg = 'Activities for Reps mentored by Foo Bar'
        eq_(response.context['pageheader'], msg)
        eq_(set(response.context['objects'].object_list),
            set([report_1, report_2]), 'Other Mentor reports are listed')
Exemplo n.º 20
0
    def test_different_current_longest_streak(self):
        today = now().date()
        past_day = now().date() - datetime.timedelta(days=30)
        user = UserFactory.create()
        # longest streak
        for i in range(0, 3):
            NGReportFactory.create(user=user, report_date=past_day - datetime.timedelta(days=i))

        # current streak
        for i in range(0, 2):
            NGReportFactory.create(user=user, report_date=today - datetime.timedelta(days=i))

        eq_(user.userprofile.current_streak_start, today - datetime.timedelta(days=1))
        eq_(user.userprofile.longest_streak_start, past_day - datetime.timedelta(days=2))
        eq_(user.userprofile.longest_streak_end, past_day)
Exemplo n.º 21
0
 def test_get_as_admin(self):
     user = UserFactory.create(groups=['Admin'])
     report = NGReportFactory.create()
     response = self.get(url=report.get_absolute_edit_url(),
                         user=user)
     eq_(response.context['report'], report)
     self.assertTemplateUsed('edit_ng_report.html')
Exemplo n.º 22
0
    def test_base(self):
        mentor = UserFactory.create(groups=['Mentor'])
        today = datetime.utcnow().date()
        rep = UserFactory.create(
            groups=['Rep'], userprofile__mentor=mentor,
            userprofile__last_report_notification=today - timedelta(weeks=4))
        NGReportFactory.create(user=rep,
                               report_date=today - timedelta(weeks=5))

        subject = '[Reminder] Please share your recent activities'

        with patch('remo.reports.tasks.send_mail') as send_mail_mock:
            send_ng_report_notification()

        send_mail_mock.assert_called_with(
            subject, mockany, settings.FROM_EMAIL, [rep.email])
Exemplo n.º 23
0
 def test_get_remo_url(self):
     report = NGReportFactory.create()
     url = '/api/beta/activities/%s' % report.id
     request = RequestFactory().get(url)
     data = ActivitiesDetailedSerializer(
         report, context={'request': request}).data
     ok_(report.get_absolute_url() in data['remo_url'])
Exemplo n.º 24
0
 def test_base(self):
     mentor = UserFactory.create()
     user = UserFactory.create(userprofile__mentor=mentor)
     event = EventFactory.create()
     functional_areas = [FunctionalAreaFactory.create()]
     campaign = CampaignFactory.create()
     activity = ActivityFactory.create()
     report = NGReportFactory.create(
         functional_areas=functional_areas, mentor=mentor,
         campaign=campaign, user=user, event=event, activity=activity)
     url = '/api/beta/activities/%s' % report.id
     request = RequestFactory().get(url)
     data = ActivitiesDetailedSerializer(report,
                                         context={'request': request}).data
     eq_(data['user']['first_name'], user.first_name)
     eq_(data['user']['last_name'], user.last_name)
     eq_(data['user']['display_name'], user.userprofile.display_name)
     ok_(data['user']['_url'])
     eq_(data['activity'], activity.name)
     eq_(data['initiative'], campaign.name)
     eq_(data['functional_areas'][0]['name'], functional_areas[0].name)
     eq_(data['activity_description'], report.activity_description)
     eq_(data['location'], report.location)
     eq_(data['latitude'], float(report.latitude))
     eq_(data['longitude'], float(report.longitude))
     eq_(data['report_date'], report.report_date.strftime('%Y-%m-%d'))
     eq_(data['link'], report.link)
     eq_(data['link_description'], report.link_description)
     eq_(data['mentor']['first_name'], mentor.first_name)
     eq_(data['mentor']['last_name'], mentor.last_name)
     eq_(data['mentor']['display_name'], mentor.userprofile.display_name)
     ok_(data['mentor']['_url'])
     eq_(data['passive_report'], report.is_passive)
     eq_(data['event']['name'], event.name)
     ok_(data['event']['_url'])
Exemplo n.º 25
0
 def test_get_as_admin(self):
     user = UserFactory.create(groups=['Admin'])
     report = NGReportFactory.create()
     with self.login(user) as client:
         response = client.get(report.get_absolute_edit_url(), user=user)
     eq_(response.context['report'], report)
     self.assertJinja2TemplateUsed(response, 'edit_ng_report.jinja')
Exemplo n.º 26
0
 def test_as_admin(self, redirect_mock):
     user = UserFactory.create(groups=['Admin'])
     report = NGReportFactory.create()
     report_comment = NGReportCommentFactory.create(report=report)
     self.post(user=user, url=report_comment.get_absolute_delete_url())
     ok_(not NGReportComment.objects.filter(pk=report_comment.id).exists())
     redirect_mock.assert_called_with(report.get_absolute_url())
Exemplo n.º 27
0
 def test_as_other_rep(self):
     user = UserFactory.create()
     report = NGReportFactory.create()
     report_comment = NGReportCommentFactory.create(report=report)
     with self.login(user) as client:
         client.post(report_comment.get_absolute_delete_url(), user=user)
     ok_(NGReportComment.objects.filter(pk=report_comment.id).exists())
Exemplo n.º 28
0
 def test_as_owner(self, redirect_mock):
     report = NGReportFactory.create()
     report_comment = NGReportCommentFactory.create(report=report)
     with self.login(report.user) as client:
         client.post(report_comment.get_absolute_delete_url(), user=report.user)
     ok_(not NGReportComment.objects.filter(pk=report_comment.id).exists())
     redirect_mock.assert_called_with(report.get_absolute_url())
Exemplo n.º 29
0
    def test_comment_multiple_users(self):
        """Test sending email when a new comment is added on a NGReport
        and the users have the option enabled in their settings.
        """
        commenter = UserFactory.create()
        reporter = UserFactory.create(
            userprofile__receive_email_on_add_comment=True)
        report = NGReportFactory.create(user=reporter)
        users_with_comments = UserFactory.create_batch(
            2, userprofile__receive_email_on_add_comment=True)
        # disconnect the signals in order to add two users in NGReportComment
        for user_obj in users_with_comments:
            NGReportCommentFactoryNoSignals.create(
                user=user_obj, report=report, comment='This is a comment')
        NGReportCommentFactory.create(user=commenter, report=report,
                                      comment='This is a comment')

        eq_(len(mail.outbox), 3)
        recipients = ['%s <%s>' % (reporter.get_full_name(), reporter.email),
                      '%s <%s>' % (users_with_comments[0].get_full_name(),
                                   users_with_comments[0].email),
                      '%s <%s>' % (users_with_comments[1].get_full_name(),
                                   users_with_comments[1].email)]
        receivers = [mail.outbox[0].to[0], mail.outbox[1].to[0],
                     mail.outbox[2].to[0]]
        eq_(set(recipients), set(receivers))
        msg = ('[Report] User {0} commented on {1}'
               .format(commenter.get_full_name(), report))
        eq_(mail.outbox[0].subject, msg)
Exemplo n.º 30
0
 def test_as_admin(self, redirect_mock):
     user = UserFactory.create(groups=['Admin'])
     report = NGReportFactory.create()
     with self.login(user) as client:
         client.post(report.get_absolute_delete_url(), user=user)
     ok_(not NGReport.objects.filter(pk=report.id).exists())
     redirect_mock.assert_called_with('profiles_view_profile',
                                      display_name=report.user.userprofile.display_name)
Exemplo n.º 31
0
    def test_inactive(self):
        reps = UserFactory.create_batch(12, groups=['Rep'])
        active = timedelta(days=5)
        inactive_low = timedelta(weeks=5)
        inactive_high = timedelta(weeks=9)

        active_reps = reps[:5]
        inactive_low_reps = reps[5:9]
        inactive_high_reps = reps[9:]

        for user in active_reps:
            # Activities in future and past 4 weeks
            NGReportFactory.create(user=user, report_date=now().date() - active)
            NGReportFactory.create(user=user, report_date=now().date() + active)

            # Activities in future and past 4+ weeks
            NGReportFactory.create(user=user, report_date=now().date() - inactive_low)
            NGReportFactory.create(user=user, report_date=now().date() + inactive_low)

            # Activities in future and past 8+ weeks
            NGReportFactory.create(user=user, report_date=now().date() - inactive_high)
            NGReportFactory.create(user=user, report_date=now().date() + inactive_high)

        for user in inactive_low_reps:
            # Activities in future and past 4+ weeks
            NGReportFactory.create(user=user, report_date=now().date() - inactive_low)
            NGReportFactory.create(user=user, report_date=now().date() + inactive_low)

            # Activities in future and past 8+ weeks
            NGReportFactory.create(user=user, report_date=now().date() - inactive_high)
            NGReportFactory.create(user=user, report_date=now().date() + inactive_high)

        for user in inactive_high_reps:
            # Activities in future and past 8+ weeks
            NGReportFactory.create(user=user, report_date=now().date() - inactive_high)
            NGReportFactory.create(user=user, report_date=now().date() + inactive_high)

        response = Client().get(reverse('stats_dashboard'))

        eq_(response.status_code, 200)
        self.assertJinja2TemplateUsed(response, 'stats_dashboard.jinja')
        eq_(response.context['active_users'], 5)
        eq_(response.context['inactive_low_users'], 4)
        eq_(response.context['inactive_high_users'], 3)
Exemplo n.º 32
0
 def test_get_as_other_rep(self):
     user = UserFactory.create()
     report = NGReportFactory.create()
     self.get(url=report.get_absolute_edit_url(), user=user)
Exemplo n.º 33
0
 def test_get_as_admin(self):
     user = UserFactory.create(groups=['Admin'])
     report = NGReportFactory.create()
     response = self.get(url=report.get_absolute_edit_url(), user=user)
     eq_(response.context['report'], report)
     self.assertTemplateUsed('edit_ng_report.html')
Exemplo n.º 34
0
 def test_get_as_owner(self):
     report = NGReportFactory.create()
     response = self.get(url=report.get_absolute_edit_url(),
                         user=report.user)
     eq_(response.context['report'], report)
     self.assertTemplateUsed('edit_ng_report.html')