def test_import_lab_from_path_virl(client_library_server_2_0_0, mocked_session,
                                   tmp_path: Path):
    cl = ClientLibrary(url="http://0.0.0.0/fake_url/",
                       username="******",
                       password="******")
    Lab.sync = Mock()

    (tmp_path /
     "topology.virl").write_text("<?xml version='1.0' encoding='UTF-8'?>")
    with patch.object(Lab, "sync", autospec=True) as sync_mock:
        lab = cl.import_lab_from_path(topology=(tmp_path /
                                                "topology.virl").as_posix())

    assert lab.title is not None
    assert lab.lab_base_url.startswith("https://0.0.0.0/fake_url/api/v0/labs/")

    cl.session.post.assert_called_once_with(
        "https://0.0.0.0/fake_url/api/v0/import/virl-1x?title=topology.virl",
        data="<?xml version='1.0' encoding='UTF-8'?>",
    )
    cl.session.post.assert_called_once()
    cl.session.post.return_value.raise_for_status.assert_called_once()
    sync_mock.assert_called_once_with()
def test_import_lab_from_path_ng(client_library_server_2_0_0, mocked_session,
                                 tmp_path: Path):
    client_library = ClientLibrary(url="http://0.0.0.0/fake_url/",
                                   username="******",
                                   password="******")
    Lab.sync = Mock()

    topology_data = '{"nodes": [], "links": [], "interfaces": []}'
    (tmp_path / "topology.ng").write_text(topology_data)
    with patch.object(Lab, "sync", autospec=True) as sync_mock:
        lab = client_library.import_lab_from_path(
            topology=(tmp_path / "topology.ng").as_posix())

    assert lab.title is not None
    assert lab.lab_base_url.startswith("https://0.0.0.0/fake_url/api/v0/labs/")

    client_library.session.post.assert_called_once_with(
        "https://0.0.0.0/fake_url/api/v0/import?is_json=true&title=topology.ng",
        data=topology_data,
    )
    client_library.session.post.assert_called_once()
    client_library.session.post.return_value.raise_for_status.assert_called_once(
    )
    sync_mock.assert_called_once_with()
示例#3
0
def test_client_library_str_and_repr():
    client_library = ClientLibrary("somehost", "virl2", password="******")

    assert (repr(client_library) ==
            "ClientLibrary('somehost', 'virl2', 'virl2', True, False, False)")
    assert str(client_library) == "ClientLibrary URL: https://somehost/api/v0/"
def test_auth_and_reauth_token(client_library_server_2_0_0):
    # TODO: need to check what the purpose of this test is, and how it
    # works with the automatic auth check on CL init
    # if there's environ vars for username and password set
    # then delete them b/c we rely on specific usernames
    # and passwords for this test!
    # docs: https://github.com/getsentry/responses
    try:
        del os.environ["VIRL2_PASS"]
        del os.environ["VIRL2_USER"]
    except KeyError:
        pass

    # mock failed authentication:
    responses.add(responses.POST,
                  "https://0.0.0.0/fake_url/api/v0/authenticate",
                  status=403)
    responses.add(responses.GET,
                  "https://0.0.0.0/fake_url/api/v0/authok",
                  status=401)

    # mock successful authentication:
    responses.add(
        responses.POST,
        "https://0.0.0.0/fake_url/api/v0/authenticate",
        json="7bbcan78a98bch7nh3cm7hao3nc7",
    )
    responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/authok")

    # mock get labs
    responses.add(responses.GET,
                  "https://0.0.0.0/fake_url/api/v0/labs",
                  json=[])

    with pytest.raises(InitializationError):
        # Test returns custom exception when instructed to raise on failure
        ClientLibrary(
            url="http://0.0.0.0/fake_url/",
            username="******",
            password="******",
            raise_for_auth_failure=True,
        )

    cl = ClientLibrary(url="http://0.0.0.0/fake_url/",
                       username="******",
                       password="******")

    cl.all_labs()

    # for idx, item in enumerate(responses.calls):
    #     print(idx, item.request.url)
    #
    # this is what we expect:
    # 0 https://0.0.0.0/fake_url/api/v0/authenticate
    # 1 https://0.0.0.0/fake_url/api/v0/authenticate
    # 2 https://0.0.0.0/fake_url/api/v0/authok
    # 3 https://0.0.0.0/fake_url/api/v0/authenticate
    # 4 https://0.0.0.0/fake_url/api/v0/authok
    # 5 https://0.0.0.0/fake_url/api/v0/labs

    assert (responses.calls[0].request.url ==
            "https://0.0.0.0/fake_url/api/v0/authenticate")
    assert json.loads(responses.calls[0].request.body) == {
        "username": "******",
        "password": "******",
    }
    assert (responses.calls[1].request.url ==
            "https://0.0.0.0/fake_url/api/v0/authenticate")
    assert responses.calls[
        2].request.url == "https://0.0.0.0/fake_url/api/v0/authok"
    assert (responses.calls[3].request.url ==
            "https://0.0.0.0/fake_url/api/v0/authenticate")
    assert responses.calls[
        4].request.url == "https://0.0.0.0/fake_url/api/v0/authok"
    assert responses.calls[
        5].request.url == "https://0.0.0.0/fake_url/api/v0/labs"
    assert len(responses.calls) == 6
def test_major_version_mismatch(client_library_incompatible_version):
    with pytest.raises(InitializationError) as err:
        ClientLibrary("somehost", "virl2", password="******")
    assert str(err.value) == "Major version mismatch. server 1.0.0, client 2.1.0"
示例#6
0
def test_client_library_init_disallow_http(client_library_server_current):
    with pytest.raises(InitializationError, match="must be https"):
        ClientLibrary("http://somehost", "virl2", "virl2")
    with pytest.raises(InitializationError, match="must be https"):
        ClientLibrary("http://somehost", "virl2", "virl2", allow_http=False)
示例#7
0
def test_auth_and_reauth_token(client_library_server_current):
    # mock failed authentication:
    responses.add(responses.POST,
                  "https://0.0.0.0/fake_url/api/v0/authenticate",
                  status=403)
    responses.add(responses.GET,
                  "https://0.0.0.0/fake_url/api/v0/authok",
                  status=401)

    # mock successful authentication:
    responses.add(
        responses.POST,
        "https://0.0.0.0/fake_url/api/v0/authenticate",
        json="7bbcan78a98bch7nh3cm7hao3nc7",
    )
    responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/authok")

    # mock get labs
    responses.add(responses.GET,
                  "https://0.0.0.0/fake_url/api/v0/labs",
                  json=[])

    with pytest.raises(InitializationError):
        # Test returns custom exception when instructed to raise on failure
        ClientLibrary(
            url="https://0.0.0.0/fake_url/",
            username="******",
            password="******",
            raise_for_auth_failure=True,
        )

    cl = ClientLibrary(url="https://0.0.0.0/fake_url/",
                       username="******",
                       password="******")

    cl.all_labs()

    # for idx, item in enumerate(responses.calls):
    #     print(idx, item.request.url)
    #
    # this is what we expect:
    # 0 https://0.0.0.0/fake_url/api/v0/authenticate
    # 1 https://0.0.0.0/fake_url/api/v0/authenticate
    # 2 https://0.0.0.0/fake_url/api/v0/authok
    # 3 https://0.0.0.0/fake_url/api/v0/authenticate
    # 4 https://0.0.0.0/fake_url/api/v0/authok
    # 5 https://0.0.0.0/fake_url/api/v0/labs

    assert (responses.calls[0].request.url ==
            "https://0.0.0.0/fake_url/api/v0/authenticate")
    assert json.loads(responses.calls[0].request.body) == {
        "username": "******",
        "password": "******",
    }
    assert (responses.calls[1].request.url ==
            "https://0.0.0.0/fake_url/api/v0/authenticate")
    assert responses.calls[
        2].request.url == "https://0.0.0.0/fake_url/api/v0/authok"
    assert (responses.calls[3].request.url ==
            "https://0.0.0.0/fake_url/api/v0/authenticate")
    assert responses.calls[
        4].request.url == "https://0.0.0.0/fake_url/api/v0/authok"
    assert responses.calls[
        5].request.url == "https://0.0.0.0/fake_url/api/v0/labs"
    assert len(responses.calls) == 6