def test_current(monkeypatch): account = Record.new() acid, *tokens = (utils.unique_hash() for _ in range(3)) # Split the list (aproximatly) in half and save the tokens cookies, sessions = [tokens[0]], [tokens[-1]] for x in cookies: auth.cookies.save(acct=account, acid=acid, token=x) for x in sessions: auth.cookies.save(acct=account, acid=acid, token=x, session=True) # Test the attributes and the return value for token in tokens: current = auth.Current(token=token, acid=acid) assert current.loggedin assert not current.multi assert current.record == account # TODO: Be consistent and use `patch` from `unittest.mock`? # Test session lifetime monkeypatch.setattr("time.time", session_time) for token in sessions: current = auth.Current(token=token, acid=acid) assert not current.loggedin assert not current.multi # Test cookie lifetime monkeypatch.setattr("time.time", cookie_time) for token in cookies: current = auth.Current(token=token, acid=acid) assert not current.loggedin assert not current.multi
def test_multilogin(monkeypatch): # TODO: Test switch_to acct1, acct2 = Record.new(), Record.new() ids = {acct1.id, acct2.id} acid, token1, token2 = (utils.unique_hash() for _ in range(3)) # Save the first token auth.cookies.save(acct=acct1, token=token1, acid=acid) # Save the second token (with different lifetime) auth.cookies.save(acct=acct2, token=token2, acid=acid, session=True) # Both tokens must work correctly current = auth.Current(token=token1, acid=acid) assert current.loggedin assert current.record == acct1 assert current.multi assert {x.id for x in current.records} == ids current = auth.Current(token=token2, acid=acid) assert current.loggedin assert current.record == acct2 assert current.multi assert {x.id for x in current.records} == ids # Test tokens' lifetime monkeypatch.setattr("time.time", session_time) current = auth.Current(token=token2, acid=acid) assert not current.loggedin assert not current.multi monkeypatch.setattr("time.time", cookie_time) current = auth.Current(token=token1, acid=acid) assert not current.loggedin assert not current.multi
def test_cookies(): account = Record.new() # No distinction between real cookies and session acid, *tokens = (utils.unique_hash() for _ in range(3)) for token in tokens: auth.cookies.save(acct=account, acid=acid, token=token) it = auth.cookies.get_by_acid(acid) assert sum(1 for _ in it) == len(tokens) auth.cookies.delete_tokens(tokens) assert next(auth.cookies.get_by_acid(acid), None) is None