Mitja Nikolaus | 03e412b | 2018-09-18 17:50:15 +0200 | [diff] [blame^] | 1 | """Tests for the views module.""" |
| 2 | |
| 3 | import unittest |
| 4 | from urllib.parse import urlencode |
| 5 | |
| 6 | from django.conf import settings |
| 7 | from django.urls import reverse |
| 8 | |
| 9 | from rest_framework import status |
| 10 | |
| 11 | from crashreport_stats.tests.utils import Dummy, HiccupStatsAPITestCase |
| 12 | |
| 13 | # pylint: disable=too-many-public-methods |
| 14 | |
| 15 | |
| 16 | class ViewsTestCase(HiccupStatsAPITestCase): |
| 17 | """Test cases for the statistics views.""" |
| 18 | |
| 19 | home_url = reverse("device") |
| 20 | device_url = reverse("hiccup_stats_device") |
| 21 | versions_url = reverse("hiccup_stats_versions") |
| 22 | versions_all_url = reverse("hiccup_stats_versions_all") |
| 23 | |
| 24 | @staticmethod |
| 25 | def _url_with_params(url, params): |
| 26 | # Encode params, but keep slashes because we want to accept URLs as |
| 27 | # parameter values. |
| 28 | encoded_params = urlencode(params, safe="/") |
| 29 | return "{}?{}".format(url, encoded_params) |
| 30 | |
| 31 | def _get_with_params(self, url, params): |
| 32 | return self.fp_staff_client.get(self._url_with_params(url, params)) |
| 33 | |
| 34 | @unittest.skip( |
| 35 | "Fails because the view is currently not accessible for admin users." |
| 36 | ) |
| 37 | def test_home_view_as_admin(self): |
| 38 | """Test that admin users can access the home view.""" |
| 39 | self._assert_get_as_admin_user_succeeds(self.home_url) |
| 40 | |
| 41 | def test_home_view_as_fp_staff(self): |
| 42 | """Test that Fairphone staff users can access the home view.""" |
| 43 | self._assert_get_as_fp_staff_succeeds(self.home_url) |
| 44 | |
| 45 | def test_home_view_as_device_owner(self): |
| 46 | """Test that device owner users can not access the home view.""" |
| 47 | # Assert that the response is a redirect (to the login page) |
| 48 | self._assert_get_as_device_owner_fails( |
| 49 | self.home_url, expected_status=status.HTTP_302_FOUND |
| 50 | ) |
| 51 | |
| 52 | def test_home_view_no_auth(self): |
| 53 | """Test that one can not access the home view without auth.""" |
| 54 | # Assert that the response is a redirect (to the login page) |
| 55 | self._assert_get_without_authentication_fails( |
| 56 | self.home_url, expected_status=status.HTTP_302_FOUND |
| 57 | ) |
| 58 | |
| 59 | @unittest.skip( |
| 60 | "Fails because the view is currently not accessible for admin users." |
| 61 | ) |
| 62 | def test_device_view_as_admin(self): |
| 63 | """Test that admin users can access the device view.""" |
| 64 | self._assert_get_as_admin_user_succeeds( |
| 65 | self._url_with_params( |
| 66 | self.device_url, {"uuid": self.device_owner_device.uuid} |
| 67 | ) |
| 68 | ) |
| 69 | |
| 70 | def test_device_view_as_fp_staff(self): |
| 71 | """Test that Fairphone staff users can access the device view.""" |
| 72 | self._assert_get_as_fp_staff_succeeds( |
| 73 | self._url_with_params( |
| 74 | self.device_url, {"uuid": self.device_owner_device.uuid} |
| 75 | ) |
| 76 | ) |
| 77 | |
| 78 | def test_device_view_as_device_owner(self): |
| 79 | """Test that device owner users can not access the device view.""" |
| 80 | # Assert that the response is a redirect (to the login page) |
| 81 | self._assert_get_as_device_owner_fails( |
| 82 | self._url_with_params( |
| 83 | self.device_url, {"uuid": self.device_owner_device.uuid} |
| 84 | ), |
| 85 | expected_status=status.HTTP_302_FOUND, |
| 86 | ) |
| 87 | |
| 88 | def test_device_view_no_auth(self): |
| 89 | """Test that non-authenticated users can not access the device view.""" |
| 90 | # Assert that the response is a redirect (to the login page) |
| 91 | self._assert_get_without_authentication_fails( |
| 92 | self._url_with_params( |
| 93 | self.device_url, {"uuid": self.device_owner_device.uuid} |
| 94 | ), |
| 95 | expected_status=status.HTTP_302_FOUND, |
| 96 | ) |
| 97 | |
| 98 | @unittest.skip( |
| 99 | "Fails because the view is currently not accessible for admin users." |
| 100 | ) |
| 101 | def test_versions_view_as_admin(self): |
| 102 | """Test that admin users can access the versions view.""" |
| 103 | self._assert_get_as_admin_user_succeeds(self.versions_url) |
| 104 | |
| 105 | def test_versions_view_as_fp_staff(self): |
| 106 | """Test that Fairphone staff users can access the versions view.""" |
| 107 | self._assert_get_as_fp_staff_succeeds(self.versions_url) |
| 108 | |
| 109 | def test_versions_view_as_device_owner(self): |
| 110 | """Test that device owner users can not access the versions view.""" |
| 111 | # Assert that the response is a redirect (to the login page) |
| 112 | self._assert_get_as_device_owner_fails( |
| 113 | self.versions_url, expected_status=status.HTTP_302_FOUND |
| 114 | ) |
| 115 | |
| 116 | def test_versions_view_no_auth(self): |
| 117 | """Test one can not access the versions view without auth.""" |
| 118 | # Assert that the response is a redirect (to the login page) |
| 119 | self._assert_get_without_authentication_fails( |
| 120 | self.versions_url, expected_status=status.HTTP_302_FOUND |
| 121 | ) |
| 122 | |
| 123 | @unittest.skip( |
| 124 | "Fails because the view is currently not accessible for admin users." |
| 125 | ) |
| 126 | def test_versions_all_view_as_admin(self): |
| 127 | """Test that admin users can access the versions all view.""" |
| 128 | self._assert_get_as_admin_user_succeeds(self.versions_all_url) |
| 129 | |
| 130 | def test_versions_all_view_as_fp_staff(self): |
| 131 | """Test that Fairphone staff users can access the versions all view.""" |
| 132 | self._assert_get_as_fp_staff_succeeds(self.versions_all_url) |
| 133 | |
| 134 | def test_versions_all_view_as_device_owner(self): |
| 135 | """Test that device owner users can not access the versions all view.""" |
| 136 | # Assert that the response is a redirect (to the login page) |
| 137 | self._assert_get_as_device_owner_fails( |
| 138 | self.versions_all_url, expected_status=status.HTTP_302_FOUND |
| 139 | ) |
| 140 | |
| 141 | def test_versions_all_view_no_auth(self): |
| 142 | """Test that one can not access the versions all view without auth.""" |
| 143 | # Assert that the response is a redirect (to the login page) |
| 144 | self._assert_get_without_authentication_fails( |
| 145 | self.versions_all_url, expected_status=status.HTTP_302_FOUND |
| 146 | ) |
| 147 | |
| 148 | @unittest.skip( |
| 149 | "Fails because the view is currently not accessible for admin users." |
| 150 | ) |
| 151 | def test_home_view_post_as_admin_user(self): |
| 152 | """Test HTTP POST method to home view as admin user.""" |
| 153 | response = self.admin.post( |
| 154 | self.home_url, data={"uuid": str(self.device_owner_device.uuid)} |
| 155 | ) |
| 156 | |
| 157 | # Assert that the response is a redirect to the device page |
| 158 | self.assertRedirects( |
| 159 | response, |
| 160 | self._url_with_params( |
| 161 | self.device_url, {"uuid": self.device_owner_device.uuid} |
| 162 | ), |
| 163 | ) |
| 164 | |
| 165 | def test_home_view_post_as_fp_staff(self): |
| 166 | """Test HTTP POST method to home view as Fairphone staff user.""" |
| 167 | response = self.fp_staff_client.post( |
| 168 | self.home_url, data={"uuid": str(self.device_owner_device.uuid)} |
| 169 | ) |
| 170 | |
| 171 | # Assert that the response is a redirect to the device page |
| 172 | self.assertRedirects( |
| 173 | response, |
| 174 | self._url_with_params( |
| 175 | self.device_url, {"uuid": self.device_owner_device.uuid} |
| 176 | ), |
| 177 | ) |
| 178 | |
| 179 | def test_home_view_post_no_auth(self): |
| 180 | """Test HTTP POST method to home view without authentication.""" |
| 181 | response = self.client.post( |
| 182 | self.home_url, data={"uuid": str(self.device_owner_device.uuid)} |
| 183 | ) |
| 184 | |
| 185 | # Assert that the response is a redirect to the login page |
| 186 | self.assertRedirects( |
| 187 | response, |
| 188 | self._url_with_params( |
| 189 | settings.ACCOUNT_LOGOUT_REDIRECT_URL, |
| 190 | {"next": settings.LOGIN_REDIRECT_URL}, |
| 191 | ), |
| 192 | ) |
| 193 | |
| 194 | def test_home_view_post_as_device_owner(self): |
| 195 | """Test HTTP POST method to home view as device owner.""" |
| 196 | response = self.device_owner_client.post( |
| 197 | self.home_url, data={"uuid": str(self.device_owner_device.uuid)} |
| 198 | ) |
| 199 | |
| 200 | # Assert that the response is a redirect to the login page |
| 201 | |
| 202 | self.assertRedirects( |
| 203 | response, |
| 204 | self._url_with_params( |
| 205 | settings.ACCOUNT_LOGOUT_REDIRECT_URL, |
| 206 | {"next": settings.LOGIN_REDIRECT_URL}, |
| 207 | ), |
| 208 | ) |
| 209 | |
| 210 | def test_get_home_view(self): |
| 211 | """Test getting the home view with device search form.""" |
| 212 | response = self.fp_staff_client.get(self.home_url) |
| 213 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 214 | self.assertTemplateUsed( |
| 215 | response, "crashreport_stats/home.html", count=1 |
| 216 | ) |
| 217 | self.assertEqual(response.context["devices"], None) |
| 218 | |
| 219 | def test_home_view_filter_devices_by_uuid(self): |
| 220 | """Test filtering devices by UUID.""" |
| 221 | # Create a device |
| 222 | device = Dummy.create_dummy_device(Dummy.create_dummy_user()) |
| 223 | |
| 224 | # Filter devices by UUID of the created device |
| 225 | response = self.fp_staff_client.post( |
| 226 | self.home_url, data={"uuid": str(device.uuid)} |
| 227 | ) |
| 228 | |
| 229 | # Assert that the the client is redirected to the device page |
| 230 | self.assertRedirects( |
| 231 | response, |
| 232 | self._url_with_params(self.device_url, {"uuid": device.uuid}), |
| 233 | ) |
| 234 | |
| 235 | def test_home_view_filter_devices_by_uuid_part(self): |
| 236 | """Test filtering devices by start of UUID.""" |
| 237 | # Create a device |
| 238 | device = Dummy.create_dummy_device(Dummy.create_dummy_user()) |
| 239 | |
| 240 | # Filter devices with start of the created device's UUID |
| 241 | response = self.fp_staff_client.post( |
| 242 | self.home_url, data={"uuid": str(device.uuid)[:4]} |
| 243 | ) |
| 244 | |
| 245 | # Assert that the the client is redirected to the device page |
| 246 | self.assertRedirects( |
| 247 | response, |
| 248 | self._url_with_params(self.device_url, {"uuid": device.uuid}), |
| 249 | ) |
| 250 | |
| 251 | def test_home_view_filter_devices_by_uuid_part_ambiguous_result(self): |
| 252 | """Test filtering devices with common start of UUIDs.""" |
| 253 | # Create two devices |
| 254 | device1 = Dummy.create_dummy_device(Dummy.create_dummy_user()) |
| 255 | device2 = Dummy.create_dummy_device( |
| 256 | Dummy.create_dummy_user(username=Dummy.USERNAMES[1]) |
| 257 | ) |
| 258 | |
| 259 | # Adapt the devices' UUID so that they start with the same characters |
| 260 | device1.uuid = "4060fd90-6de1-4b03-a380-4277c703e913" |
| 261 | device1.save() |
| 262 | device2.uuid = "4061c59b-823d-4ec6-a463-8ac0c1cea67d" |
| 263 | device2.save() |
| 264 | |
| 265 | # Filter devices with first three (common) characters of the UUID |
| 266 | response = self.fp_staff_client.post( |
| 267 | self.home_url, data={"uuid": str(device1.uuid)[:3]} |
| 268 | ) |
| 269 | |
| 270 | # Assert that both devices are part of the result |
| 271 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 272 | self.assertTemplateUsed( |
| 273 | response, "crashreport_stats/home.html", count=1 |
| 274 | ) |
| 275 | self.assertEqual(set(response.context["devices"]), {device1, device2}) |
| 276 | |
| 277 | def test_home_view_filter_devices_empty_database(self): |
| 278 | """Test filtering devices on an empty database.""" |
| 279 | response = self.fp_staff_client.post( |
| 280 | self.home_url, data={"uuid": "TestUUID"} |
| 281 | ) |
| 282 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 283 | self.assertIsNotNone(response.content) |
| 284 | |
| 285 | def test_home_view_filter_devices_no_uuid(self): |
| 286 | """Test filtering devices without specifying UUID.""" |
| 287 | response = self.fp_staff_client.post(self.home_url) |
| 288 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 289 | self.assertTemplateUsed( |
| 290 | response, "crashreport_stats/home.html", count=1 |
| 291 | ) |
| 292 | self.assertEqual(response.context["devices"], None) |
| 293 | |
| 294 | def test_get_device_view_empty_database(self): |
| 295 | """Test getting device view on an empty database.""" |
| 296 | response = self.fp_staff_client.get(self.device_url) |
| 297 | self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
| 298 | |
| 299 | def test_get_device_view(self): |
| 300 | """Test getting device view.""" |
| 301 | # Create a device |
| 302 | device = Dummy.create_dummy_device(Dummy.create_dummy_user()) |
| 303 | |
| 304 | # Get the corresponding device view |
| 305 | response = self._get_with_params(self.device_url, {"uuid": device.uuid}) |
| 306 | |
| 307 | # Assert that the view is constructed from the correct templates and |
| 308 | # the response context contains the device UUID |
| 309 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 310 | self.assertTemplateUsed( |
| 311 | response, "crashreport_stats/device.html", count=1 |
| 312 | ) |
| 313 | self.assertTemplateUsed( |
| 314 | response, "crashreport_stats/tags/device_overview.html", count=1 |
| 315 | ) |
| 316 | self.assertTemplateUsed( |
| 317 | response, |
| 318 | "crashreport_stats/tags/device_update_history.html", |
| 319 | count=1, |
| 320 | ) |
| 321 | self.assertTemplateUsed( |
| 322 | response, |
| 323 | "crashreport_stats/tags/device_report_history.html", |
| 324 | count=1, |
| 325 | ) |
| 326 | self.assertTemplateUsed( |
| 327 | response, |
| 328 | "crashreport_stats/tags/device_crashreport_table.html", |
| 329 | count=1, |
| 330 | ) |
| 331 | self.assertEqual(response.context["uuid"], str(device.uuid)) |
| 332 | |
| 333 | def _assert_versions_view_templates_are_used(self, response): |
| 334 | self.assertTemplateUsed( |
| 335 | response, "crashreport_stats/versions.html", count=1 |
| 336 | ) |
| 337 | self.assertTemplateUsed( |
| 338 | response, "crashreport_stats/tags/versions_table.html", count=1 |
| 339 | ) |
| 340 | self.assertTemplateUsed( |
| 341 | response, "crashreport_stats/tags/versions_pie_chart.html", count=1 |
| 342 | ) |
| 343 | self.assertTemplateUsed( |
| 344 | response, "crashreport_stats/tags/versions_bar_chart.html", count=1 |
| 345 | ) |
| 346 | self.assertTemplateUsed( |
| 347 | response, "crashreport_stats/tags/versions_area_chart.html", count=1 |
| 348 | ) |
| 349 | |
| 350 | @unittest.skip("Fails because of wrong boolean usage in views.py") |
| 351 | def test_get_versions_view_empty_database(self): |
| 352 | """Test getting versions view on an empty database.""" |
| 353 | response = self.fp_staff_client.get(self.versions_url) |
| 354 | |
| 355 | # Assert that the correct templates are used and the response context |
| 356 | # contains the correct value for is_official_release |
| 357 | self._assert_versions_view_templates_are_used(response) |
| 358 | self.assertEqual(response.context["is_official_release"], True) |
| 359 | |
| 360 | @unittest.skip("Fails because of wrong boolean usage in views.py") |
| 361 | def test_get_versions_view(self): |
| 362 | """Test getting versions view.""" |
| 363 | # Create a version |
| 364 | Dummy.create_dummy_version() |
| 365 | |
| 366 | # Get the versions view |
| 367 | response = self.fp_staff_client.get(self.versions_url) |
| 368 | |
| 369 | # Assert that the correct templates are used and the response context |
| 370 | # contains the correct value for is_official_release |
| 371 | self._assert_versions_view_templates_are_used(response) |
| 372 | self.assertEqual(response.context["is_official_release"], True) |
| 373 | |
| 374 | @unittest.skip("Fails because of wrong boolean usage in views.py") |
| 375 | def test_get_versions_all_view_no_versions(self): |
| 376 | """Test getting versions all view on an empty database.""" |
| 377 | response = self.fp_staff_client.get(self.versions_all_url) |
| 378 | |
| 379 | # Assert that the correct templates are used and the response context |
| 380 | # contains an empty value for is_official_release |
| 381 | self._assert_versions_view_templates_are_used(response) |
| 382 | self.assertEqual(response.context.get("is_official_release", ""), "") |
| 383 | |
| 384 | @unittest.skip("Fails because of wrong boolean usage in views.py") |
| 385 | def test_get_versions_all_view(self): |
| 386 | """Test getting versions view.""" |
| 387 | # Create a version |
| 388 | Dummy.create_dummy_version() |
| 389 | |
| 390 | # Get the versions view |
| 391 | response = self.fp_staff_client.get(self.versions_all_url) |
| 392 | |
| 393 | # Assert that the correct templates are used and the response context |
| 394 | # contains the an empty value for is_official_release |
| 395 | self._assert_versions_view_templates_are_used(response) |
| 396 | self.assertEqual(response.context.get("is_official_release", ""), "") |