2023-11-18 11:57:50 +00:00
|
|
|
from django.test import TestCase, Client
|
2023-12-13 07:09:29 +00:00
|
|
|
from django.contrib.auth.models import Permission
|
2023-11-18 11:57:50 +00:00
|
|
|
|
2023-12-11 21:18:33 +00:00
|
|
|
from authentication.models import ExtendedUser
|
2023-11-18 11:57:50 +00:00
|
|
|
from files.models import File
|
|
|
|
from inventory.models import Event, Container, Item
|
2023-12-11 21:18:33 +00:00
|
|
|
from knox.models import AuthToken
|
2023-11-18 11:57:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FileTestCase(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2023-12-11 21:18:33 +00:00
|
|
|
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
2023-12-13 07:09:29 +00:00
|
|
|
self.user.user_permissions.add(*Permission.objects.all())
|
2023-12-11 21:18:33 +00:00
|
|
|
self.user.save()
|
|
|
|
self.token = AuthToken.objects.create(user=self.user)
|
|
|
|
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
2023-11-18 11:57:50 +00:00
|
|
|
self.event = Event.objects.create(slug='EVENT', name='Event')
|
|
|
|
self.box = Container.objects.create(name='BOX')
|
|
|
|
|
|
|
|
def test_list_files(self):
|
|
|
|
import base64
|
2023-11-20 19:09:58 +00:00
|
|
|
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
2023-12-11 21:18:33 +00:00
|
|
|
response = self.client.get('/api/2/files/')
|
2023-11-18 11:57:50 +00:00
|
|
|
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
|
2023-11-20 19:09:58 +00:00
|
|
|
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
2023-12-11 21:18:33 +00:00
|
|
|
response = self.client.get(f'/api/2/files/{item.hash}/')
|
2023-11-18 11:57:50 +00:00
|
|
|
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')
|
2023-12-11 21:18:33 +00:00
|
|
|
response = self.client.post('/api/2/files/',
|
2023-12-13 07:09:29 +00:00
|
|
|
{'data': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
|
|
|
content_type='application/json')
|
2023-11-18 11:57:50 +00:00
|
|
|
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')
|
2023-11-20 19:09:58 +00:00
|
|
|
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'))
|
2023-11-18 11:57:50 +00:00
|
|
|
self.assertEqual(len(File.objects.all()), 2)
|
2023-12-11 21:18:33 +00:00
|
|
|
response = self.client.delete(f'/api/2/files/{file.hash}/')
|
2023-11-18 11:57:50 +00:00
|
|
|
self.assertEqual(response.status_code, 204)
|