<s> import datetime <EOL> import uuid <EOL> from oslo_config import cfg <EOL> from st2tests . base import DbTestCase <EOL> from st2common . util import isotime <EOL> from st2common . util import date as date_utils <EOL> from st2common . exceptions . auth import TokenNotFoundError <EOL> from st2common . persistence . auth import Token <EOL> from st2common . services import access <EOL> import st2tests . config as tests_config <EOL> USERNAME = '<STR_LIT>' <EOL> class AccessServiceTest ( DbTestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> super ( AccessServiceTest , cls ) . setUpClass ( ) <EOL> tests_config . parse_args ( ) <EOL> def test_create_token ( self ) : <EOL> token = access . create_token ( USERNAME ) <EOL> self . assertTrue ( token is not None ) <EOL> self . assertTrue ( token . token is not None ) <EOL> self . assertEqual ( token . user , USERNAME ) <EOL> def test_create_token_fail ( self ) : <EOL> try : <EOL> access . create_token ( None ) <EOL> self . assertTrue ( False , '<STR_LIT>' ) <EOL> except ValueError : <EOL> self . assertTrue ( True ) <EOL> def test_delete_token ( self ) : <EOL> token = access . create_token ( USERNAME ) <EOL> access . delete_token ( token . token ) <EOL> try : <EOL> token = Token . get ( token . token ) <EOL> self . assertTrue ( False , '<STR_LIT>' ) <EOL> except TokenNotFoundError : <EOL> self . assertTrue ( True ) <EOL> def test_delete_non_existent_token ( self ) : <EOL> token = uuid . uuid4 ( ) . hex <EOL> self . assertRaises ( TokenNotFoundError , Token . get , token ) <EOL> access . delete_token ( token ) <EOL> def test_create_token_ttl_ok ( self ) : <EOL> ttl = <NUM_LIT:10> <EOL> token = access . create_token ( USERNAME , <NUM_LIT:10> ) <EOL> self . assertTrue ( token is not None ) <EOL> self . assertTrue ( token . token is not None ) <EOL> self . assertEqual ( token . user , USERNAME ) <EOL> expected_expiry = date_utils . get_datetime_utc_now ( ) + datetime . timedelta ( seconds = ttl ) <EOL> expected_expiry = date_utils . add_utc_tz ( expected_expiry ) <EOL> self . assertLess ( isotime . parse ( token . expiry ) , expected_expiry ) <EOL> def test_create_token_ttl_capped ( self ) : <EOL> ttl = cfg . CONF . auth . token_ttl + <NUM_LIT:10> <EOL> expected_expiry = date_utils . get_datetime_utc_now ( ) + datetime . timedelta ( seconds = ttl ) <EOL> expected_expiry = date_utils . add_utc_tz ( expected_expiry ) <EOL> token = access . create_token ( USERNAME , <NUM_LIT:10> ) <EOL> self . assertTrue ( token is not None ) <EOL> self . assertTrue ( token . token is not None ) <EOL> self . assertEqual ( token . user , USERNAME ) <EOL> self . assertLess ( isotime . parse ( token . expiry ) , expected_expiry ) </s>
