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 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_repo_for_revision(phabfactory): repo_response = phabfactory.repo() phabfactory.revision(id='D5') expected_repo = first_result_in_response(repo_response) phab = PhabricatorClient(api_key='api-key') revision = phab.get_revision(id='D5') repo = phab.get_revision_repo(revision) assert repo == expected_repo
def test_get_author_for_revision(phabfactory): user_response = phabfactory.user() phabfactory.revision(id='D5') expected_user = first_result_in_response(user_response) phab = PhabricatorClient(api_key='api-key') revision = phab.get_revision(id='D5') author = phab.get_revision_author(revision) assert author == expected_user
def user(self, username=None, phid=None): """Return a Phabricator User.""" response = deepcopy(CANNED_USER_1) user = first_result_in_response(response) if username: user['userName'] = username user['realName'] = "{} Name".format(username) user['uri'] = 'http://phabricator.test/p/{}'.format(username) if phid: user['phid'] = phid self.mock.get(phab_url('user.query'), status_code=200, additional_matcher=form_matcher('phids[]', user['phid']), json=response) return response
def diff(self, **kwargs): """Create a Phabricator Diff along with stub API endpoints. Use the kwargs to customize the diff being created. If they are not provided, a default template will be used instead. kwargs: id: The integer diff id to be used. The diff's phid will be based on this. patch: The patch file to be used when generating the diff's rawdiff. All diffs must have a corresponding rawdiff. Returns: The full JSON response dict for the generated Diff. """ diff = deepcopy(CANNED_DIFF_1) if 'id' in kwargs: diff_id = kwargs['id'] else: diff_id = first_result_in_response(diff)['id'] diff = self._replace_key(diff, 'id', diff_id) # Create the mock PHID endpoint. diff_phid = 'PHID-DIFF-{diff_id}'.format(diff_id=diff_id) diff_phid_resp = self._replace_key(CANNED_DIFF_PHID_QUERY_RESULT_1, 'phid', diff_phid) diff_phid_resp['uri'] = "{url}/differential/diff/{diff_id}/".format( url=os.getenv('PHABRICATOR_URL'), diff_id=diff_id) diff_phid_resp['name'] = "Diff {diff_id}".format(diff_id=diff_id) diff_phid_resp['full_name'] = diff_phid_resp['name'] self.phid(diff_phid_resp) # Create the mock raw diff endpoint. if 'patch' in kwargs: self.rawdiff(diff_id=diff_id, patch=kwargs['patch']) else: self.rawdiff(diff_id=diff_id) # Create the mock diff endpoint. self.mock.get(phab_url('differential.querydiffs'), status_code=200, json=diff, additional_matcher=form_matcher('ids[]', str(diff_id))) return diff
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
def test_get_revision_with_200_response(phabfactory): revision_response = phabfactory.revision(id='D1234') expected_revision = first_result_in_response(revision_response) phab = PhabricatorClient(api_key='api-key') revision = phab.get_revision(id=1234) assert revision == expected_revision