<s> from unittest import TestCase <EOL> from freezegun import freeze_time <EOL> from mock import Mock , patch <EOL> from alf . managers import Token , TokenError , TokenManager <EOL> class BaseTokenManagerTestCase ( TestCase ) : <EOL> END_POINT = '<STR_LIT>' <EOL> CLIENT_ID = '<STR_LIT>' <EOL> CLIENT_SECRET = '<STR_LIT>' <EOL> class TokenManagerTestCase ( BaseTokenManagerTestCase ) : <EOL> def setUp ( self ) : <EOL> with patch ( '<STR_LIT>' ) as self . mount_adapter : <EOL> self . manager = TokenManager ( <EOL> self . END_POINT , self . CLIENT_ID , self . CLIENT_SECRET ) <EOL> def test_should_start_with_no_token ( self ) : <EOL> self . assertFalse ( self . manager . _has_token ( ) ) <EOL> def test_should_NOT_setup_token_retries_by_default ( self ) : <EOL> self . assertEqual ( self . mount_adapter . call_count , <NUM_LIT:0> ) <EOL> def test_should_detect_expired_token ( self ) : <EOL> self . manager . _token = Token ( '<STR_LIT>' , expires_on = Token . calc_expires_on ( <NUM_LIT:0> ) ) <EOL> self . assertFalse ( self . manager . _has_token ( ) ) <EOL> def test_should_respect_valid_token ( self ) : <EOL> self . manager . _token = Token ( '<STR_LIT>' , expires_on = Token . calc_expires_on ( <NUM_LIT:10> ) ) <EOL> self . assertTrue ( self . manager . _has_token ( ) ) <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_should_reset_token ( self ) : <EOL> self . manager . reset_token ( ) <EOL> self . assertEqual ( self . manager . _token . access_token , '<STR_LIT>' ) <EOL> self . assertEqual ( self . manager . _token . expires_on , <EOL> Token . calc_expires_on ( <NUM_LIT:0> ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_should_be_able_to_request_a_new_token ( self , post ) : <EOL> post . return_value . json . return_value = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : Token . calc_expires_on ( <NUM_LIT:10> ) , <EOL> } <EOL> self . manager . _request_token ( ) <EOL> post . assert_called_with ( self . END_POINT , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> auth = ( self . CLIENT_ID , self . CLIENT_SECRET ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_should_raise_token_error_for_bad_token ( self , post ) : <EOL> post . return_value = Mock ( ) <EOL> post . return_value . ok = False <EOL> post . return_value . status_code = <NUM_LIT> <EOL> with self . assertRaises ( TokenError ) as context : <EOL> self . manager . _request_token ( ) <EOL> self . assertEqual ( context . exception . response . status_code , <NUM_LIT> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_token_data_should_obtain_new_token ( self , _request_token ) : <EOL> self . manager . reset_token ( ) <EOL> self . manager . _get_token_data ( ) <EOL> self . assertTrue ( _request_token . called ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_update_token_should_set_a_token_with_data_retrieved ( self , _request_token ) : <EOL> self . manager . reset_token ( ) <EOL> expires = <NUM_LIT:100> <EOL> _request_token . return_value = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : expires } <EOL> self . manager . _token = Token ( '<STR_LIT>' , <EOL> expires_on = <NUM_LIT:0> ) <EOL> self . manager . _update_token ( ) <EOL> self . assertTrue ( _request_token . called ) <EOL> self . assertEqual ( self . manager . _token . access_token , '<STR_LIT>' ) <EOL> self . assertEqual ( self . manager . _token . expires_on , Token . calc_expires_on ( expires ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_update_token_should_set_a_token_with_data_retrieved_from_storage ( self , _request_token ) : <EOL> expires = Token . calc_expires_on ( <NUM_LIT:100> ) <EOL> _request_token . return_value = dict ( ) <EOL> self . manager . _token = Token ( '<STR_LIT>' , <EOL> expires_on = expires ) <EOL> self . manager . _token_storage ( self . manager . _token ) <EOL> self . manager . _update_token ( ) <EOL> self . assertFalse ( _request_token . called ) <EOL> self . assertEqual ( self . manager . _token . access_token , '<STR_LIT>' ) <EOL> self . assertEqual ( self . manager . _token . expires_on , expires ) <EOL> def test_should_return_token_value ( self ) : <EOL> self . manager . _token = Token ( '<STR_LIT>' , expires_on = Token . calc_expires_on ( <NUM_LIT:10> ) ) <EOL> self . assertEqual ( self . manager . get_token ( ) , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_token_should_request_a_new_token_if_do_not_have_a_token ( self , _has_token , _update_token ) : <EOL> _has_token . return_value = False <EOL> self . manager . get_token ( ) <EOL> self . assertTrue ( _update_token . called ) <EOL> class TokenManagerWithRetriesTestCase ( BaseTokenManagerTestCase ) : <EOL> def test_should_start_with_no_token ( self ) : <EOL> with patch ( '<STR_LIT>' ) as self . mount_adapter : <EOL> self . manager = TokenManager ( <EOL> self . END_POINT , self . CLIENT_ID , self . CLIENT_SECRET , <EOL> token_retries = <NUM_LIT> ) <EOL> self . mount_adapter . assert_called_once_with ( <EOL> self . manager . _session , <NUM_LIT> ) </s>
