Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements/tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ freezegun
pretend
pytest>=3.0.0
pytest-icdiff
pytest-mock
pytest-postgresql>=3.1.3,<8.0.0
pytest-randomly
pytest-socket
Expand Down
5 changes: 5 additions & 0 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ pytest==8.3.5 \
# via
# -r requirements/tests.in
# pytest-icdiff
# pytest-mock
# pytest-postgresql
# pytest-randomly
# pytest-socket
Expand All @@ -261,6 +262,10 @@ pytest-icdiff==0.9 \
--hash=sha256:13aede616202e57fcc882568b64589002ef85438046f012ac30a8d959dac8b75 \
--hash=sha256:efee0da3bd1b24ef2d923751c5c547fbb8df0a46795553fba08ef57c3ca03d82
# via -r requirements/tests.in
pytest-mock==3.14.0 \
--hash=sha256:0b72c38033392a5f4621342fe11e9219ac11ec9d375f8e2a0c164539e0d70f6f \
--hash=sha256:2719255a1efeceadbc056d6bf3df3d1c5015530fb40cf347c0f9afac88410bd0
# via -r requirements/tests.in
pytest-postgresql==7.0.1 \
--hash=sha256:7723dfbfc57ea6f6f9876c2828e7b36f8b0e60b6cb040b1ddd444a60eed06e0a \
--hash=sha256:cbc6a67bbad5128b1f00def8cca5cf597020acc79893723f7a9cb60981b6840f
Expand Down
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,10 @@ def search_service():


@pytest.fixture
def domain_status_service():
return account_services.NullDomainStatusService()
def domain_status_service(mocker):
service = account_services.NullDomainStatusService()
mocker.spy(service, "get_domain_status")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the fixture sets up the tracking internally, and doesn't change what's returned by calling the fixture, only adds some methods.

If I try to spy on a method that doesn't exist, an AttributeError is raised, so that protects us from spying on things that don't exist, whereas a call_recorder on a created stub offers no such protection.

return service


class QueryRecorder:
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/admin/views/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sqlalchemy.orm import joinedload
from webob.multidict import MultiDict, NoVars

from warehouse.accounts import IDomainStatusService
from warehouse.accounts.interfaces import IEmailBreachedService, IUserService
from warehouse.accounts.models import (
DisableReason,
Expand Down Expand Up @@ -1542,14 +1543,16 @@ def test_no_recovery_codes_provided(self, db_request, monkeypatch, user_service)


class TestUserEmailDomainCheck:
def test_user_email_domain_check(self, db_request):
def test_user_email_domain_check(self, db_request, domain_status_service, mocker):
user = UserFactory.create(with_verified_primary_email=True)
db_request.POST["email_address"] = user.primary_email.email
db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/foobar")
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

spied_req = mocker.spy(db_request, "find_service")

result = views.user_email_domain_check(user, db_request)

assert isinstance(result, HTTPSeeOther)
Expand All @@ -1562,3 +1565,14 @@ def test_user_email_domain_check(self, db_request):
]
assert user.primary_email.domain_last_checked is not None
assert user.primary_email.domain_last_status == ["active"]

# Total calls
assert domain_status_service.get_domain_status.call_count == 1
# most recent return value
assert domain_status_service.get_domain_status.spy_return == ["active"]
# all return values
assert domain_status_service.get_domain_status.spy_return_list == [["active"]]
# most recent exception
assert domain_status_service.get_domain_status.spy_exception is None

spied_req.assert_called_once_with(IDomainStatusService)