Mitja Nikolaus | 03e412b | 2018-09-18 17:50:15 +0200 | [diff] [blame] | 1 | """Tests for the crashreports REST API.""" |
Mitja Nikolaus | bc03e68 | 2018-11-13 16:48:20 +0100 | [diff] [blame] | 2 | from datetime import timedelta |
| 3 | |
| 4 | from django.db import connection |
Mitja Nikolaus | fd452f8 | 2018-11-07 11:53:59 +0100 | [diff] [blame] | 5 | from django.urls import reverse |
| 6 | from rest_framework import status |
Mitja Nikolaus | 03e412b | 2018-09-18 17:50:15 +0200 | [diff] [blame] | 7 | |
Mitja Nikolaus | bc03e68 | 2018-11-13 16:48:20 +0100 | [diff] [blame] | 8 | from crashreports.models import Crashreport |
| 9 | from crashreports.tests.utils import Dummy, RaceConditionsTestCase |
Mitja Nikolaus | 03e412b | 2018-09-18 17:50:15 +0200 | [diff] [blame] | 10 | from crashreports.tests.test_rest_api_heartbeats import HeartbeatsTestCase |
| 11 | |
| 12 | |
| 13 | class CrashreportsTestCase(HeartbeatsTestCase): |
| 14 | """Test cases for crash reports.""" |
| 15 | |
| 16 | # pylint: disable=too-many-ancestors |
| 17 | |
| 18 | LIST_CREATE_URL = "api_v1_crashreports" |
| 19 | RETRIEVE_URL = "api_v1_crashreport" |
| 20 | LIST_CREATE_BY_UUID_URL = "api_v1_crashreports_by_uuid" |
| 21 | RETRIEVE_BY_UUID_URL = "api_v1_crashreport_by_uuid" |
| 22 | |
| 23 | @staticmethod |
| 24 | def _create_dummy_data(**kwargs): |
| 25 | return Dummy.crashreport_data(**kwargs) |
Mitja Nikolaus | fd452f8 | 2018-11-07 11:53:59 +0100 | [diff] [blame] | 26 | |
| 27 | @staticmethod |
| 28 | def _create_alternative_dummy_data(**kwargs): |
| 29 | return Dummy.alternative_crashreport_data(**kwargs) |
| 30 | |
| 31 | def test_create_duplicate(self): |
| 32 | """Test creation of a duplicate crashreport.""" |
| 33 | # Create a first crashreport |
| 34 | report_data = self._create_dummy_data(uuid=self.uuid) |
| 35 | response_first = self.user.post( |
| 36 | reverse(self.LIST_CREATE_URL), report_data |
| 37 | ) |
| 38 | self.assertEqual(response_first.status_code, status.HTTP_201_CREATED) |
| 39 | |
| 40 | # Create a second crashreport for the same day and the same time |
| 41 | response_second = self.user.post( |
| 42 | reverse(self.LIST_CREATE_URL), report_data |
| 43 | ) |
| 44 | self.assertEqual(response_second.status_code, status.HTTP_201_CREATED) |
| 45 | |
| 46 | # Assert that only one crashreport instance was created |
| 47 | url = reverse(self.LIST_CREATE_BY_UUID_URL, args=[self.uuid]) |
| 48 | response = self.fp_staff_client.get(url) |
| 49 | self.assertEqual(len(response.data["results"]), 1) |
| 50 | |
| 51 | def test_create_with_datetime(self): |
| 52 | """Override to just pass because crashreports always use datetime.""" |
| 53 | pass |
Mitja Nikolaus | bc03e68 | 2018-11-13 16:48:20 +0100 | [diff] [blame] | 54 | |
| 55 | |
Mitja Nikolaus | bc03e68 | 2018-11-13 16:48:20 +0100 | [diff] [blame] | 56 | class CrashreportRaceConditionsTestCase(RaceConditionsTestCase): |
| 57 | """Test cases for crashreport race conditions.""" |
| 58 | |
| 59 | LIST_CREATE_URL = "api_v1_crashreports" |
| 60 | |
| 61 | def test_create_multiple_crashreports(self): |
| 62 | """Test that no race condition occurs when creating crashreports.""" |
| 63 | uuid, user, _ = self._register_device() |
| 64 | |
| 65 | def upload_report(client, data): |
| 66 | response = client.post(reverse(self.LIST_CREATE_URL), data) |
| 67 | self.assertEqual(status.HTTP_201_CREATED, response.status_code) |
| 68 | connection.close() |
| 69 | |
| 70 | data = Dummy.crashreport_data(uuid=uuid) |
| 71 | argslist = [ |
| 72 | [user, dict(data, date=data["date"] + timedelta(milliseconds=i))] |
| 73 | for i in range(10) |
| 74 | ] |
| 75 | |
| 76 | self._test_create_multiple( |
| 77 | Crashreport, upload_report, argslist, "device_local_id" |
| 78 | ) |