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_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_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.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_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_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_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_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")
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.path("TestShare", "/file_to_rename").write_text("Test Rename") def raise_failure(*args): raise OperationFailure("Mock for rename failure.", []) samba_mock.add_callback("rename", raise_failure) with pytest.raises(pyndows.PyndowsException) 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_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_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_async_retrieval(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() retrieved_file = try_get(samba_mock.path("TestShare", "/TestFilePath"), timeout=4) thread.join() assert gzip.decompress(retrieved_file.read_bytes()) == b"Test Content Move"
def test_get_all_shared_folder_contents(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/2").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/i/2").write_text("Test Find") shared_folder_contents = pyndows.get_folder_content(connection, "TestShare", folder_path="") assert shared_folder_contents == [ SharedFileMock(filename="1", isDirectory=False), SharedFileMock(filename="A", isDirectory=True), ]