Example #1
0
    def test_filter_country(self):
        """
        If the country field is set, only return users within that country.
        """
        user1 = FacebookUserFactory.create(leaderboard_position=1, country='us')
        FacebookUserFactory.create(leaderboard_position=2, country='fr')
        user3 = FacebookUserFactory.create(leaderboard_position=3, country='us')

        form = LeaderboardFilterForm({'country': 'us'})
        eq_([user1, user3], list(form.get_top_users()))
Example #2
0
    def test_exclude_unranked_users(self):
        """
        If a user has a leaderboard position of -1, do not include them in the
        top users list.
        """
        user1 = FacebookUserFactory.create(leaderboard_position=1)
        FacebookUserFactory.create(leaderboard_position=-1)
        user3 = FacebookUserFactory.create(leaderboard_position=2)

        form = LeaderboardFilterForm()
        eq_([user1, user3], list(form.get_top_users()))
Example #3
0
    def test_get_top_users(self):
        """
        Test that get_top_users, er, gets the top users ranked by
        leaderboard_position.
        """
        user1 = FacebookUserFactory.create(leaderboard_position=1)
        user2 = FacebookUserFactory.create(leaderboard_position=2)
        user3 = FacebookUserFactory.create(leaderboard_position=3)

        form = LeaderboardFilterForm()
        eq_([user1, user2, user3], list(form.get_top_users()))
Example #4
0
    def test_get_top_users(self):
        """
        Test that get_top_users, er, gets the top users ranked by
        leaderboard_position.
        """
        user1 = FacebookUserFactory.create(leaderboard_position=1)
        user2 = FacebookUserFactory.create(leaderboard_position=2)
        user3 = FacebookUserFactory.create(leaderboard_position=3)

        form = LeaderboardFilterForm()
        eq_([user1, user2, user3], list(form.get_top_users()))
Example #5
0
    def test_exclude_unranked_users(self):
        """
        If a user has a leaderboard position of -1, do not include them in the
        top users list.
        """
        user1 = FacebookUserFactory.create(leaderboard_position=1)
        FacebookUserFactory.create(leaderboard_position=-1)
        user3 = FacebookUserFactory.create(leaderboard_position=2)

        form = LeaderboardFilterForm()
        eq_([user1, user3], list(form.get_top_users()))
Example #6
0
    def test_filter_country(self):
        """
        If the country field is set, only return users within that country.
        """
        user1 = FacebookUserFactory.create(leaderboard_position=1,
                                           country='us')
        FacebookUserFactory.create(leaderboard_position=2, country='fr')
        user3 = FacebookUserFactory.create(leaderboard_position=3,
                                           country='us')

        form = LeaderboardFilterForm({'country': 'us'})
        eq_([user1, user3], list(form.get_top_users()))
Example #7
0
    def test_flush_session(self, update_user_info):
        """
        If a previous login session is found and logging in as a different user,
        flush the previous session.
        """
        request = self.request()
        user1 = FacebookUserFactory()
        login(request, user1)

        request.session['somedata'] = 1
        user2 = FacebookUserFactory()
        login(request, user2)
        ok_(not 'somedata' in request.session)
Example #8
0
 def test_total_for_month(self):
     """Test that the click sum logic is correct."""
     user = FacebookUserFactory.create()
     self._mkstats(user, 2012, 5, 5)
     self._mkstats(user, 2012, 3, 7)
     self._mkstats(user, 2012, 5, 2)
     eq_(self.manager.total_for_month(user, 2012, 5), 7)
Example #9
0
    def test_new_user_first_run(self):
        """If the logged in user is new, redirect them to the first run page."""
        user = FacebookUserFactory.create()
        self.client.fb_login(user)

        response = self.banner_list()
        self.assertTemplateUsed(response, 'facebook/first_run.html')
Example #10
0
 def test_create_link_no_account(self):
     """
     If no user exists with the given email, create_link should return False.
     """
     fb_user = FacebookUserFactory.create()
     eq_(self.manager.create_link(fb_user, '*****@*****.**'),
         False)
Example #11
0
    def test_old_user_banner_list(self):
        """If the logged in user is not new, render the banner list page."""
        user = FacebookUserFactory.create()
        self.client.fb_login(user)

        response = self.banner_list()
        self.assertTemplateUsed(response, 'facebook/banner_list.html')
Example #12
0
    def test_old_user_banner_list(self):
        """If the logged in user is not new, render the banner list page."""
        user = FacebookUserFactory.create()
        self.client.fb_login(user)

        response = self.banner_list()
        self.assertTemplateUsed(response, 'facebook/banner_list.html')
Example #13
0
    def test_new_user_first_run(self):
        """If the logged in user is new, redirect them to the first run page."""
        user = FacebookUserFactory.create()
        self.client.fb_login(user)

        response = self.banner_list()
        self.assertTemplateUsed(response, 'facebook/first_run.html')
Example #14
0
 def test_newsletter_en_only(self):
     """Only show the newsletter to English locales."""
     user = FacebookUserFactory()
     ok_('newsletter_form' in self._app_context(user=user, locale='en-US'))
     ok_('newsletter_form' in self._app_context(user=user, locale='en-AU'))
     ok_(not 'newsletter_form' in self._app_context(user=user, locale='fr'))
     ok_(not 'newsletter_form' in self._app_context(user=user, locale='de'))
Example #15
0
    def test_purge_email(self):
        """
        If a user is being purged and has banners that have passed ad review,
        send an email to the admin with info about the ads that need to be
        removed.
        """
        user = FacebookUserFactory.create()
        instance1 = FacebookBannerInstanceFactory.create(
            user=user, review_status=FacebookBannerInstance.PASSED)
        instance2 = FacebookBannerInstanceFactory.create(
            user=user, review_status=FacebookBannerInstance.PASSED)
        instance3 = FacebookBannerInstanceFactory.create(
            user=user, review_status=FacebookBannerInstance.FAILED)

        user_id = 'User ID: %s' % user.id
        instance1_id = 'Banner Instance ID: %s' % instance1.id
        instance2_id = 'Banner Instance ID: %s' % instance2.id
        instance3_id = 'Banner Instance ID: %s' % instance3.id
        self.manager.purge_user_data(user)

        eq_(len(mail.outbox), 1)
        msg = mail.outbox[0]
        ok_('*****@*****.**' in msg.to)
        ok_(user_id in msg.body)
        ok_(instance1_id in msg.body)
        ok_(instance2_id in msg.body)
        ok_(not instance3_id in msg.body)
Example #16
0
 def test_valid_user(self, purge_user_data):
     """
     If the supplied user id is valid, call purge_user_data with the user.
     """
     user = FacebookUserFactory.create()
     response = self.deauthorize({'user_id': user.id})
     eq_(response.status_code, 200)
     purge_user_data.assert_called_with(user)
Example #17
0
 def test_valid_user(self, purge_user_data):
     """
     If the supplied user id is valid, call purge_user_data with the user.
     """
     user = FacebookUserFactory.create()
     response = self.deauthorize({'user_id': user.id})
     eq_(response.status_code, 200)
     purge_user_data.assert_called_with(user)
Example #18
0
 def test_save_locale(self):
     """The form should save the current locale on the instance."""
     locale = FacebookBannerLocaleFactory.create(locale='es')
     with self.activate('es'):
         form = self.form('es', {'text': 'asdf', 'banner': locale.banner.id},
                          user=FacebookUserFactory.create())
         instance = form.save()
         eq_(instance.locale, 'es')
Example #19
0
 def test_notify_new_instance(self):
     """
     If the instance being saved is new, no notification should be created.
     """
     user = FacebookUserFactory.create()
     banner = FacebookBannerFactory.create()
     instance = FacebookBannerInstanceFactory(user=user, banner=banner)
     instance.save()
     eq_(user.appnotification_set.exists(), False)
Example #20
0
 def test_create_link_affiliates_already_linked(self):
     """
     If the Affiliates user is already linked to another account, create_link
     should return False.
     """
     link = FacebookAccountLinkFactory.create(is_active=True)
     fb_user = FacebookUserFactory.create()
     result = self.manager.create_link(fb_user, link.affiliates_user.email)
     eq_(result, False)
Example #21
0
 def test_notify_new_instance(self):
     """
     If the instance being saved is new, no notification should be created.
     """
     user = FacebookUserFactory.create()
     banner = FacebookBannerFactory.create()
     instance = FacebookBannerInstanceFactory(user=user, banner=banner)
     instance.save()
     eq_(user.appnotification_set.exists(), False)
Example #22
0
 def test_request_user(self, update_user_info):
     """
     After a user is logged in, the user attribute should be set on the
     request object.
     """
     request = self.request()
     user = FacebookUserFactory()
     login(request, user)
     eq_(request.user, user)
Example #23
0
 def test_old_user_task_scheduled(self, update_task, update_method):
     """
     If the user logging in isn't new, use the asynchronous task to update
     their info instead of the normal method.
     """
     request = self.request()
     user = FacebookUserFactory()
     login(request, user)
     update_task.delay.assert_called_once_with(user.id)
     ok_(not update_method.called)
    def test_account_link_form(self):
        """
        If the user doesn't have a linked account, the account_link_form should
        be included in the context.
        """
        unlinked_account = FacebookUserFactory.create()
        ok_('account_link_form' in self._app_context(user=unlinked_account))

        account_link = FacebookAccountLinkFactory.create(is_active=True)
        linked_account = account_link.facebook_user
        ok_('account_link_form' not in self._app_context(user=linked_account))
Example #25
0
    def test_facebook_auth(self, update_user_info):
        """
        If the user has authed via the Facebook auth mechanism, execute the
        view.
        """
        request = self.request()
        user = FacebookUserFactory()
        fb_login(request, user)

        response = view(request)
        eq_(response, True)
Example #26
0
    def test_account_link_form(self):
        """
        If the user doesn't have a linked account, the account_link_form should
        be included in the context.
        """
        unlinked_account = FacebookUserFactory.create()
        ok_('account_link_form' in self._app_context(user=unlinked_account))

        account_link = FacebookAccountLinkFactory.create(is_active=True)
        linked_account = account_link.facebook_user
        ok_('account_link_form' not in self._app_context(user=linked_account))
Example #27
0
    def test_new_session_key(self, update_user_info):
        """
        If there is an existing, unauthenticated session, change the session
        key on the request.
        """
        request = self.request()
        old_key = request.session.session_key
        user = FacebookUserFactory()

        login(request, user)
        ok_(request.session.session_key != old_key)
Example #28
0
 def test_save_locale(self):
     """The form should save the current locale on the instance."""
     locale = FacebookBannerLocaleFactory.create(locale='es')
     with self.activate('es'):
         form = self.form('es', {
             'text': 'asdf',
             'banner': locale.banner.id
         },
                          user=FacebookUserFactory.create())
         instance = form.save()
         eq_(instance.locale, 'es')
Example #29
0
    def test_country_saved(self, login, update_user_info):
        """
        When a user enters the app, their country should be set and
        login should be called with the updated user object so that it will be
        saved to the database.
        """
        user = FacebookUserFactory.create(country='us')
        payload = create_payload(user_id=user.id, country='fr')
        self.load_app(payload)

        eq_(login.called, True)
        eq_(login.call_args[0][1].country, 'fr')
Example #30
0
    def test_last_login_attribute(self, mock_datetime, update_user_info):
        """
        During the login process, the last_login attribute on the user must be
        set to the current datetime.
        """
        mock_datetime.now.return_value = datetime(2012, 1, 1)
        request = self.request()
        user = FacebookUserFactory.create(last_login=datetime(2000, 1, 1))
        login(request, user)

        user = refresh_model(user)
        eq_(user.last_login, datetime(2012, 1, 1))
Example #31
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 #32
0
    def test_country_missing(self, login, update_user_info):
        """
        If the user's country is not included in the signed_request, keep their
        old country value intact.
        """
        user = FacebookUserFactory.create(country='us')
        payload = create_payload(user_id=user.id)
        del payload['user']['country']
        self.load_app(payload)

        eq_(login.called, True)
        eq_(login.call_args[0][1].country, 'us')
Example #33
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())
Example #34
0
    def test_country_saved(self, login, update_user_info):
        """
        When a user enters the app, their country should be set and
        login should be called with the updated user object so that it will be
        saved to the database.
        """
        user = FacebookUserFactory.create(country='us')
        payload = create_payload(user_id=user.id, country='fr')
        self.load_app(payload)

        eq_(login.called, True)
        eq_(login.call_args[0][1].country, 'fr')
Example #35
0
    def test_country_missing(self, login, update_user_info):
        """
        If the user's country is not included in the signed_request, keep their
        old country value intact.
        """
        user = FacebookUserFactory.create(country='us')
        payload = create_payload(user_id=user.id)
        del payload['user']['country']
        self.load_app(payload)

        eq_(login.called, True)
        eq_(login.call_args[0][1].country, 'us')
Example #36
0
    def test_user_found(self, activate_locale):
        """
        If there is an authenticated session with an existing user, the
        specified user should be available via the user attribute.
        """
        request = self.request()
        user = FacebookUserFactory.create(locale='fr')
        request.session[SESSION_KEY] = user.id
        self.auth_middleware.process_request(request)
        eq_(request.user, user)

        # Check that the locale from the user object was activated.
        activate_locale.assert_called_once_with(ANY, 'fr')
Example #37
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)
Example #38
0
    def test_purge_delete_images(self):
        """
        If there are any custom-generated images for a user's banner instances,
        they must be deleted.
        """
        user = FacebookUserFactory.create()
        instance = FacebookBannerInstanceFactory.create(user=user)
        with open(path('images', 'banner.png')) as banner:
            instance.custom_image.save('custom.png', File(banner))
        file_path = ''.join((settings.MEDIA_ROOT, instance.custom_image.name))

        self.manager.purge_user_data(user)
        ok_(not os.path.exists(file_path))
Example #39
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)
Example #40
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)
Example #41
0
    def test_user_found(self, activate_locale):
        """
        If there is an authenticated session with an existing user, the
        specified user should be available via the user attribute.
        """
        request = self.request()
        user = FacebookUserFactory.create(locale='fr')
        request.session[SESSION_KEY] = user.id
        self.auth_middleware.process_request(request)
        eq_(request.user, user)

        # Check that the locale from the user object was activated.
        activate_locale.assert_called_once_with(ANY, 'fr')
Example #42
0
    def test_validate_user_owns_banner(self):
        """
        The delete form must validate that the user passed in the constructor
        owns the banner instance.
        """
        user = FacebookUserFactory.create()
        instance1 = FacebookBannerInstanceFactory.create(user=user)
        instance2 = FacebookBannerInstanceFactory.create()

        form = BannerInstanceDeleteForm(user, {'banner_instance': instance1.id})
        ok_(form.is_valid())

        form = BannerInstanceDeleteForm(user, {'banner_instance': instance2.id})
        ok_(not form.is_valid())
Example #43
0
    def test_user_non_fb_app(self, activate_locale):
        """
        If there is an authenticated session with an existing user outside of
        the app, the specified user should not be a FacebookUser.
        """
        request = self.request(url='/', locale='pt-BR')
        user = FacebookUserFactory.create()
        request.session[SESSION_KEY] = user.id

        self.auth_middleware.process_request(request)
        ok_(not request.user == user)
        eq_(request.user.is_anonymous(), True)

        # Check that the FB locale stuff was never called.
        eq_(activate_locale.called, False)
Example #44
0
    def test_user_non_fb_app(self, activate_locale):
        """
        If there is an authenticated session with an existing user outside of
        the app, the specified user should not be a FacebookUser.
        """
        request = self.request(url='/', locale='pt-BR')
        user = FacebookUserFactory.create()
        request.session[SESSION_KEY] = user.id

        self.auth_middleware.process_request(request)
        ok_(not request.user == user)
        eq_(request.user.is_anonymous(), True)

        # Check that the FB locale stuff was never called.
        eq_(activate_locale.called, False)
Example #45
0
    def test_notifications_cleared(self):
        """
        If a page is using the base Facebook template, any AppNotifications for
        the current user should be displayed and then removed from the database.
        """
        user = FacebookUserFactory.create()
        AppNotificationFactory.create(user=user)
        AppNotificationFactory.create(user=user)

        eq_(len(user.appnotification_set.all()), 2)

        with self.activate('en-US'):
            self.client.fb_login(user)
            self.client.get(reverse('facebook.base_test'))

        eq_(len(user.appnotification_set.all()), 0)
Example #46
0
    def test_validate_user_owns_banner(self):
        """
        The delete form must validate that the user passed in the constructor
        owns the banner instance.
        """
        user = FacebookUserFactory.create()
        instance1 = FacebookBannerInstanceFactory.create(user=user)
        instance2 = FacebookBannerInstanceFactory.create()

        form = BannerInstanceDeleteForm(user,
                                        {'banner_instance': instance1.id})
        ok_(form.is_valid())

        form = BannerInstanceDeleteForm(user,
                                        {'banner_instance': instance2.id})
        ok_(not form.is_valid())
Example #47
0
    def test_delayed_task_overwritten(self, update_user_info):
        """
        Regression test: If DEV is true, the delayed task will execute
        immediately. But because the task does not alter the user object, if the
        old user object is saved these changes will be overwritten.
        """
        request = self.request()
        user = FacebookUserFactory.create(first_name='Unchanged')

        def alter_user(user):
            user.first_name = 'Changed'
            user.save()
        update_user_info.side_effect = alter_user

        login(request, user)
        user = refresh_model(user)
        eq_(user.first_name, 'Changed')
Example #48
0
 def user(self, **kwargs):
     user = FacebookUserFactory(**kwargs)
     user.save = Mock()
     return user
Example #49
0
 def setUp(self):
     self.user = FacebookUserFactory.create()
     with self.activate('en-US'):
         self.url = reverse('facebook.link_accounts')
Example #50
0
 def setUp(self):
     self.user = FacebookUserFactory.create()
     self.client.fb_login(self.user)
     FacebookBannerInstanceFactory.create(user=self.user)
 def test_app_notifications(self):
     """If the user is logged in, include all of their app notifications."""
     user = FacebookUserFactory.create()
     notes = [AppNotificationFactory.create(user=user) for x in range(2)]
     eq_(list(self._app_context(user=user)['app_notifications']), notes)
Example #52
0
 def setUp(self):
     self.user = FacebookUserFactory.create()
     self.client.fb_login(self.user)