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." )
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." )
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." )
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), ]
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" )
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." )
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" )
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
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")
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"
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"
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
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 == []
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")
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")
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 == []
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"
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"
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"))
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", } }, )
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"
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")
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"
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}", } }, )
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" )
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), ]
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", } }, )
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")
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")
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")