Skip to content

Commit 4f16c20

Browse files
committed
refactor(tests): dedupe mock google user exists yes/no
1 parent fb6c52d commit 4f16c20

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ def mock_google_user_exists(mock_module_name):
136136
return mock_module_name("user_exists")
137137

138138

139+
@pytest.fixture
140+
def mock_google_user_exists_no(mock_google_user_exists):
141+
"""Fixture returns a function that patches the user_exists function from a given module to return False."""
142+
143+
def _mock_google_user_exists_no(module):
144+
exists = mock_google_user_exists(module)
145+
exists.return_value = False
146+
return exists
147+
148+
return _mock_google_user_exists_no
149+
150+
151+
@pytest.fixture
152+
def mock_google_user_exists_yes(mock_google_user_exists):
153+
"""Fixture returns a function that patches the user_exists function from a given module to return True."""
154+
155+
def _mock_google_user_exists_yes(module):
156+
exists = mock_google_user_exists(module)
157+
exists.return_value = True
158+
return exists
159+
160+
return _mock_google_user_exists_yes
161+
162+
139163
@pytest.fixture
140164
def mock_google_user_info(mock_module_name):
141165
"""Fixture returns a function that patches the user_info function from a given module."""

tests/services/test_google.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ def mock_google_CallGAMCommand(mock_google_CallGAMCommand):
3838

3939

4040
@pytest.fixture
41-
def mock_google_user_exists(mock_google_user_exists):
42-
return mock_google_user_exists(MODULE)
41+
def mock_google_user_exists_no(mock_google_user_exists_no):
42+
return mock_google_user_exists_no(MODULE)
43+
44+
45+
@pytest.fixture
46+
def mock_google_user_exists_yes(mock_google_user_exists_yes):
47+
return mock_google_user_exists_yes(MODULE)
4348

4449

4550
@pytest.fixture
@@ -217,29 +222,26 @@ def test_user_info_user_does_not_exists(mock_gam_CallGAMCommand):
217222
assert res == {}
218223

219224

220-
def test_user_in_group_user_does_not_exist(mock_google_user_exists, capfd):
221-
mock_google_user_exists.return_value = False
222-
225+
@pytest.mark.usefixtures("mock_google_user_exists_no")
226+
def test_user_in_group_user_does_not_exist(capfd):
223227
res = user_in_group("username", "group")
224228
captured = capfd.readouterr()
225229

226230
assert res is False
227231
assert "User does not exist" in captured.out
228232

229233

230-
@pytest.mark.usefixtures("mock_google_CallGAMCommand")
231-
def test_user_in_group_user_exists_in_group(mock_google_user_exists, mock_NamedTemporaryFile_with_readlines):
232-
mock_google_user_exists.return_value = True
234+
@pytest.mark.usefixtures("mock_google_CallGAMCommand", "mock_google_user_exists_yes")
235+
def test_user_in_group_user_exists_in_group(mock_NamedTemporaryFile_with_readlines):
233236
mock_NamedTemporaryFile_with_readlines(MODULE, ["group"])
234237

235238
res = user_in_group("username", "group")
236239

237240
assert res is True
238241

239242

240-
@pytest.mark.usefixtures("mock_google_CallGAMCommand")
241-
def test_user_in_group_user_exists_not_in_group(mock_google_user_exists, mock_NamedTemporaryFile_with_readlines):
242-
mock_google_user_exists.return_value = True
243+
@pytest.mark.usefixtures("mock_google_CallGAMCommand", "mock_google_user_exists_yes")
244+
def test_user_in_group_user_exists_not_in_group(mock_NamedTemporaryFile_with_readlines):
243245
mock_NamedTemporaryFile_with_readlines(MODULE, ["group"])
244246

245247
res = user_in_group("username", "nope")

0 commit comments

Comments
 (0)