Skip to content

Commit

Permalink
Add methods for creating and deleting test users
Browse files Browse the repository at this point in the history
Methods added to the FacebookTestCase for creating and deleting
test users. A list containing the test users is initialised in
the setup method. A teardown method is added to delete the test
users after running the tests requiring them.
  • Loading branch information
Pääkkönen Juho committed Dec 14, 2016
1 parent ff49d18 commit 12f8b22
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion test/test_facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@


class FacebookTestCase(unittest.TestCase):
"""Sets up application ID and secret from environment."""
"""
Sets up application ID and secret from environment and initialises an
empty list for test users.
"""
def setUp(self):
try:
self.app_id = os.environ["FACEBOOK_APP_ID"]
Expand All @@ -34,6 +38,18 @@ def setUp(self):
raise Exception("FACEBOOK_APP_ID and FACEBOOK_SECRET "
"must be set as environmental variables.")

self.test_users = []

def tearDown(self):
"""Deletes the test users included in the test user list."""
token = facebook.GraphAPI().get_app_access_token(
self.app_id, self.secret)
graph = facebook.GraphAPI(token)

for user in self.test_users:
graph.request(user['id'], {}, None, method='DELETE')
del self.test_users[:]

def assert_raises_multi_regex(
self, expected_exception, expected_regexp, callable_obj=None,
*args, **kwargs):
Expand All @@ -48,6 +64,26 @@ def assert_raises_multi_regex(
except facebook.GraphAPIError as error:
self.assertEqual(error.message, expected_regexp)

def create_test_users(self, app_id, graph, amount):
"""Function for creating test users."""
for i in range(amount):
u = graph.request(app_id + '/accounts/test-users', {}, {},
method='POST')
self.test_users.append(u)

def create_friend_connections(self, user, friends):
"""Function for creating friend connections for a test user."""
user_graph = facebook.GraphAPI(user['access_token'])

for friend in friends:
if user['id'] == friend['id']:
continue
user_graph.request(user['id'] + '/friends/' + friend['id'],
{}, {}, method='POST')
respondent_graph = facebook.GraphAPI(friend['access_token'])
respondent_graph.request(friend['id'] + '/friends/' + user['id'],
{}, {}, method='POST')


class TestGetAppAccessToken(FacebookTestCase):
"""
Expand Down

0 comments on commit 12f8b22

Please sign in to comment.