예제 #1
0
def test_auth2_refresh_access_token():
    # given OAuth2Token with expired access_token
    api_client = FakeClass()
    api_client.set_oauth2_token = FakeMethod()
    refresh_token = "refresh-token-value"
    scope = [
        "email",
        "profile",
        "openid",
        "accounting.reports.read",
        "accounting.attachments.read",
        "accounting.settings",
        "accounting.settings.read",
        "accounting.attachments",
        "accounting.transactions",
        "accounting.journals.read",
        "accounting.transactions.read",
        "accounting.contacts",
        "accounting.contacts.read",
        "offline_access",
    ]
    new_token = {
        "id_token": "new-id-token-value",
        "access_token": "new-access-token-value",
        "expires_in": 1800,
        "expires_at": time.time() + 1800,
        "token_type": "Bearer",
        "refresh_token": "new-refresh-token-value",
        "scope": scope,
    }
    oauth2_token = OAuth2Token(client_id="client_id",
                               client_secret="client_secret")
    oauth2_token.refresh_token = refresh_token
    oauth2_token.scope = scope
    oauth2_token.fetch_access_token = FakeMethod(return_value=new_token)
    # When refreshing access_token
    assert oauth2_token.refresh_access_token(api_client=api_client)
    # Then expected set new token function called
    assert len(oauth2_token.fetch_access_token.calls) == 1
    assert len(api_client.set_oauth2_token.calls) == 1
    call_args, call_kwargs = api_client.set_oauth2_token.calls[0]
    assert call_args == (new_token, )
    assert call_kwargs == {}
    # Then expected new valid access and refresh tokens set on oauth2_token
    assert oauth2_token.expires_at == new_token["expires_at"]
    assert oauth2_token.is_access_token_valid()
    assert oauth2_token.id_token == new_token["id_token"]
    assert oauth2_token.expires_in == new_token["expires_in"]
    assert oauth2_token.token_type == new_token["token_type"]
    assert oauth2_token.scope == new_token["scope"]
    assert oauth2_token.access_token == new_token["access_token"]
    assert oauth2_token.refresh_token == new_token["refresh_token"]
예제 #2
0
def test_auth2_fetch_access_token():
    # Given OAuth2Token with valid refresh_token
    oauth2_token = OAuth2Token()
    token_valid_from = time.time()
    scope = ("email profile openid accounting.reports.read "
             "accounting.attachments.read accounting.settings "
             "accounting.settings.read accounting.attachments "
             "accounting.transactions accounting.journals.read "
             "accounting.transactions.read accounting.contacts "
             "accounting.contacts.read offline_access")
    new_token = {
        "id_token": "new-id-token-value",
        "access_token": "new-access-token-value",
        "expires_in": 1800,
        "token_type": "Bearer",
        "refresh_token": "new-refresh-token-value",
        "scope": scope,
    }
    oauth2_token.call_refresh_token_api = FakeMethod(
        return_value=new_token.copy())
    token_api = None
    # When we fetch new oauth2 token
    received_token = oauth2_token.fetch_access_token(
        token_api=token_api, token_valid_from=token_valid_from)
    # Then new token valid token to be received
    assert len(oauth2_token.call_refresh_token_api.calls) == 1
    assert received_token["id_token"] == new_token["id_token"]
    assert received_token["access_token"] == new_token["access_token"]
    assert received_token["expires_in"] == new_token["expires_in"]
    assert received_token["token_type"] == new_token["token_type"]
    assert received_token["refresh_token"] == new_token["refresh_token"]
    assert received_token["scope"] == scope.split()
    assert received_token[
        "expires_at"] == token_valid_from + new_token["expires_in"]
예제 #3
0
def test_auth2_call_refresh_token_api(oauth2_refresh_token):
    # Given valid refresh token and client credentials
    oauth2_token = OAuth2Token()
    oauth2_token.update_token(**oauth2_refresh_token)
    token = {}
    token_api = FakeClass()
    token_api.refresh_token = FakeMethod(return_value=token)
    # When refresh token API endpoint called
    new_token = oauth2_token.call_refresh_token_api(token_api)
    # Then new oauth2 token received
    assert len(token_api.refresh_token.calls) == 1
    call_args, call_kwargs = token_api.refresh_token.calls[0]
    assert call_args == (oauth2_token.refresh_token, oauth2_token.scope)
    assert call_kwargs == {}
    assert new_token is token
예제 #4
0
def test_deserialize():
    # given default deserialize function
    default_deserialize = deserialize.registry[
        ""]  # get default implementation
    data = {"field": "value"}

    class User:
        pass

    model_finder = FakeClass()
    model_finder.find_model = FakeMethod(return_value=User)
    # when finding deserialize routing key
    with mock_deserialize("deserialize_model") as deserialize_model:
        result = default_deserialize("User", data, model_finder)

    # then expect find_model called
    assert len(model_finder.find_model.calls) == 1
    call_args, call_kwargs = model_finder.find_model.calls[0]
    assert call_args == ("User", )
    assert call_kwargs == {}
    # then expect deserialize_mode called
    deserialize_model.assert_called_once_with(User, data, model_finder)
    # then expected result to returned from deserialize_model()
    assert result is data