Jump to >


Test cases for testing hosting services.

class HttpTestContext(test_case, hosting_account, http_request_func)[source]

Bases: object

State and functions for an HTTP test.

This is provided when calling HostingServiceTestCase.setup_http_test(). It serves up state that can be tested with, along with useful functions for creating necessary objects and asserting results.

New in version 3.0.4.


reviewboard.hostingsvcs.service.HostingServiceClient – The hosting service client used to perform HTTP requests for this test.


reviewboard.hostingsvcs.models.HostingServiceAccount – The hosting account used for the test.


reviewboard.hostingsvcs.service.HostingService – The hosting service instance used for the test.

__init__(test_case, hosting_account, http_request_func)[source]

Initialize the test context.


The HTTP calls made by the service.

This is a list of spy calls from KGB.


Create a repository using the current hosting account.

This wraps HostingServiceTestCase.create_repository(), specifying the hosting account that was set up in this test context.

Parameters:**kwargs (dict) – Keyword arguments to pass to HostingServiceTestCase.create_repository().
Returns:The new repository.
Return type:reviewboard.scmtools.models.Repository
assertHTTPCall(index=0, method=u'GET', body=None, headers=None, **kwargs)[source]

Assert that an HTTP call was made.

This sets some defaults based on the test case, helping both to eliminate the amount of data that needs to be checked and helping avoid missed data.

Any keyword argument accepted by HostingServiceClient.http_request() can be provided.

If username or password are not explicitly provided, the values from HostingServiceTestCase.default_username or HostingServiceTestCase.default_password will be used.

  • index (int, optional) – The index of the HTTP call.
  • method (unicode, optional) – The HTTP method expected for the call.
  • body (unicode, optional) – The expected body for the call (used for POST/PUT requests).
  • headers (dict, optional) – The expected headers for the call.
  • **kwargs (dict) – Additional parameters to check in the call.
class HostingServiceTestCase(*args, **kwargs)[source]

Bases: kgb.agency.SpyAgency, reviewboard.testing.testcase.TestCase

Base class for unit tests for hosting services.

service_name = None[source]

The registered name of the service.

default_username = u'myuser'[source]

The default username to use for accounts.

default_password = u'mypass'[source]

The default password to use for accounts.

default_hosting_url = u'https://example.com'[source]

The default hosting URL to use for accounts.

This is only used for accounts set with use_url=True (or when default_use_hosting_url is True).

default_use_hosting_url = False[source]

Whether to create accounts attached to a hosting URL by default.

default_account_data = {}[source]

Default data to set for created hosting service accounts.

default_repository_extra_data = {}[source]

Default data to set for created repositories.

default_repository_tool_name = None[source]

Default SCMTool to set for created repositories.

fixtures = [u'test_scmtools'][source]
classmethod setUpClass()[source]

Hook method for setting up class fixture before running tests in the class.


Hook method for setting up the test fixture before exercising it.


Set up state for HTTP-related tests.

This takes the hard work out of testing hosting service functionality that needs to communicate over HTTP. It’s a context manager that takes in information on what to return to the client and yields a context containing state and functions for performing and checking calls.

By default, this will cause any HTTP requests to return an empty byte string as a payload. Explicit payloads can also be provided, as can an HTTP error code. For more advanced needs, a function can be provided that will handle the HTTP request (which is useful when your test may invoke several HTTP endpoints).

This may be called repeatedly in the same test function.

  • http_request_func (callable, optional) – An explicit HTTP request function to call when performing an HTTP request. This will override reviewboard.hostingsvcs.service.HostingServiceClient .http_request().
  • payload (bytes, optional) – An explicit payload to return to the client.
  • headers (dict, optional) – Headers to send along with the result.
  • status_code (int, optional) – An explicit HTTP status code. Only values >= 400 are used. Providing an error code will raise an HTTP error to the client, using the payload value if provided.
  • hosting_account (reviewboard.hostingsvcs.models.HostingServiceAccount, optional) – An explicit hosting account to use. If not provided, one will be created using create_hosting_account().
  • expected_http_calls (int, optional) – The number of HTTP calls expected. If provided, this will assert that there were this many HTTP calls.

HttpTestContext – The context for the test, containing state, helper functions, and results.


Return an HTTP handler function for serving the supplied paths.

This is meant to be passed to setup_http_test().

This takes a dictionary matching paths to information to return. Each key is a path relative to the domain, which may optionally contain a full query string to match. It may also be None, which is the fallback.

Each value is a dictionary containing optional payload, status_code, or headers values.

Parameters:paths (dict) – The dictionary of paths.
Returns:The resulting HTTP handler function.
Return type:callable


handler = make_handler_for_paths({
    '/api/1/diffs/': {
        'payload': b'...',
        'headers': {
            b'My-Header': b'value',
    '/api/1/bad/': {
        'status_code': 404,
        'payload': b'Not found.',
    None: {
        'payload': b'fallback data...',

Dump JSON-compatible data to a byte string.

Parameters:data (object) – The data to dump.
Returns:The serialized byte string.
Return type:bytes
get_form(plan=None, fields={})[source]

Return the configuration form for the hosting service.

  • plan (unicode, optional) – The hosting plan that the configuration form is for.
  • fields (dict, optional) – The initial field data to populate the form with.

Return type:


create_hosting_account(use_url=None, local_site=None, data=None)[source]

Create a hosting account to test with.

  • use_url (unicode, optional) – Whether the account should be attached to a given hosting URL, for self-hosted services. If set, this will use https://example.com.
  • local_site (reviewboard.site.models.LocalSite, optional) – A Local Site to attach the account to.
  • data (dict, optional) – Optional data to set for the account. If this is None, default_account_data will be used.

The new hosting service account.

Return type:



Create a repository for a test.

This wraps TestCase.create_repository(), specifying a default SCM Tool name (if default_repository_tool_name) is set) and extra data (if default_repository_extra_data is set).

Parameters:**kwargs (dict) – Keyword arguments to pass to TestCase.create_repository().
Returns:The new repository.
Return type:reviewboard.scmtools.models.Repository
get_repository_fields(tool_name, fields, plan=None, with_url=None, hosting_account=None)[source]

Return populated fields for a repository.

  • tool_name (unicode) – The name of the SCM Tool used for the repository.
  • fields (dict) – A dictionary of fields for the hosting service form.
  • plan (unicode, optional) – The optional hosting plan to use for the configuration.
  • with_url (unicode, optional) –

    Whether the account should be attached to a given hosting URL, for self-hosted services. If set, this will use https://example.com. This is ignored if hosting_account is provided.

    This value defaults to default_use_hosting_url

  • hosting_account (reviewboard.hostingsvcs.models.HostingServiceAccount, optional) – An explicit hosting service account to use.

The populated field data for the repository.

Return type: