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:
object
A 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:
object
Factory 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
links
and add the provided expanded payload to the object payload.- Parameters:
payload (
dict
) – The payload where the epanded resource will be provided, and wherelinks
resides.link_key (
unicode
) – The name of the link key.expanded_payload (
dict
orlist
ofdict
) – 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_key
or 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 theid
field. The URL will also include this value.name (
unicode
, optional) – The value of thename
field.tool (
unicode
, optional) – The value of thetool
field.path (
unicode
, optional) – The value of thepath
field.mirror_path (
unicode
, optional) – The value of themirror_path
field.bug_tracker (
unicode
, optional) – The value of thebug_tracker
field.visible (
bool
, optional) – The value of thevisible
field.extra_data (
dict
, optional) – The value of theextra_data
field.
- 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_failure
field.approved (
bool
, optional) – The value of theapproved
field.blocks_ids (
list
ofint
, optional) – A list of review request IDs that this blocks, for use in theblocks
field.branch (
unicode
, optional) – The value of thebranch
field.bugs_closed (
list
ofunicode
, optional) – The value of thebugs_closed
field.changenum (
int
, optional) – The value of thechangenum
field.close_description (
unicode
, optional) – The value of theclose_description
field._text_type (close_description) – The value of the
close_description_text_type
field.commit_id (
unicode
, optional) – The value of thecommit_id
field.created_with_history (
bool
, optional) – The value of thecreated_with_history
field.depends_on_ids (
list
ofint
, optional) – A list of review request IDs that this depends on, for use in thedepends_on
field.description (
unicode
, optional) – The value of thedescription
field.description_text_type (
unicode
, optional) – The value of thedescription_text_type
field.extra_data (
dict
, optional) – The value of theextra_data
field.issue_dropped_count (
int
, optional) – The value of theissue_dropped_count
field.issue_open_count (
int
, optional) – The value of theissue_open_count
field.issue_resolved_count (
int
, optional) – The value of theissue_resolved_count
field.issue_verifying_count (
int
, optional) – The value of theissue_verifying_count
field.last_updated (
unicode
, optional) – The value of thelast_updated
field.public (
bool
, optional) – The value of thepublic
field.repository_id (
int
, optional) – The ID of the repository, for therepository
link.ship_it_count (
int
, optional) – The value of theship_it_count
field.status (
unicode
, optional) – The value of thestatus
field.submitter_username (
unicode
, optional) – The username of the submitter, for thesubmitter
link.summary (
unicode
, optional) – The value of thesummary
field.target_group_names (
unicode
, optional) – A list of group names, for use in thetarget_groups
field.target_people_usernames (
unicode
, optional) – A list of usernames, for use in thetarget_people
field.testing_done (
unicode
, optional) – The value of thetesting_done
field.testing_done_text_type (
unicode
, optional) – The value of thetesting_done_text_type
field.text_type (
unicode
, optional) – The value of thetext_type
field.time_added (
unicode
, optional) – The value of thetime_added
field.latest_diff_id (
int
, optional) – The ID of the last diff on the review request, for use in thelatest_diff
link.
- 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 thebranch
field.bugs_closed (
list
ofunicode
, optional) – The value of thebugs_closed
field.close_description (
unicode
, optional) – The value of theclose_description
field._text_type (close_description) – The value of the
close_description_text_type
field.commit_id (
unicode
, optional) – The value of thecommit_id
field.depends_on_ids (
list
ofint
, optional) – A list of review request IDs that this depends on, for use in thedepends_on
field.description (
unicode
, optional) – The value of thedescription
field.description_text_type (
unicode
, optional) – The value of thedescription_text_type
field.extra_data (
dict
, optional) – The value of theextra_data
field.last_updated (
unicode
, optional) – The value of thelast_updated
field.public (
bool
, optional) – The value of thepublic
field.submitter_username (
unicode
, optional) – The username of the submitter, for thesubmitter
link.summary (
unicode
, optional) – The value of thesummary
field.target_group_names (
unicode
, optional) – A list of group names, for use in thetarget_groups
field.target_people_usernames (
unicode
, optional) – A list of usernames, for use in thetarget_people
field.testing_done (
unicode
, optional) – The value of thetesting_done
field.testing_done_text_type (
unicode
, optional) – The value of thetesting_done_text_type
field.text_type (
unicode
, optional) – The value of thetext_type
field.
- 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 theusername
field.email (
unicode
, optional) – The value of theemail
field.first_name (
unicode
, optional) –The value of the
first_name
field.This also affects the
fullname
field.last_name (
unicode
, optional) –The value of the
last_name
field.This also affects the
fullname
field.is_active (
bool
, optional) – The value of theis_active
field.avatar_html (
unicode
, optional) – The value of theavatar_html
field.avatar_urls (
dict
, optional) –The value of the
avatar_urls
field.If set, and if it contains a
1x
key, this will also affect theavatar_url
field.
- Returns:
The resource payload and metadata. See the class documentation for details.
- Return type: