]> git.ipfire.org Git - pbs.git/commitdiff
tests: Add tests for uploads
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 12 Oct 2022 15:41:26 +0000 (15:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 12 Oct 2022 15:41:26 +0000 (15:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
tests/upload.py [new file with mode: 0755]

index 1bba8dadaf8262d5f3eb5b053d5f92b4aebe96ef..11bd666e930c33d0fb80d7d88d5c9536ab8ccb91 100644 (file)
@@ -487,7 +487,8 @@ TESTS_ENVIRONMENT = \
 dist_check_SCRIPTS = \
        tests/setup.py \
        tests/distro.py \
-       tests/repo.py
+       tests/repo.py \
+       tests/upload.py
 
 EXTRA_DIST += \
        tests/test.py
diff --git a/tests/upload.py b/tests/upload.py
new file mode 100755 (executable)
index 0000000..5320209
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/python3
+
+import os
+import tempfile
+import unittest
+
+import test
+
+from buildservice import uploads
+
+class UploadTestCase(test.TestCase):
+       """
+               Tests everything around uploads
+       """
+       def test_allocate(self):
+               """
+                       Tests whether we can allocate temporary files in the right place
+               """
+               file = self.backend.uploads.allocate_file()
+
+               # Check if we received the correct type
+               self.assertIsInstance(file, tempfile._TemporaryFileWrapper)
+
+               # Check whether the file is located within basedir
+               self.assertTrue(file.name.startswith(self.backend.basepath))
+
+       async def test_create_delete(self):
+               """
+                       Tests whether uploads can be created and deleted
+               """
+               # Create a new temporary file
+               file = self.backend.uploads.allocate_file()
+               self.assertIsNotNone(file)
+
+               # Create the upload object
+               upload = self.backend.uploads.create("test.blob", file.name, user=self.user)
+
+               self.assertIsInstance(upload, uploads.Upload)
+               self.assertEqual(upload.filename, "test.blob")
+               self.assertEqual(upload.path, file.name)
+
+               # "Free" file
+               del file
+
+               # Check if the file still exists
+               self.assertTrue(os.path.exists(upload.path))
+
+               # Delete the upload
+               await upload.delete()
+
+               # Check if the file has been removed
+               self.assertFalse(os.path.exists(upload.path))
+
+
+if __name__ == "__main__":
+       unittest.main()