diff --git a/.gitignore b/.gitignore index 8a6830957..a07b4a7df 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ config.log autobackup.json Makefile oshino + +__pycache__/ +.pytest_cache/ +*.py[cod] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..35f6977f6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +pytest +pytest-redis +requests +factory_boy \ No newline at end of file diff --git a/tests/python/factories/__init__.py b/tests/python/factories/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/python/factories/archivefactory.py b/tests/python/factories/archivefactory.py new file mode 100644 index 000000000..9e599670e --- /dev/null +++ b/tests/python/factories/archivefactory.py @@ -0,0 +1,38 @@ +import factory +import os +import zipfile + +from factories.models.archives import Archive + +class ArchiveFactory(factory.Factory): + class Meta: + model = Archive + + name = factory.Faker("first_name") + tags = "date_added:1736124197" + summary = "" + arcsize = "16532135" + file = factory.LazyAttribute(lambda p: '/home/koyomi/lanraragi/content/test/{}.zip'.format(p.name)) + title = factory.LazyAttribute(lambda p: p.name) + isnew = "true" + pagecount = "30" + thumbhash = "ec2a0ca3a3da67a9390889f0910fe494241faa9a" + + @factory.post_generation + def generate_file(obj, create, extracted, **kwargs): + if not create: + return + + logo_path = "/lanraragi/public/img/logo.png" + content_path = "/lanraragi/content/test" + + if not os.path.exists(content_path): + os.makedirs(content_path) + + zip_path = os.path.join(content_path, f"{obj.name}.zip") + + with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: + file_name = os.path.basename(logo_path) + zipf.write(logo_path, file_name) + + return "/home/koyomi"+zip_path \ No newline at end of file diff --git a/tests/python/factories/models/__init__.py b/tests/python/factories/models/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/python/factories/models/archives.py b/tests/python/factories/models/archives.py new file mode 100644 index 000000000..3c6ca245e --- /dev/null +++ b/tests/python/factories/models/archives.py @@ -0,0 +1,13 @@ +from dataclasses import dataclass + +@dataclass +class Archive: + name: str + tags: str + summary: str + arcsize: str + file: str + title: str + isnew: str + pagecount: str + thumbhash: str \ No newline at end of file diff --git a/tests/python/test_archive.py b/tests/python/test_archive.py new file mode 100644 index 000000000..6b013e26b --- /dev/null +++ b/tests/python/test_archive.py @@ -0,0 +1,47 @@ +import unittest +import requests +import redis +import shutil +import base64 +import os +from utils.utils import generate_archive_id +from factories.archivefactory import ArchiveFactory + +class ArchiveAPITestCase(unittest.TestCase): + def setUp(self): + self.redis_client = redis.Redis(host='redis', port=6379) + self.archivefactory = ArchiveFactory + + # Set Apikey + self.redis_client.select(2) + self.redis_client.hset("LRR_CONFIG", "apikey", "lanraragi") + self.redis_client.select(0) + + def tearDown(self): + if os.path.exists('/lanraragi/content/test'): + shutil.rmtree('/lanraragi/content/test') + self.redis_client.flushall() + + def test_archives_list(self): + arcid = "28697b96f0ac5858be2614ed10ca47742c9522fd" #generate_archive_id() + archive = self.archivefactory.create() + self.redis_client.hset(arcid, mapping=archive.__dict__) + + response = requests.get('http://lanraragi:3000/api/archives') + data = response.json() + + self.assertEqual(response.status_code, 200) + self.assertEqual(data[0]["arcid"], arcid) + self.assertEqual(data[0]["filename"], archive.name) + + def test_plugins_list(self): + bearer = base64.b64encode("lanraragi".encode(encoding='utf-8')).decode('utf-8') + header = {'accept':'application/json', 'authorization': 'Bearer '+bearer} + response = requests.get('http://lanraragi:3000/api/plugins/login', headers=header) + data = response.json() + + self.assertEqual(response.status_code, 200) + self.assertEqual(data[0]["author"], "Difegue") + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/python/utils/__init__.py b/tests/python/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/python/utils/utils.py b/tests/python/utils/utils.py new file mode 100644 index 000000000..e4180e852 --- /dev/null +++ b/tests/python/utils/utils.py @@ -0,0 +1,8 @@ +import uuid +import hashlib + +def generate_archive_id(): + base_id = uuid.uuid4().hex + hashid = hashlib.sha1(base_id.encode()).hexdigest() + + return hashid \ No newline at end of file diff --git a/tools/build/docker/Dockerfile-python b/tools/build/docker/Dockerfile-python new file mode 100644 index 000000000..22444e8c1 --- /dev/null +++ b/tools/build/docker/Dockerfile-python @@ -0,0 +1,6 @@ +FROM python:3.8 + +COPY requirements.txt /tests/ +RUN pip install -r /tests/requirements.txt + +WORKDIR /tests diff --git a/tools/build/docker/docker-compose-testing.yml b/tools/build/docker/docker-compose-testing.yml new file mode 100644 index 000000000..725d61c68 --- /dev/null +++ b/tools/build/docker/docker-compose-testing.yml @@ -0,0 +1,43 @@ +--- +# Docker compose file for dev environments. +services: + lanraragi: + build: + dockerfile: tools/build/docker/Dockerfile-dev + context: ../../.. + volumes: + - ../../../:/home/koyomi/lanraragi + ports: + - "3000:3000" + environment: + - "LRR_REDIS_ADDRESS=redis:6379" + networks: + - lrr + + redis: + image: "docker.io/redis:7" + volumes: + - redis_test_data:/data + networks: + - lrr + + python-tests: + build: + dockerfile: tools/build/docker/Dockerfile-python + context: ../../.. + volumes: + - ../../../tests/python:/tests + - ../../../:/lanraragi + working_dir: /tests + networks: + - lrr + depends_on: + - lanraragi + - redis + command: ["tail", "-F", "Anything"] + +networks: + lrr: + +volumes: + redis_test_data: