Skip to content

Commit

Permalink
Fix a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Aug 15, 2024
1 parent 3a34433 commit 82e4470
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
3 changes: 0 additions & 3 deletions h/security/policy/_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ def identity(self, request):

userid, ticket_id = self._get_cookie_value()

if not ticket_id:
return None

user = request.find_service(AuthTicketService).verify_ticket(userid, ticket_id)

if (not user) or user.deleted:
Expand Down
5 changes: 2 additions & 3 deletions h/services/auth_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ def __init__(self, session, user_service):
self._user_service = user_service
self._user = None

def verify_ticket(self, userid: str, ticket_id: str) -> User | None:
def verify_ticket(self, userid: str | None, ticket_id: str | None) -> User | None:
"""
Return the User object matching the given userid and ticket_id, or None.
Verify that there is an unexpired AuthTicket in the DB matching the
given `userid` and `ticket_id` and if so return the User corresponding
User object.
given `userid` and `ticket_id` and if so return the corresponding User.
"""

if self._user:
Expand Down
7 changes: 0 additions & 7 deletions tests/unit/h/security/policy/_cookie_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ def test_identity(self, pyramid_request, auth_ticket_service, cookie_policy, use
user=auth_ticket_service.verify_ticket.return_value
)

def test_identity_when_no_ticket_in_cookie(
self, cookie, cookie_policy, pyramid_request
):
cookie.get_value.return_value = None

assert cookie_policy.identity(pyramid_request) is None

def test_identity_when_user_marked_as_deleted(
self, pyramid_request, auth_ticket_service, cookie_policy
):
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/h/services/auth_ticket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ def test_verify_ticket_short_circuits_if_user_cache_is_set(self, service):
service.verify_ticket(sentinel.userid, sentinel.ticket_id) == service._user
)

def test_verify_ticket_returns_None_if_there_is_no_ticket(self, service, user):
@pytest.mark.usefixtures("auth_ticket")
def test_verify_ticket_returns_None_if_theres_no_matching_ticket(
self, service, user
):
assert service.verify_ticket(user.userid, ticket_id="does_not_exist") is None

def test_verify_ticket_when_theres_no_userid(self, service, auth_ticket):
assert service.verify_ticket(None, ticket_id=auth_ticket.id) is None

@pytest.mark.usefixtures("auth_ticket")
def test_verify_ticket_when_theres_no_ticket_id(self, service, user):
assert service.verify_ticket(user.userid, ticket_id=None) is None

def test_verify_ticket_returns_None_if_the_ticket_has_expired(
self, service, auth_ticket
):
Expand Down

0 comments on commit 82e4470

Please sign in to comment.