<s> from openstack_dashboard import api <EOL> from openstack_dashboard . test import helpers as test <EOL> from neutronclient . v2_0 . client import Client as neutronclient <EOL> class FwaasApiTests ( test . APITestCase ) : <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_rule_create ( self ) : <EOL> rule1 = self . fw_rules . first ( ) <EOL> rule1_dict = self . api_fw_rules . first ( ) <EOL> form_data = { '<STR_LIT:name>' : rule1 . name , <EOL> '<STR_LIT:description>' : rule1 . description , <EOL> '<STR_LIT>' : rule1 . protocol , <EOL> '<STR_LIT:action>' : rule1 . action , <EOL> '<STR_LIT>' : rule1 . source_ip_address , <EOL> '<STR_LIT>' : rule1 . source_port , <EOL> '<STR_LIT>' : rule1 . destination_ip_address , <EOL> '<STR_LIT>' : rule1 . destination_port , <EOL> '<STR_LIT>' : rule1 . shared , <EOL> '<STR_LIT>' : rule1 . enabled <EOL> } <EOL> form_dict = { '<STR_LIT>' : form_data } <EOL> ret_dict = { '<STR_LIT>' : rule1_dict } <EOL> neutronclient . create_firewall_rule ( form_dict ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . rule_create ( self . request , ** form_data ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Rule ) <EOL> self . assertEqual ( rule1 . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_rule_list ( self ) : <EOL> exp_rules = self . fw_rules . list ( ) <EOL> api_rules = { '<STR_LIT>' : self . api_fw_rules . list ( ) } <EOL> api_policies = { '<STR_LIT>' : self . api_fw_policies . list ( ) } <EOL> neutronclient . list_firewall_rules ( ) . AndReturn ( api_rules ) <EOL> neutronclient . list_firewall_policies ( ) . AndReturn ( api_policies ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . rule_list ( self . request ) <EOL> for ( v , d ) in zip ( ret_val , exp_rules ) : <EOL> self . assertIsInstance ( v , api . fwaas . Rule ) <EOL> self . assertEqual ( d . name , v . name ) <EOL> self . assertTrue ( v . id ) <EOL> if d . policy : <EOL> self . assertEqual ( d . firewall_policy_id , v . policy . id , ) <EOL> self . assertEqual ( d . policy . name , v . policy . name ) <EOL> else : <EOL> self . assertIsNone ( v . policy ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_rule_get ( self ) : <EOL> exp_rule = self . fw_rules . first ( ) <EOL> ret_dict = { '<STR_LIT>' : self . api_fw_rules . first ( ) } <EOL> policy_dict = { '<STR_LIT>' : self . api_fw_policies . first ( ) } <EOL> neutronclient . show_firewall_rule ( exp_rule . id ) . AndReturn ( ret_dict ) <EOL> neutronclient . show_firewall_policy ( <EOL> exp_rule . firewall_policy_id ) . AndReturn ( policy_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . rule_get ( self . request , exp_rule . id ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Rule ) <EOL> self . assertEqual ( exp_rule . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> self . assertEqual ( exp_rule . firewall_policy_id , ret_val . policy . id ) <EOL> self . assertEqual ( exp_rule . policy . name , ret_val . policy . name ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_rule_update ( self ) : <EOL> rule = self . fw_rules . first ( ) <EOL> rule_dict = self . api_fw_rules . first ( ) <EOL> rule . name = '<STR_LIT>' <EOL> rule . description = '<STR_LIT>' <EOL> rule . protocol = '<STR_LIT>' <EOL> rule . action = '<STR_LIT>' <EOL> rule . shared = True <EOL> rule . enabled = False <EOL> rule_dict [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> rule_dict [ '<STR_LIT:description>' ] = '<STR_LIT>' <EOL> rule_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> rule_dict [ '<STR_LIT:action>' ] = '<STR_LIT>' <EOL> rule_dict [ '<STR_LIT>' ] = True <EOL> rule_dict [ '<STR_LIT>' ] = False <EOL> form_data = { '<STR_LIT:name>' : rule . name , <EOL> '<STR_LIT:description>' : rule . description , <EOL> '<STR_LIT>' : rule . protocol , <EOL> '<STR_LIT:action>' : rule . action , <EOL> '<STR_LIT>' : rule . shared , <EOL> '<STR_LIT>' : rule . enabled <EOL> } <EOL> form_dict = { '<STR_LIT>' : form_data } <EOL> ret_dict = { '<STR_LIT>' : rule_dict } <EOL> neutronclient . update_firewall_rule ( <EOL> rule . id , form_dict ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . rule_update ( self . request , <EOL> rule . id , ** form_data ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Rule ) <EOL> self . assertEqual ( rule . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_policy_create ( self ) : <EOL> policy1 = self . fw_policies . first ( ) <EOL> policy1_dict = self . api_fw_policies . first ( ) <EOL> form_data = { '<STR_LIT:name>' : policy1 . name , <EOL> '<STR_LIT:description>' : policy1 . description , <EOL> '<STR_LIT>' : policy1 . firewall_rules , <EOL> '<STR_LIT>' : policy1 . shared , <EOL> '<STR_LIT>' : policy1 . audited <EOL> } <EOL> form_dict = { '<STR_LIT>' : form_data } <EOL> ret_dict = { '<STR_LIT>' : policy1_dict } <EOL> neutronclient . create_firewall_policy ( form_dict ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . policy_create ( self . request , ** form_data ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Policy ) <EOL> self . assertEqual ( policy1 . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_policy_list ( self ) : <EOL> exp_policies = self . fw_policies . list ( ) <EOL> policies_dict = { '<STR_LIT>' : self . api_fw_policies . list ( ) } <EOL> rules_dict = { '<STR_LIT>' : self . api_fw_rules . list ( ) } <EOL> neutronclient . list_firewall_policies ( ) . AndReturn ( policies_dict ) <EOL> neutronclient . list_firewall_rules ( ) . AndReturn ( rules_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . policy_list ( self . request ) <EOL> for ( v , d ) in zip ( ret_val , exp_policies ) : <EOL> self . assertIsInstance ( v , api . fwaas . Policy ) <EOL> self . assertEqual ( d . name , v . name ) <EOL> self . assertTrue ( v . id ) <EOL> self . assertEqual ( len ( d . firewall_rules ) , len ( v . rules ) ) <EOL> self . assertEqual ( len ( d . firewall_rules ) , len ( v . firewall_rules ) ) <EOL> for ( r , exp_r ) in zip ( v . rules , d . rules ) : <EOL> self . assertEqual ( exp_r . id , r . id ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_policy_get ( self ) : <EOL> exp_policy = self . fw_policies . first ( ) <EOL> policy_dict = self . api_fw_policies . first ( ) <EOL> api_rules = self . api_fw_rules . list ( ) [ : <NUM_LIT:2> ] <EOL> ret_dict = { '<STR_LIT>' : policy_dict } <EOL> neutronclient . show_firewall_policy ( exp_policy . id ) . AndReturn ( ret_dict ) <EOL> filters = { '<STR_LIT>' : exp_policy . id } <EOL> ret_dict = { '<STR_LIT>' : api_rules } <EOL> neutronclient . list_firewall_rules ( ** filters ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . policy_get ( self . request , exp_policy . id ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Policy ) <EOL> self . assertEqual ( exp_policy . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> self . assertEqual ( len ( exp_policy . rules ) , len ( ret_val . rules ) ) <EOL> for ( exp , ret ) in zip ( exp_policy . rules , ret_val . rules ) : <EOL> self . assertEqual ( exp . id , ret . id ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_policy_get_no_rule ( self ) : <EOL> exp_policy = self . fw_policies . list ( ) [ <NUM_LIT:1> ] <EOL> policy_dict = self . api_fw_policies . list ( ) [ <NUM_LIT:1> ] <EOL> ret_dict = { '<STR_LIT>' : policy_dict } <EOL> neutronclient . show_firewall_policy ( exp_policy . id ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . policy_get ( self . request , exp_policy . id ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Policy ) <EOL> self . assertEqual ( exp_policy . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> self . assertFalse ( len ( ret_val . rules ) ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_policy_update ( self ) : <EOL> policy = self . fw_policies . first ( ) <EOL> policy_dict = self . api_fw_policies . first ( ) <EOL> policy . name = '<STR_LIT>' <EOL> policy . description = '<STR_LIT>' <EOL> policy . shared = True <EOL> policy . audited = False <EOL> policy_dict [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> policy_dict [ '<STR_LIT:description>' ] = '<STR_LIT>' <EOL> policy_dict [ '<STR_LIT>' ] = True <EOL> policy_dict [ '<STR_LIT>' ] = False <EOL> form_data = { '<STR_LIT:name>' : policy . name , <EOL> '<STR_LIT:description>' : policy . description , <EOL> '<STR_LIT>' : policy . shared , <EOL> '<STR_LIT>' : policy . audited <EOL> } <EOL> form_dict = { '<STR_LIT>' : form_data } <EOL> ret_dict = { '<STR_LIT>' : policy_dict } <EOL> neutronclient . update_firewall_policy ( <EOL> policy . id , form_dict ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . policy_update ( self . request , <EOL> policy . id , ** form_data ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Policy ) <EOL> self . assertEqual ( policy . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_policy_insert_rule ( self ) : <EOL> policy = self . fw_policies . first ( ) <EOL> policy_dict = self . api_fw_policies . first ( ) <EOL> new_rule_id = '<STR_LIT>' <EOL> policy . firewall_rules . append ( new_rule_id ) <EOL> policy_dict [ '<STR_LIT>' ] . append ( new_rule_id ) <EOL> body = { '<STR_LIT>' : new_rule_id , <EOL> '<STR_LIT>' : policy . firewall_rules [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : policy . firewall_rules [ <NUM_LIT:0> ] } <EOL> neutronclient . firewall_policy_insert_rule ( <EOL> policy . id , body ) . AndReturn ( policy_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . policy_insert_rule ( self . request , <EOL> policy . id , ** body ) <EOL> self . assertIn ( new_rule_id , ret_val . firewall_rules ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_policy_remove_rule ( self ) : <EOL> policy = self . fw_policies . first ( ) <EOL> policy_dict = self . api_fw_policies . first ( ) <EOL> remove_rule_id = policy . firewall_rules [ <NUM_LIT:0> ] <EOL> policy_dict [ '<STR_LIT>' ] . remove ( remove_rule_id ) <EOL> body = { '<STR_LIT>' : remove_rule_id } <EOL> neutronclient . firewall_policy_remove_rule ( <EOL> policy . id , body ) . AndReturn ( policy_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . policy_remove_rule ( self . request , <EOL> policy . id , ** body ) <EOL> self . assertNotIn ( remove_rule_id , ret_val . firewall_rules ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_firewall_create ( self ) : <EOL> firewall = self . firewalls . first ( ) <EOL> firewall_dict = self . api_firewalls . first ( ) <EOL> form_data = { '<STR_LIT:name>' : firewall . name , <EOL> '<STR_LIT:description>' : firewall . description , <EOL> '<STR_LIT>' : firewall . firewall_policy_id , <EOL> '<STR_LIT>' : firewall . shared , <EOL> '<STR_LIT>' : firewall . admin_state_up <EOL> } <EOL> form_dict = { '<STR_LIT>' : form_data } <EOL> ret_dict = { '<STR_LIT>' : firewall_dict } <EOL> neutronclient . create_firewall ( form_dict ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . firewall_create ( self . request , ** form_data ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Firewall ) <EOL> self . assertEqual ( firewall . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_firewall_list ( self ) : <EOL> exp_firewalls = self . firewalls . list ( ) <EOL> firewalls_dict = { '<STR_LIT>' : self . api_firewalls . list ( ) } <EOL> policies_dict = { '<STR_LIT>' : self . api_fw_policies . list ( ) } <EOL> neutronclient . list_firewalls ( ) . AndReturn ( firewalls_dict ) <EOL> neutronclient . list_firewall_policies ( ) . AndReturn ( policies_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . firewall_list ( self . request ) <EOL> for ( v , d ) in zip ( ret_val , exp_firewalls ) : <EOL> self . assertIsInstance ( v , api . fwaas . Firewall ) <EOL> self . assertEqual ( d . name , v . name ) <EOL> self . assertTrue ( v . id ) <EOL> self . assertEqual ( d . firewall_policy_id , v . policy . id ) <EOL> self . assertEqual ( d . policy . name , v . policy . name ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_firewall_get ( self ) : <EOL> exp_firewall = self . firewalls . first ( ) <EOL> ret_dict = { '<STR_LIT>' : self . api_firewalls . first ( ) } <EOL> policy_dict = { '<STR_LIT>' : self . api_fw_policies . first ( ) } <EOL> neutronclient . show_firewall ( exp_firewall . id ) . AndReturn ( ret_dict ) <EOL> neutronclient . show_firewall_policy ( <EOL> exp_firewall . firewall_policy_id ) . AndReturn ( policy_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . firewall_get ( self . request , exp_firewall . id ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Firewall ) <EOL> self . assertEqual ( exp_firewall . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) <EOL> self . assertEqual ( exp_firewall . firewall_policy_id , ret_val . policy . id ) <EOL> self . assertEqual ( exp_firewall . policy . name , ret_val . policy . name ) <EOL> @ test . create_stubs ( { neutronclient : ( '<STR_LIT>' , ) } ) <EOL> def test_firewall_update ( self ) : <EOL> firewall = self . firewalls . first ( ) <EOL> firewall_dict = self . api_firewalls . first ( ) <EOL> firewall . name = '<STR_LIT>' <EOL> firewall . description = '<STR_LIT>' <EOL> firewall . admin_state_up = False <EOL> firewall_dict [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> firewall_dict [ '<STR_LIT:description>' ] = '<STR_LIT>' <EOL> firewall_dict [ '<STR_LIT>' ] = False <EOL> form_data = { '<STR_LIT:name>' : firewall . name , <EOL> '<STR_LIT:description>' : firewall . description , <EOL> '<STR_LIT>' : firewall . admin_state_up <EOL> } <EOL> form_dict = { '<STR_LIT>' : form_data } <EOL> ret_dict = { '<STR_LIT>' : firewall_dict } <EOL> neutronclient . update_firewall ( <EOL> firewall . id , form_dict ) . AndReturn ( ret_dict ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . fwaas . firewall_update ( self . request , <EOL> firewall . id , ** form_data ) <EOL> self . assertIsInstance ( ret_val , api . fwaas . Firewall ) <EOL> self . assertEqual ( firewall . name , ret_val . name ) <EOL> self . assertTrue ( ret_val . id ) </s>
