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