class SocialConnected:
    """
    Contain logic to create two socially connected devs from GitHub and Twitter
    """
    def __init__(self, source_dev: str = '', target_dev: str = '') -> None:
        self.source_developer: str = source_dev
        self.target_developer: str = target_dev

        self.github = GithubConnected(source_dev, target_dev)
        self.twitter = TwitterConnected(source_dev, target_dev)

    def connected(
        self,
    ) -> Union[Tuple[Dict[str, List], int], Tuple[Dict[str, bool], int]]:
        """
        Check if two developers are connected in both GitHub and Twitter.
        The condition to be connected are:
            1. The two developers have accounts in GitHub and Twitter
            2. The developers follow each other on twitter
            3. The developers have to at least one organization
            in common in GitHub

        :return: a positive connected status
        if users are connected or a dict with a list of errors.
        """
        github_connected, github_status = self.github.connected()
        twitter_connected, twitter_status = self.twitter.connected()

        if 'errors' in github_connected or 'errors' in twitter_connected:
            status = HTTP_400_BAD_REQUEST
            # if Twitter or GitHub returns a status code other than 200
            # there is an error and we should return the erroneous status.
            if github_status != HTTP_200_OK:
                status = github_status
            if twitter_status != HTTP_200_OK:
                status = twitter_status

            return (
                {
                    'errors':
                    github_connected.get('errors', []) +
                    twitter_connected.get('errors', []),
                },
                status,
            )

        response = {}
        status = HTTP_200_OK
        try:
            # if both twitter and github responses are
            # connected than the devs are both connected
            if connected := (github_connected['connected']
                             and twitter_connected['connected']):
                # connected is True
                response = github_connected
            else:
Exemplo n.º 2
0
class TestGithubConnected(TestCase):
    def test_connect_success(self):
        dev1, dev2 = 'dev1', 'dev2'
        self.github_connected = GithubConnected(dev1, dev2)

        with patch(
                'social_connected.controller_logic.github_connected.requests'
        ) as mock_session:
            status = 200
            mock_request = MagicMock()
            mock_request.status_code = status
            mock_request.json.return_value = [{'login': '******'}]

            mock_session.Session().get.return_value = mock_request

            response = self.github_connected.connected()

            self.assertEqual(({
                'connected': True,
                'organizations': ['organization']
            }, status), response)

    def test_connect_devs_do_not_exist_fail(self):
        dev1, dev2 = 'dev1', 'dev2'
        self.github_connected = GithubConnected(dev1, dev2)

        with patch(
                'social_connected.controller_logic.github_connected.requests'
        ) as mock_session:
            status = 404
            mock_request = MagicMock()
            mock_request.status_code = status

            mock_session.Session().get.return_value = mock_request

            response = self.github_connected.connected()

            self.assertEqual(
                (
                    {
                        'errors': [
                            'dev2 is not a valid user in github',
                            'dev1 is not a valid user in github',
                        ],
                    },
                    status,
                ),
                response,
            )

    def test_connect_403_forbidden_request(self):
        dev1, dev2 = 'dev1', 'dev2'
        self.github_connected = GithubConnected(dev1, dev2)

        with patch(
                'social_connected.controller_logic.github_connected.requests'
        ) as mock_session:
            status = 403
            mock_request = MagicMock()
            mock_request.status_code = status
            mock_request.json.return_value = {
                'error': f'{dev2} is not a valid user in github'
            }
            mock_session.Session().get.return_value = mock_request

            response = self.github_connected.connected()

            self.assertEqual(
                (
                    {
                        'errors': [{
                            'error':
                            'dev2 is not a valid user in github'
                        }]
                    },
                    403,
                ),
                response,
            )

    def test_github_org_endpoint_success(self):
        developer_login = '******'
        url = f'https://api.github.com/users/{developer_login}/orgs'
        built_url = GithubConnected._github_org_endpoint(developer_login)

        self.assertEqual(url, built_url)

    def test_github_org_endpoint_empty_login_exception(self):
        developer_login = ''
        with self.assertRaises(ValueError):
            GithubConnected._github_org_endpoint(developer_login)