blob: 491839b30fe63f7d1296f85eb3d571c470c2e495 [file] [log] [blame]
Mitja Nikolaus03e412b2018-09-18 17:50:15 +02001"""Utility functions shared by all crashreport stats tests."""
2
Mitja Nikolaus7dc86722018-11-27 14:57:39 +01003from datetime import datetime
Mitja Nikolaus03e412b2018-09-18 17:50:15 +02004
5import pytz
6from django.contrib.auth.models import Group
7from rest_framework import status
8from rest_framework.authtoken.models import Token
9from rest_framework.test import APITestCase, APIClient
10
11from crashreport_stats.models import (
12 Version,
13 VersionDaily,
14 RadioVersion,
15 RadioVersionDaily,
16 StatsMetadata,
17)
18
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010019from crashreports.models import User
20from crashreports.tests.utils import Dummy as CrashreportsDummy
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020021from hiccup.allauth_adapters import FP_STAFF_GROUP_NAME
22
23
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010024class Dummy(CrashreportsDummy):
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020025 """Class for creating dummy instances for testing."""
26
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020027 DEFAULT_DUMMY_VERSION_VALUES = {
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010028 "build_fingerprint": CrashreportsDummy.BUILD_FINGERPRINTS[0],
29 "first_seen_on": CrashreportsDummy.DATES[1],
30 "released_on": CrashreportsDummy.DATES[0],
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020031 "is_beta_release": False,
32 "is_official_release": True,
33 }
34
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010035 DEFAULT_DUMMY_VERSION_DAILY_VALUES = {"date": CrashreportsDummy.DATES[1]}
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020036
37 DEFAULT_DUMMY_RADIO_VERSION_VALUES = {
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010038 "radio_version": CrashreportsDummy.RADIO_VERSIONS[0],
39 "first_seen_on": CrashreportsDummy.DATES[1],
40 "released_on": CrashreportsDummy.DATES[0],
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020041 }
42
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010043 DEFAULT_DUMMY_RADIO_VERSION_DAILY_VALUES = {
44 "date": CrashreportsDummy.DATES[1]
45 }
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020046
47 DEFAULT_DUMMY_STATSMETADATA_VALUES = {
48 "updated_at": datetime(2018, 6, 15, 2, 12, 24, tzinfo=pytz.utc)
49 }
50
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020051 @staticmethod
Mitja Nikolaus35a02652018-10-01 15:10:18 +020052 def create_dummy_version(version_type=Version, **kwargs):
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020053 """Create a dummy version instance.
54
55 The dummy instance is created and saved to the database.
56 Args:
Mitja Nikolaus35a02652018-10-01 15:10:18 +020057 version_type: The class of the version type to be created.
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020058 **kwargs:
59 Optional arguments to extend/overwrite the default values.
60
61 Returns: The created version instance.
62
Mitja Nikolaus35a02652018-10-01 15:10:18 +020063 Raises:
64 ValueError: If version_type is not a valid version class type.
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020065
66 """
Mitja Nikolaus35a02652018-10-01 15:10:18 +020067 if version_type == Version:
68 entity = Version(
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010069 **Dummy._update_copy(Dummy.DEFAULT_DUMMY_VERSION_VALUES, kwargs)
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020070 )
Mitja Nikolaus35a02652018-10-01 15:10:18 +020071 elif version_type == RadioVersion:
72 entity = RadioVersion(
Mitja Nikolaus7dc86722018-11-27 14:57:39 +010073 **Dummy._update_copy(
Mitja Nikolaus35a02652018-10-01 15:10:18 +020074 Dummy.DEFAULT_DUMMY_RADIO_VERSION_VALUES, kwargs
75 )
76 )
77 else:
78 raise ValueError(
79 "No dummy version instance can be created for {}".format(
80 version_type.__name__
81 )
82 )
Mitja Nikolaus03e412b2018-09-18 17:50:15 +020083 entity.save()
84 return entity
85
86 @staticmethod
87 def create_dummy_daily_version(version, **kwargs):
88 """Create a dummy daily version instance.
89
90 The dummy instance is created and saved to the database.
91 Args:
92 **kwargs:
93 Optional arguments to extend/overwrite the default values.
94
95 Returns: The created daily version instance.
96
97 """
98 entity = VersionDaily(
99 version=version,
Mitja Nikolaus7dc86722018-11-27 14:57:39 +0100100 **Dummy._update_copy(
Mitja Nikolaus03e412b2018-09-18 17:50:15 +0200101 Dummy.DEFAULT_DUMMY_VERSION_DAILY_VALUES, kwargs
102 )
103 )
104 entity.save()
105 return entity
106
107 @staticmethod
108 def create_dummy_daily_radio_version(version, **kwargs):
109 """Create a dummy daily radio version instance.
110
111 The dummy instance is created and saved to the database.
112 Args:
113 **kwargs:
114 Optional arguments to extend/overwrite the default values.
115
116 Returns: The created daily radio version instance.
117
118 """
119 entity = RadioVersionDaily(
120 version=version,
Mitja Nikolaus7dc86722018-11-27 14:57:39 +0100121 **Dummy._update_copy(
Mitja Nikolaus03e412b2018-09-18 17:50:15 +0200122 Dummy.DEFAULT_DUMMY_RADIO_VERSION_DAILY_VALUES, kwargs
123 )
124 )
125 entity.save()
126 return entity
127
128 @staticmethod
129 def create_dummy_stats_metadata(**kwargs):
130 """Create a dummy stats metadata instance.
131
132 The dummy instance is created and saved to the database.
133 Args:
134 **kwargs:
135 Optional arguments to extend/overwrite the default values.
136
137 Returns: The created stats metadata instance.
138
139 """
140 entity = StatsMetadata(
Mitja Nikolaus7dc86722018-11-27 14:57:39 +0100141 **Dummy._update_copy(
Mitja Nikolaus03e412b2018-09-18 17:50:15 +0200142 Dummy.DEFAULT_DUMMY_STATSMETADATA_VALUES, kwargs
143 )
144 )
145 entity.save()
146 return entity
147
148
149class HiccupStatsAPITestCase(APITestCase):
150 """Abstract class for Hiccup stats REST API test cases to inherit from."""
151
152 @classmethod
153 def setUpTestData(cls): # noqa: N802
Mitja Nikolause0e83772018-11-05 10:00:53 +0100154 """Create client users for accessing the API.
Mitja Nikolaus03e412b2018-09-18 17:50:15 +0200155
156 The APIClient that can be used to make authenticated requests as
Mitja Nikolause0e83772018-11-05 10:00:53 +0100157 Fairphone staff user is stored in self.fp_staff_client. Additionally, a
158 client which is related to a device owner user is stored in
159 self.device_owner_client.
Mitja Nikolaus03e412b2018-09-18 17:50:15 +0200160 """
Mitja Nikolausd6907dd2018-10-17 14:13:06 +0200161 fp_staff_group = Group.objects.get(name=FP_STAFF_GROUP_NAME)
Mitja Nikolaus03e412b2018-09-18 17:50:15 +0200162 fp_staff_user = User.objects.create_user(
163 "fp_staff", "somebody@fairphone.com", "thepassword"
164 )
165 fp_staff_user.groups.add(fp_staff_group)
166 cls.fp_staff_client = APIClient()
167 cls.fp_staff_client.force_login(fp_staff_user)
168
169 cls.device_owner_user = User.objects.create_user(
170 "device_owner", "somebody@somemail.com", "thepassword"
171 )
172 Token.objects.create(user=cls.device_owner_user)
173 cls.device_owner_device = Dummy.create_dummy_device(
174 user=cls.device_owner_user
175 )
176 cls.device_owner_client = APIClient()
177 cls.device_owner_client.credentials(
178 HTTP_AUTHORIZATION="Token " + cls.device_owner_user.auth_token.key
179 )
180
Mitja Nikolaus03e412b2018-09-18 17:50:15 +0200181 def _assert_get_as_fp_staff_succeeds(
182 self, url, expected_status=status.HTTP_200_OK
183 ):
184 response = self.fp_staff_client.get(url)
185 self.assertEqual(response.status_code, expected_status)
186
187 def _assert_get_without_authentication_fails(
188 self, url, expected_status=status.HTTP_401_UNAUTHORIZED
189 ):
190 response = self.client.get(url)
191 self.assertEqual(response.status_code, expected_status)
192
193 def _assert_get_as_device_owner_fails(
194 self, url, expected_status=status.HTTP_403_FORBIDDEN
195 ):
196 response = self.device_owner_client.get(url)
197 self.assertEqual(response.status_code, expected_status)