예제 #1
0
def test_forest_session_request_authenticated_with_qmi_auth():
    config = PyquilConfig(TEST_CONFIG_PATHS)
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "qmi_auth_token_path",
        api_fixture_path("qmi_auth_token_valid.json"))
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path",
        api_fixture_path("user_auth_token_invalid.json"))
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "url",
                                            "mock://forest")
    config._parse_auth_tokens()

    session = ForestSession(config=config)
    mock_adapter = requests_mock.Adapter()
    session.mount("mock", mock_adapter)

    url = "%s/devices" % config.forest_url
    headers = {
        # access token from ./data/qmi_auth_token_valid.json.
        "X-QMI-AUTH-TOKEN": "secret"
    }
    mock_adapter.register_uri("GET",
                              url,
                              status_code=200,
                              json=[{
                                  "id": 0
                              }],
                              headers=headers)

    devices = session.get(url).json()
    assert len(devices) == 1
    assert devices[0]["id"] == 0
예제 #2
0
def test_forest_session_request_refresh_user_auth_token():
    config = PyquilConfig(TEST_CONFIG_PATHS)
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "qmi_auth_token_path", api_fixture_path("qmi_auth_token_invalid.json")
    )
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path", api_fixture_path("user_auth_token_valid.json")
    )
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "url", "mock://forest")
    config._parse_auth_tokens()

    session = ForestSession(config=config)
    mock_adapter = requests_mock.Adapter()
    session.mount("mock", mock_adapter)

    url = "%s/devices" % config.forest_url
    response_list = [
        # access token from ./data/user_auth_token_valid.json.
        {
            "status_code": 401,
            "json": {"error": "user_unauthorized"},
            "headers": {"Authorization": "Bearer secret"},
        },
        # access token from new_user_auth_token.
        {"status_code": 200, "json": [{"id": 0}], "headers": {"Authorization": "Bearer secret2"}},
    ]
    mock_adapter.register_uri("GET", url, response_list=response_list)

    refresh_url = "%s/auth/idp/oauth2/v1/token" % config.forest_url

    def refresh_matcher(request):
        body = dict(urllib.parse.parse_qsl(request.text))
        return (body["refresh_token"] == "supersecret") and (body["grant_type"] == "refresh_token")

    new_user_auth_token = {
        "access_token": "secret2",
        "refresh_token": "supersecret2",
        "scope": "openid offline_access profile",
    }
    mock_adapter.register_uri(
        "POST",
        refresh_url,
        status_code=200,
        json=new_user_auth_token,
        additional_matcher=refresh_matcher,
    )

    # refresh will write the new auth tokens to file. Do not over-write text fixture data.
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "qmi_auth_token_path", "/tmp/qmi_auth_token_invalid.json"
    )
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path", "/tmp/user_auth_token_valid.json"
    )
    devices = session.get(url).json()
    assert len(devices) == 1
    assert devices[0]["id"] == 0
예제 #3
0
def test_forest_session_request_engagement_failure():
    """
    If engagement fails, no QPU URL is available to the client.
    """
    config = PyquilConfig(TEST_CONFIG_PATHS)
    config.config_parsers["FOREST_CONFIG"].remove_section("Rigetti Forest")
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path",
        api_fixture_path("user_auth_token_invalid.json"))
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "url",
                                            "mock://forest")
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "dispatch_url",
                                            "mock://dispatch")
    config._parse_auth_tokens()

    session = ForestSession(config=config, lattice_name="fake-lattice")
    mock_adapter = requests_mock.Adapter()
    session.mount("mock", mock_adapter)

    url = config.dispatch_url
    response_list = [
        # access token from ./data/user_auth_token_valid.json.
        {
            "status_code": 200,
            "json": {
                "data": FAILED_ENGAGEMENT_RESPONSE
            }
        }
    ]
    mock_adapter.register_uri("POST", url, response_list=response_list)
예제 #4
0
def test_forest_session_engagement_not_requested_if_config_present():
    """
    Engagement is the source-of-last-resort for configuration data. If all endpoints are
      provided elsewhere, then engagement should never be requested.
    """
    config = PyquilConfig(TEST_CONFIG_PATHS)
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path",
        api_fixture_path("user_auth_token_invalid.json"))
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "url",
                                            "mock://forest")
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "dispatch_url",
                                            "mock://dispatch")
    config._parse_auth_tokens()

    session = ForestSession(config=config, lattice_name="fake-lattice")
    mock_adapter = requests_mock.Adapter()
    session.mount("mock", mock_adapter)

    url = config.dispatch_url
    response_list = [
        # access token from ./data/user_auth_token_valid.json.
        {
            "status_code": 200,
            "json": {
                "data": SUCCESSFUL_ENGAGEMENT_RESPONSE
            }
        }
    ]
    mock_adapter.register_uri("POST", url, response_list=response_list)

    assert len(mock_adapter.request_history) == 0
    assert config.qpu_url == config.config_parsers["FOREST_CONFIG"].get(
        "Rigetti Forest", "qpu_endpoint_address")
예제 #5
0
def test_forest_session_request_engagement():
    """
    The QPU Endpoint address provided by engagement should be available to the
      PyQuilConfig object.
    """
    config = PyquilConfig(TEST_CONFIG_PATHS)
    config.config_parsers["FOREST_CONFIG"].remove_section("Rigetti Forest")
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path", api_fixture_path("user_auth_token_invalid.json")
    )
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "url", "mock://forest")
    config.config_parsers["QCS_CONFIG"].set("Rigetti Forest", "dispatch_url", "mock://dispatch")
    config._parse_auth_tokens()

    session = ForestSession(config=config, lattice_name="fake-lattice")
    mock_adapter = requests_mock.Adapter()
    session.mount("mock", mock_adapter)

    url = config.dispatch_url
    response_list = [
        # access token from ./data/user_auth_token_valid.json.
        {"status_code": 200, "json": {"data": SUCCESSFUL_ENGAGEMENT_RESPONSE}}
    ]
    mock_adapter.register_uri("POST", url, response_list=response_list)

    assert (
        config.qpu_url == SUCCESSFUL_ENGAGEMENT_RESPONSE["engage"]["engagement"]["qpu"]["endpoint"]
    )
    assert (
        config.qpu_compiler_url
        == SUCCESSFUL_ENGAGEMENT_RESPONSE["engage"]["engagement"]["compiler"]["endpoint"]
    )
예제 #6
0
def test_config_assert_valid_auth_credential():
    config = PyquilConfig(TEST_CONFIG_PATHS)
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "qmi_auth_token_path",
        api_fixture_path("qmi_auth_token_invalid.json"))
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path",
        api_fixture_path("user_auth_token_invalid.json"))
    config._parse_auth_tokens()
    assert config.user_auth_token is None
    assert config.qmi_auth_token is None
    with pytest.raises(UserMessageError):
        config.assert_valid_auth_credential()

    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "qmi_auth_token_path",
        api_fixture_path("qmi_auth_token_valid.json"))
    config.config_parsers["QCS_CONFIG"].set(
        "Rigetti Forest", "user_auth_token_path",
        api_fixture_path("user_auth_token_valid.json"))
    config._parse_auth_tokens()
    assert config.user_auth_token is not None
    assert config.qmi_auth_token is not None
    config.assert_valid_auth_credential()
예제 #7
0
    "program": "bAsE64==",
    "debug": {},
    "memory_descriptors": {},
    "ro_sources": [],
    "_type": "QuiltBinaryExecutableResponse",
}

CALIBRATIONS_RESPONSE = {
    "_type": "QuiltCalibrationsResponse",
    "quilt": "",
}

DUMMY_ISA_DICT = {"1Q": {"0": {}, "1": {}}, "2Q": {"0-1": {}}}

TEST_CONFIG_PATHS = {
    "QCS_CONFIG": api_fixture_path("qcs_config.ini"),
    "FOREST_CONFIG": api_fixture_path("forest_config.ini"),
}


def test_http_compilation(compiler):
    device_name = "test_device"
    mock_url = "http://mock-qpu-compiler"

    config = PyquilConfig(TEST_CONFIG_PATHS)
    session = get_session(config=config)
    mock_adapter = requests_mock.Adapter()
    session.mount("http://", mock_adapter)

    headers = {
        # access token from ./data/user_auth_token_valid.json.