Skip to content

Commit

Permalink
Add integration test for uploading files with maximum allowed size
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Dec 4, 2023
1 parent 48a33a7 commit 298aa4a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/test_http.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 40 additions & 3 deletions tests/test_utf8_uploads.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
#

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
Expand All @@ -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)

Expand All @@ -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()

0 comments on commit 298aa4a

Please sign in to comment.