create file API v2 and X-Accel-Redirect endpoint for images
This commit is contained in:
parent
ab5e8f36d1
commit
77828295f8
10 changed files with 164 additions and 1 deletions
0
core/files/tests/v1/__init__.py
Normal file
0
core/files/tests/v1/__init__.py
Normal file
49
core/files/tests/v1/test_files.py
Normal file
49
core/files/tests/v1/test_files.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
from django.test import TestCase, Client
|
||||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class FileTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||
self.box = Container.objects.create(name='BOX')
|
||||
|
||||
def test_list_files(self):
|
||||
import base64
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
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)
|
||||
|
||||
def test_one_file(self):
|
||||
import base64
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
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)
|
||||
|
||||
def test_create_file(self):
|
||||
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('/api/1/file',
|
||||
{'data': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
|
||||
def test_delete_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"bar").decode('utf-8'))
|
||||
self.assertEqual(len(File.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/file/{file.hash}')
|
||||
self.assertEqual(response.status_code, 204)
|
Loading…
Add table
Add a link
Reference in a new issue