示例#1
0
    def test_clear_old(self):
        """
        When the leaderboard is updated, old standings should be
        cleared.
        """
        user1 = UserFactory.create()  # Total: 38 clicks
        self._link_with_clicks(user1, 5, [4, 6, 3])  # 18 clicks
        self._link_with_clicks(user1, 1, [8, 9, 2])  # 20 clicks

        user2 = UserFactory.create()  # Total: 49 clicks
        self._link_with_clicks(user2, 13, [12, 11, 13])  # 49 clicks

        # Create existing leaderboard with users in opposite order.
        LeaderboardStandingFactory.create(user=user1,
                                          ranking=1,
                                          metric='link_clicks')
        LeaderboardStandingFactory.create(user=user2,
                                          ranking=2,
                                          metric='link_clicks')

        self.command.handle()
        ok_(not (LeaderboardStanding.objects.filter(
            user=user1, ranking=1, metric='link_clicks').exists()))
        ok_(not (LeaderboardStanding.objects.filter(
            user=user2, ranking=2, metric='link_clicks').exists()))
示例#2
0
    def test_basic(self):
        # Create users and links with the noted number of clicks.

        # User with clicks in both aggregate and datapoints across many
        # links.
        user1 = UserFactory.create() # Total: 38 clicks
        self._link_with_clicks(user1, 5, [4, 6, 3]) # 18 clicks
        self._link_with_clicks(user1, 1, [8, 9, 2]) # 20 clicks

        # User with clicks in both aggregate and datapoints in 1 link.
        user2 = UserFactory.create() # Total: 49 clicks
        self._link_with_clicks(user2, 13, [12, 11, 13]) # 49 clicks

        # User with no links.
        user3 = UserFactory.create() # Total: 0 clicks

        # User with links that have no aggregate clicks or no datapoint
        # clicks.
        user4 = UserFactory.create() # Total: 9 clicks
        self._link_with_clicks(user4, 1, [2, 2]) # 5 clicks
        self._link_with_clicks(user4, 0, [2]) # 2 clicks
        self._link_with_clicks(user4, 2, []) # 2 clicks

        # This one just sort've rounds out the set I guess.
        user5 = UserFactory.create() # Total: 9 clicks
        self._link_with_clicks(user5, 1, [2, 2, 2]) # 7 clicks
        self._link_with_clicks(user5, 0, [2]) # 2 clicks

        self.command.handle()
        eq_([s.user for s in LeaderboardStanding.objects.order_by('ranking')],
            [user2, user1, user4, user5, user3])
示例#3
0
    def test_basic(self):
        # Create users and links with the noted number of clicks.

        # User with clicks in both aggregate and datapoints across many
        # links.
        user1 = UserFactory.create() # Total: 38 clicks
        self._link_with_clicks(user1, 5, [4, 6, 3]) # 18 clicks
        self._link_with_clicks(user1, 1, [8, 9, 2]) # 20 clicks

        # User with clicks in both aggregate and datapoints in 1 link.
        user2 = UserFactory.create() # Total: 49 clicks
        self._link_with_clicks(user2, 13, [12, 11, 13]) # 49 clicks

        # User with no links.
        user3 = UserFactory.create() # Total: 0 clicks

        # User with links that have no aggregate clicks or no datapoint
        # clicks.
        user4 = UserFactory.create() # Total: 9 clicks
        self._link_with_clicks(user4, 1, [2, 2]) # 5 clicks
        self._link_with_clicks(user4, 0, [2]) # 2 clicks
        self._link_with_clicks(user4, 2, []) # 2 clicks

        # This one just sort've rounds out the set I guess.
        user5 = UserFactory.create() # Total: 9 clicks
        self._link_with_clicks(user5, 1, [2, 2, 2]) # 7 clicks
        self._link_with_clicks(user5, 0, [2]) # 2 clicks

        self.command.handle()
        eq_([s.user for s in LeaderboardStanding.objects.order_by('ranking')],
            [user2, user1, user4, user5, user3])
示例#4
0
    def test_link_failure(self, create_link):
        """If creating a link fails, still return a 200 OK."""
        create_link.return_value = None
        self.client.fb_login(self.user)
        UserFactory.create(email='*****@*****.**')

        response = self.client.post(self.url,
                                    {'affiliates_email': '*****@*****.**'})
        eq_(response.status_code, 200)
示例#5
0
    def test_link_failure(self, create_link):
        """If creating a link fails, still return a 200 OK."""
        create_link.return_value = None
        self.client.fb_login(self.user)
        UserFactory.create(email='*****@*****.**')

        response = self.client.post(self.url,
                                    {'affiliates_email': '*****@*****.**'})
        eq_(response.status_code, 200)
示例#6
0
    def test_save_model_with_pk(self):
        """
        If a NewsItem exists in the DB (has a pk), do not change the
        author.
        """
        original_author = UserFactory.create()
        newsitem = NewsItemFactory.create(author=original_author)
        request = Mock(user=UserFactory.create())

        self.model_admin.save_model(request, newsitem, None, False)
        eq_(newsitem.author, original_author)
示例#7
0
    def test_link_success(self, create_link, send_activation_email):
        """
        If creating a link succeeds, send an activation email and return a 200
        OK.
        """
        link = FacebookAccountLinkFactory.create()
        create_link.return_value = link
        self.client.fb_login(self.user)
        UserFactory.create(email='*****@*****.**')

        response = self.client.post(self.url,
                                    {'affiliates_email': '*****@*****.**'})
        eq_(response.status_code, 200)
        ok_(send_activation_email.called)
        eq_(send_activation_email.call_args[0][1], link)
示例#8
0
    def test_link_success(self, create_link, send_activation_email):
        """
        If creating a link succeeds, send an activation email and return a 200
        OK.
        """
        link = FacebookAccountLinkFactory.create()
        create_link.return_value = link
        self.client.fb_login(self.user)
        UserFactory.create(email='*****@*****.**')

        response = self.client.post(self.url,
                                    {'affiliates_email': '*****@*****.**'})
        eq_(response.status_code, 200)
        ok_(send_activation_email.called)
        eq_(send_activation_email.call_args[0][1], link)
示例#9
0
    def test_not_created(self):
        """If user is not new, don't bother with permissions."""
        user = UserFactory.create()
        user.user_permissions.clear()

        add_default_permissions(User, created=False, instance=user)
        ok_(self.can_share_website not in user.user_permissions.all())
示例#10
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))
示例#11
0
    def test_not_created(self):
        """If user is not new, don't bother with permissions."""
        user = UserFactory.create()
        user.user_permissions.clear()

        add_default_permissions(User, created=False, instance=user)
        ok_(self.can_share_website not in user.user_permissions.all())
示例#12
0
    def test_create_link(self):
        """
        create_link should create a Link object using this banner's
        description and generated code.
        """
        banner = Banner(destination='https://www.mozilla.org')
        banner.generate_banner_code = Mock(return_value="""
            <a href="{href}">Link!</a>
        """)
        banner.get_banner_type = Mock(return_value='generic_banner')
        user = UserFactory.create()
        variation = TextBannerVariationFactory.create()

        with patch.object(Link, 'get_referral_url') as get_referral_url:
            get_referral_url.return_value = 'asdf'
            link = banner.create_link(user, variation)

        ok_(isinstance(link, Link))
        eq_(link.user, user)
        eq_(link.banner_variation, variation)
        self.assertHTMLEqual(
            link.html, """
            <a href="asdf">Link!</a>
        """)
        banner.generate_banner_code.assert_called_with(variation)
示例#13
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,
        })
示例#14
0
    def test_create_link(self):
        """
        create_link should create a Link object using this banner's
        description and generated code.
        """
        banner = Banner(destination="https://www.mozilla.org")
        banner.generate_banner_code = Mock(
            return_value="""
            <a href="{href}">Link!</a>
        """
        )
        banner.get_banner_type = Mock(return_value="generic_banner")
        user = UserFactory.create()
        variation = TextBannerVariationFactory.create()

        with patch.object(Link, "get_referral_url") as get_referral_url:
            get_referral_url.return_value = "asdf"
            link = banner.create_link(user, variation)

        ok_(isinstance(link, Link))
        eq_(link.user, user)
        eq_(link.banner_variation, variation)
        self.assertHTMLEqual(
            link.html,
            """
            <a href="asdf">Link!</a>
        """,
        )
        banner.generate_banner_code.assert_called_with(variation)
示例#15
0
 def test_user(self):
     """Passing a user returns the gravatar url for that user's email."""
     user = UserFactory.create(email='*****@*****.**')
     url = gravatar(user)
     eq_(url,
         'https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=80&d={default}'
         .format(default=self.default))
示例#16
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,
        })
示例#17
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))
示例#18
0
 def test_user(self):
     """Passing a user returns the gravatar url for that user's email."""
     user = UserFactory.create(email='*****@*****.**')
     url = gravatar(user)
     eq_(
         url,
         'https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=80&d={default}'
         .format(default=self.default))
示例#19
0
    def test_post(self):
        user = UserFactory.create()
        request = Mock(user=user)
        self.view.get_object = Mock(return_value=user.userprofile)

        # Defer to the parent's post.
        with patch('affiliates.users.views.UpdateView.post') as super_post:
            eq_(self.view.post(request, user.pk), super_post.return_value)
            super_post.assert_called_with(request, user.pk)
示例#20
0
    def test_post(self):
        user = UserFactory.create()
        request = Mock(user=user)
        self.view.get_object = Mock(return_value=user.userprofile)

        # Defer to the parent's post.
        with patch('affiliates.users.views.UpdateView.post') as super_post:
            eq_(self.view.post(request, user.pk), super_post.return_value)
            super_post.assert_called_with(request, user.pk)
示例#21
0
    def test_milestone_date(self):
        user = UserFactory.create()
        display = MilestoneDisplay(user)

        DataPointFactory.create(link_clicks=4, date=aware_date(2014, 1, 1), link__user=user)
        DataPointFactory.create(link_clicks=3, date=aware_date(2014, 1, 2), link__user=user)
        DataPointFactory.create(link_clicks=2, date=aware_date(2014, 1, 3), link__user=user)

        eq_(display.milestone_date('link_clicks', 10, 4), aware_date(2014, 1, 2))
示例#22
0
    def test_save_model_no_pk(self):
        """
        If a NewsItem isn't saved yet (has no pk), set the author to the
        request's current user.
        """
        newsitem = NewsItemFactory.build()
        request = Mock(user=UserFactory.create())

        self.model_admin.save_model(request, newsitem, None, False)
        eq_(newsitem.author, request.user)
示例#23
0
    def test_post_user_mismatch(self):
        """
        If the user being edited doesn't match the current user,
        redirect to the profile page for the user being edited without
        making any changes.
        """
        request_user = UserFactory.create()
        request = Mock(user=request_user)

        edited_user = UserFactory.create(display_name='Bob')
        self.view.get_object = Mock(return_value=edited_user.userprofile)

        # Redirect should be called and given the profile, while the
        # parent's post should not be called.
        with patch('affiliates.users.views.UpdateView.post') as super_post:
            with patch('affiliates.users.views.redirect') as redirect:
                eq_(self.view.post(request, edited_user.pk), redirect.return_value)
                redirect.assert_called_with(edited_user.userprofile)
                ok_(not super_post.called)
示例#24
0
    def test_permission_granted(self):
        """
        Newly created users should be granted the can_share_website
        permission.
        """
        user = UserFactory.create()
        user.user_permissions.clear()

        add_default_permissions(User, created=True, instance=user)
        ok_(self.can_share_website in user.user_permissions.all())
示例#25
0
    def test_permission_granted(self):
        """
        Newly created users should be granted the can_share_website
        permission.
        """
        user = UserFactory.create()
        user.user_permissions.clear()

        add_default_permissions(User, created=True, instance=user)
        ok_(self.can_share_website in user.user_permissions.all())
示例#26
0
    def test_milestone_date_not_reached(self):
        """
        If the milestone hasn't been reached by the user, return None.
        """
        user = UserFactory.create()
        display = MilestoneDisplay(user)

        DataPointFactory.create(link_clicks=4, date=aware_date(2014, 1, 1), link__user=user)

        eq_(display.milestone_date('link_clicks', 8, 2), None)
示例#27
0
    def test_affiliates_email_validation(self):
        """
        The affiliates_email field is only valid if an Affiliates user exists
        with the specified email address.
        """
        form = FacebookAccountLinkForm({'affiliates_email': '*****@*****.**'})
        eq_(form.is_valid(), False)

        user = UserFactory.create()
        form = FacebookAccountLinkForm({'affiliates_email': user.email})
        eq_(form.is_valid(), True)
示例#28
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)
示例#29
0
    def test_post_user_mismatch(self):
        """
        If the user being edited doesn't match the current user,
        redirect to the profile page for the user being edited without
        making any changes.
        """
        request_user = UserFactory.create()
        request = Mock(user=request_user)

        edited_user = UserFactory.create(display_name='Bob')
        self.view.get_object = Mock(return_value=edited_user.userprofile)

        # Redirect should be called and given the profile, while the
        # parent's post should not be called.
        with patch('affiliates.users.views.UpdateView.post') as super_post:
            with patch('affiliates.users.views.redirect') as redirect:
                eq_(self.view.post(request, edited_user.pk),
                    redirect.return_value)
                redirect.assert_called_with(edited_user.userprofile)
                ok_(not super_post.called)
示例#30
0
    def test_milestone_date_invalid_metric(self):
        """
        If an invalid metric name is given, raise an AttributeError.
        """
        user = UserFactory.create()
        display = MilestoneDisplay(user)

        DataPointFactory.create(link_clicks=4, date=aware_date(2014, 1, 1), link__user=user)

        with self.assertRaises(AttributeError):
            display.milestone_date('invalid', 2, 1)
示例#31
0
    def test_create_link_active_link(self):
        """If an active link already exists, create_link should return False."""
        link = FacebookAccountLinkFactory.create(is_active=True)
        result = self.manager.create_link(link.facebook_user,
                                          link.affiliates_user.email)
        eq_(result, False)

        # Test an active link with a different email address.
        user = UserFactory.create()
        result = self.manager.create_link(link.facebook_user, user.email)
        eq_(result, False)
示例#32
0
    def test_affiliates_email_validation(self):
        """
        The affiliates_email field is only valid if an Affiliates user exists
        with the specified email address.
        """
        form = FacebookAccountLinkForm({'affiliates_email': '*****@*****.**'})
        eq_(form.is_valid(), False)

        user = UserFactory.create()
        form = FacebookAccountLinkForm({'affiliates_email': user.email})
        eq_(form.is_valid(), True)
示例#33
0
    def test_links_filters(self):
        category = CategoryFactory.create()
        user = UserFactory.create()

        text_banner_variation = TextBannerVariationFactory.create(banner__category=category)
        upgrade_banner_variation = FirefoxUpgradeBannerVariationFactory.create(
            banner__category=category)

        text_link = LinkFactory.create(banner_variation=text_banner_variation, user=user)
        LinkFactory.create(banner_variation=upgrade_banner_variation)

        eq_(list(category.links(user=user)), [text_link])
示例#34
0
    def test_milestone_date_not_reached(self):
        """
        If the milestone hasn't been reached by the user, return None.
        """
        user = UserFactory.create()
        display = MilestoneDisplay(user)

        DataPointFactory.create(link_clicks=4,
                                date=aware_date(2014, 1, 1),
                                link__user=user)

        eq_(display.milestone_date('link_clicks', 8, 2), None)
示例#35
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)
示例#36
0
    def test_links_filters(self):
        category = CategoryFactory.create()
        user = UserFactory.create()

        text_banner_variation = TextBannerVariationFactory.create(banner__category=category)
        upgrade_banner_variation = FirefoxUpgradeBannerVariationFactory.create(
            banner__category=category)

        text_link = LinkFactory.create(banner_variation=text_banner_variation, user=user)
        LinkFactory.create(banner_variation=upgrade_banner_variation)

        eq_(list(category.links(user=user)), [text_link])
示例#37
0
    def test_display_name_none(self):
        """
        If a user's profile has no display name set, return a localized
        default.
        """
        user = UserFactory.create()
        user.userprofile.display_name = ''

        with patch('affiliates.users.models._') as ugettext:
            ugettext.return_value = 'Affiliate'
            eq_(user.display_name, 'Affiliate')
            ugettext.assert_called_with(u'Affiliate')
示例#38
0
 def test_create_link_success(self):
     """
     If no link exists, create_link should create one and save it to the
     database.
     """
     fb_user = FacebookUserFactory.create()
     user = UserFactory.create()
     link = self.manager.create_link(fb_user, user.email)
     eq_(link.affiliates_user, user)
     eq_(link.facebook_user, fb_user)
     eq_(link.is_active, False)
     ok_(self.manager.filter(pk=link.pk).exists())
示例#39
0
    def test_display_name_none(self):
        """
        If a user's profile has no display name set, return a localized
        default.
        """
        user = UserFactory.create()
        user.userprofile.display_name = ''

        with patch('affiliates.users.models._') as ugettext:
            ugettext.return_value = 'Affiliate'
            eq_(user.display_name, 'Affiliate')
            ugettext.assert_called_with(u'Affiliate')
示例#40
0
    def test_purge_delete_everything(self):
        """Ensure that purge deletes all relevant database entries."""
        fb_user = FacebookUserFactory.create()
        user = UserFactory.create()
        link = FacebookAccountLinkFactory.create(affiliates_user=user,
                                                 facebook_user=fb_user)
        instance = FacebookBannerInstanceFactory.create(user=fb_user)
        personal_items = [(item.__class__, item.id) for item in
                          (fb_user, link, instance)]

        self.manager.purge_user_data(fb_user)
        for klass, id in personal_items:
            eq_(klass.objects.filter(id=id).exists(), False)
示例#41
0
    def test_process_request_facebook_user(self):
        """
        If the current user doesn't have a profile (either as a
        FacebookUser or no profile in the database) return None and do
        not log a message.
        """
        self.request.user = FacebookUserFactory.create()
        eq_(self.middleware.process_request(self.request), None)
        ok_(not self.messages.info.called)

        self.request.user = UserFactory.create(userprofile=None)
        eq_(self.middleware.process_request(self.request), None)
        ok_(not self.messages.info.called)
示例#42
0
    def test_milestone_date_invalid_metric(self):
        """
        If an invalid metric name is given, raise an AttributeError.
        """
        user = UserFactory.create()
        display = MilestoneDisplay(user)

        DataPointFactory.create(link_clicks=4,
                                date=aware_date(2014, 1, 1),
                                link__user=user)

        with self.assertRaises(AttributeError):
            display.milestone_date('invalid', 2, 1)
示例#43
0
    def test_process_request_facebook_user(self):
        """
        If the current user doesn't have a profile (either as a
        FacebookUser or no profile in the database) return None and do
        not log a message.
        """
        self.request.user = FacebookUserFactory.create()
        eq_(self.middleware.process_request(self.request), None)
        ok_(not self.messages.info.called)

        self.request.user = UserFactory.create(userprofile=None)
        eq_(self.middleware.process_request(self.request), None)
        ok_(not self.messages.info.called)
示例#44
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))
示例#45
0
    def test_clear_old(self):
        """
        When the leaderboard is updated, old standings should be
        cleared.
        """
        user1 = UserFactory.create() # Total: 38 clicks
        self._link_with_clicks(user1, 5, [4, 6, 3]) # 18 clicks
        self._link_with_clicks(user1, 1, [8, 9, 2]) # 20 clicks

        user2 = UserFactory.create() # Total: 49 clicks
        self._link_with_clicks(user2, 13, [12, 11, 13]) # 49 clicks

        # Create existing leaderboard with users in opposite order.
        LeaderboardStandingFactory.create(user=user1, ranking=1, metric='link_clicks')
        LeaderboardStandingFactory.create(user=user2, ranking=2, metric='link_clicks')

        self.command.handle()
        ok_(not (LeaderboardStanding.objects
                 .filter(user=user1, ranking=1, metric='link_clicks')
                 .exists()))
        ok_(not (LeaderboardStanding.objects
                 .filter(user=user2, ranking=2, metric='link_clicks')
                 .exists()))
示例#46
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))
示例#47
0
    def test_permission_doesnt_exist(self):
        """
        If the can_share_website permission isn't created yet, do
        nothing.
        """
        user = UserFactory.create()
        user.user_permissions.clear()

        with patch('affiliates.users.models.Permission') as MockPermission:
            MockPermission.DoesNotExist = Exception
            MockPermission.objects.get.side_effect = MockPermission.DoesNotExist

            add_default_permissions(User, created=True, instance=user)
            ok_(self.can_share_website not in user.user_permissions.all())
示例#48
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))
示例#49
0
    def test_permission_doesnt_exist(self):
        """
        If the can_share_website permission isn't created yet, do
        nothing.
        """
        user = UserFactory.create()
        user.user_permissions.clear()

        with patch('affiliates.users.models.Permission') as MockPermission:
            MockPermission.DoesNotExist = Exception
            MockPermission.objects.get.side_effect = MockPermission.DoesNotExist

            add_default_permissions(User, created=True, instance=user)
            ok_(self.can_share_website not in user.user_permissions.all())
示例#50
0
    def test_no_available_newsitem(self):
        """
        If there are no visible NewsItems, pass None to the template
        context.
        """
        NewsItemFactory.create_batch(3, visible=False)

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

        render.assert_called_with(request, 'base/dashboard.html', {
            'links': ANY,
        })
示例#51
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))
示例#52
0
    def test_milestone_date(self):
        user = UserFactory.create()
        display = MilestoneDisplay(user)

        DataPointFactory.create(link_clicks=4,
                                date=aware_date(2014, 1, 1),
                                link__user=user)
        DataPointFactory.create(link_clicks=3,
                                date=aware_date(2014, 1, 2),
                                link__user=user)
        DataPointFactory.create(link_clicks=2,
                                date=aware_date(2014, 1, 3),
                                link__user=user)

        eq_(display.milestone_date('link_clicks', 10, 4),
            aware_date(2014, 1, 2))
示例#53
0
    def test_no_available_newsitem(self):
        """
        If there are no visible NewsItems, pass None to the template
        context.
        """
        NewsItemFactory.create_batch(3, visible=False)

        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': None,
            'milestones': ANY,
            'links': ANY,
        })
示例#54
0
    def test_get_queryset(self):
        """
        Available links should be limited to those owned by the current
        user.
        """
        request = self.factory.get('/')
        request.user = UserFactory.create()

        link1, link2 = LinkFactory.create_batch(2, user=request.user)
        unowned_link = LinkFactory.create()

        view = views.LinkDetailView()
        view.request = request
        qs = view.get_queryset()

        ok_(link1 in qs)
        ok_(link2 in qs)
        ok_(unowned_link not in qs)
示例#55
0
    def test_get_queryset(self):
        """
        Available links should be limited to those owned by the current
        user.
        """
        request = self.factory.get('/')
        request.user = UserFactory.create()

        link1, link2 = LinkFactory.create_batch(2, user=request.user)
        unowned_link = LinkFactory.create()

        view = views.LinkDetailView()
        view.request = request
        qs = view.get_queryset()

        ok_(link1 in qs)
        ok_(link2 in qs)
        ok_(unowned_link not in qs)
示例#56
0
    def setUp(self):
        self.user = UserFactory.create()
        self.display = MilestoneDisplay(self.user)

        self.display.milestone_date = Mock()
        self.display.milestone_cmp = Mock()
        self.display.surrounding_milestones = Mock()
        self.display.close_to_milestone = Mock()
        self.display.metric_milestones = [5, 10, 20, 50]
        self.display.creation_milestones = [3, 5, 10]

        self.messages = {
            'nothing_yet': 'nothing_yet_{0}',
            'close_to_milestone': 'close_to_milestone_{0}',
            'achieved_milestone': 'achieved_milestone_{0}',
            'total_next_milestone': 'total_next_milestone_{0}_{1}',
            'total_no_milestone': 'total_no_milestone_{0}',
            'link_created': 'link_created'
        }
示例#57
0
    def test_create_link(self):
        """
        create_link should create a Link object using this banner's
        description and generated code.
        """
        banner = Banner(destination='https://www.mozilla.org')
        banner.generate_banner_code = Mock(return_value="""
            <a href="{href}">Link!</a>
        """)
        banner.get_banner_type = Mock(return_value='generic_banner')
        user = UserFactory.create()

        with patch.object(Link, 'get_referral_url') as get_referral_url:
            get_referral_url.return_value = 'asdf'
            link = banner.create_link(user, foo='bar', baz=1)

        ok_(isinstance(link, Link))
        eq_(link.user, user)
        eq_(link.destination, 'https://www.mozilla.org')
        self.assertHTMLEqual(link.html, """
            <a href="asdf">Link!</a>
        """)
        banner.generate_banner_code.assert_called_with(foo='bar', baz=1)