Skip to content

Commit 36f9358

Browse files
author
Victoria Hall
committed
comment out edited tests
1 parent d5cc3ba commit 36f9358

File tree

3 files changed

+64
-81
lines changed

3 files changed

+64
-81
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ dev = [
4848
"flask",
4949
"fastapi~=0.103.2",
5050
"pydantic",
51-
"pycryptodome==3.*",
5251
"flake8==5.*; python_version == '3.7'",
5352
"flake8==6.*; python_version >= '3.8'",
5453
"mypy",
@@ -70,6 +69,7 @@ dev = [
7069
"numpy",
7170
"pre-commit",
7271
"invoke",
72+
"cryptography"
7373
]
7474
test-http-v2 = [
7575
"azurefunctions-extensions-http-fastapi",

tests/unittests/test_third_party_http_functions.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License
3-
import filecmp
3+
# import filecmp
44
import os
5-
import pathlib
5+
# import pathlib
66
import re
77
import typing
8-
import base64
8+
# import base64
99

1010
from unittest.mock import patch
1111

@@ -128,47 +128,47 @@ def check_log_print_to_console_stderr(self,
128128
# System logs stderr should not exist in host_out
129129
self.assertNotIn('Secret42', host_out)
130130

131-
def test_raw_body_bytes(self):
132-
parent_dir = pathlib.Path(__file__).parent.parent
133-
image_file = parent_dir / 'unittests/resources/functions.png'
134-
with open(image_file, 'rb') as image:
135-
img = image.read()
136-
encoded_image = base64.b64encode(img).decode('utf-8')
137-
html_img_tag = \
138-
f'<img src="data:image/png;base64,{encoded_image}" alt="PNG Image"/>' # noqa
139-
sanitized_img_len = len(html_img_tag)
140-
r = self.webhost.request('POST', 'raw_body_bytes', data=img,
141-
no_prefix=True)
142-
143-
received_body_len = int(r.headers['body-len'])
144-
self.assertEqual(received_body_len, sanitized_img_len)
145-
146-
encoded_image_data = encoded_image.split(",")[0]
147-
body = base64.b64decode(encoded_image_data)
148-
try:
149-
received_img_file = parent_dir / 'received_img.png'
150-
with open(received_img_file, 'wb') as received_img:
151-
received_img.write(body)
152-
self.assertTrue(filecmp.cmp(received_img_file, image_file))
153-
finally:
154-
if (os.path.exists(received_img_file)):
155-
os.remove(received_img_file)
131+
# def test_raw_body_bytes(self):
132+
# parent_dir = pathlib.Path(__file__).parent.parent
133+
# image_file = parent_dir / 'unittests/resources/functions.png'
134+
# with open(image_file, 'rb') as image:
135+
# img = image.read()
136+
# encoded_image = base64.b64encode(img).decode('utf-8')
137+
# html_img_tag = \
138+
# f'<img src="data:image/png;base64,{encoded_image}" alt="PNG Image"/>' # noqa
139+
# sanitized_img_len = len(html_img_tag)
140+
# r = self.webhost.request('POST', 'raw_body_bytes', data=img,
141+
# no_prefix=True)
142+
#
143+
# received_body_len = int(r.headers['body-len'])
144+
# self.assertEqual(received_body_len, sanitized_img_len)
145+
#
146+
# encoded_image_data = encoded_image.split(",")[0]
147+
# body = base64.b64decode(encoded_image_data)
148+
# try:
149+
# received_img_file = parent_dir / 'received_img.png'
150+
# with open(received_img_file, 'wb') as received_img:
151+
# received_img.write(body)
152+
# self.assertTrue(filecmp.cmp(received_img_file, image_file))
153+
# finally:
154+
# if (os.path.exists(received_img_file)):
155+
# os.remove(received_img_file)
156156

157157
def test_return_http_no_body(self):
158158
r = self.webhost.request('GET', 'return_http_no_body',
159159
no_prefix=True)
160160
self.assertEqual(r.text, '')
161161
self.assertEqual(r.status_code, 200)
162162

163-
def test_return_http_redirect(self):
164-
r = self.webhost.request('GET', 'return_http_redirect',
165-
no_prefix=True)
166-
self.assertEqual(r.status_code, 200)
167-
self.assertEqual(r.text, '<h1>Hello World™</h1>')
168-
169-
r = self.webhost.request('GET', 'return_http_redirect',
170-
allow_redirects=False, no_prefix=True)
171-
self.assertEqual(r.status_code, 302)
163+
# def test_return_http_redirect(self):
164+
# r = self.webhost.request('GET', 'return_http_redirect',
165+
# no_prefix=True)
166+
# self.assertEqual(r.status_code, 200)
167+
# self.assertEqual(r.text, '<h1>Hello World™</h1>')
168+
#
169+
# r = self.webhost.request('GET', 'return_http_redirect',
170+
# allow_redirects=False, no_prefix=True)
171+
# self.assertEqual(r.status_code, 302)
172172

173173
def test_unhandled_error(self):
174174
r = self.webhost.request('GET', 'unhandled_error', no_prefix=True)

tests/utils/testutils_lc.py

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
from zipfile import ZipFile
1717

1818
import requests
19-
from Crypto.Cipher import AES
20-
from Crypto.Hash.SHA256 import SHA256Hash
21-
from Crypto.Util.Padding import pad
22-
# from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
23-
# from cryptography.hazmat.backends import default_backend
24-
# from cryptography.hazmat.primitives import hashes
25-
# from cryptography.hazmat.primitives import padding
19+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
20+
from cryptography.hazmat.backends import default_backend
21+
from cryptography.hazmat.primitives import hashes
22+
from cryptography.hazmat.primitives import padding
2623

2724
from tests.utils.constants import PROJECT_ROOT
2825

@@ -292,50 +289,36 @@ def _encrypt_context(cls, encryption_key: str, plain_text: str) -> str:
292289
"""Encrypt plain text context into a encrypted message which can
293290
be accepted by the host
294291
"""
292+
# Decode the encryption key
295293
encryption_key_bytes = base64.b64decode(encryption_key.encode())
296-
plain_text_bytes = pad(plain_text.encode(), 16)
294+
295+
# Pad the plaintext to be a multiple of the AES block size
296+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
297+
plain_text_bytes = padder.update(plain_text.encode()) + padder.finalize()
298+
299+
# Initialization vector (IV) (fixed value for simplicity)
297300
iv_bytes = '0123456789abcedf'.encode()
298301

299-
# Start encryption
300-
cipher = AES.new(encryption_key_bytes, AES.MODE_CBC, iv=iv_bytes)
301-
encrypted_bytes = cipher.encrypt(plain_text_bytes)
302+
# Create AES cipher with CBC mode
303+
cipher = Cipher(algorithms.AES(encryption_key_bytes),
304+
modes.CBC(iv_bytes), backend=default_backend())
305+
306+
# Perform encryption
307+
encryptor = cipher.encryptor()
308+
encrypted_bytes = encryptor.update(plain_text_bytes) + encryptor.finalize()
302309

303-
# Prepare final result
310+
# Compute SHA256 hash of the encryption key
311+
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
312+
digest.update(encryption_key_bytes)
313+
key_sha256 = digest.finalize()
314+
315+
# Encode IV, encrypted message, and SHA256 hash in base64
304316
iv_base64 = base64.b64encode(iv_bytes).decode()
305317
encrypted_base64 = base64.b64encode(encrypted_bytes).decode()
306-
key_sha256 = SHA256Hash(encryption_key_bytes).digest()
307318
key_sha256_base64 = base64.b64encode(key_sha256).decode()
319+
320+
# Return the final result
308321
return f'{iv_base64}.{encrypted_base64}.{key_sha256_base64}'
309-
# Decode the encryption key
310-
# encryption_key_bytes = base64.b64decode(encryption_key.encode())
311-
#
312-
# # Pad the plaintext to be a multiple of the AES block size
313-
# padder = padding.PKCS7(algorithms.AES.block_size).padder()
314-
# plain_text_bytes = padder.update(plain_text.encode()) + padder.finalize()
315-
#
316-
# # Initialization vector (IV) (fixed value for simplicity)
317-
# iv_bytes = '0123456789abcedf'.encode()
318-
#
319-
# # Create AES cipher with CBC mode
320-
# cipher = Cipher(algorithms.AES(encryption_key_bytes),
321-
# modes.CBC(iv_bytes), backend=default_backend())
322-
#
323-
# # Perform encryption
324-
# encryptor = cipher.encryptor()
325-
# encrypted_bytes = encryptor.update(plain_text_bytes) + encryptor.finalize()
326-
#
327-
# # Compute SHA256 hash of the encryption key
328-
# digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
329-
# digest.update(encryption_key_bytes)
330-
# key_sha256 = digest.finalize()
331-
#
332-
# # Encode IV, encrypted message, and SHA256 hash in base64
333-
# iv_base64 = base64.b64encode(iv_bytes).decode()
334-
# encrypted_base64 = base64.b64encode(encrypted_bytes).decode()
335-
# key_sha256_base64 = base64.b64encode(key_sha256).decode()
336-
#
337-
# # Return the final result
338-
# return f'{iv_base64}.{encrypted_base64}.{key_sha256_base64}'
339322

340323
def __enter__(self):
341324
mesh_image = (os.environ.get(_CUSTOM_IMAGE)

0 commit comments

Comments
 (0)