Esempio n. 1
0
    def test_connect_raises_error_if_user_social_auth_does_not_exist(
            self, load_strategy, refresh, mocked_credentials, mocked_client):
        api_object = Mock()
        mocked_client.discovery.build.return_value = api_object

        self.other_social_auth.delete()

        connect = YoutubeConnect(self.user)
        with pytest.raises(SocialUserNotFound):
            connection = connect.connect(self.other_user)
        api_object.subscriptions().insert.assert_not_called()
Esempio n. 2
0
    def test_connect_users_with_paging_and_unexisting_user(
            self, load_strategy, refresh, mocked_credentials, mocked_client):
        api_object = Mock()
        subscriptions = Mock()
        list_action = Mock()
        list_action.execute.side_effect = [
            {
                'items': [
                    {
                        'snippet': {
                            'resourceId': {
                                'channelId':
                                self.other_social_auth.
                                extra_data['youtube_channel'],
                                'kind':
                                'youtube#channel'
                            }
                        }
                    },
                ],
                'nextPageToken':
                'next_page_token'
            },
            {
                'items': [
                    {
                        'snippet': {
                            'resourceId': {
                                'channelId': 'UCaabbbccc',
                                'kind': 'youtube#channel'
                            }
                        }
                    },
                ]
            },
        ]
        subscriptions.list.return_value = list_action
        api_object.subscriptions.return_value = subscriptions
        mocked_client.discovery.build.return_value = api_object

        service = YoutubeConnect(self.user)
        connections = service.connect_users()

        connection = Connection.objects.first()
        assert connections == [connection]
        assert connection.user_1 == self.user
        assert connection.user_2 == self.other_user
        assert connection.confirmed is True
Esempio n. 3
0
    def test_dont_connect_users_if_no_channel_ids(self, load_strategy, refresh,
                                                  mocked_credentials,
                                                  mocked_client):
        api_object = Mock()
        subscriptions = Mock()
        list_action = Mock()
        list_action.execute.side_effect = [
            {
                'items': [
                    {
                        'snippet': {
                            'resourceId': {
                                'channelId':
                                self.other_social_auth.
                                extra_data['youtube_channel'],
                                'kind':
                                'youtube#playlist'
                            }
                        }
                    },
                ],
                'nextPageToken':
                'next_page_token'
            },
            {
                'items': [
                    {
                        'snippet': {
                            'resourceId': {
                                'channelId': 'UCaabbbccc',
                                'kind': 'youtube#channel'
                            }
                        }
                    },
                ]
            },
        ]
        subscriptions.list.return_value = list_action
        api_object.subscriptions.return_value = subscriptions
        mocked_client.discovery.build.return_value = api_object

        service = YoutubeConnect(self.user)
        connections = service.connect_users()

        assert connections == []
        assert 0 == Connection.objects.count()
Esempio n. 4
0
    def test_connect_calls_create_subscription(self, load_strategy, refresh,
                                               mocked_credentials,
                                               mocked_client):
        api_object = Mock()
        mocked_client.discovery.build.return_value = api_object

        connect = YoutubeConnect(self.user)
        connection = connect.connect(self.other_user)
        assert connection is True
        api_object.subscriptions().insert.assert_called_once_with(
            body={
                'snippet': {
                    'resourceId': {
                        'kind': 'youtube#channel',
                        'channelId': 'UCTestYoutubeChannel',
                    }
                }
            },
            part='snippet')
        api_object.subscriptions().insert().execute.assert_called_once_with()
Esempio n. 5
0
    def test_connect_users(self, mocked_strategy, mocked_refresh,
                           mocked_credentials, mocked_client):
        api_object = Mock()
        subscriptions = Mock()
        list_action = Mock()
        list_action.execute.return_value = {
            'items': [
                {
                    'snippet': {
                        'resourceId': {
                            'channelId':
                            self.other_social_auth.
                            extra_data['youtube_channel'],
                            'kind':
                            'youtube#channel'
                        }
                    }
                },
                {
                    'snippet': {
                        'resourceId': {
                            'channelId': 'UCaabbbccc',
                            'kind': 'youtube#playlist'
                        }
                    }
                },
            ]
        }
        subscriptions.list.return_value = list_action
        api_object.subscriptions.return_value = subscriptions
        mocked_client.discovery.build.return_value = api_object

        service = YoutubeConnect(self.user)
        connections = service.connect_users()

        connection = Connection.objects.first()
        assert connections == [connection]
        assert connection.user_1 == self.user
        assert connection.user_2 == self.other_user
        assert connection.confirmed is True
Esempio n. 6
0
 def test_authenticate_calls_api_with_tokens(self, mocked_strategy,
                                             mocked_refresh,
                                             mocked_credentials,
                                             mocked_google):
     connect = YoutubeConnect(self.user)
     mocked_credentials.Credentials.assert_called_once_with(
         token='123456',
         client_id=settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY,
         client_secret=settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET)
     mocked_google.discovery.build.assert_called_once_with(
         'youtube',
         'v3',
         credentials=mocked_credentials.Credentials.return_value)
Esempio n. 7
0
 def test_authenticate_calls_refresh_if_token_expired(
         self, load_strategy, refresh, credentials, google):
     self.user_social_auth.extra_data.update({
         'authtime': int(time.time()) - 360,
         'expires': 60
     })
     self.user_social_auth.save()
     connect = YoutubeConnect(self.user)
     credentials.Credentials.assert_called_once_with(
         token='123456',
         client_id=settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY,
         client_secret=settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET,
     )
     google.discovery.build.assert_called_once_with(
         'youtube', 'v3', credentials=credentials.Credentials.return_value)
     refresh.assert_called_once_with(load_strategy.return_value)
Esempio n. 8
0
 def test_authenticate_raises_error_if_no_user_credentials(self):
     self.user_social_auth.extra_data = {}
     self.user_social_auth.save()
     with pytest.raises(CredentialsNotFound):
         connect = YoutubeConnect(self.user)
Esempio n. 9
0
 def test_authenticate_raises_error_if_no_user_social_auth(self):
     self.user_social_auth.delete()
     with pytest.raises(CredentialsNotFound):
         connect = YoutubeConnect(self.user)
Esempio n. 10
0
 def test_initialization_calls_authenticate_for_user(self, authenticate):
     connect = YoutubeConnect(self.user)
     authenticate.assert_called_once_with(self.user)