<s> from __future__ import unicode_literals <EOL> import hashlib <EOL> import mock <EOL> from mock . mock import MagicMock <EOL> from spockbot . plugins . core . auth import AuthCore , AuthPlugin , java_hex_digest <EOL> def test_java_hex_digest ( ) : <EOL> negative_hash = java_hex_digest ( hashlib . sha1 ( b'<STR_LIT:a>' ) ) <EOL> assert negative_hash == '<STR_LIT>' <EOL> positive_hash = java_hex_digest ( hashlib . sha1 ( b'<STR_LIT:d>' ) ) <EOL> assert positive_hash == '<STR_LIT>' <EOL> def get_mocked_auth_core ( ) : <EOL> event_mock = MagicMock ( ) <EOL> auth_core = AuthCore ( event_mock , MagicMock ( ) , MagicMock ( ) ) <EOL> auth_core . online_mode = False <EOL> auth_core . auth_timeout = <NUM_LIT:3> <EOL> auth_core . ygg = ygg_mock = MagicMock ( ) <EOL> auth_core . ygg . password = '<STR_LIT>' <EOL> auth_core . ygg . username = '<STR_LIT>' <EOL> return auth_core , event_mock , ygg_mock <EOL> def test_offline_username ( ) : <EOL> auth , event , ygg = get_mocked_auth_core ( ) <EOL> auth . online_mode = False <EOL> assert auth . username is None <EOL> auth . username = '<STR_LIT>' <EOL> assert auth . username is None <EOL> auth . start_session ( ) <EOL> assert auth . username == '<STR_LIT>' <EOL> assert ygg . mock_calls == [ ] <EOL> def test_online_username ( ) : <EOL> auth , event , ygg = get_mocked_auth_core ( ) <EOL> auth . online_mode = True <EOL> assert auth . username is None <EOL> auth . username = '<STR_LIT>' <EOL> auth . ygg . selected_profile = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> assert auth . username is None <EOL> ygg . login . return_value = False <EOL> auth . start_session ( ) <EOL> assert auth . username is None <EOL> ygg . login . return_value = True <EOL> auth . start_session ( ) <EOL> assert auth . username == '<STR_LIT>' <EOL> def test_password ( ) : <EOL> auth , event , ygg = get_mocked_auth_core ( ) <EOL> assert auth . password is False <EOL> auth . password = '<STR_LIT>' <EOL> assert auth . password is False <EOL> auth . password = '<STR_LIT:password>' <EOL> assert auth . password is True <EOL> def test_start_session_online_success ( ) : <EOL> auth , event , ygg = get_mocked_auth_core ( ) <EOL> auth . online_mode = True <EOL> ygg . login . return_value = True <EOL> res = auth . start_session ( ) <EOL> assert res <EOL> assert not event . called <EOL> assert auth . username <EOL> def test_start_session_online_failure ( ) : <EOL> auth , event , ygg = get_mocked_auth_core ( ) <EOL> auth . online_mode = True <EOL> ygg . login . return_value = False <EOL> res = auth . start_session ( ) <EOL> assert not res <EOL> assert event . emit . called <EOL> assert not auth . username <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_shared_secret ( rnd ) : <EOL> auth , event , ygg = get_mocked_auth_core ( ) <EOL> assert not rnd . called <EOL> assert not auth . _shared_secret <EOL> ss = auth . shared_secret <EOL> assert auth . shared_secret == ss <EOL> assert rnd . called <EOL> def get_mocked_auth_plugin ( ) : <EOL> auth_plugin = AuthPlugin ( MagicMock ( ) , MagicMock ( ) ) <EOL> auth_plugin . auth_quit = False <EOL> auth_plugin . sess_quit = False <EOL> auth_plugin . event = MagicMock ( ) <EOL> return auth_plugin <EOL> def test_handle_auth_error ( ) : <EOL> auth = get_mocked_auth_plugin ( ) <EOL> auth . handle_auth_error ( None , None ) <EOL> assert not auth . event . kill . called <EOL> auth . auth_quit = True <EOL> auth . handle_auth_error ( None , None ) <EOL> assert auth . event . kill . called <EOL> def test_handle_session_error ( ) : <EOL> auth = get_mocked_auth_plugin ( ) <EOL> auth . handle_session_error ( None , None ) <EOL> assert not auth . event . kill . called <EOL> auth . sess_quit = True <EOL> auth . handle_session_error ( None , None ) <EOL> assert auth . event . kill . called </s>
