<s> from __future__ import print_function <EOL> import os <EOL> from authcode . _compat import to_unicode <EOL> from authcode . views import pop_next_url <EOL> from flask import Flask , request <EOL> from sqlalchemy_wrapper import SQLAlchemy <EOL> import authcode <EOL> from helpers import SECRET_KEY <EOL> class Session ( dict ) : <EOL> saved = <NUM_LIT:0> <EOL> def save ( self ) : <EOL> self . saved += <NUM_LIT:1> <EOL> def _get_flask_app ( roles = False , ** kwargs ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( <EOL> SECRET_KEY , db = db , roles = roles , password_minlen = <NUM_LIT:3> , ** kwargs ) <EOL> User = auth . User <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . add ( user ) <EOL> user2 = User ( login = u'<STR_LIT:foo>' , password = '<STR_LIT:bar>' ) <EOL> db . add ( user2 ) <EOL> db . commit ( ) <EOL> app = Flask ( '<STR_LIT:test>' ) <EOL> app . secret_key = os . urandom ( <NUM_LIT:32> ) <EOL> app . testing = True <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ auth . protected ( ) <EOL> def protected ( ) : <EOL> return u'<STR_LIT>' <EOL> authcode . setup_for_flask ( auth , app ) <EOL> auth . session = { } <EOL> return auth , app , user <EOL> def test_pop_next_url ( ) : <EOL> auth = authcode . Auth ( SECRET_KEY ) <EOL> session = { auth . redirect_key : '<STR_LIT>' } <EOL> next_url = pop_next_url ( auth , request , session ) <EOL> assert next_url == '<STR_LIT>' <EOL> auth . sign_in_redirect = '<STR_LIT>' <EOL> next_url = pop_next_url ( auth , request , { } ) <EOL> assert next_url == auth . sign_in_redirect <EOL> auth . sign_in_redirect = lambda request : '<STR_LIT>' <EOL> next_url = pop_next_url ( auth , request , { } ) <EOL> assert next_url == '<STR_LIT>' <EOL> auth . sign_in_redirect = None <EOL> next_url = pop_next_url ( auth , request , { } ) <EOL> assert next_url == '<STR_LIT:/>' <EOL> def test_login_view ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> r = client . get ( auth . url_sign_in ) <EOL> assert u'<STR_LIT>' in to_unicode ( r . data ) <EOL> r = client . post ( auth . url_sign_in ) <EOL> assert u'<STR_LIT>' not in to_unicode ( r . data ) <EOL> data = { <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> r = client . post ( auth . url_sign_in , data = data ) <EOL> assert u'<STR_LIT>' in to_unicode ( r . data ) <EOL> assert auth . session_key not in auth . session <EOL> def test_login_wrong_credentials ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> r = client . post ( auth . url_sign_in , data = data ) <EOL> assert u'<STR_LIT>' in to_unicode ( r . data ) <EOL> assert auth . session_key not in auth . session <EOL> def test_login_very_wrong_credentials ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:password>' : int , <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> r = client . post ( auth . url_sign_in , data = data ) <EOL> assert u'<STR_LIT>' in to_unicode ( r . data ) <EOL> assert auth . session_key not in auth . session <EOL> def test_login_right_credentials ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : user . login , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> r = client . post ( auth . url_sign_in , data = data ) <EOL> assert r . status == '<STR_LIT>' <EOL> assert auth . session_key in auth . session <EOL> def test_login_redirect_if_already_logged_in ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> auth . session [ auth . redirect_key ] = '<STR_LIT>' <EOL> r = client . get ( auth . url_sign_in ) <EOL> assert r . status == '<STR_LIT>' <EOL> def test_logout_before_login_again ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : user . login , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> client . post ( auth . url_sign_in , data = data ) <EOL> assert auth . session_key in auth . session <EOL> uhmac1 = auth . session [ auth . session_key ] <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> client . post ( auth . url_sign_in , data = data ) <EOL> assert auth . session_key in auth . session <EOL> uhmac2 = auth . session [ auth . session_key ] <EOL> assert uhmac1 != uhmac2 <EOL> def test_unprotected_logout ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> r = client . get ( auth . url_sign_out ) <EOL> assert auth . session_key in auth . session <EOL> assert r . status == '<STR_LIT>' <EOL> r = client . post ( auth . url_sign_out ) <EOL> assert auth . session_key in auth . session <EOL> assert r . status == '<STR_LIT>' <EOL> def test_redirect_after_logout ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> url = '<STR_LIT>' . format ( auth . url_sign_out , auth . get_csrf_token ( ) ) <EOL> r = client . get ( url ) <EOL> assert r . status == '<STR_LIT>' <EOL> assert auth . session_key not in auth . session <EOL> auth . login ( user ) <EOL> auth . sign_out_redirect = '<STR_LIT>' <EOL> url = '<STR_LIT>' . format ( auth . url_sign_out , auth . get_csrf_token ( ) ) <EOL> r = client . get ( url ) <EOL> assert r . status == '<STR_LIT>' <EOL> assert r . location . endswith ( '<STR_LIT>' ) <EOL> assert auth . session_key not in auth . session <EOL> auth . login ( user ) <EOL> auth . sign_out_redirect = lambda request : '<STR_LIT>' <EOL> url = '<STR_LIT>' . format ( auth . url_sign_out , auth . get_csrf_token ( ) ) <EOL> r = client . get ( url ) <EOL> assert r . status == '<STR_LIT>' <EOL> assert r . location . endswith ( '<STR_LIT>' ) <EOL> assert auth . session_key not in auth . session <EOL> def test_reset_password ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> log = [ ] <EOL> def send_email ( user , subject , msg ) : <EOL> log . append ( msg ) <EOL> auth . send_email = send_email <EOL> user . get_token ( ) <EOL> r = client . get ( auth . url_reset_password ) <EOL> data = to_unicode ( r . data ) <EOL> print ( data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_reset_password_wrong_account ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> log = [ ] <EOL> def send_email ( user , subject , msg ) : <EOL> log . append ( msg ) <EOL> auth . send_email = send_email <EOL> user . get_token ( ) <EOL> data = dict ( login = u'<STR_LIT>' , _csrf_token = auth . get_csrf_token ( ) ) <EOL> r = client . post ( auth . url_reset_password , data = data ) <EOL> data = to_unicode ( r . data ) <EOL> print ( data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_reset_password_email_sent ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> log = [ ] <EOL> def send_email ( user , subject , msg ) : <EOL> log . append ( msg ) <EOL> auth . send_email = send_email <EOL> token = user . get_token ( ) <EOL> data = dict ( login = user . login , _csrf_token = auth . get_csrf_token ( ) ) <EOL> r = client . post ( auth . url_reset_password , data = data ) <EOL> data = to_unicode ( r . data ) <EOL> assert u'<STR_LIT>' in data <EOL> print ( log ) <EOL> assert u'<STR_LIT>' . format ( auth . url_reset_password , token [ : <NUM_LIT:5> ] ) in log [ <NUM_LIT:0> ] <EOL> def test_reset_password_default_emailer ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> data = dict ( login = user . login , _csrf_token = auth . get_csrf_token ( ) ) <EOL> resp = client . post ( auth . url_reset_password , data = data ) <EOL> data = to_unicode ( resp . data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_reset_password_wrong_token ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> log = [ ] <EOL> def send_email ( user , subject , msg ) : <EOL> log . append ( msg ) <EOL> auth . send_email = send_email <EOL> user . get_token ( ) <EOL> r = client . get ( auth . url_reset_password + '<STR_LIT>' ) <EOL> data = to_unicode ( r . data ) <EOL> print ( data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_reset_password_good_token ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> log = [ ] <EOL> def send_email ( user , subject , msg ) : <EOL> log . append ( msg ) <EOL> auth . send_email = send_email <EOL> token = user . get_token ( ) <EOL> r = client . get ( auth . url_reset_password + token + '<STR_LIT:/>' ) <EOL> data = to_unicode ( r . data ) <EOL> assert auth . session_key in auth . session <EOL> assert u'<STR_LIT>' in data <EOL> assert u'<STR_LIT>' not in data <EOL> r = client . get ( auth . url_reset_password ) <EOL> assert r . status == '<STR_LIT>' <EOL> def test_change_password_redir ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> r = client . get ( auth . url_change_password ) <EOL> assert r . status == '<STR_LIT>' <EOL> auth . login ( user ) <EOL> r = client . get ( auth . url_change_password ) <EOL> data = to_unicode ( r . data ) <EOL> assert u'<STR_LIT>' in data <EOL> assert u'<STR_LIT>' in data <EOL> def test_change_password_no_current_password ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> csrf_token = auth . get_csrf_token ( ) <EOL> r = client . post ( auth . url_change_password , data = dict ( <EOL> np1 = '<STR_LIT>' , np2 = '<STR_LIT>' , _csrf_token = csrf_token ) ) <EOL> data = to_unicode ( r . data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_change_password_wrong_current_password ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> csrf_token = auth . get_csrf_token ( ) <EOL> r = client . post ( auth . url_change_password , data = dict ( <EOL> password = '<STR_LIT>' , np1 = '<STR_LIT>' , np2 = '<STR_LIT>' , _csrf_token = csrf_token ) ) <EOL> data = to_unicode ( r . data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_change_password_too_short ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> csrf_token = auth . get_csrf_token ( ) <EOL> r = client . post ( auth . url_change_password , data = dict ( <EOL> password = '<STR_LIT>' , np1 = '<STR_LIT:a>' , np2 = '<STR_LIT:a>' , _csrf_token = csrf_token ) ) <EOL> data = to_unicode ( r . data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_change_password_too_long ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> csrf_token = auth . get_csrf_token ( ) <EOL> HUGE_PASSWORD = '<STR_LIT:x>' * <NUM_LIT> <EOL> r = client . post ( auth . url_change_password , data = dict ( <EOL> password = '<STR_LIT>' , np1 = HUGE_PASSWORD , np2 = HUGE_PASSWORD , _csrf_token = csrf_token ) ) <EOL> data = to_unicode ( r . data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_change_password_mismatch ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> csrf_token = auth . get_csrf_token ( ) <EOL> r = client . post ( auth . url_change_password , data = dict ( <EOL> password = '<STR_LIT>' , np1 = '<STR_LIT>' , np2 = '<STR_LIT:a>' , _csrf_token = csrf_token ) ) <EOL> data = to_unicode ( r . data ) <EOL> print ( data ) <EOL> assert u'<STR_LIT>' in data <EOL> def test_change_password_no_csrf ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> r = client . post ( auth . url_change_password , data = dict ( <EOL> password = '<STR_LIT>' , np1 = '<STR_LIT>' , np2 = '<STR_LIT>' ) ) <EOL> assert r . status == '<STR_LIT>' <EOL> def test_change_password_ok ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> client = app . test_client ( ) <EOL> auth . login ( user ) <EOL> csrf_token = auth . get_csrf_token ( ) <EOL> r = client . post ( auth . url_change_password , data = dict ( <EOL> password = '<STR_LIT>' , np1 = '<STR_LIT>' , np2 = '<STR_LIT>' , _csrf_token = csrf_token ) ) <EOL> data = to_unicode ( r . data ) <EOL> assert u'<STR_LIT>' in data <EOL> assert user . has_password ( '<STR_LIT>' ) <EOL> def test_custom_templates ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> options = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> inbox = [ ] <EOL> def send_email ( user , subject , msg ) : <EOL> inbox . append ( msg ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , ** options ) <EOL> User = auth . User <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . add ( user ) <EOL> db . commit ( ) <EOL> custom_templates = os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> app = Flask ( '<STR_LIT:test>' , template_folder = custom_templates ) <EOL> app . secret_key = os . urandom ( <NUM_LIT:32> ) <EOL> app . testing = True <EOL> authcode . setup_for_flask ( auth , app , send_email = send_email ) <EOL> auth . session = { } <EOL> client = app . test_client ( ) <EOL> resp = client . get ( auth . url_sign_in ) <EOL> assert resp . data == b'<STR_LIT>' <EOL> resp = client . get ( auth . url_reset_password ) <EOL> assert resp . data == b'<STR_LIT>' <EOL> data = dict ( login = user . login , _csrf_token = auth . get_csrf_token ( ) ) <EOL> resp = client . post ( auth . url_reset_password , data = data ) <EOL> assert inbox [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> auth . login ( user ) <EOL> resp = client . get ( auth . url_change_password ) <EOL> assert resp . data == b'<STR_LIT>' <EOL> url = '<STR_LIT>' . format ( auth . url_sign_out , auth . get_csrf_token ( ) ) <EOL> resp = client . get ( url ) <EOL> assert resp . data == b'<STR_LIT>' <EOL> def test_pop_next_url_save ( ) : <EOL> auth = authcode . Auth ( SECRET_KEY ) <EOL> session = Session ( { auth . redirect_key : '<STR_LIT>' } ) <EOL> next_url = pop_next_url ( auth , request , session ) <EOL> assert next_url == '<STR_LIT>' <EOL> assert session . saved == <NUM_LIT:1> <EOL> def test_login_logout_save ( ) : <EOL> auth , app , user = _get_flask_app ( ) <EOL> auth . session = Session ( ) <EOL> client = app . test_client ( ) <EOL> auth . session . saved = <NUM_LIT:0> <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> print ( resp . data ) <EOL> assert auth . session_key not in auth . session <EOL> assert auth . redirect_key in auth . session <EOL> assert auth . session . saved == <NUM_LIT:1> <EOL> data = { <EOL> '<STR_LIT>' : user . login , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> client . post ( auth . url_sign_in , data = data ) <EOL> assert auth . session_key in auth . session <EOL> assert auth . session . saved == <NUM_LIT:4> <EOL> data = { <EOL> '<STR_LIT>' : auth . get_csrf_token ( ) , <EOL> } <EOL> client . post ( auth . url_sign_out , data = data ) <EOL> assert auth . session_key not in auth . session <EOL> assert auth . session . saved == <NUM_LIT:5> </s>
