Skip to content

Commit

Permalink
[#16] Add utils to afs settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dedenbangkit committed Sep 7, 2023
1 parent b071de8 commit 49b39b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion backend/afs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']
ALLOWED_HOSTS = ["*"]


# Application definition
Expand All @@ -44,6 +44,7 @@
"akvo.core_forms",
"akvo.core_node",
"akvo.core_data",
"akvo.utils",
]

MIDDLEWARE = [
Expand Down
2 changes: 1 addition & 1 deletion backend/akvo/utils/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def delete(self, url: str):
return url

def check(self, url: str):
path = Path(f"{self.storage_location}/{url}")
path = Path(f"{self.storage_path}/{url}")
return path.is_file()

def download(self, url: str):
Expand Down
30 changes: 22 additions & 8 deletions backend/akvo/utils/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,42 @@

class StorageTestCase(TestCase):
def setUp(self):
# Create a temporary directory if it doesn't exist
if not os.path.exists("/tmp"):
os.mkdir("/tmp")
# create a file
self.path = "/tmp"
self.file = "./test.txt"
with open(self.file, "w") as f:
f.write("test")
self.storage = Storage(storage_path="/tmp")
self.storage = Storage(storage_path=self.path)

def tearDown(self):
# delete the file
os.remove(self.file)

def test_upload(self):
self.storage.upload(file=self.file)
self.assertTrue(os.path.exists(self.file))
self.assertTrue(os.path.exists(f"{self.path}/{self.file}"))

# test upload with folder
self.storage.upload(file=self.file, folder="testing")
self.assertTrue(os.path.exists(f"{self.path}/testing/{self.file}"))
# test upload with filename
self.storage.upload(file=self.file, filename="test2.txt")
self.assertTrue(os.path.exists(f"{self.path}/test2.txt"))

def test_delete(self):
self.storage.upload(file=self.file)
self.storage.delete(url="test.txt")
self.assertFalse(os.path.exists(self.file))
self.assertFalse(os.path.exists(f"{self.path}/{self.file}"))

def test_check(self):
self.storage.upload(file=self.file)
self.assertTrue(self.storage.check(url="test.txt"))

def test_download(self):
self.storage.upload(file=self.file)
self.assertEqual(self.storage.download(url="test.txt"), self.file)
self.storage.upload(file=self.file, folder="testing")
self.assertTrue(os.path.exists(f"{self.path}/testing/{self.file}"))
filename = self.file.split("/")[-1]
self.assertEqual(
self.storage.download(url="testing/test.txt"),
f"{self.path}/testing/{filename}",
)

0 comments on commit 49b39b9

Please sign in to comment.