From 7369db8512cad491614f541987119486e9513614 Mon Sep 17 00:00:00 2001 From: jedi Date: Sun, 19 Nov 2023 00:13:29 +0100 Subject: [PATCH] add /api url prefix in backend --- core/core/settings.py | 33 ++++++++++++++++--------- core/core/urls.py | 6 ++--- core/files/tests.py | 8 +++--- core/inventory/tests/test_api.py | 10 ++++---- core/inventory/tests/test_containers.py | 12 ++++----- core/inventory/tests/test_events.py | 12 ++++----- core/inventory/tests/test_items.py | 20 +++++++-------- 7 files changed, 55 insertions(+), 46 deletions(-) diff --git a/core/core/settings.py b/core/core/settings.py index a9c6323..c0e1291 100644 --- a/core/core/settings.py +++ b/core/core/settings.py @@ -10,6 +10,8 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ import os +import sys + import dotenv from pathlib import Path @@ -99,16 +101,24 @@ WSGI_APPLICATION = 'core.wsgi.application' # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.mysql', - 'HOST': os.getenv('DB_HOST', 'localhost'), - 'PORT': os.getenv('DB_PORT', '3306'), - 'NAME': os.getenv('DB_NAME', 'system3'), - 'USER': os.getenv('DB_USER', 'system3'), - 'PASSWORD': os.getenv('DB_PASSWORD', 'system3'), +if 'test' in sys.argv: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', + } + } +else: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'HOST': os.getenv('DB_HOST', 'localhost'), + 'PORT': os.getenv('DB_PORT', '3306'), + 'NAME': os.getenv('DB_NAME', 'system3'), + 'USER': os.getenv('DB_USER', 'system3'), + 'PASSWORD': os.getenv('DB_PASSWORD', 'system3'), + } } -} # Password validation # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators @@ -142,10 +152,10 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.2/howto/static-files/ -STATIC_ROOT = os.getenv('STATIC_ROOT') +STATIC_ROOT = os.getenv('STATIC_ROOT', 'staticfiles') STATIC_URL = '/static/' -MEDIA_ROOT = os.getenv('MEDIA_ROOT') +MEDIA_ROOT = os.getenv('MEDIA_ROOT', 'userfiles') MEDIA_URL = '/media/' STORAGES = { @@ -171,7 +181,6 @@ DATA_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 128 # 128 MB DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' - CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels.layers.InMemoryChannelLayer', diff --git a/core/core/urls.py b/core/core/urls.py index 313768a..50e4d91 100644 --- a/core/core/urls.py +++ b/core/core/urls.py @@ -21,7 +21,7 @@ from .version import get_info urlpatterns = [ path('admin/', admin.site.urls), - path('1/', include('inventory.api_v1')), - path('1/', include('files.api_v1')), - path('', get_info), + path('api/1/', include('inventory.api_v1')), + path('api/1/', include('files.api_v1')), + path('api/', get_info), ] diff --git a/core/files/tests.py b/core/files/tests.py index 974fd71..98c0ea8 100644 --- a/core/files/tests.py +++ b/core/files/tests.py @@ -16,7 +16,7 @@ class FileTestCase(TestCase): def test_list_files(self): import base64 item = File.objects.create(data=base64.b64encode(b"foo").decode('utf-8')) - response = client.get('/1/files/') + response = client.get('/api/1/files/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json()[0]['hash'], item.hash) self.assertEqual(len(response.json()[0]['hash']), 64) @@ -24,7 +24,7 @@ class FileTestCase(TestCase): def test_one_file(self): import base64 item = File.objects.create(data=base64.b64encode(b"foo").decode('utf-8')) - response = client.get(f'/1/file/{item.hash}/') + response = client.get(f'/api/1/file/{item.hash}/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['hash'], item.hash) self.assertEqual(len(response.json()['hash']), 64) @@ -33,7 +33,7 @@ class FileTestCase(TestCase): import base64 Item.objects.create(container=self.box, event=self.event, description='1') item = Item.objects.create(container=self.box, event=self.event, description='2') - response = client.post('/1/file/', {'data': base64.b64encode(b"foo").decode('utf-8')}, content_type='application/json') + response = client.post('/api/1/file/', {'data': base64.b64encode(b"foo").decode('utf-8')}, content_type='application/json') self.assertEqual(response.status_code, 201) self.assertEqual(len(response.json()['hash']), 64) @@ -43,5 +43,5 @@ class FileTestCase(TestCase): File.objects.create(item=item, data=base64.b64encode(b"foo").decode('utf-8')) file = File.objects.create(item=item, data=base64.b64encode(b"bar").decode('utf-8')) self.assertEqual(len(File.objects.all()), 2) - response = client.delete(f'/1/file/{file.hash}/') + response = client.delete(f'/api/1/file/{file.hash}/') self.assertEqual(response.status_code, 204) diff --git a/core/inventory/tests/test_api.py b/core/inventory/tests/test_api.py index ab96a80..2f75224 100644 --- a/core/inventory/tests/test_api.py +++ b/core/inventory/tests/test_api.py @@ -7,28 +7,28 @@ class ApiTest(TestCase): def test_root(self): from core.settings import SYSTEM3_VERSION - response = client.get('/') + response = client.get('/api/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["framework_version"], SYSTEM3_VERSION) def test_events(self): - response = client.get('/1/events/') + response = client.get('/api/1/events/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_containers(self): - response = client.get('/1/boxes/') + response = client.get('/api/1/boxes/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_files(self): - response = client.get('/1/files/') + response = client.get('/api/1/files/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_items(self): from inventory.models import Event Event.objects.create(slug='TEST1', name='Event') - response = client.get('/1/TEST1/items/') + response = client.get('/api/1/TEST1/items/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) diff --git a/core/inventory/tests/test_containers.py b/core/inventory/tests/test_containers.py index 179bb35..70268b7 100644 --- a/core/inventory/tests/test_containers.py +++ b/core/inventory/tests/test_containers.py @@ -7,13 +7,13 @@ client = Client() class ContainerTestCase(TestCase): def test_empty(self): - response = client.get('/1/boxes/') + response = client.get('/api/1/boxes/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_members(self): Container.objects.create(name='BOX') - response = client.get('/1/boxes/') + response = client.get('/api/1/boxes/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0]['cid'], 1) @@ -24,12 +24,12 @@ class ContainerTestCase(TestCase): Container.objects.create(name='BOX 1') Container.objects.create(name='BOX 2') Container.objects.create(name='BOX 3') - response = client.get('/1/boxes/') + response = client.get('/api/1/boxes/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 3) def test_create_container(self): - response = client.post('/1/box/', {'name': 'BOX'}) + response = client.post('/api/1/box/', {'name': 'BOX'}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['cid'], 1) self.assertEqual(response.json()['name'], 'BOX') @@ -41,7 +41,7 @@ class ContainerTestCase(TestCase): def test_update_container(self): from rest_framework.test import APIClient box = Container.objects.create(name='BOX 1') - response = APIClient().put(f'/1/box/{box.cid}/', {'name': 'BOX 2'}) + response = APIClient().put(f'/api/1/box/{box.cid}/', {'name': 'BOX 2'}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['cid'], 1) self.assertEqual(response.json()['name'], 'BOX 2') @@ -54,6 +54,6 @@ class ContainerTestCase(TestCase): box = Container.objects.create(name='BOX 1') Container.objects.create(name='BOX 2') self.assertEqual(len(Container.objects.all()), 2) - response = client.delete(f'/1/box/{box.cid}/') + response = client.delete(f'/api/1/box/{box.cid}/') self.assertEqual(response.status_code, 204) self.assertEqual(len(Container.objects.all()), 1) diff --git a/core/inventory/tests/test_events.py b/core/inventory/tests/test_events.py index f092e54..f2655ea 100644 --- a/core/inventory/tests/test_events.py +++ b/core/inventory/tests/test_events.py @@ -7,13 +7,13 @@ client = Client() class EventTestCase(TestCase): def test_empty(self): - response = client.get('/1/events/') + response = client.get('/api/1/events/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_members(self): Event.objects.create(slug='EVENT', name='Event') - response = client.get('/1/events/') + response = client.get('/api/1/events/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0]['slug'], 'EVENT') @@ -23,12 +23,12 @@ class EventTestCase(TestCase): Event.objects.create(slug='EVENT1', name='Event 1') Event.objects.create(slug='EVENT2', name='Event 2') Event.objects.create(slug='EVENT3', name='Event 3') - response = client.get('/1/events/') + response = client.get('/api/1/events/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 3) def test_create_event(self): - response = client.post('/1/events/', {'slug': 'EVENT', 'name': 'Event'}) + response = client.post('/api/1/events/', {'slug': 'EVENT', 'name': 'Event'}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['slug'], 'EVENT') self.assertEqual(response.json()['name'], 'Event') @@ -39,7 +39,7 @@ class EventTestCase(TestCase): def test_update_event(self): from rest_framework.test import APIClient event = Event.objects.create(slug='EVENT1', name='Event 1') - response = APIClient().put(f'/1/events/{event.eid}/', {'slug': 'EVENT2', 'name': 'Event 2 new'}) + response = APIClient().put(f'/api/1/events/{event.eid}/', {'slug': 'EVENT2', 'name': 'Event 2 new'}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['slug'], 'EVENT2') self.assertEqual(response.json()['name'], 'Event 2 new') @@ -51,6 +51,6 @@ class EventTestCase(TestCase): event = Event.objects.create(slug='EVENT1', name='Event 1') Event.objects.create(slug='EVENT2', name='Event 2') self.assertEqual(len(Event.objects.all()), 2) - response = client.delete(f'/1/events/{event.eid}/') + response = client.delete(f'/api/1/events/{event.eid}/') self.assertEqual(response.status_code, 204) self.assertEqual(len(Event.objects.all()), 1) diff --git a/core/inventory/tests/test_items.py b/core/inventory/tests/test_items.py index cf58be6..4234a3c 100644 --- a/core/inventory/tests/test_items.py +++ b/core/inventory/tests/test_items.py @@ -14,13 +14,13 @@ class ItemTestCase(TestCase): self.box = Container.objects.create(name='BOX') def test_empty(self): - response = client.get(f'/1/{self.event.slug}/item/') + response = client.get(f'/api/1/{self.event.slug}/item/') self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b'[]') def test_members(self): item = Item.objects.create(container=self.box, event=self.event, description='1') - response = client.get(f'/1/{self.event.slug}/item/') + response = client.get(f'/api/1/{self.event.slug}/item/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None}]) @@ -28,7 +28,7 @@ class ItemTestCase(TestCase): import base64 item = Item.objects.create(container=self.box, event=self.event, description='1') file = File.objects.create(item=item, data=base64.b64encode(b"foo").decode('utf-8')) - response = client.get(f'/1/{self.event.slug}/item/') + response = client.get(f'/api/1/{self.event.slug}/item/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), [{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash}]) @@ -37,12 +37,12 @@ class ItemTestCase(TestCase): Item.objects.create(container=self.box, event=self.event, description='1') Item.objects.create(container=self.box, event=self.event, description='2') Item.objects.create(container=self.box, event=self.event, description='3') - response = client.get(f'/1/{self.event.slug}/item/') + response = client.get(f'/api/1/{self.event.slug}/item/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 3) def test_create_item(self): - response = client.post(f'/1/{self.event.slug}/item/', {'cid': self.box.cid, 'description': '1'}) + response = client.post(f'/api/1/{self.event.slug}/item/', {'cid': self.box.cid, 'description': '1'}) self.assertEqual(response.status_code, 201) self.assertEqual(response.json(), {'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None}) self.assertEqual(len(Item.objects.all()), 1) @@ -51,12 +51,12 @@ class ItemTestCase(TestCase): self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) #def test_create_item_fail(self): - # response = client.post(f'/1/{self.event.slug}/item/', {'cid': self.box.cid}) + # response = client.post(f'/api/1/{self.event.slug}/item/', {'cid': self.box.cid}) # self.assertEqual(response.status_code, 500) def test_update_item(self): item = Item.objects.create(container=self.box, event=self.event, description='1') - response = client.put(f'/1/{self.event.slug}/item/{item.uid}/', {'description': '2'}, content_type='application/json') + response = client.put(f'/api/1/{self.event.slug}/item/{item.uid}/', {'description': '2'}, content_type='application/json') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None}) @@ -69,7 +69,7 @@ class ItemTestCase(TestCase): item = Item.objects.create(container=self.box, event=self.event, description='1') Item.objects.create(container=self.box, event=self.event, description='2') self.assertEqual(len(Item.objects.all()), 2) - response = client.delete(f'/1/{self.event.slug}/item/{item.uid}/') + response = client.delete(f'/api/1/{self.event.slug}/item/{item.uid}/') self.assertEqual(response.status_code, 204) self.assertEqual(len(Item.objects.all()), 1) @@ -77,7 +77,7 @@ class ItemTestCase(TestCase): Item.objects.create(container=self.box, event=self.event, description='1') item2 = Item.objects.create(container=self.box, event=self.event, description='2') self.assertEqual(len(Item.objects.all()), 2) - response = client.delete(f'/1/{self.event.slug}/item/{item2.uid}/') + response = client.delete(f'/api/1/{self.event.slug}/item/{item2.uid}/') self.assertEqual(response.status_code, 204) self.assertEqual(len(Item.objects.all()), 1) item3 = Item.objects.create(container=self.box, event=self.event, description='3') @@ -87,7 +87,7 @@ class ItemTestCase(TestCase): def test_item_count(self): Item.objects.create(container=self.box, event=self.event, description='1') Item.objects.create(container=self.box, event=self.event, description='2') - response = client.get('/1/boxes/') + response = client.get('/api/1/boxes/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0]['itemCount'], 2)