#NEWLINE# def test_sqla_load_token_for_user( app , db , blueprint , request ) : #NEWLINE# #INDENT# class User ( db . Model ) : #NEWLINE# #INDENT# id = db . Column ( db . Integer , primary_key = True ) #NEWLINE# name = db . Column ( db . String ( 80 ) ) #NEWLINE# #UNINDENT# class OAuth ( db . Model , OAuthConsumerMixin ) : #NEWLINE# #INDENT# user_id = db . Column ( db . Integer , db . ForeignKey ( User . id ) ) #NEWLINE# user = db . relationship ( User ) #NEWLINE# #UNINDENT# db . create_all ( ) #NEWLINE# def done( ) : #NEWLINE# #INDENT# db . session . remove ( ) #NEWLINE# db . drop_all ( ) #NEWLINE# #UNINDENT# request . addfinalizer ( done ) #NEWLINE# blueprint . backend = SQLAlchemyBackend ( OAuth , db . session ) #NEWLINE# alice = User ( name = 'Alice' ) #NEWLINE# alice_token = { #NEWLINE# #INDENT# 'access_token' : 'alice123' , #NEWLINE# 'token_type' : 'bearer' , #NEWLINE# #UNINDENT# } #NEWLINE# alice_oauth = OAuth ( user = alice , token = alice_token , provider = 'test-service' ) #NEWLINE# bob = User ( name = 'Bob' ) #NEWLINE# bob_token = { #NEWLINE# #INDENT# 'access_token' : 'bob456' , #NEWLINE# 'token_type' : 'bearer' , #NEWLINE# #UNINDENT# } #NEWLINE# bob_oauth = OAuth ( user = bob , token = bob_token , provider = 'test-service' ) #NEWLINE# sue = User ( name = 'Sue' ) #NEWLINE# sue_token = { #NEWLINE# #INDENT# 'access_token' : 'sue789' , #NEWLINE# 'token_type' : 'bearer' , #NEWLINE# #UNINDENT# } #NEWLINE# sue_oauth = OAuth ( user = sue , token = sue_token , provider = 'test-service' ) #NEWLINE# db . session . add_all ( [ alice , bob , sue , alice_oauth , bob_oauth , sue_oauth ] ) #NEWLINE# db . session . commit ( ) #NEWLINE# sess = blueprint . session #NEWLINE# assert ( not sess . token ) #NEWLINE# assert ( not blueprint . token ) #NEWLINE# blueprint . config [ 'user' ] = alice #NEWLINE# assert ( sess . token == alice_token ) #NEWLINE# assert ( blueprint . token == alice_token ) #NEWLINE# blueprint . config [ 'user' ] = bob #NEWLINE# assert ( sess . token == bob_token ) #NEWLINE# assert ( blueprint . token == bob_token ) #NEWLINE# blueprint . config [ 'user' ] = alice #NEWLINE# assert ( sess . token == alice_token ) #NEWLINE# assert ( blueprint . token == alice_token ) #NEWLINE# blueprint . config [ 'user' ] = sue #NEWLINE# assert ( sess . token == sue_token ) #NEWLINE# assert ( blueprint . token == sue_token ) #NEWLINE# del blueprint . config [ 'user' ] #NEWLINE# blueprint . config [ 'user_id' ] = bob . id #NEWLINE# assert ( sess . token == bob_token ) #NEWLINE# assert ( blueprint . token == bob_token ) #NEWLINE# del blueprint . token #NEWLINE# assert ( sess . token == None ) #NEWLINE# assert ( blueprint . token == None ) #NEWLINE# blueprint . config [ 'user_id' ] = alice . id #NEWLINE# assert ( sess . token == alice_token ) #NEWLINE# assert ( blueprint . token == alice_token )