From a33a0fc246048e04701653b0845d1d4b9eb4e34d Mon Sep 17 00:00:00 2001 From: Pawel Peregud Date: Wed, 28 Aug 2024 17:49:05 +0200 Subject: [PATCH] add service unit test --- backend/tests/conftest.py | 15 ++++++++ .../modules/user/antisybil/test_antisybil.py | 38 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 192fcbd7e..1e2d2c2af 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -96,6 +96,13 @@ MOCK_IS_CONTRACT = Mock() +def mock_holonym_check(*args, **kwargs): + if args[0] == "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266": + return (True, ["phone"]) + else: + return (False, []) + + def mock_gitcoin_passport_issue_address_for_scoring(*args, **kwargs): if args[0] == "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266": return { @@ -1187,6 +1194,14 @@ def patch_user_budget(monkeypatch): MOCK_GET_USER_BUDGET.return_value = USER_MOCKED_BUDGET +@pytest.fixture(scope="function") +def patch_holonym_check(monkeypatch): + monkeypatch.setattr( + "app.modules.user.antisybil.service.holonym.check", + mock_holonym_check, + ) + + @pytest.fixture(scope="function") def patch_gitcoin_passport_issue_address_for_scoring(monkeypatch): monkeypatch.setattr( diff --git a/backend/tests/modules/user/antisybil/test_antisybil.py b/backend/tests/modules/user/antisybil/test_antisybil.py index 38b6195dd..fe1888c7e 100644 --- a/backend/tests/modules/user/antisybil/test_antisybil.py +++ b/backend/tests/modules/user/antisybil/test_antisybil.py @@ -5,6 +5,7 @@ from app.infrastructure import database from app.modules.common.delegation import get_hashed_addresses from app.modules.user.antisybil.service.passport import GitcoinPassportAntisybil +from app.modules.user.antisybil.service.holonym import HolonymAntisybil from tests.helpers.context import get_context @@ -13,7 +14,40 @@ def before(app): pass -def test_antisybil_service( +def test_holonym_antisybil_service( + mock_users_db, + patch_holonym_check, +): + context = get_context(4) + service = HolonymAntisybil() + + # check unknown user + unknown_address = "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720" + try: + service.get_sbt_status(context, unknown_address) + except UserNotFound: + pass # expected + + alice, _, _ = mock_users_db + + # SBT not yet cached + assert service.get_sbt_status(context, alice.address) is None + + # SBT fetching for known owner returns expected data + entry = service.fetch_sbt_status(context, alice.address) + assert entry.has_sbt is True + assert entry.sbt_details == ["phone"] + + # Lets cache the result + service.update_sbt_status(context, alice.address, entry.has_sbt, entry.sbt_details) + + # Check cache state + entry = service.get_sbt_status(context, alice.address) + assert entry.has_sbt is True + assert entry.sbt_details == ["phone"] + + +def test_passport_antisybil_service( patch_gitcoin_passport_issue_address_for_scoring, patch_gitcoin_passport_fetch_score, patch_gitcoin_passport_fetch_stamps, @@ -88,7 +122,7 @@ def test_guest_stamp_score_bump_for_both_gp_and_octant_side_application( ) # is on guest list, HAS GUEST LIST STAMP, score is from fetch -def test_antisybil_cant_be_update_when_address_is_delegated(alice, bob): +def test_passport_antisybil_cant_be_update_when_address_is_delegated(alice, bob): context = get_context(4) score = 2.572 primary, secondary, both = get_hashed_addresses(alice.address, bob.address)