Best Python code snippet using Testify_python
vulnerabilities_test.py
Source:vulnerabilities_test.py
...14 """Dummy analysis method"""15 return cfg_nodes16 def test_parse(self):17 definitions = vulnerabilities.parse(trigger_word_file=os.path.join(os.getcwd(), 'pyt', 'trigger_definitions', 'test_triggers.pyt'))18 self.assert_length(definitions.sources, expected_length=1)19 self.assert_length(definitions.sinks, expected_length=3)20 self.assert_length(definitions.sinks[0][1], expected_length=1)21 self.assert_length(definitions.sinks[1][1], expected_length=3)22 def test_parse_section(self):23 l = list(trigger_definitions_parser.parse_section(iter(['get'])))24 self.assert_length(l, expected_length=1)25 self.assertEqual(l[0][0], 'get')26 self.assertEqual(l[0][1], list())27 l = list(trigger_definitions_parser.parse_section(iter(['get', 'get -> a, b, c d s aq a'])))28 self.assert_length(l, expected_length=2)29 self.assertEqual(l[0][0], 'get')30 self.assertEqual(l[1][0], 'get')31 self.assertEqual(l[1][1], ['a', 'b', 'c d s aq a'])32 self.assert_length(l[1][1], expected_length=3)33 def test_label_contains(self):34 cfg_node = Node('label', None, line_number=None, path=None)35 trigger_words = [('get', [])]36 l = list(vulnerabilities.label_contains(cfg_node, trigger_words))37 self.assert_length(l, expected_length=0)38 cfg_node = Node('request.get("stefan")', None, line_number=None, path=None)39 trigger_words = [('get', []), ('request', [])]40 l = list(vulnerabilities.label_contains(cfg_node, trigger_words))41 self.assert_length(l, expected_length=2)42 trigger_node_1 = l[0]43 trigger_node_2 = l[1]44 self.assertEqual(trigger_node_1.trigger_word, 'get')45 self.assertEqual(trigger_node_1.cfg_node, cfg_node)46 self.assertEqual(trigger_node_2.trigger_word, 'request')47 self.assertEqual(trigger_node_2.cfg_node, cfg_node)48 cfg_node = Node('request.get("stefan")', None, line_number=None, path=None)49 trigger_words = [('get', []), ('get', [])]50 l = list(vulnerabilities.label_contains(cfg_node, trigger_words))51 self.assert_length(l, expected_length=2)52 def test_find_triggers(self):53 self.cfg_create_from_file('example/vulnerable_code/XSS.py')54 cfg_list = [self.cfg]55 FlaskAdaptor(cfg_list, [], [])56 XSS1 = cfg_list[1]57 trigger_words = [('get', [])]58 l = vulnerabilities.find_triggers(XSS1.nodes, trigger_words)59 self.assert_length(l, expected_length=1)60 def test_find_sanitiser_nodes(self):61 cfg_node = Node(None, None, line_number=None, path=None)62 sanitiser_tuple = vulnerabilities.Sanitiser('escape', cfg_node)63 sanitiser = 'escape'64 result = list(vulnerabilities.find_sanitiser_nodes(sanitiser, [sanitiser_tuple]))65 self.assert_length(result, expected_length=1)66 self.assertEqual(result[0], cfg_node)67 def test_build_sanitiser_node_dict(self):68 self.cfg_create_from_file('example/vulnerable_code/XSS_sanitised.py')69 cfg_list = [self.cfg]70 FlaskAdaptor(cfg_list, [], [])71 cfg = cfg_list[1]72 cfg_node = Node(None, None, line_number=None, path=None)73 sinks_in_file = [vulnerabilities.TriggerNode('replace', ['escape'], cfg_node)]74 sanitiser_dict = vulnerabilities.build_sanitiser_node_dict(cfg, sinks_in_file)75 self.assert_length(sanitiser_dict, expected_length=1)76 self.assertIn('escape', sanitiser_dict.keys())77 self.assertEqual(sanitiser_dict['escape'][0], cfg.nodes[2])78 def test_is_sanitized_false(self):79 cfg_node_1 = Node('Not sanitising at all', None, line_number=None, path=None)80 cfg_node_2 = Node('something.replace("this", "with this")', None, line_number=None, path=None)81 sinks_in_file = [vulnerabilities.TriggerNode('replace', ['escape'], cfg_node_2)]82 sanitiser_dict = {'escape': [cfg_node_1]}83 ReachingDefinitionsTaintAnalysis.get_lattice_elements = self.get_lattice_elements84 lattice = Lattice([cfg_node_1, cfg_node_2], analysis_type=ReachingDefinitionsTaintAnalysis)85 constraint_table[cfg_node_1] = 086 constraint_table[cfg_node_2] = 087 result = vulnerabilities.is_sanitized(sinks_in_file[0], sanitiser_dict, lattice)88 self.assertEqual(result, False)89 def test_is_sanitized_true(self):90 cfg_node_1 = Node('Awesome sanitiser', None, line_number=None, path=None)91 cfg_node_2 = Node('something.replace("this", "with this")', None, line_number=None, path=None)92 sinks_in_file = [vulnerabilities.TriggerNode('replace', ['escape'], cfg_node_2)]93 sanitiser_dict = {'escape': [cfg_node_1]}94 ReachingDefinitionsTaintAnalysis.get_lattice_elements = self.get_lattice_elements95 lattice = Lattice([cfg_node_1, cfg_node_2], analysis_type=ReachingDefinitionsTaintAnalysis)96 constraint_table[cfg_node_2] = 0b197 result = vulnerabilities.is_sanitized(sinks_in_file[0], sanitiser_dict, lattice)98 self.assertEqual(result, True)99 def test_find_vulnerabilities_no_vuln(self):100 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS_no_vuln.py')101 self.assert_length(vulnerability_log.vulnerabilities, expected_length=0)102 def test_find_vulnerabilities_sanitised(self):103 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS_sanitised.py')104 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)105 def test_find_vulnerabilities_vulnerable(self):106 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS.py')107 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)108 def test_find_vulnerabilities_reassign(self):109 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS_reassign.py')110 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)111 def test_find_vulnerabilities_variable_assign(self):112 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS_variable_assign.py')113 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)114 def run_analysis(self, path):115 self.cfg_create_from_file(path)116 cfg_list = [self.cfg]117 FlaskAdaptor(cfg_list, [], [])118 initialize_constraint_table(cfg_list)119 analyse(cfg_list, analysis_type=ReachingDefinitionsTaintAnalysis)120 return vulnerabilities.find_vulnerabilities(cfg_list, ReachingDefinitionsTaintAnalysis)121 def test_find_vulnerabilities_assign_other_var(self):122 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS_assign_to_other_var.py')123 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)124 def test_find_vulnerabilities_variable_multiple_assign(self):125 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS_variable_multiple_assign.py')126 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)127 def test_find_vulnerabilities_variable_assign_no_vuln(self):128 vulnerability_log = self.run_analysis('example/vulnerable_code/XSS_variable_assign_no_vuln.py')129 self.assert_length(vulnerability_log.vulnerabilities, expected_length=0)130 def test_find_vulnerabilities_command_injection(self):131 vulnerability_log = self.run_analysis('example/vulnerable_code/command_injection.py')132 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)133 def test_find_vulnerabilities_inter_command_injection(self):134 vulnerability_log = self.run_analysis('example/vulnerable_code/inter_command_injection.py')135 self.assert_length(vulnerability_log.vulnerabilities, expected_length=1)136 def test_find_vulnerabilities_inter_command_injection_2(self):137 vulnerability_log = self.run_analysis('example/vulnerable_code/inter_command_injection_2.py')...
test_servlet_api.py
Source:test_servlet_api.py
...23 T.assert_equal(results['pushtype'], "regular")24 def test_pushdata(self):25 push_info, contents, requests = self.api_call("pushdata?id=1")26 T.assert_equal(push_info['title'], "Test Push")27 T.assert_length(contents, 2)28 T.assert_equal(requests[0]['state'], "requested")29 def test_pushes(self):30 pushes, pushes_count = self.api_call("pushes")31 T.assert_length(pushes, 2)32 T.assert_equal(pushes_count, 2)33 pushes, pushes_count = self.api_call("pushes?rpp=1")34 T.assert_length(pushes, 1)35 T.assert_equal(pushes_count, 2)36 pushes, pushes_count = self.api_call("pushes?offset=1")37 T.assert_length(pushes, 1)38 T.assert_equal(pushes_count, 2)39 def test_pushes_order(self):40 self.insert_pushes()41 pushes, _ = self.api_call("pushes")42 T.assert_length(pushes, 6)43 lastpush = None44 for push in pushes:45 if lastpush is not None:46 if push['state'] == 'accepting':47 T.assert_equal('accepting', lastpush['state'])48 elif lastpush['state'] != 'accepting':49 T.assert_gte(push['modified'], lastpush['modified'])50 lastpush = push51 def test_pushes_user_filter(self):52 self.insert_pushes()53 pushes, pushes_count = self.api_call("pushes?user=troscoe")54 T.assert_length(pushes, 1)55 T.assert_equal(pushes_count, 1)56 for push in pushes:57 T.assert_equal(push['user'], 'troscoe')58 def test_pushes_state_filter(self):59 self.insert_pushes()60 pushes, pushes_count = self.api_call("pushes?state=live")61 T.assert_length(pushes, 1)62 T.assert_equal(pushes_count, 1)63 for push in pushes:64 T.assert_equal(push['state'], 'live')65 def test_pushes_filters(self):66 self.insert_pushes()67 pushes, pushes_count = self.api_call("pushes?user=heyjoe&state=accepting")68 T.assert_length(pushes, 1)69 T.assert_equal(pushes_count, 1)70 for push in pushes:71 T.assert_equal(push['user'], 'heyjoe')72 T.assert_equal(push['state'], 'accepting')73 pushes, pushes_count = self.api_call("pushes?user=heyjoe&state=live")74 T.assert_length(pushes, 0)75 T.assert_equal(pushes_count, 0)76 def test_pushcontents(self):77 pushcontents = self.api_call("pushcontents?id=1")78 T.assert_length(pushcontents, 1)79 T.assert_equal(pushcontents[0]['state'], 'pickme')80 def test_pushbyrequest(self):81 push = self.api_call("pushbyrequest?id=1")82 T.assert_equal(push['title'], "Test Push")83 def test_pushitems(self):84 pushitems = self.api_call("pushitems?push_id=1")85 T.assert_length(pushitems, 0)86 def test_requestsearch(self):87 requests = self.api_call("requestsearch?mbefore=%d" % time.time())88 T.assert_length(requests, 3)89 requests = self.api_call("requestsearch?cbefore=%d" % time.time())90 T.assert_length(requests, 3)91 requests = self.api_call("requestsearch?state=requested")92 T.assert_length(requests, 2)93 requests = self.api_call("requestsearch?state=pickme")94 T.assert_length(requests, 1)95 requests = self.api_call("requestsearch?user=bmetin")96 T.assert_length(requests, 2)97 requests = self.api_call("requestsearch?repo=bmetin")98 T.assert_length(requests, 2)99 requests = self.api_call("requestsearch?branch=bmetin_fix_stuff")100 T.assert_length(requests, 1)101 requests = self.api_call("requestsearch?title=fix")102 T.assert_length(requests, 2)103 requests = self.api_call("requestsearch?title=fix&limit=1")104 T.assert_length(requests, 1)105 def test_requestsearch_when_user_and_repo_are_different(self):106 requests = self.api_call("requestsearch?user=otheruser&repo=testuser&branch=testuser_important_fixes")...
assert_length_test.py
Source:assert_length_test.py
...7from pythonic_testcase import assert_length, assert_raises8from .util import exception_message9class AssertLengthTest(TestCase):10 def test_passes_if_length_matches_actual(self):11 assert_length(0, [])12 assert_length(1, ['foo'])13 def test_can_consume_generator_if_necessary(self):14 def generator():15 for i in (1, 2, 3):16 yield i17 assert_length(3, generator())18 generator_ = generator()19 assert_length(3, generator_)20 assert_length(0, generator_)21 def assert_fail(self, expected, actual, message=None):22 return assert_raises(AssertionError, lambda: assert_length(expected, actual, message=message))23 def test_fails_if_length_does_not_match_equal(self):24 self.assert_fail(2, ['foo'])25 def test_fails_with_sensible_default_error_message(self):26 e = self.assert_fail(2, ['foo'])27 assert "2 != 1" == exception_message(e), repr(exception_message(e))28 def test_can_specify_additional_custom_message(self):29 e = self.assert_fail(2, ['foo'], message='Bar')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!