async def test_client_success_call_oauth(fs, monkeypatch):
    oauth_client = Client({
        "orgUrl": ORG_URL,
        "authorizationMode": "PrivateKey",
        "clientId": CLIENT_ID,
        "scopes": SCOPES,
        "privateKey": PRIVATE_KEY
    })

    request_executor = oauth_client.get_request_executor()

    monkeypatch.setattr(OAuth, 'get_access_token', mocks.mock_access_token)
    req, err = await request_executor.create_request("GET", GET_USERS_CALL, {},
                                                     {})

    req, res_details, resp_body, error = await oauth_client\
        .get_request_executor().fire_request(req)

    assert error is None
    assert "User-Agent" in req["headers"]
    assert "Authorization" in req["headers"]
    assert req["headers"]["Authorization"].startswith("Bearer")
    assert res_details.status == 200
    assert "application/json" in res_details.headers["Content-Type"]
    assert resp_body is not None
def test_constructor_valid_no_proxy():
    org_url = "https://test.okta.com"
    token = "TOKEN"

    config = {'orgUrl': org_url, 'token': token}

    # Ensure no error is raised and proxy is None
    client = OktaClient(user_config=config)
    assert client.get_request_executor()._http_client._proxy is None
async def test_max_retries_no_timeout(monkeypatch, mocker):
    client = Client(user_config=CLIENT_CONFIG)
    query_params = {"limit": "1"}

    monkeypatch.setattr(HTTPClient, 'send_request',
                        mocks.mock_GET_HTTP_Client_response_429)

    monkeypatch.setattr(time, 'sleep', mocks.mock_pause_function)

    http_spy = mocker.spy(HTTPClient, 'send_request')

    users, resp, error = await client.list_users(query_params)

    http_spy.assert_called()
    assert http_spy.call_count ==\
        client.get_request_executor()._max_retries + 1
    assert client.get_request_executor()._request_timeout == 0
    assert isinstance(error, HTTPError)
    assert error is not None
    assert error.status == HTTPStatus.TOO_MANY_REQUESTS
    assert resp.get_status() == HTTPStatus.TOO_MANY_REQUESTS
async def test_backoff_calculation():
    client = Client(user_config=CLIENT_CONFIG)

    response_429 = (await mocks.mock_GET_HTTP_Client_response_429())[1]
    # ^ has a 1 second difference in retry and datetime
    # backoff should be 2 by Okta standards

    retry_limit_reset = float(response_429.headers["X-Rate-Limit-Reset"])
    date_time = convert_date_time_to_seconds(response_429.headers["Date"])

    backoff_time = client.get_request_executor().calculate_backoff(
        retry_limit_reset, date_time)

    assert (backoff_time == 2)
def test_constructor_valid_env_vars():
    org_url = "https://test.okta.com"
    token = "TOKEN"

    config = {'orgUrl': org_url, 'token': token}

    # Setting up env vars
    os.environ["HTTP_PROXY"] = "http://*****:*****@test.okta.com:8080"
    os.environ["HTTPS_PROXY"] = "https://*****:*****@test.okta.com:8080"

    expected = os.environ["HTTPS_PROXY"]
    client = OktaClient(user_config=config)

    # Deleting env vars
    del os.environ['HTTP_PROXY']
    del os.environ['HTTPS_PROXY']

    # Ensure no error is raised and proxy is None
    assert client.get_request_executor()._http_client._proxy == expected
def test_constructor_valid_proxy():
    org_url = "https://test.okta.com"
    token = "TOKEN"

    port = 8080
    host = "test.okta.com"
    username = "******"
    password = "******"

    config = {
        'orgUrl': org_url,
        'token': token,
        'proxy': {
            'port': port,
            'host': host,
            'username': username,
            'password': password
        }
    }
    # Ensure no error is raised and correct proxy is determined
    client = OktaClient(user_config=config)
    assert client.get_request_executor(
    )._http_client._proxy == f"http://{username}:{password}@{host}:{port}/"