|
| 1 | +import uuid |
| 2 | +from io import StringIO |
| 3 | + |
| 4 | +from django.contrib.sites.models import Site |
| 5 | +from django.core.management import CommandError, call_command |
| 6 | +from django.test import TestCase, override_settings |
| 7 | +from django.urls import reverse |
| 8 | + |
| 9 | +import requests_mock |
| 10 | +from rest_framework import status |
| 11 | + |
| 12 | +from objecttypes.config.objects import ObjectsAuthStep |
| 13 | +from objecttypes.config.site import SiteConfigurationStep |
| 14 | + |
| 15 | + |
| 16 | +@override_settings( |
| 17 | + OBJECTTYPES_DOMAIN="objecttypes.example.com", |
| 18 | + OBJECTTYPES_ORGANIZATION="ACME", |
| 19 | + OBJECTS_OBJECTTYPES_TOKEN="some-random-string", |
| 20 | + OBJECTS_OBJECTTYPES_PERSON="Some Person", |
| 21 | + OBJECTS_OBJECTTYPES_EMAIL="[email protected]", |
| 22 | +) |
| 23 | +class SetupConfigurationTests(TestCase): |
| 24 | + def setUp(self): |
| 25 | + super().setUp() |
| 26 | + |
| 27 | + self.addCleanup(Site.objects.clear_cache) |
| 28 | + |
| 29 | + @requests_mock.Mocker() |
| 30 | + def test_setup_configuration(self, m): |
| 31 | + stdout = StringIO() |
| 32 | + # mocks |
| 33 | + m.get("http://objecttypes.example.com/", status_code=200) |
| 34 | + m.get("http://objecttypes.example.com/api/v2/objecttypes", json=[]) |
| 35 | + |
| 36 | + call_command("setup_configuration", stdout=stdout) |
| 37 | + |
| 38 | + with self.subTest("Command output"): |
| 39 | + command_output = stdout.getvalue().splitlines() |
| 40 | + expected_output = [ |
| 41 | + f"Configuration will be set up with following steps: [{SiteConfigurationStep()}, " |
| 42 | + f"{ObjectsAuthStep()}]", |
| 43 | + f"Configuring {SiteConfigurationStep()}...", |
| 44 | + f"{SiteConfigurationStep()} is successfully configured", |
| 45 | + f"Configuring {ObjectsAuthStep()}...", |
| 46 | + f"{ObjectsAuthStep()} is successfully configured", |
| 47 | + "Instance configuration completed.", |
| 48 | + ] |
| 49 | + |
| 50 | + self.assertEqual(command_output, expected_output) |
| 51 | + |
| 52 | + with self.subTest("Site configured correctly"): |
| 53 | + site = Site.objects.get_current() |
| 54 | + self.assertEqual(site.domain, "objecttypes.example.com") |
| 55 | + self.assertEqual(site.name, "Objecttypes ACME") |
| 56 | + |
| 57 | + with self.subTest("Objects can query Objecttypes API"): |
| 58 | + response = self.client.get( |
| 59 | + reverse("v2:objecttype-list"), |
| 60 | + HTTP_AUTHORIZATION="Token some-random-string", |
| 61 | + ) |
| 62 | + |
| 63 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 64 | + |
| 65 | + @requests_mock.Mocker() |
| 66 | + def test_setup_configuration_selftest_fails(self, m): |
| 67 | + m.get("http://objecttypes.example.com/", status_code=200) |
| 68 | + m.get("http://objecttypes.example.com/api/v2/objecttypes", status_code=500) |
| 69 | + |
| 70 | + with self.assertRaisesMessage( |
| 71 | + CommandError, |
| 72 | + "Configuration test failed with errors: " |
| 73 | + "Objects API Authentication Configuration: " |
| 74 | + "Could not list objecttypes for the configured token", |
| 75 | + ): |
| 76 | + call_command("setup_configuration") |
| 77 | + |
| 78 | + @requests_mock.Mocker() |
| 79 | + def test_setup_configuration_without_selftest(self, m): |
| 80 | + stdout = StringIO() |
| 81 | + |
| 82 | + call_command("setup_configuration", no_selftest=True, stdout=stdout) |
| 83 | + command_output = stdout.getvalue() |
| 84 | + |
| 85 | + self.assertEqual(len(m.request_history), 0) |
| 86 | + self.assertTrue("Selftest is skipped" in command_output) |
0 commit comments