<s> from __future__ import print_function , unicode_literals <EOL> from Cookie import SimpleCookie <EOL> from aspen . utils import utcnow <EOL> import gratipay <EOL> from gratipay . security . user import User , SESSION , SESSION_REFRESH <EOL> from gratipay . testing import Harness <EOL> class TestUser ( Harness ) : <EOL> def test_anonymous_user_is_anonymous ( self ) : <EOL> user = User ( ) <EOL> assert user . ANON <EOL> def test_anonymous_user_is_not_admin ( self ) : <EOL> user = User ( ) <EOL> assert not user . ADMIN <EOL> def test_known_user_is_known ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> alice = User . from_username ( '<STR_LIT>' ) <EOL> assert not alice . ANON <EOL> def test_username_is_case_insensitive ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> actual = User . from_username ( '<STR_LIT>' ) . participant . username_lower <EOL> assert actual == '<STR_LIT>' <EOL> def test_known_user_is_not_admin ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> alice = User . from_username ( '<STR_LIT>' ) <EOL> assert not alice . ADMIN <EOL> def test_admin_user_is_admin ( self ) : <EOL> self . make_participant ( '<STR_LIT>' , is_admin = True ) <EOL> alice = User . from_username ( '<STR_LIT>' ) <EOL> assert alice . ADMIN <EOL> def test_unreviewed_user_is_not_ANON ( self ) : <EOL> self . make_participant ( '<STR_LIT>' , is_suspicious = None ) <EOL> alice = User . from_username ( '<STR_LIT>' ) <EOL> assert alice . ANON is False <EOL> def test_whitelisted_user_is_not_ANON ( self ) : <EOL> self . make_participant ( '<STR_LIT>' , is_suspicious = False ) <EOL> alice = User . from_username ( '<STR_LIT>' ) <EOL> assert alice . ANON is False <EOL> def test_blacklisted_user_is_not_ANON ( self ) : <EOL> self . make_participant ( '<STR_LIT>' , is_suspicious = True ) <EOL> alice = User . from_username ( '<STR_LIT>' ) <EOL> assert alice . ANON is False <EOL> def test_user_from_bad_session_token_is_anonymous ( self ) : <EOL> user = User . from_session_token ( '<STR_LIT>' ) <EOL> assert user . ANON <EOL> def test_user_from_expired_session_is_anonymous ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> user = User . from_username ( '<STR_LIT>' ) <EOL> user . sign_in ( SimpleCookie ( ) ) <EOL> token = user . participant . session_token <EOL> user . participant . set_session_expires ( utcnow ( ) ) <EOL> user = User . from_session_token ( token ) <EOL> assert user . ANON <EOL> def test_user_from_None_session_token_is_anonymous ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> user = User . from_session_token ( None ) <EOL> assert user . ANON <EOL> def test_user_can_be_loaded_from_session_token ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> user = User . from_username ( '<STR_LIT>' ) <EOL> user . sign_in ( SimpleCookie ( ) ) <EOL> token = user . participant . session_token <EOL> actual = User . from_session_token ( token ) . participant . username <EOL> assert actual == '<STR_LIT>' <EOL> def test_session_cookie_is_secure_if_it_should_be ( self ) : <EOL> use_secure_cookies = gratipay . use_secure_cookies <EOL> gratipay . use_secure_cookies = True <EOL> try : <EOL> cookies = SimpleCookie ( ) <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> user = User . from_username ( '<STR_LIT>' ) <EOL> user . sign_in ( cookies ) <EOL> assert '<STR_LIT>' in cookies [ SESSION ] . output ( ) <EOL> finally : <EOL> gratipay . use_secure_cookies = use_secure_cookies <EOL> def test_session_is_regularly_refreshed ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> user = User . from_username ( '<STR_LIT>' ) <EOL> user . sign_in ( SimpleCookie ( ) ) <EOL> cookies = SimpleCookie ( ) <EOL> user . keep_signed_in ( cookies ) <EOL> assert SESSION not in cookies <EOL> cookies = SimpleCookie ( ) <EOL> expires = user . participant . session_expires <EOL> user . participant . set_session_expires ( expires - SESSION_REFRESH ) <EOL> user . keep_signed_in ( cookies ) <EOL> assert SESSION in cookies <EOL> def test_user_from_bad_id_is_anonymous ( self ) : <EOL> user = User . from_id ( <NUM_LIT> ) <EOL> assert user . ANON <EOL> def test_user_from_None_id_is_anonymous ( self ) : <EOL> user = User . from_id ( None ) <EOL> assert user . ANON <EOL> def test_user_can_be_loaded_from_id ( self ) : <EOL> alice = self . make_participant ( '<STR_LIT>' ) <EOL> actual = User . from_id ( alice . id ) . participant . username <EOL> assert actual == '<STR_LIT>' <EOL> def test_user_from_bad_username_is_anonymous ( self ) : <EOL> user = User . from_username ( '<STR_LIT>' ) <EOL> assert user . ANON <EOL> def test_signed_out_user_is_anonymous ( self ) : <EOL> self . make_participant ( '<STR_LIT>' ) <EOL> alice = User . from_username ( '<STR_LIT>' ) <EOL> assert not alice . ANON <EOL> alice . sign_out ( SimpleCookie ( ) ) <EOL> assert alice . ANON </s>
