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_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(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_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_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 testNewReviewRequest1(self): """Testing new_review_request view (uploading diffs)""" self.client.login(username='******', password='******') response = self.client.get('/r/new/') self.assertEqual(response.status_code, 200) testdata_dir = os.path.join( os.path.dirname(os.path.dirname(__file__)), 'scmtools', 'testdata') svn_repo_path = os.path.join(testdata_dir, 'svn_repo') repository = Repository(name='Subversion SVN', path='file://' + svn_repo_path, tool=Tool.objects.get(name='Subversion')) repository.save() diff_filename = os.path.join(testdata_dir, 'svn_makefile.diff') f = open(diff_filename, 'r') response = self.client.post('/r/new/', { 'repository': repository.id, 'diff_path': f, 'basedir': '/trunk', }) f.close() self.assertEqual(response.status_code, 302) r = ReviewRequest.objects.order_by('-time_added')[0] self.assertEqual(response['Location'], 'http://testserver%s' % r.get_absolute_url())
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_check_repository_perforce(self): """Testing Assembla.check_repository with Perforce""" try: account = self.create_hosting_account() service = account.service service.authorize('myuser', 'abc123', None) repository = Repository(hosting_account=account, tool=Tool.objects.get(name='Perforce')) scmtool = repository.get_scmtool() self.spy_on(scmtool.check_repository, call_original=False) service.check_repository(path='mypath', username='******', password='******', scmtool_class=scmtool.__class__, local_site_name=None, assembla_project_id='myproject') self.assertTrue(scmtool.check_repository.called) self.assertIn('p4_host', scmtool.check_repository.last_call.kwargs) self.assertEqual( scmtool.check_repository.last_call.kwargs['p4_host'], 'myproject') except ImportError: raise nose.SkipTest
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_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_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_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 setUp(self): super(GitTests, self).setUp() tool = Tool.objects.get(name='Git') self.local_repo_path = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'git_repo') self.git_ssh_path = ('localhost:%s' % self.local_repo_path.replace('\\', '/')) remote_repo_path = '[email protected]:reviewboard/reviewboard.git' remote_repo_raw_url = ('http://github.com/api/v2/yaml/blob/show/' 'reviewboard/reviewboard/<revision>') self.repository = Repository(name='Git test repo', path=self.local_repo_path, tool=tool) self.remote_repository = Repository(name='Remote Git test repo', path=remote_repo_path, raw_file_url=remote_repo_raw_url, tool=tool) try: self.tool = self.repository.get_scmtool() self.remote_tool = self.remote_repository.get_scmtool() except ImportError: raise nose.SkipTest('git binary not found')
def test_check_repository_subversion(self): """Testing Assembla.check_repository with Subversion""" try: account = self.create_hosting_account() service = account.service service.authorize('myuser', 'abc123', None) repository = Repository(path='https://svn.example.com/', hosting_account=account, tool=Tool.objects.get(name='Subversion')) scmtool = repository.get_scmtool() self.spy_on(scmtool.check_repository, call_original=False) service.check_repository(path='https://svn.example.com/', username='******', password='******', scmtool_class=scmtool.__class__, local_site_name=None) self.assertTrue(scmtool.check_repository.called) self.assertNotIn('p4_host', scmtool.check_repository.last_call.kwargs) except ImportError: raise nose.SkipTest
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_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 testBadRoot(self): """Testing a bad CVSROOT""" file = "test/testfile" rev = Revision("1.1") badrepo = Repository(name="CVS", path=self.cvs_repo_path + "2", tool=Tool.objects.get(name="CVS")) badtool = badrepo.get_scmtool() self.assertRaises(SCMError, lambda: badtool.get_file(file, rev))
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 testPathWithoutPort(self): """Testing parsing a CVSROOT without a port""" repo = Repository( name="CVS", path="example.com:/cvsroot/test", username="******", tool=Tool.objects.get(name="CVS") ) tool = repo.get_scmtool() self.assertEqual(tool.repopath, "/cvsroot/test") self.assertEqual(tool.client.repository, ":pserver:[email protected]:/cvsroot/test")
def testBadRoot(self): """Testing a bad CVSROOT""" file = 'test/testfile' rev = Revision('1.1') badrepo = Repository(name='CVS', path=self.cvs_repo_path + '2', tool=Tool.objects.get(name='CVS')) badtool = badrepo.get_scmtool() self.assertRaises(SCMError, lambda: badtool.get_file(file, rev))
def add_repository(self, name, url): """Add a repository to Review Board.""" with wrap_env(): self._setup_env() from reviewboard.scmtools.models import Repository, Tool tool = Tool.objects.get(name__exact='Mercurial') r = Repository(name=name, path=url, tool=tool) r.save() return r.id
def test_https_repo(self): """Testing HgTool.file_exists with an HTTPS-based repository""" repo = Repository(name='Test HG2', path='https://bitbucket.org/pypy/pypy', tool=Tool.objects.get(name='Mercurial')) tool = repo.get_scmtool() rev = Revision('877cf1960916') self.assertTrue(tool.file_exists('TODO.rst', rev)) self.assertTrue(not tool.file_exists('TODO.rstNotFound', rev))
def test_clean_with_path_conflict_with_archived(self): """Testing Repository.clean with archived repositories ignored for path conflict """ self.repository.archive() repository = Repository(name='New test repo', path=self.repository.path, tool=self.repository.tool) with self.assertNumQueries(1): repository.clean()
def _get_repo_api_url(self, plan, fields): account = self._get_hosting_account() service = account.service self.assertNotEqual(service, None) repository = Repository(hosting_account=account) repository.extra_data['repository_plan'] = plan form = self._get_form(plan, fields) form.save(repository) return service._get_repo_api_url(repository)
def test_custom_host(self): """Testing Perforce client initialization with a custom P4HOST""" repo = Repository(name='Perforce.com', path='public.perforce.com:1666', tool=Tool.objects.get(name='Perforce'), encoding='utf8') repo.extra_data['p4_host'] = 'my-custom-host' tool = repo.get_scmtool() with tool.client._connect(): self.assertEqual(tool.client.p4.host, 'my-custom-host')
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_custom_host(self): """Testing Perforce client initialization with a custom P4HOST""" repo = Repository(name='Perforce.com', path='public.perforce.com:1666', username='******', tool=Tool.objects.get(name='Perforce'), encoding='utf8') repo.extra_data['p4_host'] = 'my-custom-host' tool = repo.get_scmtool() with tool.client.connect(): self.assertEqual(tool.client.p4.host, 'my-custom-host')
def test_clean_with_name_conflict(self): """Testing Repository.clean with name conflict""" repository = Repository(name=self.repository.name, path='path/to/repo.git', tool=self.repository.tool) with self.assertRaises(ValidationError) as ctx: with self.assertNumQueries(1): repository.clean() self.assertEqual(ctx.exception.message_dict, { 'name': ['A repository with this name already exists'], })
def test_clean_with_path_conflict(self): """Testing Repository.clean with path conflict""" repository = Repository(name='New test repo', path=self.repository.path, tool=self.repository.tool) with self.assertRaises(ValidationError) as ctx: with self.assertNumQueries(1): repository.clean() self.assertEqual(ctx.exception.message_dict, { 'path': ['A repository with this path already exists'], })
def setUp(self): super(PerforceTests, self).setUp() self.repository = Repository(name='Perforce.com', path='public.perforce.com:1666', username='******', encoding='none', tool=Tool.objects.get(name='Perforce')) try: self.tool = self.repository.get_scmtool() except ImportError: raise nose.SkipTest('perforce/p4python is not installed')
def test_https_repo(self): """Testing HgTool.file_exists with an HTTPS-based repository""" repo = Repository(name='Test HG2', path='https://www.mercurial-scm.org/repo/hg', tool=Tool.objects.get(name='Mercurial')) tool = repo.get_scmtool() self.assertTrue( tool.file_exists('mercurial/hgweb/common.py', Revision('f0735f2ce542'))) self.assertFalse( tool.file_exists('mercurial/hgweb/common.py', Revision('abcdef123456')))
def setUp(self): super(MercurialTests, self).setUp() hg_repo_path = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'hg_repo') self.repository = Repository(name='Test HG', path=hg_repo_path, tool=Tool.objects.get(name='Mercurial')) try: self.tool = self.repository.get_scmtool() except ImportError: raise nose.SkipTest('Hg is not installed')
def test_with_hosting_service_with_existing_bug_tracker_using_hosting( self): """Testing RepositoryForm with existing bug tracker using hosting service """ account = HostingServiceAccount.objects.create(username='******', service_name='test') repository = Repository(name='test', hosting_account=account) repository.extra_data['bug_tracker_use_hosting'] = True repository.extra_data['test_repo_name'] = 'testrepo' form = RepositoryForm(instance=repository) self.assertTrue(form._get_field_data('bug_tracker_use_hosting'))
def test_clean_with_path_conflict_with_archived(self): """Testing Repository.clean with archived repositories ignored for path conflict """ orig_repository = self.repository orig_repository.archive() repository = Repository(name='New test repo', path=orig_repository.path, tool=orig_repository.tool) with self.assertNumQueries(1): repository.clean()
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_repository_commits_with_no_support(self): """Testing the GET repositories/<id>/commits/ API with a repository that does not implement it""" hg_repo_path = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'hg_repo.bundle') repository = Repository(name='Test HG', path=hg_repo_path, tool=Tool.objects.get(name='Mercurial')) repository.save() rsp = self.apiGet(get_repository_commits_url(repository), query={'start': ''}, expected_status=501) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], REPO_NOT_IMPLEMENTED.code)
def setUp(self): super(BZRTests, self).setUp() self.bzr_repo_path = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'bzr_repo') self.bzr_ssh_path = ('bzr+ssh://localhost/%s' % self.bzr_repo_path.replace('\\', '/')) self.bzr_sftp_path = ('sftp://localhost/%s' % self.bzr_repo_path.replace('\\', '/')) self.repository = Repository(name='Bazaar', path='file://' + self.bzr_repo_path, tool=Tool.objects.get(name='Bazaar')) self.tool = self.repository.get_scmtool()
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 setUp(self): super(CVSTests, self).setUp() self.cvs_repo_path = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'cvs_repo') self.cvs_ssh_path = (':ext:localhost:%s' % self.cvs_repo_path.replace('\\', '/')) self.repository = Repository(name='CVS', path=self.cvs_repo_path, tool=Tool.objects.get(name='CVS')) try: self.tool = self.repository.get_scmtool() except ImportError: raise nose.SkipTest('cvs binary not found')
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_save_form_perforce_with_portfolio(self): """Testing Assembla configuration form with Perforce with Assembla portfolio IDs """ try: account = self._get_hosting_account() service = account.service service.authorize('myuser', 'abc123', None) repository = Repository(hosting_account=account, tool=Tool.objects.get(name='Perforce')) form = self._get_form( fields={ 'assembla_project_id': 'myportfolio/myproject', }) self.spy_on(get_hostname, call_fake=lambda: 'myhost.example.com') form.save(repository) self.assertIn('use_ticket_auth', repository.extra_data) self.assertTrue(repository.extra_data['use_ticket_auth']) self.assertIn('p4_host', repository.extra_data) self.assertIn('p4_client', repository.extra_data) self.assertEqual(repository.extra_data['p4_host'], 'myportfolio/myproject') self.assertEqual(repository.extra_data['p4_client'], 'myhost.example.com-myportfolio-myproject') except ImportError: raise nose.SkipTest
def setUp(self): super(PerforceTests, self).setUp() self.repository = Repository(name='Perforce.com', path='public.perforce.com:1666', username='******', encoding='none', tool=Tool.objects.get(name='Perforce'))
def test_encoding(self): """Testing PerforceTool.get_changeset with a specified encoding""" repo = Repository(name='Perforce.com', path='public.perforce.com:1666', tool=Tool.objects.get(name='Perforce'), encoding='utf8') tool = repo.get_scmtool() try: tool.get_changeset(157) self.fail('Expected an error about unicode-enabled servers. Did ' 'perforce.com turn on unicode for public.perforce.com?') except SCMError as e: # public.perforce.com doesn't have unicode enabled. Getting this # error means we at least passed the charset through correctly # to the p4 client. self.assertTrue( 'clients require a unicode enabled server' in six.text_type(e))
def setUp(self): super(BZRTests, self).setUp() self.bzr_repo_path = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'bzr_repo') self.bzr_ssh_path = ('bzr+ssh://localhost/%s' % self.bzr_repo_path.replace('\\', '/')) self.bzr_sftp_path = ('sftp://localhost/%s' % self.bzr_repo_path.replace('\\', '/')) self.repository = Repository(name='Bazaar', path='file://' + self.bzr_repo_path, tool=Tool.objects.get(name='Bazaar')) from reviewboard.scmtools.bzr import has_bzrlib if not has_bzrlib: self.tool = self.repository.get_scmtool() raise nose.SkipTest('bzrlib is not installed')
def test_with_hosting_service_with_existing_custom_bug_tracker(self): """Testing RepositoryForm with existing custom bug tracker""" repository = Repository(name='test', bug_tracker='http://example.com/issue/%s') form = RepositoryForm(instance=repository) self.assertFalse(form._get_field_data('bug_tracker_use_hosting')) self.assertEqual(form._get_field_data('bug_tracker_type'), 'custom') self.assertEqual(form.initial['bug_tracker'], 'http://example.com/issue/%s')
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_with_hosting_service_with_existing_bug_tracker_service(self): """Testing RepositoryForm with existing bug tracker service""" repository = Repository(name='test') repository.extra_data['bug_tracker_type'] = 'test' repository.extra_data['bug_tracker-test_repo_name'] = 'testrepo' repository.extra_data['bug_tracker-hosting_account_username'] = \ 'testuser' form = RepositoryForm(instance=repository) self.assertFalse(form._get_field_data('bug_tracker_use_hosting')) self.assertEqual(form._get_field_data('bug_tracker_type'), 'test') self.assertEqual( form._get_field_data('bug_tracker_hosting_account_username'), 'testuser') self.assertIn('test', form.bug_tracker_forms) self.assertIn('default', form.bug_tracker_forms['test']) bitbucket_form = form.bug_tracker_forms['test']['default'] self.assertEqual(bitbucket_form.fields['test_repo_name'].initial, 'testrepo')
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_file(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'My data\n', {} 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) if file_exists: result = service.get_file(repository, 'myfile', '123') self.assertEqual(result, 'My data\n') else: with self.assertRaises(FileNotFoundError): service.get_file(repository, 'myfile', '123') self.assertTrue(service.client.http_get.called)
def setUp(self): super(PerforceStunnelTests, self).setUp() if not is_exe_in_path('stunnel'): raise nose.SkipTest('stunnel is not installed') cert = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'stunnel.pem') self.proxy = STunnelProxy('public.perforce.com:1666') self.proxy.start_server(cert) # Find an available port to listen on path = 'stunnel:localhost:%d' % self.proxy.port self.repository = Repository(name='Perforce.com - secure', path=path, username='******', encoding='none', tool=Tool.objects.get(name='Perforce')) self.tool = self.repository.get_scmtool() self.tool.use_stunnel = True
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 setUp(self): tool = Tool.objects.get(name='Git') local_repo_path = os.path.join(os.path.dirname(__file__), 'testdata', 'git_repo') remote_repo_path = '[email protected]:reviewboard/reviewboard.git' remote_repo_raw_url = 'http://github.com/api/v2/yaml/blob/show/' \ 'reviewboard/reviewboard/<revision>' self.repository = Repository(name='Git test repo', path=local_repo_path, tool=tool) self.remote_repository = Repository(name='Remote Git test repo', path=remote_repo_path, raw_file_url=remote_repo_raw_url, tool=tool) try: self.tool = self.repository.get_scmtool() self.remote_tool = self.remote_repository.get_scmtool() except ImportError: raise nose.SkipTest('git binary not found')
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_ssh(self, repo_path, filename=None): """Helper for testing an SSH connection to a local repository. This will attempt to SSH into the local machine and connect to the given repository, checking it for validity and optionally fetching a file. If this is unable to connect to the local machine, the test will be flagged as skipped. Args: repo_path (unicode): The repository path to check. filename (unicode, optional): The optional file in the repository to fetch. """ self._check_can_test_ssh() repo = Repository(name='SSH Test', path=repo_path, tool=self.repository.tool) tool = repo.get_scmtool() try: tool.check_repository(repo_path) except SocketError as e: if e.errno == ECONNREFUSED: # This box likely isn't set up for this test. SCMTestCase._can_test_ssh = False raise nose.SkipTest( 'Cannot perform SSH access tests. No local SSH service is ' 'running.') else: raise if filename: self.assertNotEqual(tool.get_file(filename, HEAD), None)