Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 170 additions & 1 deletion images/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
OS,
Port,
)
from util.base_test import BaseSingleUserTest
from util.base_test import BaseImageTest, BaseSingleUserTest


class InitStageTest(BaseSingleUserTest):
Expand Down Expand Up @@ -104,3 +104,172 @@ def test_listing_devices(self):
self.assertEqual(json.loads(response.content), expected)

self.assertEqual(response.status_code, status.HTTP_200_OK)


class ImageListingTest(BaseImageTest): # pylint: disable=too-many-ancestors
"""Tests the getting list of images. """

def test_listing_images(self):
"""Tests if possible get list of images. """

url = reverse('token-obtain-pair', kwargs={'version': 'v1'})
auth = self.client.post(url, data=json.dumps(self._user),
content_type='application/json')
header = b'Bearer ' + json.loads(auth.content)['access'].encode()
url = reverse('images-all', kwargs={'version': 'v1'})

response = self.client.get(url, content_type='application/json',
HTTP_AUTHORIZATION=header)

expected = [
{
'image_id': self._user_image_id,
'device_name': 'Raspberry Pi 3 Model B',
'distro_name': 'Debian 10 "Buster" (32-bit)',
'flavour': 'Classic',
'started_at': None,
'status': 'Succeeded',
'notes': ''
}
]

self.assertEqual(json.loads(response.content), expected)

self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_listing_images_unauthorized(self):
"""Tests if it's not possible to get list of images when unauthorized. """

url = reverse('images-all', kwargs={'version': 'v1'})
response = self.client.delete(url, content_type='application/json')

self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)


class ImageDeleteTest(BaseImageTest): # pylint: disable=too-many-ancestors
"""Tests the deleting image. """

def test_image_delete(self):
url = reverse('token-obtain-pair', kwargs={'version': 'v1'})
auth = self.client.post(url, data=json.dumps(self._user),
content_type='application/json')
header = b'Bearer ' + json.loads(auth.content)['access'].encode()
url = reverse('image-delete', kwargs={'version': 'v1'})

data = {'image_id': self._user_image_id}
response = self.client.delete(url, data=json.dumps(data), content_type='application/json',
HTTP_AUTHORIZATION=header)

self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_image_delete_unauthorized(self):
"""Tests if it's not possible to delete a image when unauthorized. """

url = reverse('image-delete', kwargs={'version': 'v1'})

data = {'image_id': self._user_image_id}
response = self.client.delete(url, data=json.dumps(data), content_type='application/json')

self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_image_delete_no_existing_image(self):
"""Tests if it's not possible to delete a image when it doesn't exist. """

url = reverse('token-obtain-pair', kwargs={'version': 'v1'})
auth = self.client.post(url, data=json.dumps(self._user),
content_type='application/json')
header = b'Bearer ' + json.loads(auth.content)['access'].encode()
url = reverse('image-delete', kwargs={'version': 'v1'})

data = {'image_id': 'a290bde6-1cde-45db-a5ed-4cf8273c80b5'}
response = self.client.delete(url, data=json.dumps(data), content_type='application/json',
HTTP_AUTHORIZATION=header)

self.assertEqual(response.content, b'{"image_id": ["Image does not exist."]}')

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_image_delete_user_not_have_current_image(self):
"""Tests if it's not possible to delete a image when it not belong current user. """

url = reverse('token-obtain-pair', kwargs={'version': 'v1'})
auth = self.client.post(url, data=json.dumps(self._user),
content_type='application/json')
header = b'Bearer ' + json.loads(auth.content)['access'].encode()
url = reverse('image-delete', kwargs={'version': 'v1'})

data = {'image_id': self._user2_image_id}
response = self.client.delete(url, data=json.dumps(data), content_type='application/json',
HTTP_AUTHORIZATION=header)

self.assertEqual(
response.content,
b'{"image_id": ["Current user does not have current image."]}',
)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)


class ImageUpdateNotesTest(BaseImageTest): # pylint: disable=too-many-ancestors
"""Tests the updating notes of image. """

def test_update_notes(self):
url = reverse('token-obtain-pair', kwargs={'version': 'v1'})
auth = self.client.post(url, data=json.dumps(self._user),
content_type='application/json')
header = b'Bearer ' + json.loads(auth.content)['access'].encode()
url = reverse('image-notes-update', kwargs={'version': 'v1'})

data = {'image_id': self._user_image_id, 'notes': 'test'}
response = self.client.put(url, data=json.dumps(data), content_type='application/json',
HTTP_AUTHORIZATION=header)

self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_image_update_notes_unauthorized(self):
"""Tests if it's not possible to update notes of image when unauthorized. """

url = reverse('image-notes-update', kwargs={'version': 'v1'})
data = {'image_id': self._user_image_id, 'notes': 'test'}

response = self.client.put(url, data=json.dumps(data), content_type='application/json')

self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_image_update_notes_no_existing_image(self):
"""Tests if it's not possible to update notes of image when it doesn't exist. """

url = reverse('token-obtain-pair', kwargs={'version': 'v1'})
auth = self.client.post(url, data=json.dumps(self._user),
content_type='application/json')
header = b'Bearer ' + json.loads(auth.content)['access'].encode()
url = reverse('image-notes-update', kwargs={'version': 'v1'})

data = {'image_id': 'a290bde6-1cde-45db-a5ed-4cf8273c80b5', 'notes': 'test'}
response = self.client.put(url, data=json.dumps(data), content_type='application/json',
HTTP_AUTHORIZATION=header)

self.assertEqual(response.content, b'{"image_id": ["Image does not exist."]}')

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_image_update_notes_user_not_have_current_image(self):
"""Tests if it's not possible to update notes of image when it not belong current user.
"""

url = reverse('token-obtain-pair', kwargs={'version': 'v1'})
auth = self.client.post(url, data=json.dumps(self._user),
content_type='application/json')
header = b'Bearer ' + json.loads(auth.content)['access'].encode()
url = reverse('image-notes-update', kwargs={'version': 'v1'})

data = {'image_id': self._user2_image_id, 'notes': 'test'}
response = self.client.put(url, data=json.dumps(data), content_type='application/json',
HTTP_AUTHORIZATION=header)

self.assertEqual(
response.content,
b'{"image_id": ["Current user does not have current image."]}',
)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
39 changes: 39 additions & 0 deletions util/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.contrib.auth.models import User
from rest_framework.test import APITestCase

from images.models import Image


class BaseSingleUserTest(APITestCase):
"""Base class for the tests that need to have a user created before running. """
Expand All @@ -20,3 +22,40 @@ def setUp(self):

user.person.email_confirmed = True
user.person.save(update_fields=['email_confirmed'])


class BaseImageTest(BaseSingleUserTest):
"""Base class for the image tests that need to have an images created before running. """

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._user2 = {
'username': 'test.user2',
'password': 'secret',
'email': 'test.user2@domain.com',
}
self._user_image_id = '21d43a3c-2a2c-45c1-8249-024baac7c399'
self._user2_image_id = '438afe69-3ad7-4f50-b6e7-e82a3518171e'

def setUp(self):
super().setUp()

user = User.objects.get(username=self._user['username'])
Image.objects.create(
user=user,
image_id=self._user_image_id,
device_name='Raspberry Pi 3 Model B',
distro_name='Debian 10 "Buster" (32-bit)',
flavour='C',
status='S',
)

user2 = User.objects.create_user(**self._user2)
Image.objects.create(
user=user2,
image_id=self._user2_image_id,
device_name='Raspberry Pi 3 Model B',
distro_name='Debian 10 "Buster" (32-bit)',
flavour='C',
status='S',
)