reviewboard.hostingsvcs.testing.testcases¶
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.
-
client
¶ The hosting service client used to perform HTTP requests for this test.
Type: reviewboard.hostingsvcs.service.HostingServiceClient
-
hosting_account
¶ The hosting account used for the test.
Type: reviewboard.hostingsvcs.models.HostingServiceAccount
-
service
¶ The hosting service instance used for the test.
Type: reviewboard.hostingsvcs.service.HostingService
-
__init__
(test_case, hosting_account, http_request_func)[source]¶ Initialize the test context.
Parameters: - test_case (HostingServiceTestCase) – The parent test case.
- hosting_account (reviewboard.hostingsvcs.models.HostingServiceAccount) – The hosting service account set up for the test.
- http_request_func (callable) – The function used to handle HTTP requests.
-
create_repository
(**kwargs)[source]¶ 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
orpassword
are not explicitly provided, the values fromHostingServiceTestCase.default_username
orHostingServiceTestCase.default_password
will be used.Parameters: - 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.
-
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 whendefault_use_hosting_url
isTrue
).
-
default_use_hosting_url
= False[source]¶ Whether to create accounts attached to a hosting URL by default.
-
classmethod
setUpClass
()[source]¶ Hook method for setting up class fixture before running tests in the class.
-
setup_http_test
(**kwds)[source]¶ 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.
Parameters: - 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.
Context: HttpTestContext – The context for the test, containing state, helper functions, and results.
- http_request_func (callable, optional) – An explicit HTTP request function to call when performing an
HTTP request. This will override
-
make_handler_for_paths
(paths)[source]¶ 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
, orheaders
values.Parameters: paths (dict) – The dictionary of paths. Returns: The resulting HTTP handler function. Return type: callable Example
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
(data)[source]¶ 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={}, repository=None)[source]¶ Return the configuration form for the hosting service.
Parameters: - plan (unicode, optional) – The hosting plan that the configuration form is for.
- fields (dict, optional) – The initial field data to populate the form with.
- repository (reviewboard.scmtools.models.Repository, optional) – A specific repository the form will load from and save to.
Returns: The resulting hosting service form.
Return type:
-
create_hosting_account
(use_url=None, local_site=None, data=None)[source]¶ Create a hosting account to test with.
Parameters: - 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.
Returns: The new hosting service account.
Return type: - use_url (unicode, optional) – Whether the account should be attached to a given hosting URL,
for self-hosted services. If set, this will use
-
create_repository
(**kwargs)[source]¶ Create a repository for a test.
This wraps
TestCase.create_repository()
, specifying a default SCM Tool name (ifdefault_repository_tool_name
) is set) and extra data (ifdefault_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.
Parameters: - 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 ifhosting_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.
Returns: The populated field data for the repository.
Return type:
-