def revision(self, **kwargs): """Return a Phabricator Revision.""" result_json = deepcopy(CANNED_REVISION_1) revision = first_result_in_response(result_json) if 'id' in kwargs: # Convert 'D000' form to just '000'. str_id = kwargs['id'] num_id = str_id[1:] revision['id'] = num_id revision['phid'] = "PHID-DREV-%s" % num_id if 'depends_on' in kwargs: parent_revision_response_data = kwargs['depends_on'] if parent_revision_response_data: # This Revisions depends on another Revision. new_value = [phid_for_response(parent_revision_response_data)] else: # The user passed in None or an empty list, saying "this # revision has no parent revisions." new_value = [] revision['auxiliary']['phabricator:depends-on'] = new_value # Revisions have at least one Diff. diff = self.diff() diffID = extract_rawdiff_id_from_uri( first_result_in_response(diff)['uri']) rawdiff = self.rawdiff(diffID=str(diffID)) revision['activeDiffPHID'] = phid_for_response(diff) # Revisions may have a Repo. repo = self.repo() revision['repositoryPHID'] = phid_for_response(repo) def match_revision(request): # Revisions can be looked up by PHID or ID. found_phid = form_matcher('phids[]', revision['phid'])(request) found_id = form_matcher('ids[]', revision['id'])(request) return found_phid or found_id self.mock.get(phab_url('differential.query'), status_code=200, json=result_json, additional_matcher=match_revision) # Revisions can also be looked up by phid.query. self.phid(result_json) return result_json
def phid(self, response_data): """Add a phid.query matcher for the given Phabricator response object. """ phid = phid_for_response(response_data) self.mock.get(phab_url('phid.query'), status_code=200, additional_matcher=form_matcher('phids[]', phid), json=response_data)
def test_get_user_returns_with_200_response(phabfactory): user_response = phabfactory.user() expected_user = first_result_in_response(user_response) phid = phid_for_response(user_response) phab = PhabricatorClient(api_key='api-key') user = phab.get_user(phid) assert user == expected_user
def test_get_revision_with_parents(client, phabfactory): phabfactory.user() rev1 = phabfactory.revision(id='D1') phabfactory.revision(id='D2', depends_on=rev1) response = client.get('/revisions/D2?api_key=api-key') assert response.status_code == 200 assert response.content_type == 'application/json' assert len(response.json['parent_revisions']) == 1 parent_revision = response.json['parent_revisions'][0] assert parent_revision['phid'] == phid_for_response(rev1)
def revision(self, **kwargs): """Create a Phabricator Revision along with stub API endpoints. Use the kwargs to customize the revision being created. If they are not provided, a default template will be used instead. kwargs: id: String ID to give the generated revision. E.g. 'D2233'. author_phid: PHID of the author user to use, instead of making a default user. template: A template revision to base this on from. depends_on: Response data for a Revision this revision should depend on. active_diff: Response data for a Diff that should be this Revision's "active diff" (usually this Revision's most recently uploaded patch). If you manually set an active diff, it must have been made with this factory. Returns: The full JSON response dict for the generated Revision. """ if 'template' in kwargs: result_json = deepcopy(kwargs['template']) else: result_json = deepcopy(CANNED_REVISION_1) revision = first_result_in_response(result_json) if 'id' in kwargs: # Convert 'D000' form to just '000'. str_id = kwargs['id'] num_id = str_id[1:] revision['id'] = num_id revision['phid'] = "PHID-DREV-%s" % num_id if 'author_phid' in kwargs: revision['authorPHID'] = kwargs['author_phid'] else: self.user() if 'depends_on' in kwargs: parent_revision_response_data = kwargs['depends_on'] if parent_revision_response_data: # This Revisions depends on another Revision. new_value = [phid_for_response(parent_revision_response_data)] else: # The user passed in None or an empty list, saying "this # revision has no parent revisions." new_value = [] revision['auxiliary']['phabricator:depends-on'] = new_value # Create default reviewer for the Revision self.user(username='******', phid='PHID-USER-review_bot') revision['reviewers'] = { 'PHID-USER-review_bot': 'PHID-USER-review_bot' } # Revisions have at least one Diff. if 'active_diff' in kwargs: diff = kwargs['active_diff'] else: diff = self.diff() revision['activeDiffPHID'] = 'PHID-DIFF-{}'.format( first_result_in_response(diff)['id']) # Revisions may have a Repo. repo = self.repo() revision['repositoryPHID'] = phid_for_response(repo) def match_revision(request): # Revisions can be looked up by PHID or ID. found_phid = form_matcher('phids[]', revision['phid'])(request) found_id = form_matcher('ids[]', revision['id'])(request) return found_phid or found_id self.mock.get(phab_url('differential.query'), status_code=200, json=result_json, additional_matcher=match_revision) return result_json