Skip to content

Commit

Permalink
Remove all usages of mox3 in OMERO.py/OMERO.dropbox integration tests
Browse files Browse the repository at this point in the history
Due to compatibility issues with Python 3.11, these are replaced by
pytest-mock which wraps the standard library mock infrastructure.
  • Loading branch information
sbesson committed Oct 27, 2023
1 parent a0faef4 commit 892a545
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 99 deletions.
9 changes: 2 additions & 7 deletions components/tools/OmeroFS/test/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):

Expand All @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion components/tools/OmeroPy/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
8 changes: 0 additions & 8 deletions components/tools/OmeroPy/test/integration/clitest/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from omero.rtypes import rstring

from omero.testlib import ITest
from mox3 import mox


class AbstractCLITest(ITest):
Expand All @@ -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. "
Expand Down
88 changes: 40 additions & 48 deletions components/tools/OmeroPy/test/integration/clitest/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,47 +46,45 @@ 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):
with pytest.raises(NonZeroReturnCode):
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
Expand All @@ -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
"""
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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\'" \
Expand Down
4 changes: 0 additions & 4 deletions components/tools/OmeroPy/test/integration/clitest/test_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 34 additions & 31 deletions components/tools/OmeroPy/test/integration/clitest/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,29 @@ 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:
password = "ążćę"
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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down

0 comments on commit 892a545

Please sign in to comment.