예제 #1
0
def test_authorize_url():
    Trakt.site_url = 'http://mock'

    assert_url(
        Trakt['oauth'].authorize_url('urn:ietf:wg:oauth:2.0:oob'),
        '/oauth/authorize', {
            'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
            'response_type': 'code',
            'client_id': 'mock-client_id'
        })

    assert_url(
        Trakt['oauth'].authorize_url('urn:ietf:wg:oauth:2.0:oob',
                                     state='state',
                                     username='******'), '/oauth/authorize',
        {
            'username': '******',
            'state': 'state',
            'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
            'response_type': 'code',
            'client_id': 'mock-client_id'
        })

    with pytest.raises(ValueError):
        TraktClient()['oauth'].authorize_url('urn:ietf:wg:oauth:2.0:oob')
예제 #2
0
def test_code():
    with HTTMock(mock.oauth_device_code, mock.unknown):
        # Validate `code` request/response
        assert Trakt['oauth/device'].code() == {
            'device_code': 'mock-device_code',
            'user_code': 'mock-user_code',
            'verification_url': 'https://trakt.tv/activate',
            'expires_in': 600,
            'interval': 5
        }

        # Ensure `code` raises a `ValueError` on incorrect configuration
        with pytest.raises(ValueError):
            assert TraktClient()['oauth/device'].code()
예제 #3
0
def test_token():
    with HTTMock(mock.oauth_device_token, mock.unknown):
        # Validate `token` request/response
        assert Trakt['oauth/device'].token('mock-device_code') == {
            'access_token': 'mock-access_token',
            'token_type': 'bearer',
            'expires_in': 7200,
            'refresh_token': 'mock-refresh_token',
            'scope': 'public'
        }

        # Ensure `token` raises a `ValueError` on incorrect configuration
        with pytest.raises(ValueError):
            assert TraktClient()['oauth/device'].token('mock-device_code')
예제 #4
0
def test_token():
    with HTTMock(mock.oauth_token, mock.unknown):
        # Validate `token_exchange` request/response
        assert Trakt['oauth'].token('ABCD1234',
                                    'urn:ietf:wg:oauth:2.0:oob') == {
                                        'access_token': 'mock-access_token',
                                        'token_type': 'bearer',
                                        'expires_in': 7200,
                                        'refresh_token': 'mock-refresh_token',
                                        'scope': 'public'
                                    }

        # Ensure `token_exchange` raises a `ValueError` on incorrect configuration
        with pytest.raises(ValueError):
            assert TraktClient()['oauth'].token('ABCD1234',
                                                'urn:ietf:wg:oauth:2.0:oob')
예제 #5
0
def test_refresh_deadlock():
    with HTTMock(mock.oauth_token, mock.sync_get, mock.unknown):
        # Construct client
        client = TraktClient()

        # Configure client
        client.configuration.defaults.client(id='mock-client_id',
                                             secret='mock-client_secret')

        # Bind to events
        refreshed = Event()
        looped = Event()

        @client.on('oauth.refresh')
        def on_token_refreshed(username, authorization):
            if refreshed.is_set():
                looped.set()
                return

            refreshed.set()

            # Test refresh recursion
            assert client['sync/collection'].movies() is None

        # Attempt request with expired authorization
        expired_authorization = {
            'access_token':
            'mock-access_token',
            'token_type':
            'bearer',
            'created_at':
            calendar.timegm(datetime.datetime.utcnow().utctimetuple()),
            'expires_in':
            0,
            'refresh_token':
            'mock-refresh_token',
            'scope':
            'public'
        }

        with client.configuration.oauth.from_response(expired_authorization,
                                                      refresh=True,
                                                      username='******'):
            assert client['sync/collection'].movies() is not None

        # Ensure requests inside "oauth.refresh" don't cause refresh loops
        assert not looped.is_set()
예제 #6
0
def test_refresh_deadlock():
    responses.add_callback(responses.GET,
                           'http://mock/sync/collection/movies',
                           callback=authenticated_response(
                               'fixtures/sync/collection/movies.json'),
                           content_type='application/json')

    def callback(request):
        return 200, {}, json.dumps({
            "access_token":
            "mock",
            "token_type":
            "bearer",
            "created_at":
            calendar.timegm(datetime.datetime.utcnow().utctimetuple()),
            "expires_in":
            7200,
            "refresh_token":
            "mock",
            "scope":
            "public"
        })

    responses.add_callback(responses.POST,
                           'http://mock/oauth/token',
                           callback=callback,
                           content_type='application/json')

    # Construct client
    client = TraktClient()
    client.base_url = 'http://mock'

    # Configure client
    client.configuration.defaults.client(id='mock', secret='mock')

    # Bind to events
    refreshed = Event()
    looped = Event()

    @client.on('oauth.refresh')
    def on_token_refreshed(username, authorization):
        if refreshed.is_set():
            looped.set()
            return

        refreshed.set()

        # Test refresh recursion
        assert client['sync/collection'].movies() is None

    # Attempt request with expired authorization
    expired_authorization = {
        "access_token": "mock",
        "token_type": "bearer",
        "created_at":
        calendar.timegm(datetime.datetime.utcnow().utctimetuple()),
        "expires_in": 0,
        "refresh_token": "mock",
        "scope": "public"
    }

    with client.configuration.oauth.from_response(expired_authorization,
                                                  refresh=True,
                                                  username='******'):
        assert client['sync/collection'].movies() is not None

    # Ensure requests inside "oauth.refresh" don't cause refresh loops
    assert not looped.is_set()