from io import StringIO from django.core.management import call_command from django.test import Client, TestCase, override_settings from django.urls import reverse class HealthzTests(TestCase): def test_healthz_ok(self): response = Client().get("/healthz/") self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["status"], "ok") class UnderConstructionTests(TestCase): @override_settings(SITE_UNDER_CONSTRUCTION=True) def test_home_redirects_when_gated(self): response = Client().get("/") self.assertEqual(response.status_code, 302) self.assertIn("/under-construction", response["Location"]) @override_settings(SITE_UNDER_CONSTRUCTION=False) def test_home_ok_when_open(self): response = Client().get("/") self.assertEqual(response.status_code, 200) class PublicSmokeTests(TestCase): def test_public_pages_get(self): client = Client() for name in ( "public:home", "public:about", "public:services", "public:testimonials", "public:contact", "public:careers", "public:terms", ): self.assertEqual(client.get(reverse(name)).status_code, 200, name) def test_contact_form_creates_lead(self): client = Client() response = client.post( reverse("public:contact"), { "first_name": "Pat", "last_name": "Jones", "email": "pat@example.com", "phone": "6305550100", "interest": "interior", "message": "Kitchen walls", }, ) self.assertEqual(response.status_code, 302) from leads.models import Lead lead = Lead.objects.get() self.assertIn("Interior painting", lead.message) def test_careers_form_creates_lead(self): client = Client() response = client.post( reverse("public:careers"), { "first_name": "Sam", "last_name": "Lee", "email": "sam@example.com", "phone": "6305550199", "positions": ["painter"], "qualifications": "Five summers of exterior work.", "start_date": "June", "drivers_license": "yes", "has_vehicle": "yes", }, ) self.assertEqual(response.status_code, 302) from contacts.models import Contact from leads.models import Lead lead = Lead.objects.get() self.assertEqual(lead.contact.source, Contact.Source.CAREERS) self.assertIn("Employment application", lead.message) class DispatchDueTests(TestCase): def test_quiet_when_nothing_due(self): out = StringIO() call_command("dispatch_due", stdout=out) self.assertEqual(out.getvalue(), "")