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