stepford

Overview

Integration testing is generally a pain. Stepford attempts to alleviate as much of that pain as possible for apps requiring integration with the Facebook Graph API. Stepford is a Python implementation of the Facebook test user API as defined at https://developers.facebook.com/docs/test_users.

Usage

Getting your app token

App tokens are access tokens that are bound to your application. They allow you to make requests on behalf of your application. Your app access token is required to interact with Facebook’s test user API. There are two methods to get your app token:

Programmatically

from stepford import app_token
token = app_token([client_id], [client_secret])

Warning

App tokens expire the same as other tokens. So, while the Facebook tool is useful for retrieving them for immediate use, it will eventually break in an automated environment.

Getting available users

import stepford
users = stepford.get([client_id], [app_token])

This returns a list of dicts with test user details. The data is structured like so:

[
    {
        'access_token': [snip],
        'login_url': [snip],
        'id': [snip]
    },
    [...]
]

Creating a user

import stepford
user = stepford.create([client_id], [app_token])

Creates a single test user:

{
    'access_token': [snip],
    'login_url': [snip],
    'id': [snip]
}

Updating a user

Facebook offers the ability to update two test user attributes: name and password.

import stepford
stepford.update(userid, [app_token], name='[name]', pwd='[pwd]')

Warning

Changing the user’s password will result in the expiry of the current token. The only resolution to this (AFAIK) is to do a full get of the user list (stepford.get()), filtering for that user ID.

Deleting users

import stepford
stepford.delete(userid, [app_token])

Note

Attempting to delete a user who has multiple apps installed will result in an exception being raised. Users must have all apps (short of the owner app) uninstalled prior to deletion (see stepford.uninstall()).

Making friends

stepford.connect() takes an arbitrary length (langth <= 1 will result in a ValueError exception) list of users to connect to each other. Each user object is expected to be of the same format as returned by stepford.create().

import stepford
users = [stepford.create([client_id], [app_token]) for _ in range(10)]
stepford.connect(*users)

The above code creates 10 users and then creates friendships between each.

Installing apps

import stepford
stepford.install(user['id'], [install_to_app_token], [client_id],
    [app_token], scope='read_stream')

Installing applications to test users is a little odd. You need the app access token of the app being installed, which makes you unable to install arbitrary applications. client_id and app_token are those of the application used to create the test user.

Uninstalling apps

import stepford
stepford.uninstall(user['id'], [client_id], [app_token])

The above uninstalls an app for a test user. The client_id and app_token in this case are those of the install app (not the owner app).

Error handling

All errors generated by the Facebook test user API are translated to stepford.FacebookError, which is a subclass of urllib2.HTTPError. In addition to the usual HTTP code attribute, this class also provides you with the api_code, which can be used to retrieve the specific Facebook client-facing error code. Additionally, it translates the JSON error message to HTTPError.msg.

There are a few constants defined in stepford.py that define either errors encountered during testing and/or errors defined in Facebook’s test user API docs.

API

Implementation of the Facebook test user API

exception stepford.FacebookError(err)

Exposes Facebook-specific error attributes

Methods wrapped with stepford.translate_http_error() will raise a stepford.FacebookError whenever an urllib2.HTTPError is encountered. This helps expose more detailed information about the error than the simple HTTP error code and message.

In the event that stepford encounters an unexpected API error (i.e. one that doesn’t contain the usual error data payload), api_code and type will be set to None.

api_code

The Facebook client-facing error code

type

The error category as defined by Facebook

stepford.app_token(*args, **kwargs)

Gets the app token

The app token is used in all stepford transactions. It is provided by Facebook and only changes when your app secret has been changed.

Parameters:
  • client_id – Your app’s client ID, as provided by Facebook
  • client_secret – Your app’s client secret, as provided by Facebook
Returns:

A dict containing the app token

stepford.connect(*args, **kwargs)

Creates friendships between test user accounts

Parameters:users – A list of users to create friendships for.
stepford.create(*args, **kwargs)

Creates a test user

Parameters:
  • client_id – Your app’s client ID, as provided by Facebook
  • access_token – Your app’s access_token, as retrieved by app_token (alternatively, this can be retrieved by Facebook’s testing toolset).
  • installed – Whether or not the user should be created with your app installed.
  • name – The name of the test user. If None, this will be auto-generated by Facebook.
  • locale – The user’s default locale.
  • permissions – The scope approved by the test user. This should be a comma-delimited list of resource types approved by this user for your application.
Returns:

A dict containing user details

stepford.delete(*args, **kwargs)

Deletes a test user

Parameters:
  • userid – The ID of the user to delete.
  • client_id – Your app’s client ID, as provided by Facebook
  • access_token – Your app’s access_token, as retrieved by app_token (alternatively, this can be retrieved by Facebook’s testing toolset).
Returns:

True on success

stepford.get(*args, **kwargs)

Gets a list of available test users

Parameters:
  • client_id – Your app’s client ID, as provided by Facebook
  • access_token – Your app’s access_token, as retrieved by app_token (alternatively, this can be retrieved by Facebook’s testing toolset).
Returns:

A list of dict elements containing user details

stepford.install(*args, **kwargs)

Installs an app for the given user

Parameters:
  • userid – The user to install the app for
  • install_to_token – The app token for the app being installed
  • clientid – The client_id of the app that owns the test user
  • access_token – The app token of the app that owns the test user
  • scope – The scope to install the app for the test user with
Returns:

True on success

stepford.translate_http_error(func)

HTTPError to FacebookError translation decorator

Decorates functions, handles urllib2.HTTPError exceptions and translates them into stepford.FacebookError

Parameters:func – The function to decorate with translation handling
stepford.uninstall(*args, **kwargs)

Uninstalls an app for the given user

Parameters:
  • userid – The user to uninstall the app for
  • clientid – The client id of the app being removed
  • access_token – The access token of the app being removed
Returns:

True on success

stepford.update(*args, **kwargs)

Updates the given user

Parameters:
  • userid – The ID of the user to be updated
  • access_token – The app access token
  • (optional) (pwd) – If specified, what name to assign to the user
  • (optional) – If specified, what password to assign to the user
Returns:

True on success