<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import unittest <EOL> from tipfy import Rule , Tipfy <EOL> from tipfy . app import local <EOL> from google . appengine . api . xmpp import Message as ApiMessage <EOL> import test_utils <EOL> fake_local = { } <EOL> def get_app ( ) : <EOL> return Tipfy ( rules = [ <EOL> Rule ( '<STR_LIT:/>' , name = '<STR_LIT>' , handler = '<STR_LIT>' ) , <EOL> Rule ( '<STR_LIT>' , name = '<STR_LIT>' , handler = '<STR_LIT>' ) , <EOL> ] , debug = True ) <EOL> def send_message ( jids , body , from_jid = None , message_type = '<STR_LIT>' , <EOL> raw_xml = False ) : <EOL> fake_local [ '<STR_LIT:message>' ] = { <EOL> '<STR_LIT:body>' : body , <EOL> } <EOL> class InvalidMessageError ( Exception ) : <EOL> pass <EOL> class Message ( ApiMessage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vars ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . __sender = vars [ "<STR_LIT>" ] <EOL> self . __to = vars [ "<STR_LIT:to>" ] <EOL> self . __body = vars [ "<STR_LIT:body>" ] <EOL> except KeyError , e : <EOL> raise InvalidMessageError ( e [ <NUM_LIT:0> ] ) <EOL> self . __command = None <EOL> self . __arg = None <EOL> def reply ( self , body , message_type = '<STR_LIT>' , raw_xml = False , <EOL> send_message = send_message ) : <EOL> """<STR_LIT>""" <EOL> return send_message ( [ self . sender ] , body , from_jid = self . to , <EOL> message_type = message_type , raw_xml = raw_xml ) <EOL> class TestCommandHandler ( test_utils . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> from tipfy . appengine import xmpp <EOL> self . xmpp_module = xmpp . xmpp <EOL> xmpp . xmpp = sys . modules [ __name__ ] <EOL> test_utils . BaseTestCase . setUp ( self ) <EOL> def tearDown ( self ) : <EOL> fake_local . clear ( ) <EOL> from tipfy . appengine import xmpp <EOL> xmpp . xmpp = self . xmpp_module <EOL> test_utils . BaseTestCase . tearDown ( self ) <EOL> def test_no_command ( self ) : <EOL> app = get_app ( ) <EOL> client = app . get_test_client ( ) <EOL> data = { } <EOL> client . open ( method = '<STR_LIT:POST>' , data = data ) <EOL> self . assertEqual ( fake_local . get ( '<STR_LIT:message>' , None ) , None ) <EOL> def test_not_implemented ( self ) : <EOL> app = get_app ( ) <EOL> app . config [ '<STR_LIT>' ] [ '<STR_LIT>' ] = False <EOL> client = app . get_test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:to>' : '<STR_LIT>' , <EOL> '<STR_LIT:body>' : '<STR_LIT>' , <EOL> } <EOL> self . assertRaises ( NotImplementedError , client . post , path = '<STR_LIT>' , data = data ) <EOL> def test_unknown_command ( self ) : <EOL> app = get_app ( ) <EOL> client = app . get_test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:to>' : '<STR_LIT>' , <EOL> '<STR_LIT:body>' : '<STR_LIT>' , <EOL> } <EOL> client . open ( method = '<STR_LIT:POST>' , data = data ) <EOL> self . assertEqual ( fake_local . get ( '<STR_LIT:message>' , None ) , { '<STR_LIT:body>' : '<STR_LIT>' } ) <EOL> def test_command ( self ) : <EOL> app = get_app ( ) <EOL> client = app . get_test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:to>' : '<STR_LIT>' , <EOL> '<STR_LIT:body>' : '<STR_LIT>' , <EOL> } <EOL> client . open ( method = '<STR_LIT:POST>' , data = data ) <EOL> self . assertEqual ( fake_local . get ( '<STR_LIT:message>' , None ) , { '<STR_LIT:body>' : '<STR_LIT>' } ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:to>' : '<STR_LIT>' , <EOL> '<STR_LIT:body>' : '<STR_LIT>' , <EOL> } <EOL> client . open ( method = '<STR_LIT:POST>' , data = data ) <EOL> self . assertEqual ( fake_local . get ( '<STR_LIT:message>' , None ) , { '<STR_LIT:body>' : '<STR_LIT>' } ) <EOL> def test_text_message ( self ) : <EOL> app = get_app ( ) <EOL> client = app . get_test_client ( ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:to>' : '<STR_LIT>' , <EOL> '<STR_LIT:body>' : '<STR_LIT>' , <EOL> } <EOL> client . open ( method = '<STR_LIT:POST>' , data = data ) <EOL> self . assertEqual ( fake_local . get ( '<STR_LIT:message>' , None ) , { '<STR_LIT:body>' : '<STR_LIT>' } ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_utils . main ( ) </s>
