Mitja Nikolaus | 03e412b | 2018-09-18 17:50:15 +0200 | [diff] [blame^] | 1 | """Tests for the devices REST API.""" |
| 2 | |
| 3 | from django.urls import reverse |
| 4 | |
| 5 | from rest_framework import status |
| 6 | |
| 7 | from crashreports.tests.utils import HiccupCrashreportsAPITestCase, Dummy |
| 8 | |
| 9 | |
| 10 | class DeviceTestCase(HiccupCrashreportsAPITestCase): |
| 11 | """Test cases for registering devices.""" |
| 12 | |
| 13 | def test_register(self): |
| 14 | """Test registration of devices.""" |
| 15 | response = self.client.post( |
| 16 | reverse(self.REGISTER_DEVICE_URL), Dummy.device_register_data() |
| 17 | ) |
| 18 | self.assertTrue("token" in response.data) |
| 19 | self.assertTrue("uuid" in response.data) |
| 20 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 21 | |
| 22 | def test_create_missing_fields(self): |
| 23 | """Test registration with missing fields.""" |
| 24 | response = self.client.post(reverse(self.REGISTER_DEVICE_URL)) |
| 25 | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 26 | |
| 27 | def test_create_missing_board_date(self): |
| 28 | """Test registration with missing board date.""" |
| 29 | data = Dummy.device_register_data() |
| 30 | data.pop("board_date") |
| 31 | response = self.client.post(reverse(self.REGISTER_DEVICE_URL), data) |
| 32 | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 33 | |
| 34 | def test_create_missing_chipset(self): |
| 35 | """Test registration with missing chipset.""" |
| 36 | data = Dummy.device_register_data() |
| 37 | data.pop("chipset") |
| 38 | response = self.client.post(reverse(self.REGISTER_DEVICE_URL), data) |
| 39 | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 40 | |
| 41 | def test_create_invalid_board_date(self): |
| 42 | """Test registration with invalid board date.""" |
| 43 | data = Dummy.device_register_data() |
| 44 | data["board_date"] = "not_a_valid_date" |
| 45 | response = self.client.post(reverse(self.REGISTER_DEVICE_URL), data) |
| 46 | self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 47 | |
| 48 | def test_create_non_existent_time_board_date(self): |
| 49 | """Test registration with non existing time. |
| 50 | |
| 51 | Test the resolution of a naive date-time in which the |
| 52 | Europe/Amsterdam daylight saving time transition moved the time |
| 53 | "forward". The server should not crash when receiving a naive |
| 54 | date-time which does not exist in the server timezone or locale. |
| 55 | """ |
| 56 | data = Dummy.device_register_data() |
| 57 | # In 2017, the Netherlands changed from CET to CEST on March, |
| 58 | # 26 at 02:00 |
| 59 | data["board_date"] = "2017-03-26 02:34:56" |
| 60 | response = self.client.post(reverse(self.REGISTER_DEVICE_URL), data) |
| 61 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 62 | |
| 63 | def test_create_ambiguous_time_board_date(self): |
| 64 | """Test registration with ambiguous time. |
| 65 | |
| 66 | Test the resolution of a naive date-time in which the |
| 67 | Europe/Amsterdam daylight saving time transition moved the time |
| 68 | "backward". The server should not crash when receiving a naive |
| 69 | date-time that can belong to multiple timezones. |
| 70 | """ |
| 71 | data = Dummy.device_register_data() |
| 72 | # In 2017, the Netherlands changed from CEST to CET on October, |
| 73 | # 29 at 03:00 |
| 74 | data["board_date"] = "2017-10-29 02:34:56" |
| 75 | response = self.client.post(reverse(self.REGISTER_DEVICE_URL), data) |
| 76 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 77 | |
| 78 | |
| 79 | class ListDevicesTestCase(HiccupCrashreportsAPITestCase): |
| 80 | """Test cases for listing and deleting devices.""" |
| 81 | |
| 82 | LIST_CREATE_URL = "api_v1_list_devices" |
| 83 | RETRIEVE_URL = "api_v1_retrieve_device" |
| 84 | |
| 85 | def test_device_list(self): |
| 86 | """Test registration of 2 devices.""" |
| 87 | number_of_devices = 2 |
| 88 | uuids = [ |
| 89 | str(self._register_device()[0]) for _ in range(number_of_devices) |
| 90 | ] |
| 91 | |
| 92 | response = self.admin.get(reverse(self.LIST_CREATE_URL), {}) |
| 93 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 94 | self.assertEqual(len(response.data["results"]), number_of_devices) |
| 95 | for result in response.data["results"]: |
| 96 | self.assertIn(result["uuid"], uuids) |
| 97 | |
| 98 | def test_device_list_unauth(self): |
| 99 | """Test listing devices without authentication.""" |
| 100 | response = self.client.get(reverse(self.LIST_CREATE_URL), {}) |
| 101 | self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
| 102 | |
| 103 | def test_retrieve_device_auth(self): |
| 104 | """Test retrieval of devices as admin user.""" |
| 105 | uuid, _, token = self._register_device() |
| 106 | response = self.admin.get(reverse(self.RETRIEVE_URL, args=[uuid]), {}) |
| 107 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 108 | self.assertEqual(response.data["uuid"], str(uuid)) |
| 109 | self.assertEqual(response.data["token"], token) |
| 110 | |
| 111 | def test_retrieve_device_unauth(self): |
| 112 | """Test retrieval of devices without authentication.""" |
| 113 | uuid, _, _ = self._register_device() |
| 114 | response = self.client.get(reverse(self.RETRIEVE_URL, args=[uuid]), {}) |
| 115 | self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
| 116 | |
| 117 | def test_delete_device_auth(self): |
| 118 | """Test deletion of devices as admin user.""" |
| 119 | uuid, _, _ = self._register_device() |
| 120 | url = reverse(self.RETRIEVE_URL, args=[uuid]) |
| 121 | response = self.admin.delete(url, {}) |
| 122 | self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) |
| 123 | response = self.admin.delete(url, {}) |
| 124 | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |