Mitja Nikolaus | 03e412b | 2018-09-18 17:50:15 +0200 | [diff] [blame] | 1 | """Tests for the logfiles REST API.""" |
| 2 | |
| 3 | import os |
| 4 | import tempfile |
| 5 | |
| 6 | from django.urls import reverse |
| 7 | |
| 8 | from rest_framework import status |
| 9 | |
| 10 | from crashreports.tests.utils import HiccupCrashreportsAPITestCase, Dummy |
| 11 | |
| 12 | |
| 13 | class LogfileUploadTest(HiccupCrashreportsAPITestCase): |
| 14 | """Test cases for upload of log files.""" |
| 15 | |
| 16 | LIST_CREATE_URL = "api_v1_crashreports" |
| 17 | PUT_LOGFILE_URL = "api_v1_putlogfile_for_device_id" |
| 18 | |
| 19 | def _upload_crashreport(self, user, uuid): |
| 20 | """ |
| 21 | Upload dummy crashreport data. |
| 22 | |
| 23 | Args: |
| 24 | user: The user which should be used for uploading the report |
| 25 | uuid: The uuid of the device to which the report should be uploaded |
| 26 | |
| 27 | Returns: The local id of the device for which the report was uploaded. |
| 28 | |
| 29 | """ |
| 30 | data = Dummy.crashreport_data(uuid=uuid) |
| 31 | response = user.post(reverse(self.LIST_CREATE_URL), data) |
| 32 | self.assertEqual(status.HTTP_201_CREATED, response.status_code) |
| 33 | self.assertTrue("device_local_id" in response.data) |
| 34 | device_local_id = response.data["device_local_id"] |
| 35 | |
| 36 | return device_local_id |
| 37 | |
| 38 | def _test_logfile_upload(self, user, uuid): |
| 39 | # Upload crashreport |
| 40 | device_local_id = self._upload_crashreport(user, uuid) |
| 41 | |
| 42 | # Upload a logfile for the crashreport |
| 43 | logfile = tempfile.NamedTemporaryFile("w+", suffix=".log", delete=True) |
| 44 | logfile.write(u"blihblahblub") |
| 45 | response = user.post( |
| 46 | reverse( |
| 47 | self.PUT_LOGFILE_URL, |
| 48 | args=[uuid, device_local_id, os.path.basename(logfile.name)], |
| 49 | ), |
| 50 | {"file": logfile}, |
| 51 | format="multipart", |
| 52 | ) |
| 53 | self.assertEqual(status.HTTP_201_CREATED, response.status_code) |
| 54 | |
| 55 | def test_logfile_upload_as_user(self): |
| 56 | """Test upload of logfiles as device owner.""" |
| 57 | uuid, user, _ = self._register_device() |
| 58 | self._test_logfile_upload(user, uuid) |
| 59 | |
| 60 | def test_logfile_upload_as_admin(self): |
| 61 | """Test upload of logfiles as admin user.""" |
| 62 | uuid, _, _ = self._register_device() |
| 63 | self._test_logfile_upload(self.admin, uuid) |