def test_ticket_login_with_local_site(self): """Testing Perforce with ticket-based logins with Local Sites""" repo = Repository( name='Perforce.com', path='public.perforce.com:1666', tool=Tool.objects.get(name='Perforce'), username='******', password='******', local_site=LocalSite.objects.get(name='local-site-1')) repo.extra_data = { 'use_ticket_auth': True, } client = repo.get_scmtool().client self.assertTrue(client.use_ticket_auth) self.spy_on(client.get_ticket_status, call_fake=lambda *args: { 'user': '******', 'expiration_secs': 100000, }) self.spy_on(client.login, call_original=False) with client.connect(): self.assertFalse(client.login.called) self.assertEqual(client.p4.ticket_file, os.path.join(settings.SITE_DATA_DIR, 'p4', 'local-site-1', 'p4tickets'))
def _test_get_file_exists( self, tool_name, revision, base_commit_id, expected_revision, expected_found, expected_http_called=True ): def _http_get(service, url, *args, **kwargs): expected_url = "https://mydomain.beanstalkapp.com/api/" "repositories/myrepo/" if base_commit_id: expected_url += "node.json?path=/path&revision=%s&contents=0" % expected_revision else: expected_url += "blob?id=%s&name=path" % expected_revision self.assertEqual(url, expected_url) if expected_found: return "{}", {} else: raise HTTPError() account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = {"beanstalk_account_domain": "mydomain", "beanstalk_repo_name": "myrepo"} service.authorize("myuser", "abc123", None) self.spy_on(service._http_get, call_fake=_http_get) result = service.get_file_exists(repository, "/path", revision, base_commit_id) self.assertTrue(service._http_get.called) self.assertEqual(result, expected_found)
def _test_get_file_exists(self, tool_name, expect_git_blob_url=False, file_exists=True): def _http_get(service, url, *args, **kwargs): if expect_git_blob_url: self.assertEqual( url, 'https://api3.codebasehq.com/myproj/myrepo/blob/123') else: self.assertEqual( url, 'https://api3.codebasehq.com/myproj/myrepo/blob/123/' 'myfile') if file_exists: return b'{}', {} else: raise HTTPError(url, 404, '', {}, StringIO()) account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'codebasehq_project_name': 'myproj', 'codebasehq_repo_name': 'myrepo', } self._authorize(service) self.spy_on(service.client.http_get, call_fake=_http_get) result = service.get_file_exists(repository, 'myfile', '123') self.assertTrue(service.client.http_get.called) self.assertEqual(result, file_exists)
def _test_get_file(self, tool_name, revision, base_commit_id, expected_revision): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, 'https://bitbucket.org/api/1.0/repositories/' 'myuser/myrepo/raw/%s/path' % expected_revision) return b'My data', {} account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'bitbucket_repo_name': 'myrepo', } account.data['password'] = encrypt_password('abc123') self.spy_on(service.client.http_get, call_fake=_http_get) result = service.get_file(repository, 'path', revision, base_commit_id) self.assertTrue(service.client.http_get.called) self.assertEqual(result, 'My data')
def test_is_ssh_key_associated(self): """Testing that GitHub associated SSH keys are correctly identified""" associated_key = 'good_key' unassociated_key = 'bad_key' keys = simplejson.dumps([{ 'key': 'neutral_key' }, { 'key': associated_key }]) def _http_get(self, *args, **kwargs): return keys, None self.service_class._http_get = _http_get self.service_class._format_public_key = lambda self, key: key account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} service = account.service repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } self.assertTrue( service.is_ssh_key_associated(repository, associated_key)) self.assertFalse( service.is_ssh_key_associated(repository, unassociated_key)) self.assertFalse(service.is_ssh_key_associated(repository, None))
def _test_get_file(self, tool_name, revision, base_commit_id, expected_revision): def _http_get(service, url, *args, **kwargs): if tool_name == 'Git': self.assertEqual( url, 'https://mydomain.beanstalkapp.com/api/repositories/' 'myrepo/blob?id=%s&name=path' % expected_revision) payload = b'My data' else: self.assertEqual( url, 'https://mydomain.beanstalkapp.com/api/repositories/' 'myrepo/node.json?path=/path&revision=%s&contents=1' % expected_revision) payload = b'{"contents": "My data"}' return payload, {} account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'beanstalk_account_domain': 'mydomain', 'beanstalk_repo_name': 'myrepo', } service.authorize('myuser', 'abc123', None) self.spy_on(service.client.http_get, call_fake=_http_get) result = service.get_file(repository, '/path', revision, base_commit_id) self.assertTrue(service.client.http_get.called) self.assertEqual(result, 'My data')
def test_associate_ssh_key(self): """Testing that GitHub SSH key association sends expected data""" http_post_data = {} def _http_post(self, *args, **kwargs): http_post_data['args'] = args http_post_data['kwargs'] = kwargs return None, None self.service_class._http_post = _http_post self.service_class._format_public_key = lambda self, key: key account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } service = account.service service.associate_ssh_key(repository, 'mykey') req_body = simplejson.loads(http_post_data['kwargs']['body']) expected_title = ('Review Board (%s)' % Site.objects.get_current().domain) self.assertEqual( http_post_data['args'][0], 'https://api.github.com/repos/myuser/myrepo/keys?' 'access_token=abc123') self.assertEqual(http_post_data['kwargs']['content_type'], 'application/json') self.assertEqual(req_body['title'], expected_title) self.assertEqual(req_body['key'], 'mykey')
def _test_get_file(self, tool_name, revision, base_commit_id, expected_revision): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, 'https://mydomain.beanstalkapp.com/api/repositories/' 'myrepo/blob?id=%s&name=path' % expected_revision) return 'My data', {} account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'beanstalk_account_domain': 'mydomain', 'beanstalk_repo_name': 'myrepo', } service.authorize('myuser', 'abc123', None) self.spy_on(service._http_get, call_fake=_http_get) result = service.get_file(repository, '/path', revision, base_commit_id) self.assertTrue(service._http_get.called) self.assertEqual(result, 'My data')
def _test_get_file_exists(self, tool_name, revision, base_commit_id, expected_revision, expected_found, expected_http_called=True): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, 'https://bitbucket.org/api/1.0/repositories/' 'myuser/myrepo/raw/%s/path' % expected_revision) if expected_found: return b'{}', {} else: error = HTTPError(url, 404, 'Not Found', {}, None) error.read = lambda: error.msg raise error account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'bitbucket_repo_name': 'myrepo', } account.data['password'] = encrypt_password('abc123') self.spy_on(service.client.http_get, call_fake=_http_get) result = service.get_file_exists(repository, 'path', revision, base_commit_id) self.assertEqual(service.client.http_get.called, expected_http_called) self.assertEqual(result, expected_found)
def test_is_ssh_key_associated(self): """Testing that GitHub associated SSH keys are correctly identified""" associated_key = 'good_key' unassociated_key = 'bad_key' keys = simplejson.dumps([ {'key': 'neutral_key'}, {'key': associated_key} ]) def _http_get(self, *args, **kwargs): return keys, None self.service_class._http_get = _http_get self.service_class._format_public_key = lambda self, key: key account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} service = account.service repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } self.assertTrue(service.is_ssh_key_associated(repository, associated_key)) self.assertFalse(service.is_ssh_key_associated(repository, unassociated_key)) self.assertFalse(service.is_ssh_key_associated(repository, None))
def _test_get_file_exists(self, tool_name, revision, base_commit_id, expected_revision, expected_found, expected_http_called=True): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, 'https://bitbucket.org/api/1.0/repositories/' 'myuser/myrepo/raw/%s/path' % expected_revision) if expected_found: return '{}', {} else: raise HTTPError() account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'bitbucket_repo_name': 'myrepo', } service.authorize('myuser', 'abc123', None) self.spy_on(service._http_get, call_fake=_http_get) result = service.get_file_exists(repository, 'path', revision, base_commit_id) self.assertEqual(service._http_get.called, expected_http_called) self.assertEqual(result, expected_found)
def test_get_branches(self): """Testing GitHub get_branches implementation""" branches_api_response = json.dumps([ { 'ref': 'refs/heads/master', 'object': { 'sha': '859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817', } }, { 'ref': 'refs/heads/release-1.7.x', 'object': { 'sha': '92463764015ef463b4b6d1a1825fee7aeec8cb15', } }, { 'ref': 'refs/heads/some-component/fix', 'object': { 'sha': '764015ef492c8cb1546363b45fee7ab6d1a182ee', } }, { 'ref': 'refs/tags/release-1.7.11', 'object': { 'sha': 'f5a35f1d8a8dcefb336a8e3211334f1f50ea7792', } }, ]) def _http_get(self, *args, **kwargs): return branches_api_response, None account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) branches = service.get_branches(repository) self.assertTrue(service.client.http_get.called) self.assertEqual(len(branches), 3) self.assertEqual(branches, [ Branch(id='master', commit='859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817', default=True), Branch(id='release-1.7.x', commit='92463764015ef463b4b6d1a1825fee7aeec8cb15', default=False), Branch(id='some-component/fix', commit='764015ef492c8cb1546363b45fee7ab6d1a182ee', default=False), ])
def test_associate_ssh_key(self): """Testing that GitHub SSH key association sends expected data""" http_post_data = {} def _http_post(self, *args, **kwargs): http_post_data['args'] = args http_post_data['kwargs'] = kwargs return None, None self.service_class._http_post = _http_post self.service_class._format_public_key = lambda self, key: key account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } service = account.service service.associate_ssh_key(repository, 'mykey') req_body = simplejson.loads(http_post_data['kwargs']['body']) expected_title = ('Review Board (%s)' % Site.objects.get_current().domain) self.assertEqual(http_post_data['args'][0], 'https://api.github.com/repos/myuser/myrepo/keys?' 'access_token=abc123') self.assertEqual(http_post_data['kwargs']['content_type'], 'application/json') self.assertEqual(req_body['title'], expected_title) self.assertEqual(req_body['key'], 'mykey')
def test_get_branches(self): """Testing GitHub get_branches implementation""" branches_api_response = simplejson.dumps( [ {"ref": "refs/heads/master", "object": {"sha": "859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817"}}, {"ref": "refs/heads/release-1.7.x", "object": {"sha": "92463764015ef463b4b6d1a1825fee7aeec8cb15"}}, {"ref": "refs/tags/release-1.7.11", "object": {"sha": "f5a35f1d8a8dcefb336a8e3211334f1f50ea7792"}}, ] ) def _http_get(self, *args, **kwargs): return branches_api_response, None self.service_class._http_get = _http_get account = self._get_hosting_account() account.data["authorization"] = {"token": "abc123"} repository = Repository(hosting_account=account) repository.extra_data = {"repository_plan": "public", "github_public_repo_name": "myrepo"} service = account.service branches = service.get_branches(repository) self.assertEqual(len(branches), 2) self.assertEqual( branches, [ Branch("master", "859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817", True), Branch("release-1.7.x", "92463764015ef463b4b6d1a1825fee7aeec8cb15", False), ], )
def test_associate_ssh_key(self): """Testing that GitHub SSH key association sends expected data""" http_post_data = {} def _http_post(self, *args, **kwargs): http_post_data["args"] = args http_post_data["kwargs"] = kwargs return None, None self.service_class._http_post = _http_post self.service_class._format_public_key = lambda self, key: key account = self._get_hosting_account() account.data["authorization"] = {"token": "abc123"} repository = Repository(hosting_account=account) repository.extra_data = {"repository_plan": "public", "github_public_repo_name": "myrepo"} service = account.service service.associate_ssh_key(repository, "mykey") req_body = simplejson.loads(http_post_data["kwargs"]["body"]) expected_title = "Review Board (%s)" % Site.objects.get_current().domain self.assertEqual( http_post_data["args"][0], "https://api.github.com/repos/myuser/myrepo/keys?" "access_token=abc123" ) self.assertEqual(http_post_data["kwargs"]["content_type"], "application/json") self.assertEqual(req_body["title"], expected_title) self.assertEqual(req_body["key"], "mykey")
def test_get_branches(self): """Testing GitLab get_branches implementation""" branches_api_response = json.dumps([ { 'name': 'master', 'commit': { 'id': 'ed899a2f4b50b4370feeea94676502b42383c746' } }, { 'name': 'branch1', 'commit': { 'id': '6104942438c14ec7bd21c6cd5bd995272b3faff6' } }, { 'name': 'branch2', 'commit': { 'id': '21b3bcabcff2ab3dc3c9caa172f783aad602c0b0' } }, { 'branch-name': 'branch3', 'commit': { 'id': 'd5a3ff139356ce33e37e73add446f16869741b50' } } ]) def _http_get(self, *args, **kwargs): return branches_api_response, None account = self._get_hosting_account(use_url=True) account.data['private_token'] = encrypt_password('abc123') service = account.service repository = Repository(hosting_account=account) repository.extra_data = {'gitlab_project_id': 123456} self.spy_on(service.client.http_get, call_fake=_http_get) branches = service.get_branches(repository) self.assertTrue(service.client.http_get.called) self.assertEqual(len(branches), 3) self.assertEqual( branches, [ Branch(id='master', commit='ed899a2f4b50b4370feeea94676502b42383c746', default=True), Branch(id='branch1', commit='6104942438c14ec7bd21c6cd5bd995272b3faff6', default=False), Branch(id='branch2', commit='21b3bcabcff2ab3dc3c9caa172f783aad602c0b0', default=False) ])
def test_get_commits(self): """Testing ReviewBoardGateway get_commits implementation""" commits_api_response = json.dumps([{ 'author': 'myname', 'id': 'bfdde95432b3af879af969bd2377dc3e55ee46e6', 'date': '2015-02-13 22:34:01 -0700 -0700', 'message': 'mymessage', 'parent_id': '304c53c163aedfd0c0e0933776f09c24b87f5944', }, { 'author': 'myname', 'id': '304c53c163aedfd0c0e0933776f09c24b87f5944', 'date': '2015-02-13 22:32:42 -0700 -0700', 'message': 'mymessage', 'parent_id': 'fa1330719893098ae397356e8125c2aa45b49221', }, { 'author': 'anothername', 'id': 'fa1330719893098ae397356e8125c2aa45b49221', 'date': '2015-02-12 16:01:48 -0700 -0700', 'message': 'mymessage', 'parent_id': '', }]) def _http_get(self, *args, **kwargs): return commits_api_response, None account = self._get_hosting_account() account.data['private_token'] = encrypt_password('abc123') repository = Repository(hosting_account=account) repository.extra_data = { 'rbgateway_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) commits = service.get_commits( repository, branch='bfdde95432b3af879af969bd2377dc3e55ee46e6') self.assertTrue(service.client.http_get.called) self.assertEqual(len(commits), 3) self.assertEqual(commits[0].parent, commits[1].id) self.assertEqual(commits[1].parent, commits[2].id) self.assertEqual(commits[0].date, '2015-02-13 22:34:01 -0700 -0700') self.assertEqual(commits[1].id, '304c53c163aedfd0c0e0933776f09c24b87f5944') self.assertEqual(commits[2].author_name, 'anothername') self.assertEqual(commits[2].parent, '')
def test_get_branches(self): """Testing GitLab get_branches implementation""" branches_api_response = json.dumps([ { 'name': 'master', 'commit': { 'id': 'ed899a2f4b50b4370feeea94676502b42383c746' } }, { 'name': 'branch1', 'commit': { 'id': '6104942438c14ec7bd21c6cd5bd995272b3faff6' } }, { 'name': 'branch2', 'commit': { 'id': '21b3bcabcff2ab3dc3c9caa172f783aad602c0b0' } }, { 'branch-name': 'branch3', 'commit': { 'id': 'd5a3ff139356ce33e37e73add446f16869741b50' } } ]) def _http_get(self, *args, **kwargs): return branches_api_response, None account = self._get_hosting_account(use_url=True) account.data['private_token'] = encrypt_password('abc123') service = account.service repository = Repository(hosting_account=account) repository.extra_data = {'gitlab_project_id': 123456} self.spy_on(service.client.http_get, call_fake=_http_get) branches = service.get_branches(repository) self.assertTrue(service.client.http_get.called) self.assertEqual(len(branches), 3) self.assertEqual( branches, [ Branch(id='master', commit='ed899a2f4b50b4370feeea94676502b42383c746', default=True), Branch(id='branch1', commit='6104942438c14ec7bd21c6cd5bd995272b3faff6', default=False), Branch(id='branch2', commit='21b3bcabcff2ab3dc3c9caa172f783aad602c0b0', default=False) ])
def test_get_commits(self): """Testing GitHub get_commits implementation""" commits_api_response = simplejson.dumps( [ { "commit": { "author": {"name": "Christian Hammond"}, "committer": {"date": "2013-06-25T23:31:22Z"}, "message": "Fixed the bug number for the blacktriangledown bug.", }, "sha": "859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817", "parents": [{"sha": "92463764015ef463b4b6d1a1825fee7aeec8cb15"}], }, { "commit": { "author": {"name": "Christian Hammond"}, "committer": {"date": "2013-06-25T23:30:59Z"}, "message": "Merge branch 'release-1.7.x'", }, "sha": "92463764015ef463b4b6d1a1825fee7aeec8cb15", "parents": [ {"sha": "f5a35f1d8a8dcefb336a8e3211334f1f50ea7792"}, {"sha": "6c5f3465da5ed03dca8128bb3dd03121bd2cddb2"}, ], }, { "commit": { "author": {"name": "David Trowbridge"}, "committer": {"date": "2013-06-25T22:41:09Z"}, "message": "Add DIFF_PARSE_ERROR to the ValidateDiffResource.create error list.", }, "sha": "f5a35f1d8a8dcefb336a8e3211334f1f50ea7792", "parents": [], }, ] ) def _http_get(self, *args, **kwargs): return commits_api_response, None self.service_class._http_get = _http_get account = self._get_hosting_account() account.data["authorization"] = {"token": "abc123"} repository = Repository(hosting_account=account) repository.extra_data = {"repository_plan": "public", "github_public_repo_name": "myrepo"} service = account.service commits = service.get_commits(repository, "859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817") self.assertEqual(len(commits), 3) self.assertEqual(commits[0].parent, commits[1].id) self.assertEqual(commits[1].parent, commits[2].id) self.assertEqual(commits[0].date, "2013-06-25T23:31:22Z") self.assertEqual(commits[1].id, "92463764015ef463b4b6d1a1825fee7aeec8cb15") self.assertEqual(commits[2].author_name, "David Trowbridge") self.assertEqual(commits[2].parent, "")
def test_get_commits(self): """Testing GitLab get_commits implementation""" commits_api_response = json.dumps([{ 'id': 'ed899a2f4b50b4370feeea94676502b42383c746', 'author_name': 'Chester Li', 'created_at': '2015-03-10T11:50:22+03:00', 'message': 'Replace sanitize with escape once' }, { 'id': '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'author_name': 'Chester Li', 'created_at': '2015-03-10T09:06:12+03:00', 'message': 'Sanitize for network graph' }, { 'id': '21b3bcabcff2ab3dc3c9caa172f783aad602c0b0', 'author_name': 'East Coast', 'created_at': '2015-03-04T15:31:18.000-04:00', 'message': 'Add a timer to test file' }]) def _http_get(self, *args, **kargs): return commits_api_response, None account = self._get_hosting_account(use_url=True) account.data['private_token'] = encrypt_password('abc123') service = account.service repository = Repository(hosting_account=account) repository.extra_data = {'gitlab_project_id': 123456} self.spy_on(service.client.http_get, call_fake=_http_get) commits = service.get_commits( repository, start='ed899a2f4b50b4370feeea94676502b42383c746') self.assertTrue(service.client.http_get.called) self.assertEqual(len(commits), 3) self.assertEqual(commits[0].id, 'ed899a2f4b50b4370feeea94676502b42383c746') self.assertNotEqual(commits[0].author_name, 'East Coast') self.assertEqual(commits[1].date, '2015-03-10T09:06:12+03:00') self.assertNotEqual(commits[1].message, 'Replace sanitize with escape once') self.assertEqual(commits[2].author_name, 'East Coast')
def test_get_commits(self): """Testing ReviewBoardGateway get_commits implementation""" commits_api_response = json.dumps([ { 'author': 'myname', 'id': 'bfdde95432b3af879af969bd2377dc3e55ee46e6', 'date': '2015-02-13 22:34:01 -0700 -0700', 'message': 'mymessage', 'parent_id': '304c53c163aedfd0c0e0933776f09c24b87f5944', }, { 'author': 'myname', 'id': '304c53c163aedfd0c0e0933776f09c24b87f5944', 'date': '2015-02-13 22:32:42 -0700 -0700', 'message': 'mymessage', 'parent_id': 'fa1330719893098ae397356e8125c2aa45b49221', }, { 'author': 'anothername', 'id': 'fa1330719893098ae397356e8125c2aa45b49221', 'date': '2015-02-12 16:01:48 -0700 -0700', 'message': 'mymessage', 'parent_id': '', } ]) def _http_get(self, *args, **kwargs): return commits_api_response, None account = self._get_hosting_account() account.data['private_token'] = encrypt_password('abc123') repository = Repository(hosting_account=account) repository.extra_data = { 'rbgateway_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) commits = service.get_commits( repository, branch='bfdde95432b3af879af969bd2377dc3e55ee46e6') self.assertTrue(service.client.http_get.called) self.assertEqual(len(commits), 3) self.assertEqual(commits[0].parent, commits[1].id) self.assertEqual(commits[1].parent, commits[2].id) self.assertEqual(commits[0].date, '2015-02-13 22:34:01 -0700 -0700') self.assertEqual(commits[1].id, '304c53c163aedfd0c0e0933776f09c24b87f5944') self.assertEqual(commits[2].author_name, 'anothername') self.assertEqual(commits[2].parent, '')
def test_get_file_exists(self): """Testing Kiln get_file_exists""" def _http_get(service, url, *args, **kwargs): if url == ('https://mydomain.kilnhg.com/Api/1.0/Project' '?token=my-token'): data = json.dumps([{ 'sSlug': 'myproject', 'repoGroups': [{ 'sSlug': 'mygroup', 'repos': [{ 'sSlug': 'myrepo', 'ixRepo': 123, }] }] }]) else: self.assertEqual( url, 'https://mydomain.kilnhg.com/Api/1.0/Repo/123/Raw/File/' '%s?rev=%s&token=my-token' % (encoded_path, revision)) data = 'My data' return data, {} path = '/path' encoded_path = '2F70617468' revision = 123 account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name='Mercurial')) repository.extra_data = { 'kiln_account_domain': 'mydomain', 'kiln_project_name': 'myproject', 'kiln_group_name': 'mygroup', 'kiln_repo_name': 'myrepo', } repository.save() account.data.update({ 'auth_token': 'my-token', 'kiln_account_domain': 'mydomain', }) self.spy_on(service.client.http_get, call_fake=_http_get) result = service.get_file_exists(repository, path, revision) self.assertTrue(service.client.http_get.called) self.assertTrue(result)
def test_get_branches(self): """Testing GitHub get_branches implementation""" branches_api_response = simplejson.dumps([ { 'ref': 'refs/heads/master', 'object': { 'sha': '859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817', } }, { 'ref': 'refs/heads/release-1.7.x', 'object': { 'sha': '92463764015ef463b4b6d1a1825fee7aeec8cb15', } }, { 'ref': 'refs/tags/release-1.7.11', 'object': { 'sha': 'f5a35f1d8a8dcefb336a8e3211334f1f50ea7792', } }, ]) def _http_get(self, *args, **kwargs): return branches_api_response, None self.service_class._http_get = _http_get account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } service = account.service branches = service.get_branches(repository) self.assertEqual(len(branches), 2) self.assertEqual( branches, [ Branch('master', '859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817', True), Branch('release-1.7.x', '92463764015ef463b4b6d1a1825fee7aeec8cb15', False), ])
def test_get_change(self): """Testing BitBucket get_change""" commit_sha = '1c44b461cebe5874a857c51a4a13a849a4d1e52d' parent_sha = '44568f7d33647d286691517e6325fea5c7a21d5e' commits_api_response = json.dumps({ 'hash': commit_sha, 'author': { 'raw': 'Some User <*****@*****.**>', }, 'date': '2017-01-24T13:11:22+00:00', 'message': 'This is a message.', 'parents': [{'hash': parent_sha}], }) diff_api_response = b'This is a test \xc7.' norm_diff_api_response = b'This is a test \xc7.\n' def _http_get(service, url, *args, **kwargs): if url == ('https://bitbucket.org/api/2.0/repositories/' 'myuser/myrepo/commit/%s?' 'fields=author.raw%%2Chash%%2Cdate%%2C' 'message%%2Cparents.hash' % commit_sha): return commits_api_response, None elif url == ('https://bitbucket.org/api/2.0/repositories/' 'myuser/myrepo/diff/%s' % commit_sha): return diff_api_response, None else: self.fail('Unexpected URL %s' % url) account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name='Git')) repository.extra_data = { 'bitbucket_repo_name': 'myrepo', } account.data['password'] = encrypt_password('abc123') self.spy_on(service.client.http_get, call_fake=_http_get) commit = service.get_change(repository, commit_sha) self.assertEqual(commit.id, commit_sha) self.assertEqual(commit.author_name, 'Some User <*****@*****.**>') self.assertEqual(commit.message, 'This is a message.') self.assertEqual(commit.date, '2017-01-24T13:11:22+00:00') self.assertEqual(commit.parent, parent_sha) self.assertEqual(commit.diff, norm_diff_api_response)
def test_get_commits(self): """Testing GitLab get_commits implementation""" commits_api_response = json.dumps([ { 'id': 'ed899a2f4b50b4370feeea94676502b42383c746', 'author_name': 'Chester Li', 'created_at': '2015-03-10T11:50:22+03:00', 'message': 'Replace sanitize with escape once' }, { 'id': '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'author_name': 'Chester Li', 'created_at': '2015-03-10T09:06:12+03:00', 'message': 'Sanitize for network graph' }, { 'id': '21b3bcabcff2ab3dc3c9caa172f783aad602c0b0', 'author_name': 'East Coast', 'created_at': '2015-03-04T15:31:18.000-04:00', 'message': 'Add a timer to test file' } ]) def _http_get(self, *args, **kargs): return commits_api_response, None account = self._get_hosting_account(use_url=True) account.data['private_token'] = encrypt_password('abc123') service = account.service repository = Repository(hosting_account=account) repository.extra_data = {'gitlab_project_id': 123456} self.spy_on(service.client.http_get, call_fake=_http_get) commits = service.get_commits( repository, start='ed899a2f4b50b4370feeea94676502b42383c746') self.assertTrue(service.client.http_get.called) self.assertEqual(len(commits), 3) self.assertEqual(commits[0].id, 'ed899a2f4b50b4370feeea94676502b42383c746') self.assertNotEqual(commits[0].author_name, 'East Coast') self.assertEqual(commits[1].date, '2015-03-10T09:06:12+03:00') self.assertNotEqual(commits[1].message, 'Replace sanitize with escape once') self.assertEqual(commits[2].author_name, 'East Coast')
def _test_get_file(self, tool_name, revision, base_commit_id, expected_revision): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, "https://bitbucket.org/api/1.0/repositories/" "myuser/myrepo/raw/%s/path" % expected_revision ) return "My data", {} account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = {"bitbucket_repo_name": "myrepo"} service.authorize("myuser", "abc123", None) self.spy_on(service._http_get, call_fake=_http_get) result = service.get_file(repository, "path", revision, base_commit_id) self.assertTrue(service._http_get.called) self.assertEqual(result, "My data")
def test_get_branches(self): """Testing ReviewBoardGateway get_branches implementation""" branches_api_response = json.dumps([ { 'name': 'master', 'id': 'c272edcac05b00e15440d6274723b639e3acbd7c', }, { 'name': 'im_a_branch', 'id': '83904e6acb60e7ec0dcaae6c09a579ab44d0cf38', } ]) def _http_get(self, *args, **kwargs): return branches_api_response, None account = self._get_hosting_account() account.data['private_token'] = encrypt_password('abc123') repository = Repository(hosting_account=account) repository.extra_data = { 'rbgateway_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) branches = service.get_branches(repository) self.assertTrue(service.client.http_get.called) self.assertEqual(len(branches), 2) self.assertEqual( branches, [ Branch(id='master', commit='c272edcac05b00e15440d6274723b639e3acbd7c', default=True), Branch(id='im_a_branch', commit='83904e6acb60e7ec0dcaae6c09a579ab44d0cf38', default=False) ])
def test_get_change(self): """Testing ReviewBoardGateway get_change implementation""" diff = (b'diff --git a/test b/test\n' 'index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..' 'dced80a85fe1e8f13dd5ea19923e5d2e8680020d 100644\n' '--- a/test\n+++ b/test\n@@ -1 +1,3 @@\n test\n+\n+test\n') diff_encoding = md5(diff.encode('utf-8')).hexdigest() change_api_response = json.dumps( { 'author': 'myname', 'id': 'bfdde95432b3af879af969bd2377dc3e55ee46e6', 'date': '2015-02-13 22:34:01 -0700 -0700', 'message': 'mymessage', 'parent_id': '304c53c163aedfd0c0e0933776f09c24b87f5944', 'diff': diff } ) def _http_get(self, *args, **kwargs): return change_api_response, None account = self._get_hosting_account() account.data['private_token'] = encrypt_password('abc123') repository = Repository(hosting_account=account) repository.extra_data = { 'rbgateway_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) change = service.get_change( repository, 'bfdde95432b3af879af969bd2377dc3e55ee46e6') self.assertTrue(service.client.http_get.called) self.assertEqual(change.message, 'mymessage') self.assertEqual(md5(change.diff.encode('utf-8')).hexdigest(), diff_encoding)
def test_get_branches(self): """Testing ReviewBoardGateway get_branches implementation""" branches_api_response = json.dumps([{ 'name': 'master', 'id': 'c272edcac05b00e15440d6274723b639e3acbd7c', }, { 'name': 'im_a_branch', 'id': '83904e6acb60e7ec0dcaae6c09a579ab44d0cf38', }]) def _http_get(self, *args, **kwargs): return branches_api_response, None account = self._get_hosting_account() account.data['private_token'] = encrypt_password('abc123') repository = Repository(hosting_account=account) repository.extra_data = { 'rbgateway_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) branches = service.get_branches(repository) self.assertTrue(service.client.http_get.called) self.assertEqual(len(branches), 2) self.assertEqual(branches, [ Branch(id='master', commit='c272edcac05b00e15440d6274723b639e3acbd7c', default=True), Branch(id='im_a_branch', commit='83904e6acb60e7ec0dcaae6c09a579ab44d0cf38', default=False) ])
def _test_get_file(self, tool_name, revision, base_commit_id, expected_revision): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, "https://mydomain.beanstalkapp.com/api/repositories/" "myrepo/blob?id=%s&name=path" % expected_revision, ) return "My data", {} account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = {"beanstalk_account_domain": "mydomain", "beanstalk_repo_name": "myrepo"} service.authorize("myuser", "abc123", None) self.spy_on(service._http_get, call_fake=_http_get) result = service.get_file(repository, "/path", revision, base_commit_id) self.assertTrue(service._http_get.called) self.assertEqual(result, "My data")
def _test_get_file_exists(self, tool_name, revision, base_commit_id, expected_revision, expected_found=True, expected_error=False): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, 'https://mydomain.unfuddle.com/api/v1/repositories/2/' 'history/?path=/path&commit=%s&count=0' % expected_revision) if expected_found: return '{}', {} else: raise HTTPError() account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'unfuddle_account_domain': 'mydomain', 'unfuddle_project_id': 1, 'unfuddle_repo_id': 2, 'unfuddle_repo_name': 'myrepo', } account.data['password'] = encrypt_password('password') self.spy_on(service.client.http_get, call_fake=_http_get) result = service.get_file_exists(repository, '/path', revision, base_commit_id) if expected_error: self.assertFalse(service.client.http_get.called) self.assertFalse(result) else: self.assertTrue(service.client.http_get.called) self.assertEqual(result, expected_found)
def test_get_change(self): """Testing ReviewBoardGateway get_change implementation""" diff = (b'diff --git a/test b/test\n' 'index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..' 'dced80a85fe1e8f13dd5ea19923e5d2e8680020d 100644\n' '--- a/test\n+++ b/test\n@@ -1 +1,3 @@\n test\n+\n+test\n') diff_encoding = md5(diff.encode('utf-8')).hexdigest() change_api_response = json.dumps({ 'author': 'myname', 'id': 'bfdde95432b3af879af969bd2377dc3e55ee46e6', 'date': '2015-02-13 22:34:01 -0700 -0700', 'message': 'mymessage', 'parent_id': '304c53c163aedfd0c0e0933776f09c24b87f5944', 'diff': diff }) def _http_get(self, *args, **kwargs): return change_api_response, None account = self._get_hosting_account() account.data['private_token'] = encrypt_password('abc123') repository = Repository(hosting_account=account) repository.extra_data = { 'rbgateway_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) change = service.get_change( repository, 'bfdde95432b3af879af969bd2377dc3e55ee46e6') self.assertTrue(service.client.http_get.called) self.assertEqual(change.message, 'mymessage') self.assertEqual( md5(change.diff.encode('utf-8')).hexdigest(), diff_encoding)
def test_get_change_exception(self): """Testing GitHub get_change exception types""" def _http_get(service, url, *args, **kwargs): raise Exception('Not Found') account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } service = account.service commit_sha = '1c44b461cebe5874a857c51a4a13a849a4d1e52d' self.assertRaisesMessage( SCMError, 'Not Found', lambda: service.get_change(repository, commit_sha))
def test_is_ssh_key_associated(self): """Testing that GitHub associated SSH keys are correctly identified""" associated_key = "good_key" unassociated_key = "bad_key" keys = simplejson.dumps([{"key": "neutral_key"}, {"key": associated_key}]) def _http_get(self, *args, **kwargs): return keys, None self.service_class._http_get = _http_get self.service_class._format_public_key = lambda self, key: key account = self._get_hosting_account() account.data["authorization"] = {"token": "abc123"} service = account.service repository = Repository(hosting_account=account) repository.extra_data = {"repository_plan": "public", "github_public_repo_name": "myrepo"} self.assertTrue(service.is_ssh_key_associated(repository, associated_key)) self.assertFalse(service.is_ssh_key_associated(repository, unassociated_key)) self.assertFalse(service.is_ssh_key_associated(repository, None))
def _test_get_file(self, tool_name, revision, base_commit_id, expected_revision, expected_error=False): def _http_get(service, url, *args, **kwargs): self.assertEqual( url, 'https://mydomain.unfuddle.com/api/v1/repositories/2/' 'download/?path=%s&commit=%s' % (path, expected_revision)) return 'My data', {} path = '/path' account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'unfuddle_account_domain': 'mydomain', 'unfuddle_project_id': 1, 'unfuddle_repo_id': 2, 'unfuddle_repo_name': 'myrepo', } account.data['password'] = encrypt_password('password') self.spy_on(service.client.http_get, call_fake=_http_get) if expected_error: self.assertRaises( FileNotFoundError, lambda: service.get_file( repository, path, revision, base_commit_id)) self.assertFalse(service.client.http_get.called) else: result = service.get_file(repository, path, revision, base_commit_id) self.assertTrue(service.client.http_get.called) self.assertEqual(result, 'My data')
def test_ticket_login_with_no_valid_ticket(self): """Testing Perforce with ticket-based logins without a valid ticket """ repo = Repository(name='Perforce.com', path='public.perforce.com:1666', tool=Tool.objects.get(name='Perforce'), username='******', password='******') repo.extra_data = { 'use_ticket_auth': True, } client = repo.get_scmtool().client self.assertTrue(client.use_ticket_auth) self.spy_on(client.get_ticket_status, call_fake=lambda *args: None) self.spy_on(client.login, call_original=False) with client.connect(): self.assertTrue(client.login.called) self.assertEqual(client.p4.ticket_file, os.path.join(settings.SITE_DATA_DIR, 'p4', 'p4tickets'))
def _test_get_file_exists(self, tool_name, revision, base_commit_id, expected_revision, expected_found): def _http_get(service, url, *args, **kwargs): expected_url = ('https://mydomain.beanstalkapp.com/api/' 'repositories/myrepo/') if not base_commit_id and tool_name == 'Git': expected_url += 'blob?id=%s&name=path' % expected_revision else: expected_url += ('node.json?path=/path&revision=%s' % expected_revision) self.assertEqual(url, expected_url) if expected_found: return b'{}', {} else: raise HTTPError() account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name=tool_name)) repository.extra_data = { 'beanstalk_account_domain': 'mydomain', 'beanstalk_repo_name': 'myrepo', } service.authorize('myuser', 'abc123', None) self.spy_on(service.client.http_get, call_fake=_http_get) result = service.get_file_exists(repository, '/path', revision, base_commit_id) self.assertTrue(service.client.http_get.called) self.assertEqual(result, expected_found)
def test_get_change(self): """Testing GitHub get_change implementation""" commit_sha = '1c44b461cebe5874a857c51a4a13a849a4d1e52d' parent_sha = '44568f7d33647d286691517e6325fea5c7a21d5e' tree_sha = '56e25e58380daf9b4dfe35677ae6043fe1743922' commits_api_response = json.dumps([ { 'commit': { 'author': {'name': 'David Trowbridge'}, 'committer': {'date': '2013-06-25T23:31:22Z'}, 'message': 'Move .clearfix to defs.less', }, 'sha': commit_sha, 'parents': [{'sha': parent_sha}], }, ]) compare_api_response = json.dumps({ 'base_commit': { 'commit': { 'tree': {'sha': tree_sha}, }, }, 'files': [ { 'sha': '4344b3ad41b171ea606e88e9665c34cca602affb', 'filename': 'reviewboard/static/rb/css/defs.less', 'status': 'modified', 'patch': dedent("""\ @@ -182,4 +182,23 @@ } +/* !(*%!(&^ (see http://www.positioniseverything.net/easyclearing.html) */ +.clearfix { + display: inline-block; + + &:after { + clear: both; + content: \".\"; + display: block; + height: 0; + visibility: hidden; + } +} + +/* Hides from IE-mac \\*/ +* html .clearfix {height: 1%;} +.clearfix {display: block;} +/* End hide from IE-mac */ + + // vim: set et ts=2 sw=2:"""), }, { 'sha': '8e3129277b018b169cb8d13771433fbcd165a17c', 'filename': 'reviewboard/static/rb/css/reviews.less', 'status': 'modified', 'patch': dedent("""\ @@ -1311,24 +1311,6 @@ .border-radius(8px); } -/* !(*%!(&^ (see http://www.positioniseverything.net/easyclearing.html) */ -.clearfix { - display: inline-block; - - &:after { - clear: both; - content: \".\"; - display: block; - height: 0; - visibility: hidden; - } -} - -/* Hides from IE-mac \\*/ -* html .clearfix {height: 1%;} -.clearfix {display: block;} -/* End hide from IE-mac */ - /**************************************************************************** * Issue Summary"""), }, { 'sha': '17ba0791499db908433b80f37c5fbc89b870084b', 'filename': 'new_filename', 'previous_filename': 'old_filename', 'status': 'renamed', 'patch': dedent('''\ @@ -1,1 +1,1 @@ - foo + bar ''') }, ], }) trees_api_response = json.dumps({ 'tree': [ { 'path': 'reviewboard/static/rb/css/defs.less', 'sha': '830a40c3197223c6a0abb3355ea48891a1857bfd', }, { 'path': 'reviewboard/static/rb/css/reviews.less', 'sha': '535cd2c4211038d1bb8ab6beaed504e0db9d7e62', }, { 'path': 'old_filename', 'sha': '356a192b7913b04c54574d18c28d46e6395428ab', } ], }) # This has to be a list to avoid python's hinky treatment of scope of # variables assigned within a closure. step = [1] def _http_get(service, url, *args, **kwargs): parsed = urlparse(url) if parsed.path == '/repos/myuser/myrepo/commits': self.assertEqual(step[0], 1) step[0] += 1 query = parsed.query.split('&') self.assertIn(('sha=%s' % commit_sha), query) return commits_api_response, None elif parsed.path.startswith('/repos/myuser/myrepo/compare/'): self.assertEqual(step[0], 2) step[0] += 1 revs = parsed.path.split('/')[-1].split('...') self.assertEqual(revs[0], parent_sha) self.assertEqual(revs[1], commit_sha) return compare_api_response, None elif parsed.path.startswith('/repos/myuser/myrepo/git/trees/'): self.assertEqual(step[0], 3) step[0] += 1 self.assertEqual(parsed.path.split('/')[-1], tree_sha) return trees_api_response, None else: print(parsed) self.fail('Got an unexpected GET request') account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } change = service.get_change(repository, commit_sha) self.assertTrue(service.client.http_get.called) self.assertEqual(change.message, 'Move .clearfix to defs.less') self.assertEqual(md5(change.diff).hexdigest(), '2e928c77c0bf703960eb49f04e76bc11')
def test_get_commits(self): """Testing GitHub get_commits implementation""" commits_api_response = json.dumps([ { 'commit': { 'author': {'name': 'Christian Hammond'}, 'committer': {'date': '2013-06-25T23:31:22Z'}, 'message': 'Fixed the bug number for the ' 'blacktriangledown bug.', }, 'sha': '859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817', 'parents': [ {'sha': '92463764015ef463b4b6d1a1825fee7aeec8cb15'} ], }, { 'commit': { 'author': {'name': 'Christian Hammond'}, 'committer': {'date': '2013-06-25T23:30:59Z'}, 'message': "Merge branch 'release-1.7.x'", }, 'sha': '92463764015ef463b4b6d1a1825fee7aeec8cb15', 'parents': [ {'sha': 'f5a35f1d8a8dcefb336a8e3211334f1f50ea7792'}, {'sha': '6c5f3465da5ed03dca8128bb3dd03121bd2cddb2'}, ], }, { 'commit': { 'author': {'name': 'David Trowbridge'}, 'committer': {'date': '2013-06-25T22:41:09Z'}, 'message': 'Add DIFF_PARSE_ERROR to the ' 'ValidateDiffResource.create error list.', }, 'sha': 'f5a35f1d8a8dcefb336a8e3211334f1f50ea7792', 'parents': [], } ]) def _http_get(self, *args, **kwargs): return commits_api_response, None account = self._get_hosting_account() service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) account.data['authorization'] = {'token': 'abc123'} repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } commits = service.get_commits( repository, start='859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817') self.assertTrue(service.client.http_get.called) self.assertEqual(len(commits), 3) self.assertEqual(commits[0].parent, commits[1].id) self.assertEqual(commits[1].parent, commits[2].id) self.assertEqual(commits[0].date, '2013-06-25T23:31:22Z') self.assertEqual(commits[1].id, '92463764015ef463b4b6d1a1825fee7aeec8cb15') self.assertEqual(commits[2].author_name, 'David Trowbridge') self.assertEqual(commits[2].parent, '')
def test_get_branches(self): """Testing GitHub get_branches implementation""" branches_api_response = json.dumps([ { 'ref': 'refs/heads/master', 'object': { 'sha': '859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817', } }, { 'ref': 'refs/heads/release-1.7.x', 'object': { 'sha': '92463764015ef463b4b6d1a1825fee7aeec8cb15', } }, { 'ref': 'refs/heads/some-component/fix', 'object': { 'sha': '764015ef492c8cb1546363b45fee7ab6d1a182ee', } }, { 'ref': 'refs/tags/release-1.7.11', 'object': { 'sha': 'f5a35f1d8a8dcefb336a8e3211334f1f50ea7792', } }, ]) def _http_get(self, *args, **kwargs): return branches_api_response, None account = self._get_hosting_account() account.data['authorization'] = {'token': 'abc123'} repository = Repository(hosting_account=account) repository.extra_data = { 'repository_plan': 'public', 'github_public_repo_name': 'myrepo', } service = account.service self.spy_on(service.client.http_get, call_fake=_http_get) branches = service.get_branches(repository) self.assertTrue(service.client.http_get.called) self.assertEqual(len(branches), 3) self.assertEqual( branches, [ Branch(id='master', commit='859d4e148ce3ce60bbda6622cdbe5c2c2f8d9817', default=True), Branch(id='release-1.7.x', commit='92463764015ef463b4b6d1a1825fee7aeec8cb15', default=False), Branch(id='some-component/fix', commit='764015ef492c8cb1546363b45fee7ab6d1a182ee', default=False), ])
def test_get_change(self): """Testing GitLab get_change implementation""" commit_id = 'ed899a2f4b50b4370feeea94676502b42383c746' commit_api_response = json.dumps({ 'author_name': 'Chester Li', 'id': commit_id, 'created_at': '2015-03-10T11:50:22+03:00', 'message': 'Replace sanitize with escape once', 'parent_ids': ['ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba'] }) path_api_response = json.dumps( {'path_with_namespace': 'username/project_name'}) diff = dedent(b'''\ --- f1 | 1 + f2 | 1 + 2 files changed, 2 insertions(+), 0 deletions(-) diff --git a/f1 b/f1 index 11ac561..3ea0691 100644 --- a/f1 +++ b/f1 @@ -1 +1,2 @@ this is f1 +add one line to f1 diff --git a/f2 b/f2 index c837441..9302ecd 100644 --- a/f2 +++ b/f2 @@ -1 +1,2 @@ this is f2 +add one line to f2 with Unicode\xe2\x9d\xb6 ''') def _http_get(service, url, *args, **kwargs): self.assertTrue(url.startswith(account.hosting_url)) parsed = urlparse(url) if parsed.path.startswith( '/api/v3/projects/123456/repository/commits'): # If the url is commit_api_url. return commit_api_response, None elif parsed.path == '/api/v3/projects/123456': # If the url is path_api_url. return path_api_response, None elif parsed.path.endswith('.diff'): # If the url is diff_url. return diff, None else: print(parsed) self.fail('Got an unexpected GET request') account = self._get_hosting_account(use_url=True) account.data['private_token'] = encrypt_password('abc123') service = account.service repository = Repository(hosting_account=account) repository.extra_data = {'gitlab_project_id': 123456} self.spy_on(service.client.http_get, call_fake=_http_get) commit = service.get_change(repository, commit_id) self.assertTrue(service.client.http_get.called) self.assertEqual(commit.date, '2015-03-10T11:50:22+03:00') self.assertEqual(commit.diff, diff) self.assertNotEqual(commit.parent, '')
def test_get_branches(self): """Testing Bitbucket get_branches""" branches_api_response_1 = json.dumps({ 'next': ('https://bitbucket.org/api/2.0/repositories/myuser/' 'myrepo/refs/branches?pagelen=100&page=2&' 'fields=values.name%2Cvalues.target.hash%2Cnext'), 'values': [ { 'name': 'branch1', 'target': { 'hash': '1c44b461cebe5874a857c51a4a13a849a4d1e52d', }, }, { 'name': 'branch2', 'target': { 'hash': '44568f7d33647d286691517e6325fea5c7a21d5e', }, }, ], }) branches_api_response_2 = json.dumps({ 'values': [ { 'name': 'branch3', 'target': { 'hash': 'e5874a857c51a4a13a849a4d1e52d1c44b461ceb', }, }, { 'name': 'branch4', 'target': { 'hash': 'd286691517e6325fea5c7a21d5e44568f7d33647', }, }, ], }) get_repository_api_response = json.dumps({ 'mainbranch': { 'name': 'branch3', }, }) def _http_get(service, url, *args, **kwargs): url_parts = urlparse(url) path = url_parts.path query = parse_qs(url_parts.query) if path == '/api/2.0/repositories/myuser/myrepo/': self.assertEqual( query, { 'fields': ['mainbranch.name'], }) return get_repository_api_response, None elif path == '/api/2.0/repositories/myuser/myrepo/refs/branches': if 'page' in query: self.assertEqual( query, { 'fields': ['values.name,values.target.hash,next'], 'pagelen': ['100'], 'page': ['2'], }) return branches_api_response_2, None else: self.assertEqual( query, { 'fields': ['values.name,values.target.hash,next'], 'pagelen': ['100'], }) return branches_api_response_1, None else: self.fail('Unexpected URL %s' % url) account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name='Git')) repository.extra_data = { 'bitbucket_repo_name': 'myrepo', } account.data['password'] = encrypt_password('abc123') self.spy_on(service.client.http_get, call_fake=_http_get) branches = service.get_branches(repository) self.assertEqual(len(branches), 4) branch = branches[0] self.assertEqual(branch.name, 'branch1') self.assertEqual(branch.commit, '1c44b461cebe5874a857c51a4a13a849a4d1e52d') self.assertFalse(branch.default) branch = branches[1] self.assertEqual(branch.name, 'branch2') self.assertEqual(branch.commit, '44568f7d33647d286691517e6325fea5c7a21d5e') self.assertFalse(branch.default) branch = branches[2] self.assertEqual(branch.name, 'branch3') self.assertEqual(branch.commit, 'e5874a857c51a4a13a849a4d1e52d1c44b461ceb') self.assertTrue(branch.default) branch = branches[3] self.assertEqual(branch.name, 'branch4') self.assertEqual(branch.commit, 'd286691517e6325fea5c7a21d5e44568f7d33647') self.assertFalse(branch.default)
def test_get_commits(self): """Testing Bitbucket get_commits""" commits_api_response = json.dumps({ 'values': [ { 'hash': '1c44b461cebe5874a857c51a4a13a849a4d1e52d', 'author': { 'raw': 'Some User 1 <*****@*****.**>', }, 'date': '2017-01-24T13:11:22+00:00', 'message': 'This is commit 1.', 'parents': [ { 'hash': '44568f7d33647d286691517e6325fea5c7a21d5e', }, ], }, { 'hash': '44568f7d33647d286691517e6325fea5c7a21d5e', 'author': { 'raw': 'Some User 2 <*****@*****.**>', }, 'date': '2017-01-23T08:09:10+00:00', 'message': 'This is commit 2.', 'parents': [ { 'hash': 'e5874a857c51a4a13a849a4d1e52d1c44b461ceb', }, ], }, ], }) def _http_get(service, url, *args, **kwargs): url_parts = urlparse(url) path = url_parts.path query = parse_qs(url_parts.query) if path == '/api/2.0/repositories/myuser/myrepo/commits': self.assertEqual( query, { 'pagelen': ['20'], 'fields': ['values.author.raw,values.hash,' 'values.date,values.message,' 'values.parents.hash'], }) return commits_api_response, None else: self.fail('Unexpected URL %s' % url) account = self._get_hosting_account() service = account.service repository = Repository(hosting_account=account, tool=Tool.objects.get(name='Git')) repository.extra_data = { 'bitbucket_repo_name': 'myrepo', } account.data['password'] = encrypt_password('abc123') self.spy_on(service.client.http_get, call_fake=_http_get) commits = service.get_commits(repository) self.assertEqual(len(commits), 2) commit = commits[0] self.assertEqual(commit.id, '1c44b461cebe5874a857c51a4a13a849a4d1e52d') self.assertEqual(commit.author_name, 'Some User 1 <*****@*****.**>') self.assertEqual(commit.message, 'This is commit 1.') self.assertEqual(commit.date, '2017-01-24T13:11:22+00:00') self.assertEqual(commit.parent, '44568f7d33647d286691517e6325fea5c7a21d5e') self.assertIsNone(commit.diff) commit = commits[1] self.assertEqual(commit.id, '44568f7d33647d286691517e6325fea5c7a21d5e') self.assertEqual(commit.author_name, 'Some User 2 <*****@*****.**>') self.assertEqual(commit.message, 'This is commit 2.') self.assertEqual(commit.date, '2017-01-23T08:09:10+00:00') self.assertEqual(commit.parent, 'e5874a857c51a4a13a849a4d1e52d1c44b461ceb') self.assertIsNone(commit.diff)
def create_repository(self, with_local_site=False, name='Test Repo', tool_name='Git', path=None, local_site=None, extra_data=None, **kwargs): """Create a Repository for testing. The Repository may optionally be attached to a :py:class:`~reviewboard.site.models.LocalSite`. It's also populated with default data that can be overridden by the caller. Args: with_local_site (bool, optional): Whether to create the repository using a Local Site. This will choose one based on :py:attr:`local_site_name`. If ``local_site`` is provided, this argument is ignored. name (unicode, optional): The name of the repository. tool_name (unicode, optional): The name of the registered SCM Tool for the repository. path (unicode, optional): The path for the repository. If not provided, one will be computed. local_site (reviewboard.site.models.LocalSite, optional): The explicit Local Site to attach. extra_data (dict, optional): Explicit extra_data to attach to the repository. **kwargs (dict): Additional fields to set on the repository. Returns: reviewboard.scmtools.models.Repository: The new repository. """ if not local_site: if with_local_site: local_site = self.get_local_site(name=self.local_site_name) else: local_site = None testdata_dir = os.path.join(os.path.dirname(scmtools.__file__), 'testdata') if not path: if tool_name in ('Git', 'Test', 'TestToolSupportsPendingChangeSets'): path = os.path.join(testdata_dir, 'git_repo') elif tool_name == 'Subversion': path = 'file://' + os.path.join(testdata_dir, 'svn_repo') elif tool_name == 'Mercurial': path = os.path.join(testdata_dir, 'hg_repo.bundle') elif tool_name == 'CVS': path = os.path.join(testdata_dir, 'cvs_repo') elif tool_name == 'Perforce': path = 'localhost:1666' else: raise NotImplementedError repository = Repository(name=name, local_site=local_site, tool=Tool.objects.get(name=tool_name), path=path, **kwargs) if extra_data is not None: repository.extra_data = extra_data repository.save() return repository