<s> """<STR_LIT>""" <EOL> from contextlib import contextmanager <EOL> from datetime import datetime , timedelta <EOL> import json <EOL> from flask import Response <EOL> from flask . testing import FlaskClient <EOL> from werkzeug . utils import cached_property <EOL> class AutoAuthFlaskClient ( FlaskClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AutoAuthFlaskClient , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _user = None <EOL> self . _auth_scopes = None <EOL> @ contextmanager <EOL> def login ( self , user , auth_scopes = None ) : <EOL> """<STR_LIT>""" <EOL> self . _user = user <EOL> self . _auth_scopes = auth_scopes or [ ] <EOL> yield self <EOL> self . _user = None <EOL> self . _auth_scopes = None <EOL> def open ( self , * args , ** kwargs ) : <EOL> if self . _user is not None : <EOL> from app . extensions import db <EOL> from app . modules . auth . models import OAuth2Token <EOL> oauth2_bearer_token = OAuth2Token ( <EOL> client_id = <NUM_LIT:0> , <EOL> user = self . _user , <EOL> token_type = '<STR_LIT>' , <EOL> access_token = '<STR_LIT>' , <EOL> _scopes = '<STR_LIT:U+0020>' . join ( self . _auth_scopes ) , <EOL> expires = datetime . utcnow ( ) + timedelta ( days = <NUM_LIT:1> ) , <EOL> ) <EOL> db . session . add ( oauth2_bearer_token ) <EOL> db . session . commit ( ) <EOL> extra_headers = ( <EOL> ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( token = oauth2_bearer_token ) <EOL> ) , <EOL> ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] += extra_headers <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = extra_headers <EOL> response = super ( AutoAuthFlaskClient , self ) . open ( * args , ** kwargs ) <EOL> if self . _user is not None : <EOL> db . session . delete ( oauth2_bearer_token ) <EOL> db . session . commit ( ) <EOL> return response <EOL> class JSONResponse ( Response ) : <EOL> """<STR_LIT>""" <EOL> @ cached_property <EOL> def json ( self ) : <EOL> return json . loads ( self . get_data ( as_text = True ) ) <EOL> def generate_user_instance ( <EOL> user_id = None , <EOL> username = "<STR_LIT:username>" , <EOL> password = None , <EOL> email = None , <EOL> first_name = "<STR_LIT>" , <EOL> middle_name = "<STR_LIT>" , <EOL> last_name = "<STR_LIT>" , <EOL> created = None , <EOL> updated = None , <EOL> is_active = True , <EOL> is_readonly = False , <EOL> is_admin = False <EOL> ) : <EOL> """<STR_LIT>""" <EOL> from app . modules . users . models import User <EOL> if password is None : <EOL> password = '<STR_LIT>' % username <EOL> user_instance = User ( <EOL> id = user_id , <EOL> username = username , <EOL> first_name = first_name , <EOL> middle_name = middle_name , <EOL> last_name = last_name , <EOL> password = password , <EOL> email = email or '<STR_LIT>' % username , <EOL> created = created or datetime . now ( ) , <EOL> updated = updated or datetime . now ( ) , <EOL> is_active = is_active , <EOL> is_readonly = is_readonly , <EOL> is_admin = is_admin , <EOL> ) <EOL> user_instance . password_secret = password <EOL> return user_instance </s>
