def test_not_authenticated(self): """ If the current user isn't authenticated, render the home page with the correct stats. """ request = self.factory.get('/') request.user = Mock() request.user.is_authenticated.return_value = False # User count = 27 + 8 autogenerated via related factory # Click count = 64 # Link count = 3 + 5 autogenerated via related factory UserFactory.create_batch(27) for clicks in (4, 6, 9, 10): DataPointFactory.create(link_clicks=clicks, date=date(2014, 4, 26)) for clicks in (25, 5, 5): LinkFactory.create(aggregate_link_clicks=clicks) # Create a link with multiple datapoints to test for a faulty # join that would screw up the totals. link = LinkFactory.create() DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 26)) DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 27)) with patch('affiliates.base.views.render') as render: eq_(views.home(request), render.return_value) render.assert_called_with(request, 'base/home.html', { 'affiliate_count': 35, 'link_count': 8, 'click_count': 78 })
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()))
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])
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])
def test_not_authenticated(self): """ If the current user isn't authenticated, render the home page with the correct stats. """ request = self.factory.get('/') request.user = Mock() request.user.is_authenticated.return_value = False # User count = 27 + 8 autogenerated via related factory # Click count = 64 # Link count = 3 + 5 autogenerated via related factory UserFactory.create_batch(27) for clicks in (4, 6, 9, 10): DataPointFactory.create(link_clicks=clicks, date=date(2014, 4, 26)) for clicks in (25, 5, 5): LinkFactory.create(aggregate_link_clicks=clicks) # Create a link with multiple datapoints to test for a faulty # join that would screw up the totals. link = LinkFactory.create() DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 26)) DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 27)) with patch('affiliates.base.views.render') as render: eq_(views.home(request), render.return_value) render.assert_called_with(request, 'base/home.html', { 'affiliate_count': 35, 'link_count': 8, 'click_count': 78 })
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)
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)
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)
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)
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)
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())
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))
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())
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)
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)
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, })
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))
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, })
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))
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))
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)
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))
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)
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())
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)
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)
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())
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)
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)
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)
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)
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)
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)
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)
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])
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])
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())
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')
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')
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)
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)
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)
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)
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)
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)
def test_not_authenticated(self): """ If the current user isn't authenticated, render the home page with the correct stats. """ request = self.factory.get('/') request.user = Mock() request.user.is_authenticated.return_value = False # User count = 27 + 3 autogenerated via related factory # Link count = 3 autogenerated via related factory UserFactory.create_batch(27) LinkFactory.create_batch(3) with patch('affiliates.base.views.render') as render: with patch.object(Link.objects, 'total_link_clicks', return_value=64): eq_(views.home(request), render.return_value) render.assert_called_with(request, 'base/home.html', { 'affiliate_count': 30, 'link_count': 3, 'click_count': 64 })
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))
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()))
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))
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())
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))
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())
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, })
def test_not_authenticated(self): """ If the current user isn't authenticated, render the home page with the correct stats. """ request = self.factory.get('/') request.user = Mock() request.user.is_authenticated.return_value = False # User count = 27 + 3 autogenerated via related factory # Link count = 3 autogenerated via related factory UserFactory.create_batch(27) LinkFactory.create_batch(3) with patch('affiliates.base.views.render') as render: with patch.object(Link.objects, 'total_link_clicks', return_value=64): eq_(views.home(request), render.return_value) render.assert_called_with(request, 'base/home.html', { 'affiliate_count': 30, 'link_count': 3, 'click_count': 64 })
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))
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, })
def test_create_profile(self): """Create an empty profile for newly-created users.""" user = UserFactory.build() # Profile doesn't exist yet. with self.assertRaises(UserProfile.DoesNotExist): user.userprofile # After saving, empty profile should exist. user.save() profile_pk = user.userprofile.pk ok_(profile_pk is not None) # If we save again, profile should not have been replaced. user.userprofile.display_name = 'Bob' user.userprofile.save() user = User.objects.get(pk=user.pk) eq_(user.userprofile.pk, profile_pk)