rbtools.testing.api.payloads¶
API payload generation factory for unit tests.
New in version 3.1.
Classes
A type of link expansion. |
|
|
Factory for creating simulated API payloads for testing. |
- class rbtools.testing.api.payloads.LinkExpansionType[source]¶
Bases:
objectA type of link expansion.
This helps to indicate if a link should expand as an item or a list.
New in version 3.1.
- class rbtools.testing.api.payloads.ResourcePayloadFactory(server_url)[source]¶
Bases:
objectFactory for creating simulated API payloads for testing.
This can be used by unit tests (first-party or third-party) or the testing framework to create API payloads that commands or other RBTools logic can test against.
The results of these aren’t directly injected into any transport. Instead, they’re meant to be built and passed into something like a KGB spy or the
rbtools.testing.api.transport.URLMapTransport.There are two kinds of payload data that this factory can generate:
HTTP response payloads
The full payload that would normally be serialized and then sent to a browser in a HTTP response. There are list, item, and error response payload generators:
Object payload data
These generate data for an object residing in part of the resource tree, for use within either a list or an item response payload. The results are dictionaries that contain:
- Keys:
- item_key (unicode):
The name of the key that would map to the payload in an item resource. If
None, then the contents of the payload will be merged into the root of the response payload.- mimetype (unicode):
The mimetype representing the object.
- payload (dict):
The object payload data, for embedding into item or list response payloads.
- url (unicode):
The URL to where the item resource for this object would live.
- headers (dict, optional):
Any optional headers that may correspond to the result when in an item response payload.
These generators include:
Whenever unit tests need to generate a type of payload not provided in this factory, they should add a suitable function to the factory rather than create it themselves. This will help provide consistency across all unit tests.
New in version 3.1.
- DEFAULT_CAPABILITIES = {'diffs': {'base_commit_ids': True, 'moved_files': True, 'validation': {'base_commit_ids': True}}, 'extra_data': {'json_patching': True}, 'review_requests': {'commit_ids': True, 'supports_history': True, 'trivial_publish': True}, 'scmtools': {'git': {'empty_files': True, 'symlinks': True}, 'mercurial': {'empty_files': True}, 'perforce': {'empty_files': True, 'moved_files': True}, 'svn': {'empty_files': True}}, 'text': {'can_include_raw_values': True, 'markdown': True, 'per_field_text_types': True}}[source]¶
The default capabilities that go into the root resource.
- __init__(server_url)[source]¶
Initialize the factory.
- Parameters:
server_url (
unicode) – The URL to the simulated server. This must ends with/.
- expand_link(payload, link_key, expanded_payload, expand_key=None, expansion_type=None)[source]¶
Expand a link in the payload.
This will remove the link from
linksand add the provided expanded payload to the object payload.- Parameters:
payload (
dict) – The payload where the epanded resource will be provided, and wherelinksresides.link_key (
unicode) – The name of the link key.expanded_payload (
dictorlistofdict) – The payload or list of payloads to put in the object payload under the key.expand_key (
unicode, optional) – The key to use for the expanded payloads. If not provided, this defaults to the value oflink_key.
- make_item_response_payload(object_payload, item_key=None)[source]¶
Return a new item response payload.
This will generate a success payload with the provided object payload, either nested within
item_keyor merged into the root of the payload.This is used for items and singletons.
- Parameters:
- Returns:
The item response payload.
- Return type:
- make_list_response_payload(url, list_key, items)[source]¶
Return a new list response payload.
This will generate a success payload with the given list of items, total result count, and list of links.
Note that this does not handle pagination at this time. All provided items will be included.
- make_error_response_payload(error_code, error_message, payload_extra={})[source]¶
Return a new error response payload.
This will generate an error payload with the given code, message, and any extra payload state to merge into the root of the response payload.
- make_root_object_data(package_version='5.0.0', version='5.0', capabilities=None)[source]¶
Return new root resource data.
- Parameters:
package_version (
unicode) – The Review Board package version to include in the API results. Callers should expect that this value may change over time, and should provide any values they may need for the test.version (
unicode) – The Review Board display version to include in the API results. Callers should expect that this value may change over time, and should provide any values they may need for the test.capabilities (
dict, optional) – Optional explicit capabilities to include. If not provided, this will use a copy ofDEFAULT_CAPABILITIES.
- Returns:
The resource payload and metadata. See the class documentation for details.
- Return type:
- make_repository_object_data(repository_id=1, name='Test Repository', tool='Git', path='/path/to/repo.git', mirror_path='', bug_tracker='', visible=True, extra_data={})[source]¶
Return new repository resource data.
- Parameters:
repository_id (
int, optional) – The value of theidfield. The URL will also include this value.name (
unicode, optional) – The value of thenamefield.tool (
unicode, optional) – The value of thetoolfield.path (
unicode, optional) – The value of thepathfield.mirror_path (
unicode, optional) – The value of themirror_pathfield.bug_tracker (
unicode, optional) – The value of thebug_trackerfield.visible (
bool, optional) – The value of thevisiblefield.extra_data (
dict, optional) – The value of theextra_datafield.
- Returns:
The resource payload and metadata. See the class documentation for details.
- Return type:
- make_repository_info_object_data(repository_id, info_payload)[source]¶
Return new repository info resource data.
- Parameters:
- Returns:
The resource payload and metadata. See the class documentation for details.
- Return type:
- make_review_request_object_data(review_request_id, approval_failure=None, approved=False, blocks_ids=[], branch='test-branch', bugs_closed=[], changenum=None, close_description='', close_description_text_type='plain', commit_id=None, created_with_history=True, depends_on_ids=[], description='Test Description', description_text_type='plain', extra_data={}, issue_dropped_count=0, issue_open_count=0, issue_resolved_count=0, issue_verifying_count=0, last_updated='2022-04-21T15:44:00Z', public=True, repository_id=None, ship_it_count=0, status='pending', submitter_username='test-user', summary='Test Summary', target_group_names=[], target_people_usernames=[], testing_done='Test Testing Done', testing_done_text_type='plain', text_type=None, time_added='2022-04-21T12:30:00Z', latest_diff_id=1)[source]¶
Return new review request resource data.
- Parameters:
review_request_id (
int) – The ID of the review request.approval_failure (
unicode, optional) – The value of theapproval_failurefield.approved (
bool, optional) – The value of theapprovedfield.blocks_ids (
listofint, optional) – A list of review request IDs that this blocks, for use in theblocksfield.branch (
unicode, optional) – The value of thebranchfield.bugs_closed (
listofunicode, optional) – The value of thebugs_closedfield.changenum (
int, optional) – The value of thechangenumfield.close_description (
unicode, optional) – The value of theclose_descriptionfield._text_type (close_description) – The value of the
close_description_text_typefield.commit_id (
unicode, optional) – The value of thecommit_idfield.created_with_history (
bool, optional) – The value of thecreated_with_historyfield.depends_on_ids (
listofint, optional) – A list of review request IDs that this depends on, for use in thedepends_onfield.description (
unicode, optional) – The value of thedescriptionfield.description_text_type (
unicode, optional) – The value of thedescription_text_typefield.extra_data (
dict, optional) – The value of theextra_datafield.issue_dropped_count (
int, optional) – The value of theissue_dropped_countfield.issue_open_count (
int, optional) – The value of theissue_open_countfield.issue_resolved_count (
int, optional) – The value of theissue_resolved_countfield.issue_verifying_count (
int, optional) – The value of theissue_verifying_countfield.last_updated (
unicode, optional) – The value of thelast_updatedfield.public (
bool, optional) – The value of thepublicfield.repository_id (
int, optional) – The ID of the repository, for therepositorylink.ship_it_count (
int, optional) – The value of theship_it_countfield.status (
unicode, optional) – The value of thestatusfield.submitter_username (
unicode, optional) – The username of the submitter, for thesubmitterlink.summary (
unicode, optional) – The value of thesummaryfield.target_group_names (
unicode, optional) – A list of group names, for use in thetarget_groupsfield.target_people_usernames (
unicode, optional) – A list of usernames, for use in thetarget_peoplefield.testing_done (
unicode, optional) – The value of thetesting_donefield.testing_done_text_type (
unicode, optional) – The value of thetesting_done_text_typefield.text_type (
unicode, optional) – The value of thetext_typefield.time_added (
unicode, optional) – The value of thetime_addedfield.latest_diff_id (
int, optional) – The ID of the last diff on the review request, for use in thelatest_difflink.
- Returns:
The resource payload and metadata. See the class documentation for details.
- Return type:
- make_review_request_draft_object_data(draft_id, review_request_id, branch='test-branch', bugs_closed=[], close_description='', close_description_text_type='plain', commit_id=None, depends_on_ids=[], description='Test Description', description_text_type='plain', extra_data={}, last_updated='2022-04-21T15:44:00Z', public=True, submitter_username='test-user', summary='Test Summary', target_group_names=[], target_people_usernames=[], testing_done='Test Testing Done', testing_done_text_type='plain', text_type=None)[source]¶
Return new review request draft resource data.
- Parameters:
draft_id (
int) – The ID of the review request draft.review_request_id (
int) – The ID of the review request.branch (
unicode, optional) – The value of thebranchfield.bugs_closed (
listofunicode, optional) – The value of thebugs_closedfield.close_description (
unicode, optional) – The value of theclose_descriptionfield._text_type (close_description) – The value of the
close_description_text_typefield.commit_id (
unicode, optional) – The value of thecommit_idfield.depends_on_ids (
listofint, optional) – A list of review request IDs that this depends on, for use in thedepends_onfield.description (
unicode, optional) – The value of thedescriptionfield.description_text_type (
unicode, optional) – The value of thedescription_text_typefield.extra_data (
dict, optional) – The value of theextra_datafield.last_updated (
unicode, optional) – The value of thelast_updatedfield.public (
bool, optional) – The value of thepublicfield.submitter_username (
unicode, optional) – The username of the submitter, for thesubmitterlink.summary (
unicode, optional) – The value of thesummaryfield.target_group_names (
unicode, optional) – A list of group names, for use in thetarget_groupsfield.target_people_usernames (
unicode, optional) – A list of usernames, for use in thetarget_peoplefield.testing_done (
unicode, optional) – The value of thetesting_donefield.testing_done_text_type (
unicode, optional) – The value of thetesting_done_text_typefield.text_type (
unicode, optional) – The value of thetext_typefield.
- Returns:
The resource payload and metadata. See the class documentation for details.
- Return type:
- make_session_object_data(authenticated=True, username='test-user')[source]¶
Return new session resource data.
- make_user_object_data(user_id=1, username='test-user', email='test-user@example.com', first_name='Test', last_name='User', is_active=True, avatar_html=None, avatar_urls={})[source]¶
Return new user resource data.
- Parameters:
user_id (
int, optional) – The ID of the user.username (
unicode, optional) – The value of theusernamefield.email (
unicode, optional) – The value of theemailfield.first_name (
unicode, optional) –The value of the
first_namefield.This also affects the
fullnamefield.last_name (
unicode, optional) –The value of the
last_namefield.This also affects the
fullnamefield.is_active (
bool, optional) – The value of theis_activefield.avatar_html (
unicode, optional) – The value of theavatar_htmlfield.avatar_urls (
dict, optional) –The value of the
avatar_urlsfield.If set, and if it contains a
1xkey, this will also affect theavatar_urlfield.
- Returns:
The resource payload and metadata. See the class documentation for details.
- Return type: