<s> from twisted . internet import defer <EOL> from tests import unittest <EOL> from synapse . api . constants import Membership <EOL> import json <EOL> import time <EOL> class RestTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( RestTestCase , self ) . __init__ ( * args , ** kwargs ) <EOL> self . mock_resource = None <EOL> self . auth_user_id = None <EOL> @ defer . inlineCallbacks <EOL> def create_room_as ( self , room_creator , is_public = True , tok = None ) : <EOL> temp_id = self . auth_user_id <EOL> self . auth_user_id = room_creator <EOL> path = "<STR_LIT>" <EOL> content = "<STR_LIT:{}>" <EOL> if not is_public : <EOL> content = '<STR_LIT>' <EOL> if tok : <EOL> path = path + "<STR_LIT>" % tok <EOL> ( code , response ) = yield self . mock_resource . trigger ( "<STR_LIT:POST>" , path , content ) <EOL> self . assertEquals ( <NUM_LIT:200> , code , msg = str ( response ) ) <EOL> self . auth_user_id = temp_id <EOL> defer . returnValue ( response [ "<STR_LIT>" ] ) <EOL> @ defer . inlineCallbacks <EOL> def invite ( self , room = None , src = None , targ = None , expect_code = <NUM_LIT:200> , tok = None ) : <EOL> yield self . change_membership ( room = room , src = src , targ = targ , tok = tok , <EOL> membership = Membership . INVITE , <EOL> expect_code = expect_code ) <EOL> @ defer . inlineCallbacks <EOL> def join ( self , room = None , user = None , expect_code = <NUM_LIT:200> , tok = None ) : <EOL> yield self . change_membership ( room = room , src = user , targ = user , tok = tok , <EOL> membership = Membership . JOIN , <EOL> expect_code = expect_code ) <EOL> @ defer . inlineCallbacks <EOL> def leave ( self , room = None , user = None , expect_code = <NUM_LIT:200> , tok = None ) : <EOL> yield self . change_membership ( room = room , src = user , targ = user , tok = tok , <EOL> membership = Membership . LEAVE , <EOL> expect_code = expect_code ) <EOL> @ defer . inlineCallbacks <EOL> def change_membership ( self , room , src , targ , membership , tok = None , <EOL> expect_code = <NUM_LIT:200> ) : <EOL> temp_id = self . auth_user_id <EOL> self . auth_user_id = src <EOL> path = "<STR_LIT>" % ( room , targ ) <EOL> if tok : <EOL> path = path + "<STR_LIT>" % tok <EOL> data = { <EOL> "<STR_LIT>" : membership <EOL> } <EOL> ( code , response ) = yield self . mock_resource . trigger ( <EOL> "<STR_LIT>" , path , json . dumps ( data ) <EOL> ) <EOL> self . assertEquals ( expect_code , code , msg = str ( response ) ) <EOL> self . auth_user_id = temp_id <EOL> @ defer . inlineCallbacks <EOL> def register ( self , user_id ) : <EOL> ( code , response ) = yield self . mock_resource . trigger ( <EOL> "<STR_LIT:POST>" , <EOL> "<STR_LIT>" , <EOL> json . dumps ( { <EOL> "<STR_LIT:user>" : user_id , <EOL> "<STR_LIT:password>" : "<STR_LIT:test>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } ) ) <EOL> self . assertEquals ( <NUM_LIT:200> , code ) <EOL> defer . returnValue ( response ) <EOL> @ defer . inlineCallbacks <EOL> def send ( self , room_id , body = None , txn_id = None , tok = None , <EOL> expect_code = <NUM_LIT:200> ) : <EOL> if txn_id is None : <EOL> txn_id = "<STR_LIT>" % ( str ( time . time ( ) ) ) <EOL> if body is None : <EOL> body = "<STR_LIT>" <EOL> path = "<STR_LIT>" % ( room_id , txn_id ) <EOL> content = '<STR_LIT>' % body <EOL> if tok : <EOL> path = path + "<STR_LIT>" % tok <EOL> ( code , response ) = yield self . mock_resource . trigger ( "<STR_LIT>" , path , content ) <EOL> self . assertEquals ( expect_code , code , msg = str ( response ) ) <EOL> def assert_dict ( self , required , actual ) : <EOL> """<STR_LIT>""" <EOL> for key in required : <EOL> self . assertEquals ( required [ key ] , actual [ key ] , <EOL> msg = "<STR_LIT>" % ( key , actual ) ) </s>
