예제 #1
0
def test_connection_failure(samba_mock: SMBConnectionMock):
    samba_mock.add_callback("connect", lambda *args: False)
    with pytest.raises(pyndows.PyndowsException) 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."
    )
예제 #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."
    )
예제 #3
0
def test_async_retrieval_timeout(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with tempfile.TemporaryDirectory() as temp_dir:
        with gzip.open(os.path.join(temp_dir, "local_file"),
                       mode="w") as distant_file:
            distant_file.write(b"Test Content Move")

        def add_with_delay(delay: int):
            time.sleep(delay)
            pyndows.move(
                connection,
                "TestShare",
                "TestFilePath",
                os.path.join(temp_dir, "local_file"),
            )

        threading.Thread(target=add_with_delay, args=(2, )).start()

        with pytest.raises(TimeoutError) as exception_info:
            samba_mock.stored_files.try_get(("TestShare", "TestFilePath"))
        assert (
            str(exception_info.value) ==
            "('TestShare', 'TestFilePath') could not be found within 1 seconds."
        )
예제 #4
0
def test_get_all_folder_contents_matching_a_pattern(
        samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.path("TestShare", "/1").write_text("Test Find")
    samba_mock.path("TestShare", "/A").mkdir()
    samba_mock.path("TestShare", "/A/1").write_text("Test Find")
    samba_mock.path("TestShare", "/A/12").write_text("Test Find")
    samba_mock.path("TestShare", "/A/3").write_text("Test Find")
    samba_mock.path("TestShare", "/A/i").mkdir()
    samba_mock.path("TestShare", "/A/i/1").write_text("Test Find")
    samba_mock.path("TestShare", "/A/1i").mkdir()
    samba_mock.path("TestShare", "/A/1i/2").write_text("Test Find")

    shared_folder_contents = pyndows.get_folder_content(connection,
                                                        "TestShare",
                                                        folder_path="/A",
                                                        pattern="1*")

    assert shared_folder_contents == [
        SharedFileMock(filename="1", isDirectory=False),
        SharedFileMock(filename="12", isDirectory=False),
        SharedFileMock(filename="1i", isDirectory=True),
    ]
예제 #5
0
def test_retrieval_of_stored_non_text_file(samba_mock: SMBConnectionMock, tmpdir):
    connection = pyndows.connect(
        "TestComputer", "127.0.0.1", 80, "TestDomain", "TestUser", "TestPassword"
    )
    with gzip.open(os.path.join(tmpdir, "local_file"), mode="w") as distant_file:
        distant_file.write(b"Test Content Move")

    pyndows.move(
        connection, "TestShare", "/TestFilePath", os.path.join(tmpdir, "local_file")
    )

    pyndows.get(
        connection,
        "TestShare",
        "/TestFilePath",
        os.path.join(tmpdir, "local_file_retrieved"),
    )

    with gzip.open(os.path.join(tmpdir, "local_file_retrieved")) as local_file:
        assert local_file.read() == b"Test Content Move"

    assert (
        gzip.decompress(samba_mock.path("TestShare", "/TestFilePath").read_bytes())
        == b"Test Content Move"
    )
예제 #6
0
def test_async_retrieval_timeout(samba_mock: SMBConnectionMock, tmpdir):
    connection = pyndows.connect(
        "TestComputer", "127.0.0.1", 80, "TestDomain", "TestUser", "TestPassword"
    )
    with gzip.open(os.path.join(tmpdir, "local_file"), mode="w") as distant_file:
        distant_file.write(b"Test Content Move")

    def add_with_delay(delay: int):
        time.sleep(delay)
        pyndows.move(
            connection,
            "TestShare",
            "/TestFilePath",
            os.path.join(tmpdir, "local_file"),
            write_to_new_folder_after=0,
        )

    thread = threading.Thread(target=add_with_delay, args=(2,))
    thread.start()

    with pytest.raises(TimeoutError) as exception_info:
        try_get(samba_mock.path("TestShare", "/TestFilePath"))
    thread.join()
    assert (
        str(exception_info.value) == "TestFilePath could not be found within 1 seconds."
    )
예제 #7
0
def test_file_move_with_last_folder_creation_failure(
        samba_mock: SMBConnectionMock, tmpdir):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with open(os.path.join(tmpdir, "local_file"), mode="w") as distant_file:
        distant_file.write("Test Content Move")

    samba_mock.path("TestShare", "/Folder1/Folder2").mkdir(parents=True)

    def raise_failure(*args):
        raise OperationFailure("Unable to create directory", [])

    samba_mock.add_callback("createDirectory", raise_failure)
    with pytest.raises(pyndows.PyndowsException) as exception_info:
        pyndows.move(
            connection,
            "TestShare",
            "/Folder1/Folder2/Folder3/TestFilePath",
            os.path.join(tmpdir, "local_file"),
        )

    assert (
        str(exception_info.value) ==
        r"Unable to write \\TestComputer\TestShare/Folder1/Folder2/Folder3/TestFilePath.tmp"
    )
예제 #8
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.path("TestShare", "/file_to_find").write_text("Test Find")

    assert pyndows.get_file_desc(connection, "TestShare",
                                 "/non_existing") is None
예제 #9
0
def test_operation_failure_during_file_retrieval(samba_mock: SMBConnectionMock,
                                                 tmpdir):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with pytest.raises(pyndows.PyndowsException) as exception_info:
        pyndows.get(connection, "TestShare", "/TestFilePath",
                    os.path.join(tmpdir, "local_file"))
    assert (str(exception_info.value) ==
            r"Unable to retrieve \\TestComputer\TestShare/TestFilePath file")
예제 #10
0
def test_get_file_desc_file_exists(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.path("TestShare", "/file_to_find").write_text("Test Find")

    file = pyndows.get_file_desc(connection, "TestShare", "/file_to_find")

    assert file.filename == "file_to_find"
예제 #11
0
def test_file_retrieval(samba_mock: SMBConnectionMock, tmpdir):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    samba_mock.path("TestShare", "/TestFilePath").write_text("Test Content")

    pyndows.get(connection, "TestShare", "/TestFilePath",
                os.path.join(tmpdir, "local_file"))
    with open(os.path.join(tmpdir, "local_file")) as local_file:
        assert local_file.read() == "Test Content"
예제 #12
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
예제 #13
0
def test_get_all_folder_contents_non_existing_folder(
        samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    shared_folder_contents = pyndows.get_folder_content(connection,
                                                        "TestShare",
                                                        folder_path="/B")

    assert shared_folder_contents == []
예제 #14
0
def test_file_rename_file_does_not_exist(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    with pytest.raises(FileNotFoundError) as exception_info:
        pyndows.rename(connection, "TestShare\\", "file_to_rename_2",
                       "file_new_name")

    assert (str(exception_info.value) ==
            r"\\TestComputer\TestShare\file_to_rename_2 doesn't exist")
예제 #15
0
def test_file_move(samba_mock: SMBConnectionMock, tmpdir):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with open(os.path.join(tmpdir, "local_file"), mode="w") as distant_file:
        distant_file.write("Test Content Move")

    pyndows.move(connection, "TestShare", "/TestFilePath",
                 os.path.join(tmpdir, "local_file"))

    assert (samba_mock.path(
        "TestShare", "/TestFilePath").read_text() == "Test Content Move")
예제 #16
0
def test_get_all_folder_contents_empty_folder(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.path("TestShare", "/1").write_text("Test Find")

    shared_folder_contents = pyndows.get_folder_content(connection,
                                                        "TestShare",
                                                        folder_path="/A")

    assert shared_folder_contents == []
예제 #17
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"
예제 #18
0
def test_file_rename(samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.path("TestShare", "/file_to_rename").write_text("Test Rename")

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

    assert not samba_mock.path("TestShare", "/file_to_rename").exists()
    assert samba_mock.path("TestShare",
                           "/file_new_name").read_text() == "Test Rename"
예제 #19
0
def test_smbtimeout_failure_during_storefile(samba_mock: SMBConnectionMock,
                                             tmpdir):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with open(os.path.join(tmpdir, "local_file"), mode="w") as distant_file:
        distant_file.write("Test Content Move")

    def raise_failure(*args):
        raise SMBTimeout()

    samba_mock.add_callback("storeFile", raise_failure)
    with pytest.raises(SMBTimeout):
        pyndows.move(connection, "TestShare", "/TestFilePath",
                     os.path.join(tmpdir, "local_file"))
예제 #20
0
def test_pass_health_check(samba_mock: SMBConnectionMock, mock_pyndows_health_datetime):
    connection = pyndows.connect(
        "TestComputer", "127.0.0.1", 80, "TestDomain", "TestUser", "TestPassword"
    )
    assert pyndows.check("tests", connection) == (
        "pass",
        {
            "tests:echo": {
                "componentType": "TestComputer",
                "observedValue": "",
                "status": "pass",
                "time": "2018-10-11T15:05:05.663979",
            }
        },
    )
예제 #21
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"
예제 #22
0
def test_operation_failure_during_file_retrieval(
        samba_mock: SMBConnectionMock):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with tempfile.TemporaryDirectory() as temp_dir:
        with pytest.raises(Exception) as exception_info:
            pyndows.get(
                connection,
                "TestShare",
                "TestFilePath",
                os.path.join(temp_dir, "local_file"),
            )
        assert (str(
            exception_info.value
        ) == r"Unable to retrieve \\TestComputer\TestShareTestFilePath file")
예제 #23
0
def test_file_retrieval_using_path(samba_mock: SMBConnectionMock, tmpdir):
    connection = pyndows.connect(
        "TestComputer", "127.0.0.1", 80, "TestDomain", "TestUser", "TestPassword"
    )
    samba_mock.path("TestShare", "/TestFilePath").write_bytes(
        gzip.compress(b"Test Content")
    )

    pyndows.get(
        connection,
        "TestShare",
        "/TestFilePath",
        os.path.join(tmpdir, "local_file_retrieved"),
    )
    with gzip.open(os.path.join(tmpdir, "local_file_retrieved")) as local_file:
        assert local_file.read() == b"Test Content"
예제 #24
0
def test_fail_health_check(samba_mock: SMBConnectionMock, monkeypatch):
    monkeypatch.setattr(pyndows._windows, "datetime", DateTimeMock)
    connection = pyndows.connect(
        "TestComputer", "127.0.0.1", 80, "TestDomain", "TestUser", "TestPassword"
    )
    assert pyndows.check("tests", connection) == (
        "fail",
        {
            "tests:echo": {
                "componentType": "TestComputer",
                "status": "fail",
                "time": "2018-10-11T15:05:05.663979",
                "output": f"Mock for echo failure.{os.linesep}",
            }
        },
    )
예제 #25
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"
    )
예제 #26
0
def test_get_all_folder_contents_matching_a_pattern_with_question_mark_wildcard(
    samba_mock: SMBConnectionMock, ):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")

    samba_mock.path("TestShare", "/test_12345_test").write_text("Test Find")
    samba_mock.path("TestShare", "/test_123456_test").write_text("Test Find")
    samba_mock.path("TestShare", "/test_123_test").write_text("Test Find")
    samba_mock.path("TestShare", "/test_1234M_test").write_text("Test Find")

    shared_folder_contents = pyndows.get_folder_content(
        connection, "TestShare", pattern="test_?????_test")

    assert shared_folder_contents == [
        SharedFileMock(filename="test_12345_test", isDirectory=False),
        SharedFileMock(filename="test_1234M_test", isDirectory=False),
    ]
예제 #27
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",
            }
        },
    )
예제 #28
0
def test_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")

        pyndows.move(
            connection,
            "TestShare",
            "TestFilePath",
            os.path.join(temp_dir, "local_file"),
        )

        assert (samba_mock.stored_files[(
            "TestShare", "TestFilePath")] == "Test Content Move")
예제 #29
0
def test_store_file_operation_failure_during_file_move(
        samba_mock: SMBConnectionMock, tmpdir):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with open(os.path.join(tmpdir, "local_file"), mode="w") as distant_file:
        distant_file.write("Test Content Move")

    def raise_failure(*args):
        raise OperationFailure("Mock for storeFile failure.", [])

    samba_mock.add_callback("storeFile", raise_failure)
    with pytest.raises(pyndows.PyndowsException) as exception_info:
        pyndows.move(connection, "TestShare", "/TestFilePath",
                     os.path.join(tmpdir, "local_file"))

    assert (str(exception_info.value) ==
            r"Unable to write \\TestComputer\TestShare/TestFilePath.tmp")
예제 #30
0
def test_file_move_with_last_folder_creation(samba_mock: SMBConnectionMock,
                                             tmpdir):
    connection = pyndows.connect("TestComputer", "127.0.0.1", 80, "TestDomain",
                                 "TestUser", "TestPassword")
    with open(os.path.join(tmpdir, "local_file"), mode="w") as distant_file:
        distant_file.write("Test Content Move")

    samba_mock.path("TestShare", "/Folder1/Folder2").mkdir(parents=True)
    pyndows.move(
        connection,
        "TestShare",
        "/Folder1/Folder2/Folder3/TestFilePath",
        os.path.join(tmpdir, "local_file"),
    )

    assert (samba_mock.path(
        "TestShare", "/Folder1/Folder2/Folder3/TestFilePath").read_text() ==
            "Test Content Move")