diff --git a/tests/test_http.sh b/tests/test_http.sh index 005f00b924..bada74c22f 100755 --- a/tests/test_http.sh +++ b/tests/test_http.sh @@ -71,7 +71,7 @@ username = testadmin password = password _EOF_ - rlRun -t -c "./tests/test_utf8_uploads.py -v" + rlRun -t -c "PYTHONPATH=../Kiwi ./tests/test_utf8_uploads.py -v" rlPhaseEnd rlPhaseStartTest "Should send ETag header" diff --git a/tests/test_utf8_uploads.py b/tests/test_utf8_uploads.py index 35f7c35444..6c173d853e 100755 --- a/tests/test_utf8_uploads.py +++ b/tests/test_utf8_uploads.py @@ -1,16 +1,19 @@ #!/usr/bin/env python3 # -# Copyright (c) 2022 Kiwi TCMS project. All rights reserved. +# Copyright (c) 2022-2023 Kiwi TCMS project. All rights reserved. # Author: Alexander Todorov # +import base64 import ssl +import tempfile import unittest from unittest.mock import patch import requests from tcms_api import TCMS +from tcms.settings.common import FILE_UPLOAD_MAX_SIZE try: _create_unverified_https_context = ssl._create_unverified_context @@ -29,14 +32,37 @@ def get(self, url, **kwargs): class Utf8UploadTestCase(unittest.TestCase): + @staticmethod + def create_file(size, name): + """ + Create a temporary file with specific size. + """ + (_handle, file_name) = tempfile.mkstemp(prefix=f"kiwitcms-test-{name}.") + with open(file_name, "wb") as file: + for _ in range(size): + file.write("T") + + return file_name + + def b64_file(self, size, name): + """ + Create a temporary file and return it's contents in Base64, + which can be passed directly to rpc.User.add_attachment(). + """ + file_name = self.create_file(size, name) + with open(file_name, "rb") as file_handle: + file_content = file_handle.read() + file_content = base64.b64encode(file_content).decode() + return (file_name, file_content) + @classmethod def setUpClass(cls): with patch("requests.sessions.Session") as session: session.return_value = DoNotVerifySSLSession() cls.rpc = TCMS().exec - def upload_and_assert(self, filename): - result = self.rpc.User.add_attachment(filename, "a2l3aXRjbXM=") + def upload_and_assert(self, filename, contents="a2l3aXRjbXM="): + result = self.rpc.User.add_attachment(filename, contents) self.assertIn("url", result) self.assertIn("filename", result) @@ -46,6 +72,17 @@ def test_with_filename_in_french(self): def test_with_filename_in_bulgarian(self): self.upload_and_assert("Screenshot_2021-12-07 Плащане.png") + def test_uploading_file_with_maximum_allowed_size_should_work(self): + file_name, file_content = self.b64_file(FILE_UPLOAD_MAX_SIZE, "5mb-file") + self.upload_and_assert(file_name, file_content) + + def test_uploading_file_with_size_greater_than_maximum_should_fail(self): + file_name, file_content = self.b64_file(FILE_UPLOAD_MAX_SIZE + 1, "file-gt-5mb") + # fixme: this should fail + result = self.rpc.User.add_attachment(file_name, file_content) + self.assertIn("url", result) + self.assertIn("filename", result) + if __name__ == "__main__": unittest.main()