blob: 11b8e8e0e2ed8641fb00eb105d865aef40dfad0d [file] [log] [blame]
Mitja Nikolaus03e412b2018-09-18 17:50:15 +02001"""Tests for the crashreports REST API."""
Mitja Nikolausbc03e682018-11-13 16:48:20 +01002from datetime import timedelta
3
4from django.db import connection
Mitja Nikolausfd452f82018-11-07 11:53:59 +01005from django.urls import reverse
6from rest_framework import status
Mitja Nikolaus03e412b2018-09-18 17:50:15 +02007
Mitja Nikolausbc03e682018-11-13 16:48:20 +01008from crashreports.models import Crashreport
9from crashreports.tests.utils import Dummy, RaceConditionsTestCase
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020010from crashreports.tests.test_rest_api_heartbeats import HeartbeatsTestCase
11
12
13class 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 Nikolausfd452f82018-11-07 11:53:59 +010026
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 Nikolausbc03e682018-11-13 16:48:20 +010054
55
Mitja Nikolausbc03e682018-11-13 16:48:20 +010056class 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 )