Best Python code snippet using Kiwi_python
PhishLabsIOC_DRP_test.py
Source:PhishLabsIOC_DRP_test.py
...77 # Test less than exsits - get cases should query once and return 3 cases78 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?maxRecords=3',79 json=load_params_from_json('./test_data/raw_response_1.json'))80 expected_response = load_params_from_json('./test_data/client_methods/get_cases/limit_filter/output_1.json')81 tested_response = client.get_cases(max_records=3)82 assert tested_response == expected_response, 'Failed - Test less than exsits - get cases should query once ' \83 'and return 3 cases'84 @pytest.mark.get_cases85 @pytest.mark.get_cases_limit86 def test_get_cases_limit_filter_exact_results(self, requests_mock, client):87 # Test exact results - get cases should query twice and return 7 cases88 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=7',89 json=load_params_from_json('./test_data/raw_response_1.json'))90 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=7',91 json=load_params_from_json('./test_data/raw_response_2.json'))92 expected_response = load_params_from_json('./test_data/client_methods/get_cases/limit_filter/output_2.json')93 tested_response = client.get_cases(max_records=7)94 assert tested_response == expected_response, 'Failed - Test exact results - get cases should query twice' \95 ' and return 7 cases'96 @pytest.mark.get_cases97 @pytest.mark.get_cases_limit98 def test_get_cases_limit_filter_overflow_results(self, requests_mock, client):99 # Test Overflow - get cases should query three times and return 7 cases100 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=10',101 json=load_params_from_json('./test_data/raw_response_1.json'))102 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=10',103 json=load_params_from_json('./test_data/raw_response_2.json'))104 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=7&maxRecords=10',105 json=load_params_from_json('./test_data/raw_response_3.json'))106 expected_response = load_params_from_json('./test_data/client_methods/get_cases/limit_filter/output_2.json')107 tested_response = client.get_cases(max_records=10)108 assert tested_response == expected_response, 'Failed - Test Overflow - get cases should query three times and' \109 ' return 7 cases'110 @pytest.mark.get_cases111 @pytest.mark.get_cases_limit112 def test_get_cases_begin_date_filter_first_request(self, requests_mock, client):113 # Test - begin date in first request - get cases should query one time and return 1 case114 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',115 json=load_params_from_json('./test_data/raw_response_1.json'))116 expected_response = load_params_from_json('./test_data/client_methods/get_cases/begin_date_filter/output_1.json')117 tested_response = client.get_cases(begin_date="2019-12-01T01:34:48Z")118 assert tested_response == expected_response, 'Failed - begin date in first request - get cases should query one ' \119 'time and return 1 case'120 @pytest.mark.get_cases121 @pytest.mark.get_cases_begin_date122 def test_get_cases_begin_date_filter_second_request(self, requests_mock, client):123 # Test begin date in second request - get cases should query twice and return 3 cases124 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',125 json=load_params_from_json('./test_data/raw_response_1.json'))126 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=20',127 json=load_params_from_json('./test_data/raw_response_2.json'))128 expected_response = load_params_from_json(129 './test_data/client_methods/get_cases/begin_date_filter/output_2.json')130 tested_response = client.get_cases(begin_date="2019-11-10T02:09:17Z")131 assert tested_response == expected_response, 'Failed - begin date in second request - get cases should query twice' \132 ' and return 6 cases'133 @pytest.mark.get_cases134 @pytest.mark.get_cases_begin_date135 def test_get_cases_begin_date_filter_overflow(self, requests_mock, client):136 # Test begin date is more then exsits - get cases should query three-times and return 7 cases137 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',138 json=load_params_from_json('./test_data/raw_response_1.json'))139 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=20',140 json=load_params_from_json('./test_data/raw_response_2.json'))141 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=7&maxRecords=20',142 json=load_params_from_json('./test_data/raw_response_3.json'))143 expected_response = load_params_from_json('./test_data/client_methods/get_cases/begin_date_filter/output_3.json')144 tested_response = client.get_cases(begin_date="2018-11-10T02:30:37Z")145 assert tested_response == expected_response, 'Failed - begin date is more then exsits - get cases should query ' \146 'three-times and return 7 cases'147 @pytest.mark.get_cases148 @pytest.mark.get_cases_end_date149 def test_get_cases_end_date_filter_overflow_results(self, requests_mock, client):150 # Test end date is more than exsits - get cases should query three times and return 0 cases151 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',152 json=load_params_from_json('./test_data/raw_response_1.json'))153 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=20',154 json=load_params_from_json('./test_data/raw_response_2.json'))155 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=7&maxRecords=20',156 json=load_params_from_json('./test_data/raw_response_3.json'))157 expected_response = load_params_from_json('./test_data/client_methods/get_cases/end_date_filter/output_1.json')158 tested_response = client.get_cases(end_date="2018-11-10T02:30:37Z")159 assert tested_response == expected_response, 'Failed - end date is more than exsits - get cases should ' \160 'query three times and return 0 cases'161 @pytest.mark.get_cases162 @pytest.mark.get_cases_end_date163 def test_get_cases_end_date_filter_second_request(self, requests_mock, client):164 # Test end date in second request - get cases should query three times and return 2 cases165 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',166 json=load_params_from_json('./test_data/raw_response_1.json'))167 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=20',168 json=load_params_from_json('./test_data/raw_response_2.json'))169 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=7&maxRecords=20',170 json=load_params_from_json('./test_data/raw_response_3.json'))171 expected_response = load_params_from_json('./test_data/client_methods/get_cases/end_date_filter/output_2.json')172 tested_response = client.get_cases(end_date="2019-11-10T02:30:37Z")173 assert tested_response == expected_response, 'Failed - end date in second request - get cases should query three' \174 ' times and return 2 cases'175 @pytest.mark.get_cases176 @pytest.mark.get_cases_end_date177 def test_get_cases_end_date_filter_first_request(self, requests_mock, client):178 # Test end date in first request - get cases should query three-times and return 7 cases179 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',180 json=load_params_from_json('./test_data/raw_response_1.json'))181 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=20',182 json=load_params_from_json('./test_data/raw_response_2.json'))183 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=7&maxRecords=20',184 json=load_params_from_json('./test_data/raw_response_3.json'))185 expected_response = load_params_from_json('./test_data/client_methods/get_cases/end_date_filter/output_3.json')186 tested_response = client.get_cases(end_date="2019-12-01T01:40:36Z")187 assert tested_response == expected_response, 'Failed - end date in first request - get cases should query ' \188 'three-times and return 7 cases'189 @pytest.mark.get_cases190 @pytest.mark.get_cases_combined191 def test_get_cases_combined_filters_first_request(self, requests_mock, client):192 # Test end date and begin date in first request - get cases should query twice and return 5 cases193 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',194 json=load_params_from_json('./test_data/raw_response_1.json'))195 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=20',196 json=load_params_from_json('./test_data/raw_response_2.json'))197 expected_response = load_params_from_json('./test_data/client_methods/get_cases/combined_filters/output_1.json')198 tested_response = client.get_cases(end_date="2019-12-01T01:40:36Z",199 begin_date="2019-11-29T02:34:00Z")200 assert tested_response == expected_response, 'Failed - end date and begin date in first request - get cases' \201 ' should query twice and return 5 cases'202 @pytest.mark.get_cases203 @pytest.mark.get_cases_combined204 def test_get_cases_combined_filters_first_second_request(self, requests_mock, client):205 # Test end date in first request and begin date in second request - get cases should query twice and return 2 cases206 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=20',207 json=load_params_from_json('./test_data/raw_response_1.json'))208 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=20',209 json=load_params_from_json('./test_data/raw_response_2.json'))210 expected_response = load_params_from_json('./test_data/client_methods/get_cases/combined_filters/output_2.json')211 tested_response = client.get_cases(end_date="2019-11-29T02:34:00Z",212 begin_date="2019-11-10T02:09:17Z")213 assert tested_response == expected_response, 'Failed - end date in first request and begin date in second ' \214 'request - get cases should query twice and return 3 case'215 @pytest.mark.get_cases216 @pytest.mark.get_cases_combined217 def test_get_cases_combined_filters_second_request(self, requests_mock, client):218 # Test - end date in second request and begin date in not exsits - get cases should three times and return 1 case219 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=0&maxRecords=1',220 json=load_params_from_json('./test_data/raw_response_1.json'))221 requests_mock.get('https://caseapi.phishlabs.com/v1/data/cases?offset=5&maxRecords=1',222 json=load_params_from_json('./test_data/raw_response_2.json'))223 expected_response = load_params_from_json('./test_data/client_methods/get_cases/combined_filters/output_3.json')224 tested_response = client.get_cases(end_date="2019-11-29T02:34:00Z",225 begin_date="2019-11-10T02:09:17Z",226 max_records=1)227 assert tested_response == expected_response, 'Failed - end date in first request and begin date in second ' \...
test_a51_cipher.py
Source:test_a51_cipher.py
...18finally:19 from src.projects.a51 import a51_cipher as a5120DATA_DIR = pathlib.Path("data/projects/a51/")21TIME_LIMIT = 222def get_cases(category: str, *attribs: str) -> Generator:23 """Get test cases from the TOML file"""24 with open(pathlib.Path(__file__).with_suffix(".toml"), encoding="utf-8") as file:25 all_cases = toml.load(file)26 for case in all_cases[category]:27 yield tuple(case.get(a) for a in attribs)28@pytest.mark.timeout(TIME_LIMIT)29def test_majority():30 """Testing the majority function"""31 for bits, expected in zip(range(8), "00010111"):32 assert a51.majority(*tuple(f"{bits:03b}")) == expected33@pytest.mark.timeout(TIME_LIMIT)34@pytest.mark.parametrize(35 "registers_given, registers_stepped",36 get_cases("test_case_basic", "registers_given", "registers_stepped"),37)38def test_stepping(registers_given: dict[str, str], registers_stepped: dict[str, str]):39 """Testing the stepping function"""40 assert a51.step_x(registers_given["x"]) == registers_stepped["x"]41 assert a51.step_y(registers_given["y"]) == registers_stepped["y"]42 assert a51.step_z(registers_given["z"]) == registers_stepped["z"]43@pytest.mark.timeout(TIME_LIMIT)44@pytest.mark.parametrize(45 "registers_given, generated_bit",46 get_cases("test_case_basic", "registers_given", "generated_bit"),47)48def test_bit_generation(registers_given: dict[str, str], generated_bit: int):49 """Testing bit generation"""50 assert a51.generate_bit(**registers_given) == generated_bit51@pytest.mark.timeout(TIME_LIMIT)52@pytest.mark.parametrize(53 "registers_given, keystream",54 get_cases("test_case_basic", "registers_given", "keystream"),55)56def test_keystream_generation(registers_given: dict[str, str], keystream: str):57 """Testing keystream generation"""58 plaintext = "U" # Only used to get length of the keystream, 8 bits59 assert a51.generate_keystream(plaintext, **registers_given) == keystream60@pytest.mark.timeout(TIME_LIMIT)61@pytest.mark.parametrize(62 "keystream, ciphertext",63 get_cases("test_case_basic", "keystream", "ciphertext"),64)65def test_encrypt_char(keystream: str, ciphertext: str):66 """Testing encryption of a single character"""67 plaintext = "U" # Only used to get length of the keystream, 8 bits68 assert a51.encrypt(plaintext, keystream) == ciphertext69@pytest.mark.timeout(TIME_LIMIT)70@pytest.mark.parametrize(71 "secret, generated_registers",72 get_cases("test_case_encryption", "secret", "generated_registers"),73)74def test_populate_registers(secret: str, generated_registers: dict[str, str]):75 """Testing generation of the registers"""76 gen_reg_x, gen_reg_y, gen_reg_z = a51.populate_registers(secret)77 assert gen_reg_x == generated_registers["x"]78 assert gen_reg_y == generated_registers["y"]79 assert gen_reg_z == generated_registers["z"]80@pytest.mark.timeout(TIME_LIMIT)81@pytest.mark.parametrize(82 "plaintext, secret, ciphertext",83 get_cases("test_case_encryption", "plaintext", "secret", "ciphertext"),84)85def test_encrypt_text(plaintext: str, secret: str, ciphertext: str):86 """Testing encryption of a string"""87 x, y, z = a51.populate_registers(secret)88 keystream = a51.generate_keystream(plaintext, x, y, z)89 assert hex(int(a51.encrypt(plaintext, keystream), 2)) == ciphertext90@pytest.mark.timeout(TIME_LIMIT)91@pytest.mark.parametrize(92 "plaintext, secret, ciphertext",93 get_cases("test_case_encryption", "plaintext", "secret", "ciphertext"),94)95def test_decrypt_text(plaintext: str, secret: str, ciphertext: str):96 """Testing decryption of a string"""97 x, y, z = a51.populate_registers(secret)98 keystream = a51.generate_keystream(plaintext, x, y, z)99 assert a51.decrypt(ciphertext, keystream) == plaintext100@pytest.mark.timeout(TIME_LIMIT)101@pytest.mark.parametrize(102 "filename, secret, checksum",103 get_cases("test_case_file_encryption", "filename", "secret", "checksum"),104)105def test_encrypt_file(filename: str, secret: str, checksum: str):106 """Testing file encryption"""107 a51.encrypt_file(pathlib.Path(DATA_DIR / filename), secret)108 encrypted_file = pathlib.Path(DATA_DIR / filename)109 if encrypted_file.exists():110 assert (111 sha256(112 open(pathlib.Path(DATA_DIR / f"{filename}.secret"), "rb").read()113 ).hexdigest()114 == checksum115 )116if __name__ == "__main__":117 pytest.main(["-v", __file__])
test_knapsack_cipher.py
Source:test_knapsack_cipher.py
...18finally:19 from src.projects.knapsack import knapsack_cipher as knapsack20DATA_DIR = pathlib.Path("data/projects/knapsack/")21TIME_LIMIT = 222def get_cases(category: str, *attribs: str) -> Generator:23 """Get test cases from the TOML file"""24 with open(pathlib.Path(__file__).with_suffix(".toml"), encoding="utf-8") as file:25 all_cases = toml.load(file)26 for case in all_cases[category]:27 yield tuple(case.get(a) for a in attribs)28def test_generate_sik_default():29 """Testing generate_sik function with default block size"""30 sik = knapsack.generate_sik()31 for idx, item in enumerate(sik):32 assert item > sum(sik[:idx])33@pytest.mark.parametrize("block_size", [8, 32, 64, 128])34def test_generate_sik(block_size):35 """Testing generate_sik function with the specified block size"""36 sik = knapsack.generate_sik(block_size)37 assert len(sik) == block_size38 for idx, item in enumerate(sik):39 assert item > sum(sik[:idx])40@pytest.mark.timeout(TIME_LIMIT)41@pytest.mark.parametrize(42 "sik, n",43 get_cases("test_case_basic", "sik", "default_n"),44)45def test_calculate_n(sik, n):46 """Testing calculate_n function"""47 assert knapsack.calculate_n(sik) == n48@pytest.mark.timeout(TIME_LIMIT)49@pytest.mark.parametrize(50 "n, m",51 get_cases("test_case_basic", "default_n", "default_m"),52)53def test_calculate_m(n, m):54 """Testing calculate_m function"""55 assert knapsack.calculate_m(n) == m56@pytest.mark.timeout(TIME_LIMIT)57@pytest.mark.parametrize(58 "sik, i",59 get_cases("test_case_basic", "sik", "default_i"),60)61def test_calculate_inverse_default(sik, i):62 """Testing calculate_inverse function with default m and n"""63 assert knapsack.calculate_inverse(sik) == i64@pytest.mark.timeout(TIME_LIMIT)65@pytest.mark.parametrize(66 "sik, n, m, i",67 get_cases("test_case_basic", "sik", "n", "m", "i"),68)69def test_calculate_inverse(sik, n, m, i):70 """Testing calculate_inverse function with the specified n and m"""71 assert knapsack.calculate_inverse(sik, n, m) == i72@pytest.mark.timeout(TIME_LIMIT)73@pytest.mark.parametrize(74 "sik, genk",75 get_cases("test_case_basic", "sik", "default_genk"),76)77def test_generate_gk_default(sik, genk):78 """Testing generate_gk function with default parameters"""79 assert knapsack.generate_gk(sik) == tuple(genk)80@pytest.mark.timeout(TIME_LIMIT)81@pytest.mark.parametrize(82 "sik, n, m, genk",83 get_cases("test_case_basic", "sik", "n", "m", "genk"),84)85def test_generate_gk(sik, n, m, genk):86 """Testing generate_gk function"""87 assert knapsack.generate_gk(sik, n, m) == tuple(genk)88@pytest.mark.timeout(TIME_LIMIT)89@pytest.mark.parametrize(90 "plaintext, genk, ciphertext",91 get_cases("test_case_basic", "plaintext", "genk", "ciphertext"),92)93def test_encrypt(plaintext, genk, ciphertext):94 """Testing encrypt function"""95 assert knapsack.encrypt(plaintext, genk) == ciphertext96@pytest.mark.timeout(TIME_LIMIT)97@pytest.mark.parametrize(98 "ciphertext, sik, n, m, plaintext",99 get_cases("test_case_basic", "ciphertext", "sik", "n", "m", "plaintext"),100)101def test_decrypt(ciphertext, sik, n, m, plaintext):102 """Testing decrypt function"""103 assert knapsack.decrypt(ciphertext, sik, n, m) == plaintext104@pytest.mark.timeout(TIME_LIMIT)105@pytest.mark.parametrize(106 "plaintext, genk, ciphertext, block_size",107 get_cases("test_case_word", "plaintext", "genk", "ciphertext", "block_size"),108)109def test_encrypt_word(plaintext, genk, ciphertext, block_size):110 """Testing complex case of encryption"""111 assert knapsack.encrypt(plaintext, genk, block_size) == ciphertext112@pytest.mark.timeout(TIME_LIMIT)113@pytest.mark.parametrize(114 "ciphertext, sik, n, m, plaintext",115 get_cases("test_case_basic", "ciphertext", "sik", "n", "m", "plaintext"),116)117def test_decrypt_word(ciphertext, sik, n, m, plaintext):118 """Testing complex case of decryption"""119 assert knapsack.decrypt(ciphertext, sik, n, m) == plaintext120if __name__ == "__main__":...
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!!