def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = settings.DEFAULT_PRIVACY_LEVEL self.service = GitHubService(user=self.user, account=None) self.external_version = get(Version, project=self.project, type=EXTERNAL) self.external_build = get( Build, project=self.project, version=self.external_version ) self.integration = get( GitHubWebhook, project=self.project, provider_data={ 'url': 'https://github.com/' } ) self.provider_data = [ { "config": { "url": "https://example.com/webhook" }, "url": "https://api.github.com/repos/test/Hello-World/hooks/12345678", } ]
def test_sync_repositories_only_creates_one_remote_repo_per_vcs_repo(self, mock_request): mock_request.get('https://api.github.com/user/repos', json=self.payload_user_repos) self.assertEqual(RemoteRepository.objects.count(), 0) remote_repositories = self.service.sync_repositories() self.assertEqual(RemoteRepository.objects.count(), 1) self.assertEqual(len(remote_repositories), 1) self.assertEqual(RemoteRepositoryRelation.objects.count(), 1) user_2 = fixture.get(User) user_2_socialaccount = fixture.get( SocialAccount, user=user_2, provider=GitHubOAuth2Adapter.provider_id, ) fixture.get( SocialToken, account=user_2_socialaccount, ) service_2 = GitHubService(user=user_2, account=user_2_socialaccount) remote_repositories = service_2.sync_repositories() self.assertEqual(RemoteRepository.objects.count(), 1) self.assertEqual(len(remote_repositories), 1) self.assertEqual(RemoteRepositoryRelation.objects.count(), 2)
def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = self.project.version_privacy_level self.service = GitHubService(user=self.user, account=None)
def test_multiple_users_same_repo(self): repo_json = { 'name': '', 'full_name': 'testrepo/multiple', 'description': '', 'git_url': '', 'private': False, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) github_project_4 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) github_project_6 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
def handle(self, *args, **options): if len(args): for slug in args: for service in GitHubService.for_user( User.objects.get(username=slug) ): service.sync()
def token(self, request, **kwargs): project = get_object_or_404( Project.objects.api(request.user), pk=kwargs['pk']) token = GitHubService.get_token_for_project(project, force_local=True) return Response({ 'token': token })
def token(self, request, **kwargs): project = get_object_or_404(Project.objects.api(request.user), pk=kwargs['pk']) token = GitHubService.get_token_for_project(project, force_local=True) return Response({ 'token': token, })
def test_multiple_users_same_repo(self): repo_json = { "name": "", "full_name": "testrepo/multiple", "description": "", "git_url": "", "private": False, "ssh_url": "", "html_url": "", "clone_url": "", } github_project = self.service.create_repository(repo_json, organization=self.org, privacy=self.privacy) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_4 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_6 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
def setUp(self): self.user = fixture.get(User) self.socialaccount = fixture.get( SocialAccount, user=self.user, provider=GitHubOAuth2Adapter.provider_id, ) self.token = fixture.get( SocialToken, account=self.socialaccount, ) self.service = GitHubService.for_user(self.user)[0]
def test_multiple_users_same_repo(self): repo_json = { "name": "", "full_name": "testrepo/multiple", "description": "", "git_url": "", "private": False, "ssh_url": "", "html_url": "", "clone_url": "", } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_4 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_6 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
def vcs_repo(self, version=LATEST): backend = backend_cls.get(self.repo_type) if not backend: repo = None else: proj = VCSProject(self.name, self.default_branch, self.checkout_path(version), self.clean_repo) from readthedocs.oauth.services import GitHubService token = None if "github" in self.repo.lower(): token = GitHubService.get_token_for_project(self) repo = backend(proj, version, token=token) # repo = backend(proj, version) return repo
def vcs_repo(self, version=LATEST, environment=None): """ Return a Backend object for this project able to handle VCS commands. :param environment: environment to run the commands :type environment: doc_builder.environments.BuildEnvironment :param version: version slug for the backend (``LATEST`` by default) :type version: str """ # TODO: this seems to be the only method that receives a # ``version.slug`` instead of a ``Version`` instance (I prefer an # instance here) from readthedocs.oauth.services import GitHubService backend = backend_cls.get(self.repo_type) if not backend: repo = None else: token = None if 'github' in self.repo.lower(): token = GitHubService.get_token_for_project(self) repo = backend(self, version, environment, token=token) # repo = backend(self, version, environment) return repo
class GitHubOAuthTests(TestCase): fixtures = ["eric", "test_data"] def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = self.project.version_privacy_level self.service = GitHubService(user=self.user, account=None) def test_make_project_pass(self): repo_json = { "name": "testrepo", "full_name": "testuser/testrepo", "description": "Test Repo", "git_url": "git://github.com/testuser/testrepo.git", "private": False, "ssh_url": "ssh://[email protected]:testuser/testrepo.git", "html_url": "https://github.com/testuser/testrepo", "clone_url": "https://github.com/testuser/testrepo.git", } repo = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(repo, RemoteRepository) self.assertEqual(repo.name, 'testrepo') self.assertEqual(repo.full_name, 'testuser/testrepo') self.assertEqual(repo.description, 'Test Repo') self.assertIsNone(repo.avatar_url) self.assertIn(self.user, repo.users.all()) self.assertEqual(repo.organization, self.org) self.assertEqual( repo.clone_url, 'https://github.com/testuser/testrepo.git') self.assertEqual( repo.ssh_url, 'ssh://[email protected]:testuser/testrepo.git') self.assertEqual( repo.html_url, 'https://github.com/testuser/testrepo') def test_make_project_fail(self): repo_json = { "name": "", "full_name": "", "description": "", "git_url": "", "private": True, "ssh_url": "", "html_url": "", "clone_url": "", } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsNone(github_project) def test_make_organization(self): org_json = { "html_url": "https://github.com/testorg", "name": "Test Org", "email": "*****@*****.**", "login": "******", "avatar_url": "https://images.github.com/foobar", } org = self.service.create_organization(org_json) self.assertIsInstance(org, RemoteOrganization) self.assertEqual(org.slug, 'testorg') self.assertEqual(org.name, 'Test Org') self.assertEqual(org.email, '*****@*****.**') self.assertEqual(org.avatar_url, 'https://images.github.com/foobar') self.assertEqual(org.url, 'https://github.com/testorg') def test_import_with_no_token(self): '''User without a GitHub SocialToken does not return a service''' service = GitHubService.for_user(self.user) self.assertIsNone(service) def test_multiple_users_same_repo(self): repo_json = { "name": "", "full_name": "testrepo/multiple", "description": "", "git_url": "", "private": False, "ssh_url": "", "html_url": "", "clone_url": "", } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_4 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_6 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
def test_import_with_no_token(self): """User without a GitHub SocialToken does not return a service.""" services = GitHubService.for_user(self.user) self.assertEqual(services, [])
class GitHubOAuthTests(TestCase): fixtures = ['eric', 'test_data'] def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = self.project.version_privacy_level self.service = GitHubService(user=self.user, account=None) def test_make_project_pass(self): repo_json = { 'name': 'testrepo', 'full_name': 'testuser/testrepo', 'description': 'Test Repo', 'git_url': 'git://github.com/testuser/testrepo.git', 'private': False, 'ssh_url': 'ssh://[email protected]:testuser/testrepo.git', 'html_url': 'https://github.com/testuser/testrepo', 'clone_url': 'https://github.com/testuser/testrepo.git', } repo = self.service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(repo, RemoteRepository) self.assertEqual(repo.name, 'testrepo') self.assertEqual(repo.full_name, 'testuser/testrepo') self.assertEqual(repo.description, 'Test Repo') self.assertEqual( repo.avatar_url, settings.OAUTH_AVATAR_USER_DEFAULT_URL, ) self.assertIn(self.user, repo.users.all()) self.assertEqual(repo.organization, self.org) self.assertEqual(repo.clone_url, 'https://github.com/testuser/testrepo.git') self.assertEqual(repo.ssh_url, 'ssh://[email protected]:testuser/testrepo.git') self.assertEqual(repo.html_url, 'https://github.com/testuser/testrepo') def test_make_project_fail(self): repo_json = { 'name': '', 'full_name': '', 'description': '', 'git_url': '', 'private': True, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsNone(github_project) def test_make_organization(self): org_json = { 'html_url': 'https://github.com/testorg', 'name': 'Test Org', 'email': '*****@*****.**', 'login': '******', 'avatar_url': 'https://images.github.com/foobar', } org = self.service.create_organization(org_json) self.assertIsInstance(org, RemoteOrganization) self.assertEqual(org.slug, 'testorg') self.assertEqual(org.name, 'Test Org') self.assertEqual(org.email, '*****@*****.**') self.assertEqual(org.avatar_url, 'https://images.github.com/foobar') self.assertEqual(org.url, 'https://github.com/testorg') def test_import_with_no_token(self): """User without a GitHub SocialToken does not return a service.""" services = GitHubService.for_user(self.user) self.assertEqual(services, []) def test_multiple_users_same_repo(self): repo_json = { 'name': '', 'full_name': 'testrepo/multiple', 'description': '', 'git_url': '', 'private': False, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository(repo_json, organization=self.org, privacy=self.privacy) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_4 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_6 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
class GitHubOAuthTests(TestCase): fixtures = ['eric', 'test_data'] def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = self.project.version_privacy_level self.service = GitHubService(user=self.user, account=None) def test_make_project_pass(self): repo_json = { 'name': 'testrepo', 'full_name': 'testuser/testrepo', 'description': 'Test Repo', 'git_url': 'git://github.com/testuser/testrepo.git', 'private': False, 'ssh_url': 'ssh://[email protected]:testuser/testrepo.git', 'html_url': 'https://github.com/testuser/testrepo', 'clone_url': 'https://github.com/testuser/testrepo.git', } repo = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(repo, RemoteRepository) self.assertEqual(repo.name, 'testrepo') self.assertEqual(repo.full_name, 'testuser/testrepo') self.assertEqual(repo.description, 'Test Repo') self.assertEqual( repo.avatar_url, settings.OAUTH_AVATAR_USER_DEFAULT_URL, ) self.assertIn(self.user, repo.users.all()) self.assertEqual(repo.organization, self.org) self.assertEqual( repo.clone_url, 'https://github.com/testuser/testrepo.git') self.assertEqual( repo.ssh_url, 'ssh://[email protected]:testuser/testrepo.git') self.assertEqual(repo.html_url, 'https://github.com/testuser/testrepo') def test_make_project_fail(self): repo_json = { 'name': '', 'full_name': '', 'description': '', 'git_url': '', 'private': True, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsNone(github_project) def test_make_organization(self): org_json = { 'html_url': 'https://github.com/testorg', 'name': 'Test Org', 'email': '*****@*****.**', 'login': '******', 'avatar_url': 'https://images.github.com/foobar', } org = self.service.create_organization(org_json) self.assertIsInstance(org, RemoteOrganization) self.assertEqual(org.slug, 'testorg') self.assertEqual(org.name, 'Test Org') self.assertEqual(org.email, '*****@*****.**') self.assertEqual(org.avatar_url, 'https://images.github.com/foobar') self.assertEqual(org.url, 'https://github.com/testorg') def test_import_with_no_token(self): """User without a GitHub SocialToken does not return a service.""" services = GitHubService.for_user(self.user) self.assertEqual(services, []) def test_multiple_users_same_repo(self): repo_json = { 'name': '', 'full_name': 'testrepo/multiple', 'description': '', 'git_url': '', 'private': False, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_4 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_6 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
class GitHubOAuthTests(TestCase): fixtures = ['eric', 'test_data'] def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = self.project.version_privacy_level self.service = GitHubService(user=self.user, account=None) self.external_version = get(Version, project=self.project, type=EXTERNAL) self.external_build = get( Build, project=self.project, version=self.external_version ) def test_make_project_pass(self): repo_json = { 'name': 'testrepo', 'full_name': 'testuser/testrepo', 'description': 'Test Repo', 'git_url': 'git://github.com/testuser/testrepo.git', 'private': False, 'ssh_url': 'ssh://[email protected]:testuser/testrepo.git', 'html_url': 'https://github.com/testuser/testrepo', 'clone_url': 'https://github.com/testuser/testrepo.git', } repo = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(repo, RemoteRepository) self.assertEqual(repo.name, 'testrepo') self.assertEqual(repo.full_name, 'testuser/testrepo') self.assertEqual(repo.description, 'Test Repo') self.assertEqual( repo.avatar_url, settings.OAUTH_AVATAR_USER_DEFAULT_URL, ) self.assertIn(self.user, repo.users.all()) self.assertEqual(repo.organization, self.org) self.assertEqual( repo.clone_url, 'https://github.com/testuser/testrepo.git', ) self.assertEqual( repo.ssh_url, 'ssh://[email protected]:testuser/testrepo.git', ) self.assertEqual(repo.html_url, 'https://github.com/testuser/testrepo') def test_make_project_fail(self): repo_json = { 'name': '', 'full_name': '', 'description': '', 'git_url': '', 'private': True, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsNone(github_project) def test_make_organization(self): org_json = { 'html_url': 'https://github.com/testorg', 'name': 'Test Org', 'email': '*****@*****.**', 'login': '******', 'avatar_url': 'https://images.github.com/foobar', } org = self.service.create_organization(org_json) self.assertIsInstance(org, RemoteOrganization) self.assertEqual(org.slug, 'testorg') self.assertEqual(org.name, 'Test Org') self.assertEqual(org.email, '*****@*****.**') self.assertEqual(org.avatar_url, 'https://images.github.com/foobar') self.assertEqual(org.url, 'https://github.com/testorg') def test_import_with_no_token(self): """User without a GitHub SocialToken does not return a service.""" services = GitHubService.for_user(self.user) self.assertEqual(services, []) def test_multiple_users_same_repo(self): repo_json = { 'name': '', 'full_name': 'testrepo/multiple', 'description': '', 'git_url': '', 'private': False, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) github_project_4 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) github_project_6 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_send_build_status_successful(self, session, mock_logger): session().post.return_value.status_code = 201 success = self.service.send_build_status( self.external_build, BUILD_STATUS_SUCCESS ) self.assertTrue(success) mock_logger.info.assert_called_with( "GitHub commit status created for project: %s, commit status: %s", self.project, BUILD_STATUS_SUCCESS ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_send_build_status_404_error(self, session, mock_logger): session().post.return_value.status_code = 404 success = self.service.send_build_status( self.external_build, BUILD_STATUS_SUCCESS ) self.assertFalse(success) mock_logger.info.assert_called_with( 'GitHub project does not exist or user does not have ' 'permissions: project=%s', self.project ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_send_build_status_value_error(self, session, mock_logger): session().post.side_effect = ValueError success = self.service.send_build_status( self.external_build, BUILD_STATUS_SUCCESS ) self.assertFalse(success) mock_logger.exception.assert_called_with( 'GitHub commit status creation failed for project: %s', self.project, ) @override_settings(DEFAULT_PRIVACY_LEVEL='private') def test_make_private_project(self): """ Test ability to import ``public`` repositories under ``private`` level. """ repo_json = { 'name': 'testrepo', 'full_name': 'testuser/testrepo', 'description': 'Test Repo', 'git_url': 'git://github.com/testuser/testrepo.git', 'private': False, 'ssh_url': 'ssh://[email protected]:testuser/testrepo.git', 'html_url': 'https://github.com/testuser/testrepo', 'clone_url': 'https://github.com/testuser/testrepo.git', } repo = self.service.create_repository(repo_json, organization=self.org) self.assertIsNotNone(repo)
def test_import_with_no_token(self): '''User without a GitHub SocialToken does not return a service''' services = GitHubService.for_user(self.user) self.assertEqual(services, [])
def test_import_with_no_token(self): """User without a GitHub SocialToken does not return a service.""" services = GitHubService.for_user(get(User)) self.assertEqual(services, [])
class GitHubOAuthTests(TestCase): fixtures = ["eric", "test_data"] def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = self.project.version_privacy_level self.service = GitHubService(user=self.user, account=None) def test_make_project_pass(self): repo_json = { "name": "testrepo", "full_name": "testuser/testrepo", "description": "Test Repo", "git_url": "git://github.com/testuser/testrepo.git", "private": False, "ssh_url": "ssh://[email protected]:testuser/testrepo.git", "html_url": "https://github.com/testuser/testrepo", "clone_url": "https://github.com/testuser/testrepo.git", } repo = self.service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(repo, RemoteRepository) self.assertEqual(repo.name, 'testrepo') self.assertEqual(repo.full_name, 'testuser/testrepo') self.assertEqual(repo.description, 'Test Repo') self.assertIsNone(repo.avatar_url) self.assertIn(self.user, repo.users.all()) self.assertEqual(repo.organization, self.org) self.assertEqual(repo.clone_url, 'https://github.com/testuser/testrepo.git') self.assertEqual(repo.ssh_url, 'ssh://[email protected]:testuser/testrepo.git') self.assertEqual(repo.html_url, 'https://github.com/testuser/testrepo') def test_make_project_fail(self): repo_json = { "name": "", "full_name": "", "description": "", "git_url": "", "private": True, "ssh_url": "", "html_url": "", "clone_url": "", } github_project = self.service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsNone(github_project) def test_make_organization(self): org_json = { "html_url": "https://github.com/testorg", "name": "Test Org", "email": "*****@*****.**", "login": "******", "avatar_url": "https://images.github.com/foobar", } org = self.service.create_organization(org_json) self.assertIsInstance(org, RemoteOrganization) self.assertEqual(org.slug, 'testorg') self.assertEqual(org.name, 'Test Org') self.assertEqual(org.email, '*****@*****.**') self.assertEqual(org.avatar_url, 'https://images.github.com/foobar') self.assertEqual(org.url, 'https://github.com/testorg') def test_import_with_no_token(self): '''User without a GitHub SocialToken does not return a service''' services = GitHubService.for_user(self.user) self.assertEqual(services, []) def test_multiple_users_same_repo(self): repo_json = { "name": "", "full_name": "testrepo/multiple", "description": "", "git_url": "", "private": False, "ssh_url": "", "html_url": "", "clone_url": "", } github_project = self.service.create_repository(repo_json, organization=self.org, privacy=self.privacy) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_4 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy) github_project_6 = service.create_repository(repo_json, organization=self.org, privacy=self.privacy) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
class GitHubOAuthTests(TestCase): fixtures = ['eric', 'test_data'] def setUp(self): self.client.login(username='******', password='******') self.user = User.objects.get(pk=1) self.project = Project.objects.get(slug='pip') self.org = RemoteOrganization.objects.create(slug='rtfd', json='') self.privacy = self.project.version_privacy_level self.service = GitHubService(user=self.user, account=None) self.external_version = get(Version, project=self.project, type=EXTERNAL) self.external_build = get(Build, project=self.project, version=self.external_version) self.integration = get(GitHubWebhook, project=self.project, provider_data={'url': 'https://github.com/'}) self.provider_data = [{ "config": { "url": "https://example.com/webhook" }, "url": "https://api.github.com/repos/test/Hello-World/hooks/12345678", }] def test_make_project_pass(self): repo_json = { 'name': 'testrepo', 'full_name': 'testuser/testrepo', 'description': 'Test Repo', 'git_url': 'git://github.com/testuser/testrepo.git', 'private': False, 'ssh_url': 'ssh://[email protected]:testuser/testrepo.git', 'html_url': 'https://github.com/testuser/testrepo', 'clone_url': 'https://github.com/testuser/testrepo.git', } repo = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(repo, RemoteRepository) self.assertEqual(repo.name, 'testrepo') self.assertEqual(repo.full_name, 'testuser/testrepo') self.assertEqual(repo.description, 'Test Repo') self.assertEqual( repo.avatar_url, settings.OAUTH_AVATAR_USER_DEFAULT_URL, ) self.assertIn(self.user, repo.users.all()) self.assertEqual(repo.organization, self.org) self.assertEqual( repo.clone_url, 'https://github.com/testuser/testrepo.git', ) self.assertEqual( repo.ssh_url, 'ssh://[email protected]:testuser/testrepo.git', ) self.assertEqual(repo.html_url, 'https://github.com/testuser/testrepo') def test_make_project_fail(self): repo_json = { 'name': '', 'full_name': '', 'description': '', 'git_url': '', 'private': True, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsNone(github_project) def test_make_organization(self): org_json = { 'html_url': 'https://github.com/testorg', 'name': 'Test Org', 'email': '*****@*****.**', 'login': '******', 'avatar_url': 'https://images.github.com/foobar', } org = self.service.create_organization(org_json) self.assertIsInstance(org, RemoteOrganization) self.assertEqual(org.slug, 'testorg') self.assertEqual(org.name, 'Test Org') self.assertEqual(org.email, '*****@*****.**') self.assertEqual(org.avatar_url, 'https://images.github.com/foobar') self.assertEqual(org.url, 'https://github.com/testorg') def test_import_with_no_token(self): """User without a GitHub SocialToken does not return a service.""" services = GitHubService.for_user(self.user) self.assertEqual(services, []) def test_multiple_users_same_repo(self): repo_json = { 'name': '', 'full_name': 'testrepo/multiple', 'description': '', 'git_url': '', 'private': False, 'ssh_url': '', 'html_url': '', 'clone_url': '', } github_project = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) user2 = User.objects.get(pk=2) service = GitHubService(user=user2, account=None) github_project_2 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(github_project, RemoteRepository) self.assertIsInstance(github_project_2, RemoteRepository) self.assertNotEqual(github_project_2, github_project) github_project_3 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) github_project_4 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertIsInstance(github_project_3, RemoteRepository) self.assertIsInstance(github_project_4, RemoteRepository) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = self.service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) github_project_6 = service.create_repository( repo_json, organization=self.org, privacy=self.privacy, ) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_send_build_status_successful(self, session, mock_logger): session().post.return_value.status_code = 201 success = self.service.send_build_status(self.external_build, self.external_build.commit, BUILD_STATUS_SUCCESS) self.assertTrue(success) mock_logger.info.assert_called_with( "GitHub commit status created for project: %s, commit status: %s", self.project, BUILD_STATUS_SUCCESS) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_send_build_status_404_error(self, session, mock_logger): session().post.return_value.status_code = 404 success = self.service.send_build_status(self.external_build, self.external_build.commit, BUILD_STATUS_SUCCESS) self.assertFalse(success) mock_logger.info.assert_called_with( 'GitHub project does not exist or user does not have ' 'permissions: project=%s, user=%s, status=%s, url=%s', self.project, self.user, 404, 'https://api.github.com/repos/pypa/pip/statuses/297') @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_send_build_status_value_error(self, session, mock_logger): session().post.side_effect = ValueError success = self.service.send_build_status(self.external_build, self.external_build.commit, BUILD_STATUS_SUCCESS) self.assertFalse(success) mock_logger.exception.assert_called_with( 'GitHub commit status creation failed for project: %s', self.project, ) @override_settings(DEFAULT_PRIVACY_LEVEL='private') def test_make_private_project(self): """ Test ability to import ``public`` repositories under ``private`` level. """ repo_json = { 'name': 'testrepo', 'full_name': 'testuser/testrepo', 'description': 'Test Repo', 'git_url': 'git://github.com/testuser/testrepo.git', 'private': False, 'ssh_url': 'ssh://[email protected]:testuser/testrepo.git', 'html_url': 'https://github.com/testuser/testrepo', 'clone_url': 'https://github.com/testuser/testrepo.git', } repo = self.service.create_repository(repo_json, organization=self.org) self.assertIsNotNone(repo) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_setup_webhook_successful(self, session, mock_logger): session().post.return_value.status_code = 201 session().post.return_value.json.return_value = {} success, _ = self.service.setup_webhook(self.project, self.integration) self.integration.refresh_from_db() self.assertTrue(success) self.assertIsNotNone(self.integration.secret) mock_logger.info.assert_called_with( "GitHub webhook creation successful for project: %s", self.project, ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_setup_webhook_404_error(self, session, mock_logger): session().post.return_value.status_code = 404 success, _ = self.service.setup_webhook(self.project, self.integration) self.integration.refresh_from_db() self.assertFalse(success) self.assertIsNone(self.integration.secret) mock_logger.info.assert_called_with( 'GitHub project does not exist or user does not have ' 'permissions: project=%s', self.project, ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_setup_webhook_value_error(self, session, mock_logger): session().post.side_effect = ValueError success = self.service.setup_webhook(self.project, self.integration) self.integration.refresh_from_db() self.assertIsNone(self.integration.secret) mock_logger.exception.assert_called_with( 'GitHub webhook creation failed for project: %s', self.project, ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_update_webhook_successful(self, session, mock_logger): session().patch.return_value.status_code = 201 session().patch.return_value.json.return_value = {} success, _ = self.service.update_webhook(self.project, self.integration) self.integration.refresh_from_db() self.assertTrue(success) self.assertIsNotNone(self.integration.secret) mock_logger.info.assert_called_with( "GitHub webhook update successful for project: %s", self.project, ) @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') @mock.patch('readthedocs.oauth.services.github.GitHubService.setup_webhook' ) def test_update_webhook_404_error(self, setup_webhook, session): session().patch.return_value.status_code = 404 self.service.update_webhook(self.project, self.integration) setup_webhook.assert_called_once_with(self.project, self.integration) @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') @mock.patch('readthedocs.oauth.services.github.GitHubService.setup_webhook' ) def test_update_webhook_no_provider_data(self, setup_webhook, session): self.integration.provider_data = None self.integration.save() session().patch.side_effect = AttributeError self.service.update_webhook(self.project, self.integration) setup_webhook.assert_called_once_with(self.project, self.integration) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_update_webhook_value_error(self, session, mock_logger): session().patch.side_effect = ValueError self.service.update_webhook(self.project, self.integration) self.integration.refresh_from_db() self.assertIsNone(self.integration.secret) mock_logger.exception.assert_called_with( 'GitHub webhook update failed for project: %s', self.project, ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_get_provider_data_successful(self, session, mock_logger): self.integration.provider_data = {} self.integration.save() webhook_data = self.provider_data rtd_webhook_url = 'https://{domain}{path}'.format( domain=settings.PRODUCTION_DOMAIN, path=reverse( 'api_webhook', kwargs={ 'project_slug': self.project.slug, 'integration_pk': self.integration.pk, }, )) webhook_data[0]["config"]["url"] = rtd_webhook_url session().get.return_value.status_code = 200 session().get.return_value.json.return_value = webhook_data self.service.get_provider_data(self.project, self.integration) self.integration.refresh_from_db() self.assertEqual(self.integration.provider_data, webhook_data[0]) mock_logger.info.assert_called_with( 'GitHub integration updated with provider data for project: %s', self.project, ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_get_provider_data_404_error(self, session, mock_logger): self.integration.provider_data = {} self.integration.save() session().get.return_value.status_code = 404 self.service.get_provider_data(self.project, self.integration) self.integration.refresh_from_db() self.assertEqual(self.integration.provider_data, {}) mock_logger.info.assert_called_with( 'GitHub project does not exist or user does not have ' 'permissions: project=%s', self.project, ) @mock.patch('readthedocs.oauth.services.github.log') @mock.patch('readthedocs.oauth.services.github.GitHubService.get_session') def test_get_provider_data_attribute_error(self, session, mock_logger): self.integration.provider_data = {} self.integration.save() session().get.side_effect = AttributeError self.service.get_provider_data(self.project, self.integration) self.integration.refresh_from_db() self.assertEqual(self.integration.provider_data, {}) mock_logger.exception.assert_called_with( 'GitHub webhook Listing failed for project: %s', self.project, )
def test_import_with_no_token(self): '''User without a GitHub SocialToken does not return a service''' service = GitHubService.for_user(self.user) self.assertIsNone(service)