<s> import unittest <EOL> from smtproutes import Route , RoutingException <EOL> from smtproutes . sender_auth import DKIMAuth , GmailSPFAuth , SenderAuthException <EOL> from smtproutes . decorators import route <EOL> class TestRoute ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . valid_dkim_eml = file ( '<STR_LIT>' ) . read ( ) <EOL> self . invalid_dkim_eml = file ( '<STR_LIT>' ) . read ( ) <EOL> def test_route_regexes_extracted_from_methods_on_class_inheriting_from_Route ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route1 ( self , route = r'<STR_LIT>' ) : <EOL> pass <EOL> def route2 ( self , route = r'<STR_LIT>' ) : <EOL> pass <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( ) <EOL> self . assertTrue ( '<STR_LIT>' in route . _routes ) <EOL> self . assertTrue ( '<STR_LIT>' in route . _routes ) <EOL> def test_calling_route_with_a_matching_regex_results_in_the_appropriate_route_being_invoked ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route1 ( self , route = r'<STR_LIT>' ) : <EOL> self . bar = '<STR_LIT:bar>' <EOL> def route2 ( self , route = r'<STR_LIT>' ) : <EOL> self . bar = '<STR_LIT:foo>' <EOL> message = '<STR_LIT>' <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( ) <EOL> route . _route ( <EOL> message_data = message <EOL> ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , route . bar ) <EOL> def test_route_decorator_can_be_used_to_define_endpoint_rather_than_kwarg ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> @ route ( '<STR_LIT>' ) <EOL> def route1 ( self ) : <EOL> self . bar = '<STR_LIT:bar>' <EOL> @ route ( '<STR_LIT>' ) <EOL> def route2 ( self ) : <EOL> self . bar = '<STR_LIT:foo>' <EOL> message = '<STR_LIT>' <EOL> r = RouteImpl ( ) <EOL> r . _initialize ( ) <EOL> r . _route ( <EOL> message_data = message <EOL> ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , r . bar ) <EOL> def test_a_cc_field_that_matches_a_route_causes_the_route_to_be_triggered ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> @ route ( r'<STR_LIT>' ) <EOL> def route1 ( self ) : <EOL> self . bar = '<STR_LIT:bar>' <EOL> @ route ( r'<STR_LIT>' ) <EOL> def route2 ( self ) : <EOL> self . bar = '<STR_LIT:foo>' <EOL> message = '<STR_LIT>' <EOL> r = RouteImpl ( ) <EOL> r . _initialize ( ) <EOL> r . _route ( <EOL> message_data = message <EOL> ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , r . bar ) <EOL> def test_a_bcc_field_that_matches_a_route_causes_the_route_to_be_triggered ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> @ route ( r'<STR_LIT>' ) <EOL> def route1 ( self ) : <EOL> self . bar = '<STR_LIT:bar>' <EOL> @ route ( r'<STR_LIT>' ) <EOL> def route2 ( self ) : <EOL> self . bar = '<STR_LIT:foo>' <EOL> message = '<STR_LIT>' <EOL> r = RouteImpl ( ) <EOL> r . _initialize ( ) <EOL> r . _route ( <EOL> message_data = message <EOL> ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , r . bar ) <EOL> def test_x_forwarded_to_address_is_treated_as_to_address ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> @ route ( r'<STR_LIT>' ) <EOL> def route1 ( self ) : <EOL> self . bar = '<STR_LIT:bar>' <EOL> message = '<STR_LIT>' <EOL> r = RouteImpl ( ) <EOL> r . _initialize ( ) <EOL> r . _route ( message ) <EOL> self . assertEqual ( r . tos [ <NUM_LIT:3> ] . email , '<STR_LIT>' ) <EOL> def test_a_routing_exception_should_be_raised_if_the_route_is_not_found ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> pass <EOL> message = '<STR_LIT>' <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( ) <EOL> try : <EOL> route . _route ( <EOL> message_data = message <EOL> ) <EOL> self . assertTrue ( False ) <EOL> except RoutingException , re : <EOL> self . assertTrue ( '<STR_LIT>' in str ( re ) ) <EOL> self . assertTrue ( True ) <EOL> def test_named_groups_stored_as_instance_variables_on_route ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route ( self , route = r'<STR_LIT>' ) : <EOL> self . called = True <EOL> message = '<STR_LIT>' <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( ) <EOL> route . _route ( message_data = message ) <EOL> self . assertEqual ( route . user , '<STR_LIT>' ) <EOL> self . assertEqual ( route . folder , '<STR_LIT>' ) <EOL> self . assertEqual ( route . called , True ) <EOL> def test_instance_variables_populated_based_on_email_message ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route ( self , route = r'<STR_LIT>' ) : <EOL> self . called = True <EOL> message = '<STR_LIT>' <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( ) <EOL> route . _route ( message_data = message ) <EOL> self . assertTrue ( route . message ) <EOL> self . assertEqual ( route . tos [ <NUM_LIT:0> ] . email , '<STR_LIT>' ) <EOL> self . assertEqual ( route . tos [ <NUM_LIT:1> ] . email , '<STR_LIT>' ) <EOL> self . assertEqual ( route . mailfrom . email , '<STR_LIT>' ) <EOL> self . assertEqual ( route . ccs [ <NUM_LIT:0> ] . email , '<STR_LIT>' ) <EOL> self . assertEqual ( route . ccs [ <NUM_LIT:1> ] . email , '<STR_LIT>' ) <EOL> self . assertEqual ( route . bccs [ <NUM_LIT:0> ] . email , '<STR_LIT>' ) <EOL> def test_exception_raised_when_sender_auth_fails_on_route ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route ( self , route = r'<STR_LIT>' , sender_auth = DKIMAuth ) : <EOL> self . called = True <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( ) <EOL> route . called = False <EOL> try : <EOL> route . _route ( <EOL> message_data = self . invalid_dkim_eml <EOL> ) <EOL> self . assertTrue ( False ) <EOL> except SenderAuthException : <EOL> self . assertTrue ( True ) <EOL> self . assertFalse ( route . called ) <EOL> def test_no_exception_raised_when_sender_auth_succeeds_on_route ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route ( self , route = r'<STR_LIT>' , sender_auth = GmailSPFAuth ) : <EOL> self . called = True <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( peer_ip = '<STR_LIT>' ) <EOL> route . _route ( <EOL> message_data = self . valid_dkim_eml <EOL> ) <EOL> self . assertTrue ( route . called ) <EOL> def test_route_decorator_can_be_used_rather_than_kwarg_to_specify_sender_auth ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> @ route ( '<STR_LIT>' , sender_auth = GmailSPFAuth ) <EOL> def route ( self ) : <EOL> self . called = True <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( peer_ip = '<STR_LIT>' ) <EOL> route . _route ( <EOL> message_data = self . invalid_dkim_eml <EOL> ) <EOL> self . assertTrue ( route . called ) <EOL> def test_if_list_of_sender_authentication_approaches_is_provided_route_called_if_any_pass ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route ( self , route = r'<STR_LIT>' , sender_auth = [ DKIMAuth , GmailSPFAuth ] ) : <EOL> self . called = True <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( peer_ip = '<STR_LIT>' ) <EOL> route . _route ( <EOL> message_data = self . invalid_dkim_eml <EOL> ) <EOL> self . assertTrue ( route . called ) <EOL> def test_if_list_of_sender_authentication_approaches_is_provided_exception_raised_if_all_fail ( self ) : <EOL> class RouteImpl ( Route ) : <EOL> def route ( self , route = r'<STR_LIT>' , sender_auth = [ DKIMAuth , GmailSPFAuth ] ) : <EOL> self . called = True <EOL> route = RouteImpl ( ) <EOL> route . _initialize ( ) <EOL> route . called = False <EOL> try : <EOL> route . _route ( <EOL> message_data = self . invalid_dkim_eml <EOL> ) <EOL> self . assertTrue ( False ) <EOL> except SenderAuthException : <EOL> self . assertTrue ( True ) <EOL> self . assertFalse ( route . called ) </s>
