Skip to content

Commit

Permalink
Create test_api.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH committed May 11, 2024
1 parent d0d8125 commit e2962d6
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# API test file
import os
import tempfile
import shutil
import unittest
from app import app, db
from app.models import User, File
from app.services.encryption import encrypt_file

class TestAPI(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
self.client = app.test_client()
db.create_all()

def tearDown(self):
db.session.remove()
db.drop_all()

def test_upload_file(self):
with tempfile.TemporaryDirectory() as temp_dir:
file_path = os.path.join(temp_dir, 'test_file.txt')
with open(file_path, 'w') as f:
f.write('Test file content')
with open(file_path, 'rb') as f:
file = f.read()
encrypted_file = encrypt_file(file)
response = self.client.post('/files', data={
'username': 'test_user',
'file': (file_path, 'test_file.txt')
})
self.assertEqual(response.status_code, 200)
self.assertTrue(File.query.count() > 0)

def test_download_file(self):
with tempfile.TemporaryDirectory() as temp_dir:
file_path = os.path.join(temp_dir, 'test_file.txt')
with open(file_path, 'w') as f:
f.write('Test file content')
with open(file_path, 'rb') as f:
file = f.read()
encrypted_file = encrypt_file(file)
ipfs_hash = IPFS.add(encrypted_file)
file = File(name='test_file.txt', content=ipfs_hash, user=User(username='test_user'))
db.session.add(file)
db.session.commit()
response = self.client.get(f'/files/{ipfs_hash}')
self.assertEqual(response.status_code, 200)
self.assertTrue(response.content)

if __name__ == '__main__':
unittest.main()

0 comments on commit e2962d6

Please sign in to comment.