diff --git a/components/tools/OmeroFS/test/drivers.py b/components/tools/OmeroFS/test/drivers.py index a621a2ce751..da9388bc04a 100644 --- a/components/tools/OmeroFS/test/drivers.py +++ b/components/tools/OmeroFS/test/drivers.py @@ -29,7 +29,6 @@ # Python 2 from path import path from omero.util import ServerContext -from mox3 import mox from functools import wraps from omero.util.temp_files import create_path from fsDropBoxMonitorClient import MonitorClientI @@ -352,15 +351,10 @@ def addObjectFactory(self, *args): class MockServerContext(ServerContext): def __init__(self, ic, get_root): - self.mox = mox.Mox() self.communicator = ic self.getSession = get_root self.stop_event = threading.Event() - def newSession(self, *args): - sess = self.mox.CreateMock(omero.api.ServiceFactoryPrx.__class__) - return sess - class MockMonitor(MonitorClientI): @@ -381,9 +375,10 @@ def __init__(self, dir=None, pre=None, post=None): post = [] self.root = None ic = mock_communicator() + self.ctx = MockServerContext(ic, self.get_root) MonitorClientI.__init__( self, dir, ic, getUsedFiles=self.used_files, - ctx=MockServerContext(ic, self.get_root), worker_wait=0.1) + ctx=self.ctx, worker_wait=0.1) self.log = logging.getLogger("MockMonitor") self.events = [] self.files = {} diff --git a/components/tools/OmeroPy/setup.py b/components/tools/OmeroPy/setup.py index f740ace018b..a92df6ce192 100755 --- a/components/tools/OmeroPy/setup.py +++ b/components/tools/OmeroPy/setup.py @@ -59,4 +59,4 @@ package_data={ 'omero.gateway': ['pilfonts/*'], 'omero.gateway.scripts': ['imgs/*']}, - tests_require=['pytest', 'pytest-xdist', 'mox3']) + tests_require=['pytest', 'pytest-xdist', 'pytest-mock']) diff --git a/components/tools/OmeroPy/test/integration/clitest/cli.py b/components/tools/OmeroPy/test/integration/clitest/cli.py index d90b4c28a5c..d51d0b351a9 100644 --- a/components/tools/OmeroPy/test/integration/clitest/cli.py +++ b/components/tools/OmeroPy/test/integration/clitest/cli.py @@ -30,7 +30,6 @@ from omero.rtypes import rstring from omero.testlib import ITest -from mox3 import mox class AbstractCLITest(ITest): @@ -44,13 +43,6 @@ def setup_class(cls): cls.cli = CLI() cls.cli.register("sessions", SessionsControl, "TEST") - def setup_mock(self): - self.mox = mox.Mox() - - def teardown_mock(self): - self.mox.UnsetStubs() - self.mox.VerifyAll() - class CLITest(AbstractCLITest): warnings.warn("Deprecated in 5.4.2. " diff --git a/components/tools/OmeroPy/test/integration/clitest/test_db.py b/components/tools/OmeroPy/test/integration/clitest/test_db.py index 3a35ecaa040..e61ab7599f0 100644 --- a/components/tools/OmeroPy/test/integration/clitest/test_db.py +++ b/components/tools/OmeroPy/test/integration/clitest/test_db.py @@ -19,7 +19,6 @@ from omero.util.temp_files import create_path from omero.cli import NonZeroReturnCode from omero.cli import CLI -from mox3 import mox import getpass import builtins import re @@ -47,18 +46,8 @@ def setup_method(self, method): self.file = create_path() - self.mox = mox.Mox() - self.mox.StubOutWithMock(getpass, 'getpass') - try: - self.mox.StubOutWithMock(__builtins__, "raw_input") - except AttributeError: - # Python 3 - self.mox.StubOutWithMock(builtins, "input") - def teardown_method(self, method): self.file.remove() - self.mox.UnsetStubs() - self.mox.VerifyAll() @pytest.mark.skipif(OMERODIR is False, reason="Needs omero.db.profile") def testBadVersionDies(self): @@ -66,28 +55,36 @@ def testBadVersionDies(self): self.cli.invoke("db script NONE NONE pw", strict=True) @pytest.mark.skipif(OMERODIR is False, reason="db password fails") - def testPasswordIsAskedForAgainIfDiffer(self): - self.expectPassword("ome") - self.expectConfirmation("bad") - self.expectPassword("ome") - self.expectConfirmation("ome") - self.mox.ReplayAll() + def testPasswordIsAskedForAgainIfDiffer(self, mocker): + mock_get_pass = mocker.patch('getpass.getpass') + mock_get_pass.side_effect = ["ome", "bad", "ome", "ome"] self.cli.invoke("db password", strict=True) + expected_calls = [ + mocker.call('Please enter password for OMERO root user: '), + mocker.call('Please re-enter password for OMERO root user: '), + mocker.call('Please enter password for OMERO root user: '), + mocker.call('Please re-enter password for OMERO root user: ') + ] + mock_get_pass.assert_has_calls(expected_calls) @pytest.mark.skipif(OMERODIR is False, reason="db password fails") - def testPasswordIsAskedForAgainIfEmpty(self): - self.expectPassword("") - self.expectPassword("ome") - self.expectConfirmation("ome") - self.mox.ReplayAll() + def testPasswordIsAskedForAgainIfEmpty(self, mocker): + mock_get_pass = mocker.patch('getpass.getpass') + mock_get_pass.side_effect = ["", "ome", "ome"] self.cli.invoke("db password", strict=True) - - @pytest.mark.xfail(reason="https://github.com/ome/omero-py/issues/112") + expected_calls = [ + mocker.call('Please enter password for OMERO root user: '), + mocker.call('Please enter password for OMERO root user: '), + mocker.call('Please re-enter password for OMERO root user: ') + ] + mock_get_pass.assert_has_calls(expected_calls) + + # @pytest.mark.xfail(reason="https://github.com/ome/omero-py/issues/112") @pytest.mark.skipif(OMERODIR is False, reason="self.password() fails") @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('user_id', ['', '0', '1']) @pytest.mark.parametrize('password', ['', 'ome']) - def testPassword(self, user_id, password, no_salt, capsys): + def testPassword(self, user_id, password, no_salt, capsys, mocker): args = "db password" if user_id: args += " --user-id=%s " % user_id @@ -96,20 +93,31 @@ def testPassword(self, user_id, password, no_salt, capsys): if password: args += " %s" % password else: - self.expectPassword("ome", id=user_id) - self.expectConfirmation("ome", id=user_id) - self.mox.ReplayAll() + mock_get_pass = mocker.patch('getpass.getpass') + mock_get_pass.return_value = "ome" self.cli.invoke(args, strict=True) - self.password(args) out, err = capsys.readouterr() assert out.strip() == self.password_output(user_id, no_salt) + if not password: + if user_id != '' and user_id != '0': + expected_calls = [ + mocker.call(f'Please enter password for OMERO user {user_id}: '), + mocker.call(f'Please re-enter password for OMERO user {user_id}: ') + ] + else: + expected_calls = [ + mocker.call('Please enter password for OMERO root user: '), + mocker.call('Please re-enter password for OMERO root user: ') + ] + mock_get_pass.assert_has_calls(expected_calls) + @pytest.mark.skipif(OMERODIR is False, reason="self.script() fails") @pytest.mark.parametrize('file_arg', ['', '-f', '--file']) @pytest.mark.parametrize('no_salt', ['', '--no-salt']) @pytest.mark.parametrize('password', ['', '--password ome']) - def testScript(self, no_salt, file_arg, password, capsys): + def testScript(self, no_salt, file_arg, password, capsys, mocker): """ Recommended usage of db script """ @@ -120,9 +128,8 @@ def testScript(self, no_salt, file_arg, password, capsys): args += " %s %s" % (file_arg, str(self.file)) if not password: - self.expectPassword("ome") - self.expectConfirmation("ome") - self.mox.ReplayAll() + mock_get_pass = mocker.patch('getpass.getpass') + mock_get_pass.return_value = "ome" self.cli.invoke(args, strict=True) @@ -163,7 +170,6 @@ def testScriptDeveloperArgs(self, pos_args, no_salt, file_arg, capsys): args += " %s" % no_salt if file_arg: args += " %s %s" % (file_arg, str(self.file)) - self.mox.ReplayAll() with pytest.raises(NonZeroReturnCode): self.cli.invoke(args, strict=True) @@ -175,20 +181,6 @@ def testScriptDeveloperArgs(self, pos_args, no_salt, file_arg, capsys): assert 'Using password from commandline' in err assert 'Invalid Database version/patch' in err - def password_ending(self, user, id): - if id and id != '0': - rv = "user %s: " % id - else: - rv = "%s user: " % user - return "password for OMERO " + rv - - def expectPassword(self, pw, user="root", id=None): - getpass.getpass("Please enter %s" % - self.password_ending(user, id)).AndReturn(pw) - - def expectConfirmation(self, pw, user="root", id=None): - getpass.getpass("Please re-enter %s" % - self.password_ending(user, id)).AndReturn(pw) def password_output(self, user_id, no_salt): update_msg = "UPDATE password SET hash = \'%s\'" \ diff --git a/components/tools/OmeroPy/test/integration/clitest/test_obj.py b/components/tools/OmeroPy/test/integration/clitest/test_obj.py index 853e257858a..6c325da7c20 100644 --- a/components/tools/OmeroPy/test/integration/clitest/test_obj.py +++ b/components/tools/OmeroPy/test/integration/clitest/test_obj.py @@ -36,10 +36,6 @@ def setup_method(self, method): super(TestObj, self).setup_method(method) self.cli.register("obj", ObjControl, "TEST") self.args += ["obj"] - self.setup_mock() - - def teardown_method(self, method): - self.teardown_mock() def go(self): self.cli.invoke(self.args, strict=True) diff --git a/components/tools/OmeroPy/test/integration/clitest/test_user.py b/components/tools/OmeroPy/test/integration/clitest/test_user.py index c64328544dd..588d575b65c 100644 --- a/components/tools/OmeroPy/test/integration/clitest/test_user.py +++ b/components/tools/OmeroPy/test/integration/clitest/test_user.py @@ -184,7 +184,7 @@ def testEmail(self, capsys, oneperline_arg): # Password subcommand # ======================================================================== @pytest.mark.parametrize("is_unicode", [True, False]) - def testPassword(self, is_unicode): + def testPassword(self, is_unicode, mocker): self.args += ["password"] login = self.ctx.userName if is_unicode: @@ -192,19 +192,21 @@ def testPassword(self, is_unicode): else: password = self.uuid() - self.setup_mock() - self.mox.StubOutWithMock(getpass, 'getpass') - i1 = 'Please enter password for your user (%s): ' % login - i2 = 'Please enter password to be set: ' - i3 = 'Please re-enter password to be set: ' - getpass.getpass(i1).AndReturn(login) - getpass.getpass(i2).AndReturn(password) - getpass.getpass(i3).AndReturn(password) - self.mox.ReplayAll() + mock_get_pass = mocker.patch('getpass.getpass') + mock_get_pass.side_effect = [ + login, + password, + password + ] try: self.cli.invoke(self.args, strict=True) - self.teardown_mock() + expected_calls = [ + mocker.call(f'Please enter password for your user ({login}): '), + mocker.call('Please enter password to be set: '), + mocker.call('Please re-enter password to be set: ') + ] + mock_get_pass.assert_has_calls(expected_calls) # Check session creation using new password self.new_client(user=login, password=password) @@ -389,7 +391,7 @@ def testAddGroup(self, groupfixture): @pytest.mark.parametrize("password_prefix", password_prefixes) @pytest.mark.parametrize("is_unicode", [True, False]) - def testAddPassword(self, password_prefix, is_unicode): + def testAddPassword(self, password_prefix, is_unicode, mocker): group = self.new_group() login = self.uuid() firstname = self.uuid() @@ -404,17 +406,16 @@ def testAddPassword(self, password_prefix, is_unicode): if password_prefix: self.args += [password_prefix, "%s" % password] else: - self.setup_mock() - self.mox.StubOutWithMock(getpass, 'getpass') - i1 = 'Please enter password for your new user (%s): ' % login - i2 = 'Please re-enter password for your new user (%s): ' % login - getpass.getpass(i1).AndReturn(password) - getpass.getpass(i2).AndReturn(password) - self.mox.ReplayAll() + mock_get_pass = mocker.patch('getpass.getpass') + mock_get_pass.return_value = password self.cli.invoke(self.args, strict=True) if not password_prefix: - self.teardown_mock() + expected_calls = [ + mocker.call(f'Please enter password for your new user ({login}): '), + mocker.call(f'Please re-enter password for your new user ({login}): '), + ] + mock_get_pass.assert_has_calls(expected_calls) # Check user has been added to the list of member/owners user = self.root.sf.getAdminService().lookupExperimenter(login) @@ -447,7 +448,7 @@ def testAddNoPassword(self): # Password subcommand # ======================================================================== @pytest.mark.parametrize("is_unicode", [True, False]) - def testPassword(self, is_unicode): + def testPassword(self, is_unicode, mocker): user = self.new_user() login = user.omeName.val self.args += ["password", "%s" % login] @@ -456,18 +457,20 @@ def testPassword(self, is_unicode): else: password = self.uuid() - self.setup_mock() - self.mox.StubOutWithMock(getpass, 'getpass') - i1 = 'Please enter password for your user (root): ' - i2 = 'Please enter password to be set: ' - i3 = 'Please re-enter password to be set: ' - getpass.getpass(i1).AndReturn(self.root.getProperty("omero.rootpass")) - getpass.getpass(i2).AndReturn(password) - getpass.getpass(i3).AndReturn(password) - self.mox.ReplayAll() + mock_get_pass = mocker.patch('getpass.getpass') + mock_get_pass.side_effect = [ + self.root.getProperty("omero.rootpass"), + password, + password + ] self.cli.invoke(self.args, strict=True) - self.teardown_mock() + expected_calls = [ + mocker.call('Please enter password for your user (root): '), + mocker.call('Please enter password to be set: '), + mocker.call('Please re-enter password to be set: ') + ] + mock_get_pass.assert_has_calls(expected_calls) # Check session creation using new password self.new_client(user=login, password=password)