Ejemplo n.º 1
0
 def setUp(self):
     self.client.login(username='******', password='******')
     self.user = User.objects.get(pk=1)
     self.project = Project.objects.get(slug='pip')
     self.project.repo = 'https://bitbucket.org/testuser/testrepo/'
     self.project.save()
     self.org = RemoteOrganization.objects.create(slug='rtfd', json='')
     self.privacy = self.project.version_privacy_level
     self.service = BitbucketService(user=self.user, account=None)
     self.integration = get(GitHubWebhook,
                            project=self.project,
                            provider_data={
                                'links': {
                                    'self': {
                                        'href': 'https://bitbucket.org/'
                                    }
                                }
                            })
     self.provider_data = {
         'values': [
             {
                 'links': {
                     'self': {
                         'href': 'https://bitbucket.org/'
                     }
                 },
                 'url':
                 'https://readthedocs.io/api/v2/webhook/test/99999999/',
             },
         ]
     }
Ejemplo n.º 2
0
 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 = BitbucketService(user=self.user, account=None)
Ejemplo n.º 3
0
 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 = BitbucketService(user=self.user, account=None)
Ejemplo n.º 4
0
 def test_import_with_no_token(self):
     """User without a Bitbucket SocialToken does not return a service."""
     services = BitbucketService.for_user(self.user)
     self.assertEqual(services, [])
Ejemplo n.º 5
0
class BitbucketOAuthTests(TestCase):

    fixtures = ['eric', 'test_data']
    repo_response_data = {
        'scm': 'hg',
        'has_wiki': True,
        'description': 'Site for tutorial101 files',
        'links': {
            'watchers': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/watchers',
            },
            'commits': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/commits',
            },
            'self': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org',
            },
            'html': {
                'href': 'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
            },
            'avatar': {
                'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png',
            },
            'forks': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/forks',
            },
            'clone': [
                {
                    'href': 'https://[email protected]/tutorials/tutorials.bitbucket.org',
                    'name': 'https',
                },
                {
                    'href': 'ssh://[email protected]/tutorials/tutorials.bitbucket.org',
                    'name': 'ssh',
                },
            ],
            'pullrequests': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/pullrequests',
            },
        },
        'fork_policy': 'allow_forks',
        'name': 'tutorials.bitbucket.org',
        'language': 'html/css',
        'created_on': '2011-12-20T16:35:06.480042+00:00',
        'full_name': 'tutorials/tutorials.bitbucket.org',
        'has_issues': True,
        'owner': {
            'username': '******',
            'display_name': 'tutorials account',
            'uuid': '{c788b2da-b7a2-404c-9e26-d3f077557007}',
            'links': {
                'self': {
                    'href': 'https://api.bitbucket.org/2.0/users/tutorials',
                },
                'html': {
                    'href': 'https://bitbucket.org/tutorials',
                },
                'avatar': {
                    'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png',
                },
            },
        },
        'updated_on': '2014-11-03T02:24:08.409995+00:00',
        'size': 76182262,
        'is_private': False,
        'uuid': '{9970a9b6-2d86-413f-8555-da8e1ac0e542}',
    }

    team_response_data = {
        'username': '******',
        'website': None,
        'display_name': 'Teams In Space',
        'uuid': '{61fc5cf6-d054-47d2-b4a9-061ccf858379}',
        'links': {
            'self': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace',
            },
            'repositories': {
                'href': 'https://api.bitbucket.org/2.0/repositories/teamsinspace',
            },
            'html': {'href': 'https://bitbucket.org/teamsinspace'},
            'followers': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/followers',
            },
            'avatar': {
                'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/teamsinspace-avatar-3731530358-7_avatar.png',
            },
            'members': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/members',
            },
            'following': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/following',
            },
        },
        'created_on': '2014-04-08T00:00:14.070969+00:00',
        'location': None,
        'type': 'team',
    }

    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 = BitbucketService(user=self.user, account=None)

    def test_make_project_pass(self):
        repo = self.service.create_repository(
            self.repo_response_data, organization=self.org,
            privacy=self.privacy)
        self.assertIsInstance(repo, RemoteRepository)
        self.assertEqual(repo.name, 'tutorials.bitbucket.org')
        self.assertEqual(repo.full_name, 'tutorials/tutorials.bitbucket.org')
        self.assertEqual(repo.description, 'Site for tutorial101 files')
        self.assertEqual(
            repo.avatar_url, (
                'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/'
                'tutorials.bitbucket.org-logo-1456883302-9_avatar.png'))
        self.assertIn(self.user, repo.users.all())
        self.assertEqual(repo.organization, self.org)
        self.assertEqual(
            repo.clone_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org')
        self.assertEqual(
            repo.ssh_url,
            'ssh://[email protected]/tutorials/tutorials.bitbucket.org')
        self.assertEqual(
            repo.html_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org')

    def test_make_project_fail(self):
        data = self.repo_response_data.copy()
        data['is_private'] = True
        repo = self.service.create_repository(
            data, organization=self.org, privacy=self.privacy)
        self.assertIsNone(repo)

    def test_make_organization(self):
        org = self.service.create_organization(self.team_response_data)
        self.assertIsInstance(org, RemoteOrganization)
        self.assertEqual(org.slug, 'teamsinspace')
        self.assertEqual(org.name, 'Teams In Space')
        self.assertEqual(
            org.avatar_url, (
                'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/'
                'teamsinspace-avatar-3731530358-7_avatar.png'))
        self.assertEqual(org.url, 'https://bitbucket.org/teamsinspace')

    def test_import_with_no_token(self):
        """User without a Bitbucket SocialToken does not return a service."""
        services = BitbucketService.for_user(self.user)
        self.assertEqual(services, [])
Ejemplo n.º 6
0
 def test_import_with_no_token(self):
     '''User without a Bitbucket SocialToken does not return a service'''
     services = BitbucketService.for_user(self.user)
     self.assertEqual(services, [])
Ejemplo n.º 7
0
class BitbucketOAuthTests(TestCase):

    fixtures = ["eric", "test_data"]
    repo_response_data = {
        "scm": "hg",
        "has_wiki": True,
        "description": "Site for tutorial101 files",
        "links": {
            "watchers": {
                "href":
                "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/watchers"
            },
            "commits": {
                "href":
                "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/commits"
            },
            "self": {
                "href":
                "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org"
            },
            "html": {
                "href":
                "https://bitbucket.org/tutorials/tutorials.bitbucket.org"
            },
            "avatar": {
                "href":
                "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png"
            },
            "forks": {
                "href":
                "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/forks"
            },
            "clone": [{
                "href":
                "https://[email protected]/tutorials/tutorials.bitbucket.org",
                "name": "https"
            }, {
                "href":
                "ssh://[email protected]/tutorials/tutorials.bitbucket.org",
                "name": "ssh"
            }],
            "pullrequests": {
                "href":
                "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/pullrequests"
            }
        },
        "fork_policy": "allow_forks",
        "name": "tutorials.bitbucket.org",
        "language": "html/css",
        "created_on": "2011-12-20T16:35:06.480042+00:00",
        "full_name": "tutorials/tutorials.bitbucket.org",
        "has_issues": True,
        "owner": {
            "username": "******",
            "display_name": "tutorials account",
            "uuid": "{c788b2da-b7a2-404c-9e26-d3f077557007}",
            "links": {
                "self": {
                    "href": "https://api.bitbucket.org/2.0/users/tutorials"
                },
                "html": {
                    "href": "https://bitbucket.org/tutorials"
                },
                "avatar": {
                    "href":
                    "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png"
                }
            }
        },
        "updated_on": "2014-11-03T02:24:08.409995+00:00",
        "size": 76182262,
        "is_private": False,
        "uuid": "{9970a9b6-2d86-413f-8555-da8e1ac0e542}"
    }

    team_response_data = {
        "username": "******",
        "website": None,
        "display_name": "Teams In Space",
        "uuid": "{61fc5cf6-d054-47d2-b4a9-061ccf858379}",
        "links": {
            "self": {
                "href": "https://api.bitbucket.org/2.0/teams/teamsinspace"
            },
            "repositories": {
                "href":
                "https://api.bitbucket.org/2.0/repositories/teamsinspace"
            },
            "html": {
                "href": "https://bitbucket.org/teamsinspace"
            },
            "followers": {
                "href":
                "https://api.bitbucket.org/2.0/teams/teamsinspace/followers"
            },
            "avatar": {
                "href":
                "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/teamsinspace-avatar-3731530358-7_avatar.png"
            },
            "members": {
                "href":
                "https://api.bitbucket.org/2.0/teams/teamsinspace/members"
            },
            "following": {
                "href":
                "https://api.bitbucket.org/2.0/teams/teamsinspace/following"
            }
        },
        "created_on": "2014-04-08T00:00:14.070969+00:00",
        "location": None,
        "type": "team"
    }

    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 = BitbucketService(user=self.user, account=None)

    def test_make_project_pass(self):
        repo = self.service.create_repository(self.repo_response_data,
                                              organization=self.org,
                                              privacy=self.privacy)
        self.assertIsInstance(repo, RemoteRepository)
        self.assertEqual(repo.name, 'tutorials.bitbucket.org')
        self.assertEqual(repo.full_name, 'tutorials/tutorials.bitbucket.org')
        self.assertEqual(repo.description, 'Site for tutorial101 files')
        self.assertEqual(repo.avatar_url, (
            'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/'
            'tutorials.bitbucket.org-logo-1456883302-9_avatar.png'))
        self.assertIn(self.user, repo.users.all())
        self.assertEqual(repo.organization, self.org)
        self.assertEqual(
            repo.clone_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org')
        self.assertEqual(
            repo.ssh_url,
            'ssh://[email protected]/tutorials/tutorials.bitbucket.org')
        self.assertEqual(
            repo.html_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org')

    def test_make_project_fail(self):
        data = self.repo_response_data.copy()
        data['is_private'] = True
        repo = self.service.create_repository(data,
                                              organization=self.org,
                                              privacy=self.privacy)
        self.assertIsNone(repo)

    def test_make_organization(self):
        org = self.service.create_organization(self.team_response_data)
        self.assertIsInstance(org, RemoteOrganization)
        self.assertEqual(org.slug, 'teamsinspace')
        self.assertEqual(org.name, 'Teams In Space')
        self.assertEqual(org.avatar_url, (
            'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/'
            'teamsinspace-avatar-3731530358-7_avatar.png'))
        self.assertEqual(org.url, 'https://bitbucket.org/teamsinspace')

    def test_import_with_no_token(self):
        '''User without a Bitbucket SocialToken does not return a service'''
        services = BitbucketService.for_user(self.user)
        self.assertEqual(services, [])
Ejemplo n.º 8
0
class BitbucketOAuthTests(TestCase):

    fixtures = ['eric', 'test_data']
    repo_response_data = {
        'scm': 'hg',
        'has_wiki': True,
        'description': 'Site for tutorial101 files',
        'links': {
            'watchers': {
                'href':
                'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/watchers',
            },
            'commits': {
                'href':
                'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/commits',
            },
            'self': {
                'href':
                'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org',
            },
            'html': {
                'href':
                'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
            },
            'avatar': {
                'href':
                'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png',
            },
            'forks': {
                'href':
                'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/forks',
            },
            'clone': [
                {
                    'href':
                    'https://[email protected]/tutorials/tutorials.bitbucket.org',
                    'name': 'https',
                },
                {
                    'href':
                    'ssh://[email protected]/tutorials/tutorials.bitbucket.org',
                    'name': 'ssh',
                },
            ],
            'pullrequests': {
                'href':
                'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/pullrequests',
            },
        },
        'fork_policy': 'allow_forks',
        'name': 'tutorials.bitbucket.org',
        'language': 'html/css',
        'created_on': '2011-12-20T16:35:06.480042+00:00',
        'full_name': 'tutorials/tutorials.bitbucket.org',
        'has_issues': True,
        'owner': {
            'username': '******',
            'display_name': 'tutorials account',
            'uuid': '{c788b2da-b7a2-404c-9e26-d3f077557007}',
            'links': {
                'self': {
                    'href': 'https://api.bitbucket.org/2.0/users/tutorials',
                },
                'html': {
                    'href': 'https://bitbucket.org/tutorials',
                },
                'avatar': {
                    'href':
                    'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png',
                },
            },
        },
        'updated_on': '2014-11-03T02:24:08.409995+00:00',
        'size': 76182262,
        'is_private': False,
        'uuid': '{9970a9b6-2d86-413f-8555-da8e1ac0e542}',
    }

    team_response_data = {
        'username': '******',
        'website': None,
        'display_name': 'Teams In Space',
        'uuid': '{61fc5cf6-d054-47d2-b4a9-061ccf858379}',
        'links': {
            'self': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace',
            },
            'repositories': {
                'href':
                'https://api.bitbucket.org/2.0/repositories/teamsinspace',
            },
            'html': {
                'href': 'https://bitbucket.org/teamsinspace'
            },
            'followers': {
                'href':
                'https://api.bitbucket.org/2.0/teams/teamsinspace/followers',
            },
            'avatar': {
                'href':
                'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/teamsinspace-avatar-3731530358-7_avatar.png',
            },
            'members': {
                'href':
                'https://api.bitbucket.org/2.0/teams/teamsinspace/members',
            },
            'following': {
                'href':
                'https://api.bitbucket.org/2.0/teams/teamsinspace/following',
            },
        },
        'created_on': '2014-04-08T00:00:14.070969+00:00',
        'location': None,
        'type': 'team',
    }

    def setUp(self):
        self.client.login(username='******', password='******')
        self.user = User.objects.get(pk=1)
        self.project = Project.objects.get(slug='pip')
        self.project.repo = 'https://bitbucket.org/testuser/testrepo/'
        self.project.save()
        self.org = RemoteOrganization.objects.create(slug='rtfd', json='')
        self.privacy = self.project.version_privacy_level
        self.service = BitbucketService(user=self.user, account=None)
        self.integration = get(GitHubWebhook,
                               project=self.project,
                               provider_data={
                                   'links': {
                                       'self': {
                                           'href': 'https://bitbucket.org/'
                                       }
                                   }
                               })
        self.provider_data = {
            'values': [
                {
                    'links': {
                        'self': {
                            'href': 'https://bitbucket.org/'
                        }
                    },
                    'url':
                    'https://readthedocs.io/api/v2/webhook/test/99999999/',
                },
            ]
        }

    def test_make_project_pass(self):
        repo = self.service.create_repository(
            self.repo_response_data,
            organization=self.org,
            privacy=self.privacy,
        )
        self.assertIsInstance(repo, RemoteRepository)
        self.assertEqual(repo.name, 'tutorials.bitbucket.org')
        self.assertEqual(repo.full_name, 'tutorials/tutorials.bitbucket.org')
        self.assertEqual(repo.description, 'Site for tutorial101 files')
        self.assertEqual(
            repo.avatar_url,
            ('https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/'
             'tutorials.bitbucket.org-logo-1456883302-9_avatar.png'),
        )
        self.assertIn(self.user, repo.users.all())
        self.assertEqual(repo.organization, self.org)
        self.assertEqual(
            repo.clone_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
        )
        self.assertEqual(
            repo.ssh_url,
            'ssh://[email protected]/tutorials/tutorials.bitbucket.org',
        )
        self.assertEqual(
            repo.html_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
        )

    def test_make_project_fail(self):
        data = self.repo_response_data.copy()
        data['is_private'] = True
        repo = self.service.create_repository(
            data,
            organization=self.org,
            privacy=self.privacy,
        )
        self.assertIsNone(repo)

    @override_settings(DEFAULT_PRIVACY_LEVEL='private')
    def test_make_private_project(self):
        """
        Test ability to import ``public`` repositories under ``private`` level.
        """
        data = self.repo_response_data.copy()
        data['is_private'] = False
        repo = self.service.create_repository(data, organization=self.org)
        self.assertIsNotNone(repo)

    def test_make_organization(self):
        org = self.service.create_organization(self.team_response_data)
        self.assertIsInstance(org, RemoteOrganization)
        self.assertEqual(org.slug, 'teamsinspace')
        self.assertEqual(org.name, 'Teams In Space')
        self.assertEqual(
            org.avatar_url,
            ('https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/'
             'teamsinspace-avatar-3731530358-7_avatar.png'),
        )
        self.assertEqual(org.url, 'https://bitbucket.org/teamsinspace')

    def test_import_with_no_token(self):
        """User without a Bitbucket SocialToken does not return a service."""
        services = BitbucketService.for_user(self.user)
        self.assertEqual(services, [])

    @mock.patch('readthedocs.oauth.services.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.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.assertTrue(success)
        mock_logger.info.assert_called_with(
            "Bitbucket webhook creation successful for project: %s",
            self.project,
        )

    @mock.patch('readthedocs.oauth.services.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.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.assertFalse(success)
        mock_logger.info.assert_called_with(
            'Bitbucket project does not exist or user does not have '
            'permissions: project=%s',
            self.project,
        )

    @mock.patch('readthedocs.oauth.services.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.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)

        mock_logger.exception.assert_called_with(
            'Bitbucket webhook creation failed for project: %s',
            self.project,
        )

    @mock.patch('readthedocs.oauth.services.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.get_session')
    def test_update_webhook_successful(self, session, mock_logger):
        session().put.return_value.status_code = 200
        session().put.return_value.json.return_value = {}
        success, _ = self.service.update_webhook(self.project,
                                                 self.integration)

        self.assertTrue(success)
        self.assertIsNotNone(self.integration.secret)
        mock_logger.info.assert_called_with(
            "Bitbucket webhook update successful for project: %s",
            self.project,
        )

    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.get_session')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.setup_webhook')
    def test_update_webhook_404_error(self, setup_webhook, session):
        session().put.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.bitbucket.BitbucketService.get_session')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.setup_webhook')
    def test_update_webhook_no_provider_data(self, setup_webhook, session):
        self.integration.provider_data = None
        self.integration.save()

        session().put.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.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.get_session')
    def test_update_webhook_value_error(self, session, mock_logger):
        session().put.side_effect = ValueError
        self.service.update_webhook(self.project, self.integration)

        mock_logger.exception.assert_called_with(
            'Bitbucket webhook update failed for project: %s',
            self.project,
        )

    @mock.patch('readthedocs.oauth.services.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.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['values'][0]["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['values'][0])
        mock_logger.info.assert_called_with(
            'Bitbucket integration updated with provider data for project: %s',
            self.project,
        )

    @mock.patch('readthedocs.oauth.services.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.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(
            'Bitbucket project does not exist or user does not have '
            'permissions: project=%s',
            self.project,
        )

    @mock.patch('readthedocs.oauth.services.bitbucket.log')
    @mock.patch(
        'readthedocs.oauth.services.bitbucket.BitbucketService.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(
            'Bitbucket webhook Listing failed for project: %s',
            self.project,
        )
Ejemplo n.º 9
0
 def test_import_with_no_token(self):
     '''User without a Bitbucket SocialToken does not return a service'''
     service = BitbucketService.for_user(self.user)
     self.assertIsNone(service)
Ejemplo n.º 10
0
class BitbucketOAuthTests(TestCase):

    fixtures = ["eric", "test_data"]
    repo_response_data = {
        "scm": "hg",
        "has_wiki": True,
        "description": "Site for tutorial101 files",
        "links": {
            "watchers": {
                "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/watchers"
            },
            "commits": {
                "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/commits"
            },
            "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org"
            },
            "html": {
                "href": "https://bitbucket.org/tutorials/tutorials.bitbucket.org"
            },
            "avatar": {
                "href": "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png"
            },
            "forks": {
                "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/forks"
            },
            "clone": [{
                "href": "https://[email protected]/tutorials/tutorials.bitbucket.org",
                "name": "https"
            }, {
                "href": "ssh://[email protected]/tutorials/tutorials.bitbucket.org",
                "name": "ssh"
            }],
            "pullrequests": {
                "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/pullrequests"
            }
        },
        "fork_policy": "allow_forks",
        "name": "tutorials.bitbucket.org",
        "language": "html/css",
        "created_on": "2011-12-20T16:35:06.480042+00:00",
        "full_name": "tutorials/tutorials.bitbucket.org",
        "has_issues": True,
        "owner": {
            "username": "******",
            "display_name": "tutorials account",
            "uuid": "{c788b2da-b7a2-404c-9e26-d3f077557007}",
            "links": {
                "self": {
                    "href": "https://api.bitbucket.org/2.0/users/tutorials"
                },
                "html": {
                    "href": "https://bitbucket.org/tutorials"
                },
                "avatar": {
                    "href": "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png"
                }
            }
        },
        "updated_on": "2014-11-03T02:24:08.409995+00:00",
        "size": 76182262,
        "is_private": False,
        "uuid": "{9970a9b6-2d86-413f-8555-da8e1ac0e542}"
    }

    team_response_data = {
        "username": "******",
        "website": None,
        "display_name": "Teams In Space",
        "uuid": "{61fc5cf6-d054-47d2-b4a9-061ccf858379}",
        "links": {
            "self": {
                "href": "https://api.bitbucket.org/2.0/teams/teamsinspace"
            },
            "repositories": {
                "href": "https://api.bitbucket.org/2.0/repositories/teamsinspace"
            },
            "html": {
                "href": "https://bitbucket.org/teamsinspace"
            },
            "followers": {
                "href": "https://api.bitbucket.org/2.0/teams/teamsinspace/followers"
            },
            "avatar": {
                "href": "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/teamsinspace-avatar-3731530358-7_avatar.png"
            },
            "members": {
                "href": "https://api.bitbucket.org/2.0/teams/teamsinspace/members"
            },
            "following": {
                "href": "https://api.bitbucket.org/2.0/teams/teamsinspace/following"
            }
        },
        "created_on": "2014-04-08T00:00:14.070969+00:00",
        "location": None,
        "type": "team"
    }

    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 = BitbucketService(user=self.user, account=None)

    def test_make_project_pass(self):
        repo = self.service.create_repository(
            self.repo_response_data, organization=self.org, privacy=self.privacy)
        self.assertIsInstance(repo, RemoteRepository)
        self.assertEqual(repo.name, 'tutorials.bitbucket.org')
        self.assertEqual(repo.full_name, 'tutorials/tutorials.bitbucket.org')
        self.assertEqual(repo.description, 'Site for tutorial101 files')
        self.assertEqual(
            repo.avatar_url,
            ('https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/'
             'tutorials.bitbucket.org-logo-1456883302-9_avatar.png'))
        self.assertIn(self.user, repo.users.all())
        self.assertEqual(repo.organization, self.org)
        self.assertEqual(
            repo.clone_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org')
        self.assertEqual(
            repo.ssh_url,
            'ssh://[email protected]/tutorials/tutorials.bitbucket.org')
        self.assertEqual(
            repo.html_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org')

    def test_make_project_fail(self):
        data = self.repo_response_data.copy()
        data['is_private'] = True
        repo = self.service.create_repository(
            data, organization=self.org, privacy=self.privacy)
        self.assertIsNone(repo)

    def test_make_organization(self):
        org = self.service.create_organization(self.team_response_data)
        self.assertIsInstance(org, RemoteOrganization)
        self.assertEqual(org.slug, 'teamsinspace')
        self.assertEqual(org.name, 'Teams In Space')
        self.assertEqual(
            org.avatar_url,
            ('https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/'
             'teamsinspace-avatar-3731530358-7_avatar.png'))
        self.assertEqual(org.url, 'https://bitbucket.org/teamsinspace')

    def test_import_with_no_token(self):
        '''User without a Bitbucket SocialToken does not return a service'''
        service = BitbucketService.for_user(self.user)
        self.assertIsNone(service)
Ejemplo n.º 11
0
 def test_import_with_no_token(self):
     """User without a Bitbucket SocialToken does not return a service."""
     services = BitbucketService.for_user(get(User))
     self.assertEqual(services, [])
Ejemplo n.º 12
0
    def setUp(self):
        self.client.login(username='******', password='******')
        self.user = User.objects.get(pk=1)
        self.project = Project.objects.get(slug='pip')
        self.project.repo = 'https://bitbucket.org/testuser/testrepo/'
        self.project.save()
        self.org = RemoteOrganization.objects.create(slug='rtfd')
        self.privacy = settings.DEFAULT_PRIVACY_LEVEL
        self.service = BitbucketService(
            user=self.user,
            account=get(SocialAccount, user=self.user)
        )
        self.integration = get(
            GitHubWebhook,
            project=self.project,
            provider_data={
                'links': {
                    'self': {
                        'href': 'https://bitbucket.org/'
                    }
                }
            }
        )
        self.provider_data = {
            'values': [{
                'links': {
                    'self': {
                        'href': 'https://bitbucket.org/'
                    }
                },
                'url': 'https://readthedocs.io/api/v2/webhook/test/99999999/',
            },]
        }
        self.repo_response_data = {
            'scm': 'hg',
            'has_wiki': True,
            'description': 'Site for tutorial101 files',
            'links': {
                'watchers': {
                    'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/watchers',
                },
                'commits': {
                    'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/commits',
                },
                'self': {
                    'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org',
                },
                'html': {
                    'href': 'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
                },
                'avatar': {
                    'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png',
                },
                'forks': {
                    'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/forks',
                },
                'clone': [
                    {
                        'href': 'https://[email protected]/tutorials/tutorials.bitbucket.org',
                        'name': 'https',
                    },
                    {
                        'href': 'ssh://[email protected]/tutorials/tutorials.bitbucket.org',
                        'name': 'ssh',
                    },
                ],
                'pullrequests': {
                    'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/pullrequests',
                },
            },
            'fork_policy': 'allow_forks',
            'name': 'tutorials.bitbucket.org',
            'language': 'html/css',
            'created_on': '2011-12-20T16:35:06.480042+00:00',
            'full_name': 'tutorials/tutorials.bitbucket.org',
            'has_issues': True,
            'owner': {
                'username': '******',
                'display_name': 'tutorials account',
                'uuid': '{c788b2da-b7a2-404c-9e26-d3f077557007}',
                'links': {
                    'self': {
                        'href': 'https://api.bitbucket.org/2.0/users/tutorials',
                    },
                    'html': {
                        'href': 'https://bitbucket.org/tutorials',
                    },
                    'avatar': {
                        'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png',
                    },
                },
            },
            'updated_on': '2014-11-03T02:24:08.409995+00:00',
            'size': 76182262,
            'is_private': False,
            'uuid': '{9970a9b6-2d86-413f-8555-da8e1ac0e542}',
            'mainbranch': {
                'type': 'branch',
                'name': 'main',
            },
        }

        self.team_response_data = {
            'username': '******',
            'website': None,
            'display_name': 'Teams In Space',
            'uuid': '{61fc5cf6-d054-47d2-b4a9-061ccf858379}',
            'links': {
                'self': {
                    'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace',
                },
                'repositories': {
                    'href': 'https://api.bitbucket.org/2.0/repositories/teamsinspace',
                },
                'html': {'href': 'https://bitbucket.org/teamsinspace'},
                'followers': {
                    'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/followers',
                },
                'avatar': {
                    'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/teamsinspace-avatar-3731530358-7_avatar.png',
                },
                'members': {
                    'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/members',
                },
                'following': {
                    'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/following',
                },
            },
            'created_on': '2014-04-08T00:00:14.070969+00:00',
            'location': None,
            'type': 'team',
        }
Ejemplo n.º 13
0
class BitbucketOAuthTests(TestCase):

    fixtures = ['eric', 'test_data']
    repo_response_data = {
        'scm': 'hg',
        'has_wiki': True,
        'description': 'Site for tutorial101 files',
        'links': {
            'watchers': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/watchers',
            },
            'commits': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/commits',
            },
            'self': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org',
            },
            'html': {
                'href': 'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
            },
            'avatar': {
                'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png',
            },
            'forks': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/forks',
            },
            'clone': [
                {
                    'href': 'https://[email protected]/tutorials/tutorials.bitbucket.org',
                    'name': 'https',
                },
                {
                    'href': 'ssh://[email protected]/tutorials/tutorials.bitbucket.org',
                    'name': 'ssh',
                },
            ],
            'pullrequests': {
                'href': 'https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/pullrequests',
            },
        },
        'fork_policy': 'allow_forks',
        'name': 'tutorials.bitbucket.org',
        'language': 'html/css',
        'created_on': '2011-12-20T16:35:06.480042+00:00',
        'full_name': 'tutorials/tutorials.bitbucket.org',
        'has_issues': True,
        'owner': {
            'username': '******',
            'display_name': 'tutorials account',
            'uuid': '{c788b2da-b7a2-404c-9e26-d3f077557007}',
            'links': {
                'self': {
                    'href': 'https://api.bitbucket.org/2.0/users/tutorials',
                },
                'html': {
                    'href': 'https://bitbucket.org/tutorials',
                },
                'avatar': {
                    'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png',
                },
            },
        },
        'updated_on': '2014-11-03T02:24:08.409995+00:00',
        'size': 76182262,
        'is_private': False,
        'uuid': '{9970a9b6-2d86-413f-8555-da8e1ac0e542}',
    }

    team_response_data = {
        'username': '******',
        'website': None,
        'display_name': 'Teams In Space',
        'uuid': '{61fc5cf6-d054-47d2-b4a9-061ccf858379}',
        'links': {
            'self': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace',
            },
            'repositories': {
                'href': 'https://api.bitbucket.org/2.0/repositories/teamsinspace',
            },
            'html': {'href': 'https://bitbucket.org/teamsinspace'},
            'followers': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/followers',
            },
            'avatar': {
                'href': 'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/teamsinspace-avatar-3731530358-7_avatar.png',
            },
            'members': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/members',
            },
            'following': {
                'href': 'https://api.bitbucket.org/2.0/teams/teamsinspace/following',
            },
        },
        'created_on': '2014-04-08T00:00:14.070969+00:00',
        'location': None,
        'type': 'team',
    }

    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 = BitbucketService(user=self.user, account=None)

    def test_make_project_pass(self):
        repo = self.service.create_repository(
            self.repo_response_data, organization=self.org,
            privacy=self.privacy,
        )
        self.assertIsInstance(repo, RemoteRepository)
        self.assertEqual(repo.name, 'tutorials.bitbucket.org')
        self.assertEqual(repo.full_name, 'tutorials/tutorials.bitbucket.org')
        self.assertEqual(repo.description, 'Site for tutorial101 files')
        self.assertEqual(
            repo.avatar_url, (
                'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/'
                'tutorials.bitbucket.org-logo-1456883302-9_avatar.png'
            ),
        )
        self.assertIn(self.user, repo.users.all())
        self.assertEqual(repo.organization, self.org)
        self.assertEqual(
            repo.clone_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
        )
        self.assertEqual(
            repo.ssh_url,
            'ssh://[email protected]/tutorials/tutorials.bitbucket.org',
        )
        self.assertEqual(
            repo.html_url,
            'https://bitbucket.org/tutorials/tutorials.bitbucket.org',
        )

    def test_make_project_fail(self):
        data = self.repo_response_data.copy()
        data['is_private'] = True
        repo = self.service.create_repository(
            data, organization=self.org, privacy=self.privacy,
        )
        self.assertIsNone(repo)

    @override_settings(DEFAULT_PRIVACY_LEVEL='private')
    def test_make_private_project(self):
        """
        Test ability to import ``public`` repositories under ``private`` level.
        """
        data = self.repo_response_data.copy()
        data['is_private'] = False
        repo = self.service.create_repository(data, organization=self.org)
        self.assertIsNotNone(repo)

    def test_make_organization(self):
        org = self.service.create_organization(self.team_response_data)
        self.assertIsInstance(org, RemoteOrganization)
        self.assertEqual(org.slug, 'teamsinspace')
        self.assertEqual(org.name, 'Teams In Space')
        self.assertEqual(
            org.avatar_url, (
                'https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2014/Sep/24/'
                'teamsinspace-avatar-3731530358-7_avatar.png'
            ),
        )
        self.assertEqual(org.url, 'https://bitbucket.org/teamsinspace')

    def test_import_with_no_token(self):
        """User without a Bitbucket SocialToken does not return a service."""
        services = BitbucketService.for_user(self.user)
        self.assertEqual(services, [])