def test_sync_where_in_facebook_missing_in_facetools(self):
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t3()
        self.assertTrue(not all([u['installed'] for u in facebook_test_users])) # make sure all the users aren't set to have the app installed
        self.assertEquals(0, TestUser.objects.count())
        management.call_command('sync_facebook_test_users', 'testapp3')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(facebook_test_users, json.loads(requests.get(test_users_url).content)['data'], _get_app_access_token())

        # Make sure the data looks good
        self.assertEquals(3, TestUser.objects.count())
        self.assertEquals(3, len(test_users))
        self.assertEquals(3, len([u for u in test_users if 'graph_user_data' in u]))

        # Now remove the users from facetools, leaving them on facebook
        TestUser.objects.all().delete()
        self.assertEquals(0, TestUser.objects.count())
        check_users = json.loads(requests.get(test_users_url).content)['data']
        old_ids = [u['graph_user_data']['id'] for u in test_users]
        self.assertEquals(3, len([c for c in check_users if c['id'] in old_ids]))

        # After syncing again the data should be back to normal
        management.call_command('sync_facebook_test_users', 'testapp3')
        test_users = _merge_with_facebook_data(facebook_test_users, json.loads(requests.get(test_users_url).content)['data'], _get_app_access_token())
        self.assertEquals(3, TestUser.objects.count())
        self.assertEquals(3, len(test_users))
        self.assertEquals(3, len([u for u in test_users if 'graph_user_data' in u]))

        # Make sure the generated fixture is correct
        self.assertTestUserFixture(testapp3, 'testapp3', t3())
    def test_overwriting_many_users_mixed_installations(self):
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t3()
        self.assertTrue(not all([u['installed'] for u in facebook_test_users])) # make sure all the users aren't set to have the app installed
        self.assertEquals(0, TestUser.objects.count())
        management.call_command('sync_facebook_test_users', 'testapp3')
        management.call_command('sync_facebook_test_users', 'testapp3')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(facebook_test_users, json.loads(requests.get(test_users_url).content)['data'], _get_app_access_token())

        # Make sure each test user's information on facebook is correct
        self.assertEquals(3, len(test_users))
        self.assertEquals(3, len([u for u in test_users if 'graph_user_data' in u and 'graph_permission_data' in u]))
        for test_user in test_users:
            for permission in test_user['permissions']:
                self.assertTrue(permission.strip() in test_user['graph_permission_data']['data'][0])
            friends_on_facebook = _get_friends_on_facebook(test_user)
            for friend_name in test_user.get('friends', []):
                self.assertTrue(friend_name in friends_on_facebook)
                self.assertEqual(friends_on_facebook[friend_name],
                                 TestUser.objects.get(name=friend_name).facebook_id)

        # Make sure each test user's information in facetools is correct
        self.assertEquals(3, TestUser.objects.count())
        for user in TestUser.objects.all():
            test_user = [t for t in test_users if t['graph_user_data']['id'] == str(user.facebook_id)][0]
            self.assertEquals(test_user['name'], user.name)
            self.assertEquals(test_user['graph_user_data']['login_url'], user.login_url)
            self.assertEquals(test_user['installed'], _has_access_code(user.access_token))

        # Make sure the generated fixture is correct
        self.assertTestUserFixture(testapp3, 'testapp3', t3())
Esempio n. 3
0
    def test_sync_where_in_facebook_and_in_facetools_but_data_not_synced(self):
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t3()
        self.assertEquals(0, TestUser.objects.count())
        management.call_command('sync_facebook_test_users', 'testapp3')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (
            settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(
            facebook_test_users,
            json.loads(requests.get(test_users_url).content)['data'],
            _get_app_access_token())

        # Make sure the data looks good
        self.assertEquals(3, TestUser.objects.count())
        self.assertEquals(3, len(test_users))
        self.assertEquals(
            3, len([u for u in test_users if 'graph_user_data' in u]))

        # Now change the user data on facetools, leaving them out of sync with the facebook data
        old_values = {}
        try:
            for test_user in TestUser.objects.all():
                old_values[test_user.name] = {
                    'facebook_id': test_user.facebook_id,
                    'access_token': test_user.access_token
                }
                test_user.facebook_id = 0
                test_user.access_token = "failbear"
                test_user.save()

            # After syncing again the data should be back to normal
            management.call_command('sync_facebook_test_users', 'testapp3')
            test_users = _merge_with_facebook_data(
                facebook_test_users,
                json.loads(requests.get(test_users_url).content)['data'],
                _get_app_access_token())
            self.assertEquals(3, TestUser.objects.count())
            self.assertEquals(3, len(test_users))
            self.assertEquals(
                3, len([u for u in test_users if 'graph_user_data' in u]))
            for test_user in TestUser.objects.all():
                self.assertNotEquals(0, test_user.facebook_id)
                self.assertNotEquals("failbear", test_user.access_token)
        finally:
            for test_user in TestUser.objects.all():
                test_user.facebook_id = old_values[
                    test_user.name]['facebook_id']
                test_user.access_token = old_values[
                    test_user.name]['access_token']
                test_user.save()

        # Make sure the generated fixture is correct
        self.assertTestUserFixture(testapp3, 'testapp3', t3())
Esempio n. 4
0
    def test_sync_all_apps(self):
        from test_project.testapp1.facebook_test_users import facebook_test_users as t1
        from test_project.testapp2.facebook_test_users import facebook_test_users as t2
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t1 + t2() + t3()
        self.assertEquals(0, TestUser.objects.count())
        management.call_command('sync_facebook_test_users')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (
            settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(
            facebook_test_users,
            json.loads(requests.get(test_users_url).content)['data'],
            _get_app_access_token())

        # Make sure each test user's information on facebook is correct
        self.assertEquals(7, len(test_users))
        self.assertEquals(
            7,
            len([
                u for u in test_users
                if 'graph_user_data' in u and 'graph_permission_data' in u
            ]))
        for test_user in test_users:
            for permission in test_user['permissions']:
                self.assertTrue(permission.strip() in
                                test_user['graph_permission_data']['data'][0])
            friends_on_facebook = _get_friends_on_facebook(test_user)
            for friend_name in test_user.get('friends', []):
                self.assertTrue(friend_name in friends_on_facebook)
                self.assertEqual(
                    friends_on_facebook[friend_name],
                    TestUser.objects.get(name=friend_name).facebook_id)

        # Make sure each test user's information in facetools is correct
        self.assertEquals(7, TestUser.objects.count())
        for user in TestUser.objects.all():
            test_user = [
                t for t in test_users
                if int(t['graph_user_data']['id']) == user.facebook_id
            ][0]
            self.assertEquals(test_user['name'], user.name)
            self.assertEquals(test_user['graph_user_data']['login_url'],
                              user.login_url)
            self.assertEquals(test_user['installed'],
                              _has_access_code(user.access_token))

        # Make sure the generated fixture is correct
        self.assertTestUserFixture(testapp1, 'testapp1', t1)
        self.assertTestUserFixture(testapp2, 'testapp2', t2())
        self.assertTestUserFixture(testapp3, 'testapp3', t3())
Esempio n. 5
0
    def test_fandjango_users_created_correctly(self):
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t3()
        self.assertTrue(not all([
            u['installed'] for u in facebook_test_users
        ]))  # make sure all the users aren't set to have the app installed
        management.call_command('sync_facebook_test_users', 'testapp3')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (
            settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(
            facebook_test_users,
            json.loads(requests.get(test_users_url).content)['data'],
            _get_app_access_token())

        # Make sure only the test users that have the app installed have correpsonding Fandjango User records
        self.assertEquals(2, User.objects.count())
        for test_user in test_users:
            if test_user['installed']:
                user = User.objects.get(
                    facebook_id=int(test_user['graph_user_data']['id']))
                self.assertEquals(test_user['access_token'],
                                  user.oauth_token.token)
            else:
                self.assertEquals(
                    0,
                    User.objects.filter(facebook_id=int(
                        test_user['graph_user_data']['id'])).count())
Esempio n. 6
0
    def test_sync_where_in_facetools_missing_in_facebook(self):
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t3()
        self.assertTrue(not all([
            u['installed'] for u in facebook_test_users
        ]))  # make sure all the users aren't set to have the app installed
        self.assertEquals(0, TestUser.objects.count())
        management.call_command('sync_facebook_test_users', 'testapp3')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (
            settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(
            facebook_test_users,
            json.loads(requests.get(test_users_url).content)['data'],
            _get_app_access_token())

        # Make sure the data looks good
        self.assertEquals(3, TestUser.objects.count())
        self.assertEquals(3, len(test_users))
        self.assertEquals(
            3, len([u for u in test_users if 'graph_user_data' in u]))

        # Now remove the users from facebook, leaving them in facetools database
        for test_user in test_users:
            _delete_test_user_on_facebook(
                TestUser.objects.get(name=test_user['name']))
        self.assertEquals(3, TestUser.objects.count())
        check_users = json.loads(requests.get(test_users_url).content)['data']
        old_ids = [u['graph_user_data']['id'] for u in test_users]
        self.assertTrue(not any([c['id'] in old_ids for c in check_users]))

        # After syncing again the data should be back to normal
        management.call_command('sync_facebook_test_users', 'testapp3')
        test_users = _merge_with_facebook_data(
            facebook_test_users,
            json.loads(requests.get(test_users_url).content)['data'],
            _get_app_access_token())
        self.assertEquals(3, TestUser.objects.count())
        self.assertEquals(3, len(test_users))
        self.assertEquals(
            3, len([u for u in test_users if 'graph_user_data' in u]))

        # Make sure the generated fixture is correct
        self.assertTestUserFixture(testapp3, 'testapp3', t3())
    def test_sync_where_in_facebook_and_in_facetools_but_data_not_synced(self):
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t3()
        self.assertEquals(0, TestUser.objects.count())
        management.call_command('sync_facebook_test_users', 'testapp3')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(facebook_test_users, json.loads(requests.get(test_users_url).content)['data'], _get_app_access_token())

        # Make sure the data looks good
        self.assertEquals(3, TestUser.objects.count())
        self.assertEquals(3, len(test_users))
        self.assertEquals(3, len([u for u in test_users if 'graph_user_data' in u]))

        # Now change the user data on facetools, leaving them out of sync with the facebook data
        old_values = {}
        try:
            for test_user in TestUser.objects.all():
                old_values[test_user.name] = {
                    'facebook_id': test_user.facebook_id,
                    'access_token': test_user.access_token
                }
                test_user.facebook_id = 0
                test_user.access_token = "failbear"
                test_user.save()

            # After syncing again the data should be back to normal
            management.call_command('sync_facebook_test_users', 'testapp3')
            test_users = _merge_with_facebook_data(facebook_test_users, json.loads(requests.get(test_users_url).content)['data'], _get_app_access_token())
            self.assertEquals(3, TestUser.objects.count())
            self.assertEquals(3, len(test_users))
            self.assertEquals(3, len([u for u in test_users if 'graph_user_data' in u]))
            for test_user in TestUser.objects.all():
                self.assertNotEquals(0, test_user.facebook_id)
                self.assertNotEquals("failbear", test_user.access_token)
        finally:
            for test_user in TestUser.objects.all():
                test_user.facebook_id = old_values[test_user.name]['facebook_id']
                test_user.access_token = old_values[test_user.name]['access_token']
                test_user.save()

        # Make sure the generated fixture is correct
        self.assertTestUserFixture(testapp3, 'testapp3', t3())
    def test_fandjango_users_created_correctly(self):
        from test_project.testapp3.facebook_test_users import facebook_test_users as t3
        facebook_test_users = t3()
        self.assertTrue(not all([u['installed'] for u in facebook_test_users])) # make sure all the users aren't set to have the app installed
        management.call_command('sync_facebook_test_users', 'testapp3')

        # Get the test user data from facebook
        test_users_url = "https://graph.facebook.com/%s/accounts/test-users?access_token=%s" % (settings.FACEBOOK_APPLICATION_ID, _get_app_access_token())
        test_users = _merge_with_facebook_data(facebook_test_users, json.loads(requests.get(test_users_url).content)['data'], _get_app_access_token())

        # Make sure only the test users that have the app installed have correpsonding Fandjango User records
        self.assertEquals(2, User.objects.count())
        for test_user in test_users:
            if test_user['installed']:
                user = User.objects.get(facebook_id=int(test_user['graph_user_data']['id']))
                self.assertEquals(test_user['access_token'], user.oauth_token.token)
            else:
                self.assertEquals(0, User.objects.filter(facebook_id=int(test_user['graph_user_data']['id'])).count())