Example #1
0
    def test_latest_newsitem(self):
        """
        Pass the most-recently-created visible NewsItem to the template
        context.
        """
        old_newsitem = NewsItemFactory.create(visible=True)
        old_newsitem.created = aware_datetime(2014, 1, 1)
        old_newsitem.save()

        non_visible_newsitem = NewsItemFactory.create(visible=False)
        non_visible_newsitem.created = aware_datetime(2014, 1, 5)
        non_visible_newsitem.save()

        visible_newsitem = NewsItemFactory.create(visible=True)
        visible_newsitem.created = aware_datetime(2014, 1, 4)
        visible_newsitem.save()

        request = Mock()
        with patch('affiliates.base.views.render') as render:
            views.dashboard(request)

        render.assert_called_with(request, 'base/dashboard.html', {
            'newsitem': visible_newsitem,
            'milestones': ANY,
        })
Example #2
0
    def test_latest_newsitem(self):
        """
        Pass the most-recently-created visible NewsItem to the template
        context.
        """
        old_newsitem = NewsItemFactory.create(visible=True)
        old_newsitem.created = aware_datetime(2014, 1, 1)
        old_newsitem.save()

        non_visible_newsitem = NewsItemFactory.create(visible=False)
        non_visible_newsitem.created = aware_datetime(2014, 1, 5)
        non_visible_newsitem.save()

        visible_newsitem = NewsItemFactory.create(visible=True)
        visible_newsitem.created = aware_datetime(2014, 1, 4)
        visible_newsitem.save()

        request = Mock(user=UserFactory.create())
        with patch('affiliates.base.views.render') as render:
            views.dashboard(request)

        render.assert_called_with(request, 'base/dashboard.html', {
            'newsitem': visible_newsitem,
            'milestones': ANY,
            'links': ANY,
        })
Example #3
0
    def test_last_login_attribute(self, mock_timezone, update_user_info):
        """
        During the login process, the last_login attribute on the user must be
        set to the current datetime.
        """
        mock_timezone.now.return_value = aware_datetime(2012, 1, 1)
        request = self.request()
        user = FacebookUserFactory.create(last_login=aware_datetime(2000, 1, 1))
        login(request, user)

        user = refresh_model(user)
        eq_(user.last_login, aware_datetime(2012, 1, 1))
Example #4
0
    def test_last_login_attribute(self, mock_timezone, update_user_info):
        """
        During the login process, the last_login attribute on the user must be
        set to the current datetime.
        """
        mock_timezone.now.return_value = aware_datetime(2012, 1, 1)
        request = self.request()
        user = FacebookUserFactory.create(last_login=aware_datetime(2000, 1, 1))
        login(request, user)

        user = refresh_model(user)
        eq_(user.last_login, aware_datetime(2012, 1, 1))
Example #5
0
    def test_process_request_clicks_and_downloads(self):
        """
        If there were any clicks or downloads since the user's last
        visit, update their last_visit date and log a message.
        """
        self.request.user = UserFactory.create(
            userprofile__last_visit=aware_date(2014, 1, 1))

        # Date of last visit not included.
        DataPointFactory.create(link__user=self.request.user,
                                date=aware_date(2014, 1, 1),
                                link_clicks=3,
                                firefox_downloads=9)

        # Current date and dates between are included.
        DataPointFactory.create(link__user=self.request.user,
                                date=aware_date(2014, 1, 2),
                                link_clicks=4,
                                firefox_downloads=7)
        DataPointFactory.create(link__user=self.request.user,
                                date=aware_date(2014, 1, 3),
                                link_clicks=1,
                                firefox_downloads=2)

        with patch('affiliates.links.middleware.timezone.now') as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 3)
            eq_(self.middleware.process_request(self.request), None)
            ok_(self.messages.info.called)

        message = self.messages.info.call_args[0][1]
        ok_('5' in message and '9' in message)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 3))
Example #6
0
    def test_process_request_clicks_and_downloads(self):
        """
        If there were any clicks or downloads since the user's last
        visit, update their last_visit date and log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=aware_date(2014, 1, 1))

        # Date of last visit not included.
        DataPointFactory.create(
            link__user=self.request.user, date=aware_date(2014, 1, 1), link_clicks=3, firefox_downloads=9
        )

        # Current date and dates between are included.
        DataPointFactory.create(
            link__user=self.request.user, date=aware_date(2014, 1, 2), link_clicks=4, firefox_downloads=7
        )
        DataPointFactory.create(
            link__user=self.request.user, date=aware_date(2014, 1, 3), link_clicks=1, firefox_downloads=2
        )

        with patch("affiliates.links.middleware.timezone.now") as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 3)
            eq_(self.middleware.process_request(self.request), None)
            ok_(self.messages.info.called)

        message = self.messages.info.call_args[0][1]
        ok_("5" in message and "9" in message)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 3))
Example #7
0
    def test_creation_milestone_no_previous_links_created(self):
        """
        If there is no previous milestone, but the user has created at
        least one link (which is normally impossible, as the default
        milestones start at 1), show when their last link was created.
        """
        links = LinkFactory.create_batch(2, banner_type='test', user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 2, 8)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (None, 5)

        milestone = self.display.creation_milestone('test', self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'link_created'))
Example #8
0
    def test_creation_milestone_no_next_milestone(self):
        """
        If there is no next milestone, show the date of their last
        milestone.
        """
        links = LinkFactory.create_batch(4, banner_type='test', user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 1, 8)
        links[2].created = aware_datetime(2014, 1, 2, 5)  # Winner!
        links[3].created = aware_datetime(2014, 1, 3, 5)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (3, None)

        milestone = self.display.creation_milestone('test', self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'achieved_milestone_3'))
    def test_creation_milestone_far_from_next_milestone(self):
        """
        If the user isn't close to the next milestone, show the date of
        their last milestone.
        """
        variation = TextBannerVariationFactory.create()
        links = LinkFactory.create_batch(4, banner_variation=variation, user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 1, 8)
        links[2].created = aware_datetime(2014, 1, 2, 5)  # Winner!
        links[3].created = aware_datetime(2014, 1, 3, 5)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (3, 10)

        milestone = self.display.creation_milestone(TextBanner, self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'achieved_milestone_3'))
Example #10
0
    def test_date_argument_today(self):
        """
        If the date argument is the word 'today', set the query date to
        the current date.
        """
        link1, link2 = LinkFactory.create_batch(2)
        self.service.get_clicks_for_date.return_value = {
            unicode(link1.pk): '4',
            unicode(link2.pk): '7'
        }
        today = aware_datetime(2014, 1, 2).date()

        with patch.object(collect_ga_data, 'timezone') as mock_timezone:
            mock_timezone.now.return_value = aware_datetime(2014, 1, 2)
            self.command.execute('today')

        self.service.get_clicks_for_date.assert_called_with(today)
        eq_(link1.datapoint_set.get(date=today).link_clicks, 4)
        eq_(link2.datapoint_set.get(date=today).link_clicks, 7)
Example #11
0
    def test_date_argument_today(self):
        """
        If the date argument is the word 'today', set the query date to
        the current date.
        """
        link1, link2 = LinkFactory.create_batch(2)
        self.service.get_clicks_for_date.return_value = {
            unicode(link1.pk): '4',
            unicode(link2.pk): '7'
        }
        today = aware_datetime(2014, 1, 2).date()

        with patch.object(collect_ga_data, 'timezone') as mock_timezone:
            mock_timezone.now.return_value = aware_datetime(2014, 1, 2)
            self.command.execute('today')

        self.service.get_clicks_for_date.assert_called_with(today)
        eq_(link1.datapoint_set.get(date=today).link_clicks, 4)
        eq_(link2.datapoint_set.get(date=today).link_clicks, 7)
Example #12
0
    def test_add_click(self):
        request = self.factory.get('/')
        link = LinkFactory.create()

        with patch('affiliates.links.views.add_click') as add_click:
            with patch('affiliates.links.views.timezone') as timezone:
                timezone.now.return_value = aware_datetime(2014, 4, 7)

                self.view(request, pk=link.pk)
                add_click.delay.assert_called_with(link.id, date(2014, 4, 7))
Example #13
0
    def test_creation_milestone_no_previous_links_created(self):
        """
        If there is no previous milestone, but the user has created at
        least one link (which is normally impossible, as the default
        milestones start at 1), show when their last link was created.
        """
        variation = TextBannerVariationFactory.create()
        links = LinkFactory.create_batch(2,
                                         banner_variation=variation,
                                         user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 2, 8)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (None, 5)

        milestone = self.display.creation_milestone(TextBanner, self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'link_created'))
Example #14
0
    def test_creation_milestone_no_next_milestone(self):
        """
        If there is no next milestone, show the date of their last
        milestone.
        """
        variation = TextBannerVariationFactory.create()
        links = LinkFactory.create_batch(4,
                                         banner_variation=variation,
                                         user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 1, 8)
        links[2].created = aware_datetime(2014, 1, 2, 5)  # Winner!
        links[3].created = aware_datetime(2014, 1, 3, 5)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (3, None)

        milestone = self.display.creation_milestone(TextBanner, self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'achieved_milestone_3'))
Example #15
0
    def test_process_request_less_than_one_day(self):
        """
        If it has been less than one day since the user's last visit,
        return None and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=aware_date(2014, 1, 1))

        with patch("affiliates.links.middleware.timezone.now") as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)
Example #16
0
    def test_process_request_less_than_one_day(self):
        """
        If it has been less than one day since the user's last visit,
        return None and do not log a message.
        """
        self.request.user = UserFactory.create(
            userprofile__last_visit=aware_date(2014, 1, 1))

        with patch('affiliates.links.middleware.timezone.now') as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)
Example #17
0
    def test_process_request_no_last_visit(self):
        """
        If we haven't logged a last visit for the user, set their
        last_visit and return None and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=None)

        with patch("affiliates.links.middleware.timezone.now") as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 1))
Example #18
0
    def test_process_request_no_clicks_or_downloads(self):
        """
        If there were no clicks or downloads since the user's last
        visit, update their last_visit date and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=aware_date(2014, 1, 1))

        with patch("affiliates.links.middleware.timezone.now") as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 3)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 3))
Example #19
0
    def test_process_request_no_last_visit(self):
        """
        If we haven't logged a last visit for the user, set their
        last_visit and return None and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=None)

        with patch('affiliates.links.middleware.timezone.now') as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 1))
Example #20
0
    def test_process_request_no_clicks_or_downloads(self):
        """
        If there were no clicks or downloads since the user's last
        visit, update their last_visit date and do not log a message.
        """
        self.request.user = UserFactory.create(
            userprofile__last_visit=aware_date(2014, 1, 1))

        with patch('affiliates.links.middleware.timezone.now') as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 3)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 3))
Example #21
0
    def test_success(self):
        link1, link2 = LinkFactory.create_batch(2)
        self.service.get_clicks_for_date.return_value = {
            unicode(link1.pk): '4',
            unicode(link2.pk): '7'
        }
        yesterday = aware_datetime(2014, 1, 1).date()

        with patch.object(collect_ga_data, 'date_yesterday') as date_yesterday:
            date_yesterday.return_value = yesterday
            self.command.execute()

        self.service.get_clicks_for_date.assert_called_with(yesterday)
        ok_(link1.datapoint_set.filter(date=yesterday, link_clicks=4).exists())
        ok_(link2.datapoint_set.filter(date=yesterday, link_clicks=7).exists())
Example #22
0
    def test_date_argument(self):
        """
        If a date argument is given, parse it as DD-MM-YYYY and use it
        as the query date.
        """
        link1, link2 = LinkFactory.create_batch(2)
        self.service.get_clicks_for_date.return_value = {
            unicode(link1.pk): '4',
            unicode(link2.pk): '7'
        }
        query_date = aware_datetime(2014, 1, 1).date()

        # Create pre-existing data to check that it is replaced.
        DataPointFactory.create(link=link1, date=query_date, link_clicks=18)
        DataPointFactory.create(link=link2, date=query_date, link_clicks=14)

        self.command.execute('01-01-2014')

        # There must only be one datapoint for the query date, and the
        # link_clicks must match the new data.
        self.service.get_clicks_for_date.assert_called_with(query_date)
        eq_(link1.datapoint_set.get(date=query_date).link_clicks, 4)
        eq_(link2.datapoint_set.get(date=query_date).link_clicks, 7)
Example #23
0
    def test_date_argument(self):
        """
        If a date argument is given, parse it as DD-MM-YYYY and use it
        as the query date.
        """
        link1, link2 = LinkFactory.create_batch(2)
        self.service.get_clicks_for_date.return_value = {
            unicode(link1.pk): '4',
            unicode(link2.pk): '7'
        }
        query_date = aware_datetime(2014, 1, 1).date()

        # Create pre-existing data to check that it is replaced.
        DataPointFactory.create(link=link1, date=query_date, link_clicks=18)
        DataPointFactory.create(link=link2, date=query_date, link_clicks=14)

        self.command.execute('01-01-2014')

        # There must only be one datapoint for the query date, and the
        # link_clicks must match the new data.
        self.service.get_clicks_for_date.assert_called_with(query_date)
        eq_(link1.datapoint_set.get(date=query_date).link_clicks, 4)
        eq_(link2.datapoint_set.get(date=query_date).link_clicks, 7)
Example #24
0
    def test_basic(self):
        """
        Aggregate any datapoints older than 90 days into the totals
        stored on their links.
        """
        link1 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10)
        link1_old_datapoint = DataPointFactory.create(link=link1, date=aware_date(2014, 1, 1),
                                                      link_clicks=8, firefox_downloads=4)
        link1_new_datapoint = DataPointFactory.create(link=link1, date=aware_date(2014, 3, 1),
                                                      link_clicks=2, firefox_downloads=7)

        link2 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10)
        link2_old_datapoint1 = DataPointFactory.create(link=link2, date=aware_date(2014, 1, 1),
                                                       link_clicks=8, firefox_downloads=4)
        link2_old_datapoint2 = DataPointFactory.create(link=link2, date=aware_date(2013, 12, 30),
                                                       link_clicks=2, firefox_downloads=7)

        path = 'affiliates.links.management.commands.aggregate_old_datapoints.timezone'
        with patch(path) as mock_timezone:
            mock_timezone.now.return_value = aware_datetime(2014, 4, 2)
            self.command.handle()

        # link1 should have 7+8=15 clicks, 10+4=14 downloads, and the
        # new datapoint should still exist.
        link1 = Link.objects.get(pk=link1.pk)
        eq_(link1.aggregate_link_clicks, 15)
        eq_(link1.aggregate_firefox_downloads, 14)
        ok_(not DataPoint.objects.filter(pk=link1_old_datapoint.pk).exists())
        ok_(DataPoint.objects.filter(pk=link1_new_datapoint.pk).exists())

        # link2 should have 7+8+2=17 clicks, 10+4+7=21 downloads, and the
        # old datapoints should not exist.
        link2 = Link.objects.get(pk=link2.pk)
        eq_(link2.aggregate_link_clicks, 17)
        eq_(link2.aggregate_firefox_downloads, 21)
        ok_(not DataPoint.objects.filter(pk=link2_old_datapoint1.pk).exists())
        ok_(not DataPoint.objects.filter(pk=link2_old_datapoint2.pk).exists())
Example #25
0
    def test_basic(self):
        """
        Aggregate any datapoints older than 90 days into the totals
        stored on their links.
        """
        link1 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10)
        link1_old_datapoint = DataPointFactory.create(link=link1, date=aware_date(2014, 1, 1),
                                                      link_clicks=8, firefox_downloads=4)
        link1_new_datapoint = DataPointFactory.create(link=link1, date=aware_date(2014, 3, 1),
                                                      link_clicks=2, firefox_downloads=7)

        link2 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10)
        link2_old_datapoint1 = DataPointFactory.create(link=link2, date=aware_date(2014, 1, 1),
                                                       link_clicks=8, firefox_downloads=4)
        link2_old_datapoint2 = DataPointFactory.create(link=link2, date=aware_date(2013, 12, 30),
                                                       link_clicks=2, firefox_downloads=7)

        path = 'affiliates.links.management.commands.aggregate_old_datapoints.timezone'
        with patch(path) as mock_timezone:
            mock_timezone.now.return_value = aware_datetime(2014, 4, 2)
            self.command.handle()

        # link1 should have 7+8=15 clicks, 10+4=14 downloads, and the
        # new datapoint should still exist.
        link1 = Link.objects.get(pk=link1.pk)
        eq_(link1.aggregate_link_clicks, 15)
        eq_(link1.aggregate_firefox_downloads, 14)
        ok_(not DataPoint.objects.filter(pk=link1_old_datapoint.pk).exists())
        ok_(DataPoint.objects.filter(pk=link1_new_datapoint.pk).exists())

        # link2 should have 7+8+2=17 clicks, 10+4+7=21 downloads, and the
        # old datapoints should not exist.
        link2 = Link.objects.get(pk=link2.pk)
        eq_(link2.aggregate_link_clicks, 17)
        eq_(link2.aggregate_firefox_downloads, 21)
        ok_(not DataPoint.objects.filter(pk=link2_old_datapoint1.pk).exists())
        ok_(not DataPoint.objects.filter(pk=link2_old_datapoint2.pk).exists())
Example #26
0
 def _mkstats(self, user, year, month, clicks):
     hour = aware_datetime(year, month, 1, 0)
     return FacebookClickStatsFactory.create(banner_instance__user=user,
                                             hour=hour, clicks=clicks)