question 284 Advanced OOP & Design · easy
Session tokens with an injected clock
SessionTokens calls datetime.now() directly, so nobody can test whether a token really expires after 30 minutes without waiting 30 minutes. Refactor it so the time comes from a clock parameter: a function returning the current datetime, defaulting to datetime.now so existing callers don't change.
Class
SessionTokens(ttl_minutes=30, clock=datetime.now)Behaviour
issue(user) records the time; is_valid(user) is True while less than ttl_minutes have passed since it was issuedUnchanged
SessionTokens() still works with the real clock