-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for uploading files with maximum allowed size
- Loading branch information
Showing
2 changed files
with
41 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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() |