Exemplo n.º 1
0
def test_rename_operation_failure_during_file_rename(
        samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.stored_files[("TestShare/", "file_to_rename")] = "Test Rename"

    samba_mock.rename_failure = True
    with pytest.raises(Exception) as exception_info:
        pyndows.rename(connection, "TestShare/", "file_to_rename",
                       "file_new_name")

    assert (
        str(exception_info.value) ==
        r"Unable to rename \\TestComputer\TestShare/file_to_rename into \\TestComputer\TestShare/file_new_name"
    )
Exemplo n.º 2
0
def test_connection_timeout(samba_mock: SMBConnectionMock):
    samba_mock.should_connect = TimeoutError()
    with pytest.raises(Exception) as exception_info:
        pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                        "TestUser", "TestPassword")
    assert (
        str(exception_info.value) ==
        r"Impossible to connect to TestComputer (127.0.0.1:80), check connectivity or TestDomain\TestUser rights."
    )
Exemplo n.º 3
0
def test_get_file_desc_file_does_not_exist(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.stored_files[("TestShare/", "file_to_find")] = "Test Find"

    founded_file = pyndows.get_file_desc(connection, "TestShare/",
                                         "nonexistent_file_to_find")

    assert founded_file is None
Exemplo n.º 4
0
def test_file_rename(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.stored_files[("TestShare/", "file_to_rename")] = "Test Rename"

    pyndows.rename(connection, "TestShare/", "file_to_rename", "file_new_name")

    assert ("TestShare/", "file_to_rename") not in samba_mock.stored_files
    assert samba_mock.stored_files[("TestShare/",
                                    "file_new_name")] == "Test Rename"
Exemplo n.º 5
0
def test_file_retrieval_using_str_content(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with tempfile.TemporaryDirectory() as temp_dir:
        samba_mock.files_to_retrieve[("TestShare", "TestFilePath")] = "data"

        pyndows.get(
            connection,
            "TestShare",
            "TestFilePath",
            os.path.join(temp_dir, "local_file_retrieved"),
        )
        with open(os.path.join(temp_dir, "local_file_retrieved"),
                  "rt") as local_file:
            assert local_file.read() == "data"
Exemplo n.º 6
0
def test_pass_health_check(samba_mock: SMBConnectionMock, monkeypatch):
    monkeypatch.setattr(pyndows._windows, "datetime", DateTimeMock)
    connection = pyndows.connect(
        "TestComputer", "127.0.0.1", 80, "TestDomain", "TestUser", "TestPassword"
    )
    samba_mock.echo_responses[b""] = b""
    assert pyndows.check("tests", connection) == (
        "pass",
        {
            "tests:echo": {
                "componentType": "TestComputer",
                "observedValue": "",
                "status": "pass",
                "time": "2018-10-11T15:05:05.663979",
            }
        },
    )
Exemplo n.º 7
0
def test_file_retrieval_using_bytes_content(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with tempfile.TemporaryDirectory() as temp_dir:
        bytes_content_file_path = os.path.join(temp_dir, "local_file")
        with gzip.open(bytes_content_file_path, mode="w") as distant_file:
            distant_file.write(b"Test Content")
        samba_mock.files_to_retrieve[("TestShare", "TestFilePath")] = open(
            bytes_content_file_path, "rb").read()

        pyndows.get(
            connection,
            "TestShare",
            "TestFilePath",
            os.path.join(temp_dir, "local_file_retrieved"),
        )
        with gzip.open(os.path.join(temp_dir,
                                    "local_file_retrieved")) as local_file:
            assert local_file.read() == b"Test Content"
Exemplo n.º 8
0
def test_store_file_operation_failure_during_file_move(
        samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with tempfile.TemporaryDirectory() as temp_dir:
        with open(os.path.join(temp_dir, "local_file"),
                  mode="w") as distant_file:
            distant_file.write("Test Content Move")

        samba_mock.storeFile_failure = True
        with pytest.raises(Exception) as exception_info:
            pyndows.move(
                connection,
                "TestShare",
                "TestFilePath",
                os.path.join(temp_dir, "local_file"),
            )

        assert (str(exception_info.value) ==
                r"Unable to write \\TestComputer\TestShareTestFilePath.tmp")