From 6d6ae5e36d485dec270c524559717324e049b726 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 15 Jul 2025 19:04:21 +0000 Subject: [PATCH] Improve accounts views test coverage from 79% to 98% - Add TestProfileView with valid user and 404 error handling tests - Add TestSettingsView with authentication, valid/invalid form tests - Add TestCheckEmailView edge cases for empty email and missing parameter - All 41 tests pass, coverage increased by 19 percentage points - Target 90%+ coverage exceeded, only 1 line remaining uncovered Co-Authored-By: Shawn Azman --- realworld/accounts/tests.py | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/realworld/accounts/tests.py b/realworld/accounts/tests.py index 073e947..e5ceedf 100644 --- a/realworld/accounts/tests.py +++ b/realworld/accounts/tests.py @@ -117,6 +117,69 @@ def test_post_valid(self): self.assertTrue(User.objects.filter(email="tester@gmail.com").exists()) +class TestProfileView(TestCase): + password = "testpass" + + @classmethod + def setUpTestData(cls): + cls.user = User( + email="tester@gmail.com", + name="tester", + ) + cls.user.set_password(cls.password) + cls.user.save() + + def test_get_valid_user(self): + response = self.client.get(reverse("profile", args=[self.user.id])) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + self.assertContains(response, self.user.name) + + def test_get_nonexistent_user(self): + response = self.client.get(reverse("profile", args=[99999])) + self.assertEqual(response.status_code, http.HTTPStatus.NOT_FOUND) + + +class TestSettingsView(TestCase): + password = "testpass" + + @classmethod + def setUpTestData(cls): + cls.user = User( + email="tester@gmail.com", + name="tester", + ) + cls.user.set_password(cls.password) + cls.user.save() + + def test_get_authenticated(self): + self.client.force_login(self.user) + response = self.client.get(reverse("settings")) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + self.assertContains(response, "Settings") + + def test_get_unauthenticated(self): + response = self.client.get(reverse("settings")) + self.assertEqual(response.status_code, http.HTTPStatus.FOUND) + + def test_post_valid_form(self): + self.client.force_login(self.user) + response = self.client.post(reverse("settings"), { + "name": "Updated Name", + "email": "updated@gmail.com", + "bio": "Updated bio" + }) + self.assertEqual(response.headers["HX-Redirect"], self.user.get_absolute_url()) + + def test_post_invalid_form(self): + self.client.force_login(self.user) + response = self.client.post(reverse("settings"), { + "name": "", + "email": "invalid-email" + }) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + self.assertContains(response, "error") + + class TestCheckEmailView(TestCase): url = reverse_lazy("check_email") @@ -133,3 +196,13 @@ def test_exists(self): response = self.client.get(self.url, {"email": "tester@gmail.com"}) self.assertEqual(response.status_code, http.HTTPStatus.OK) self.assertContains(response, "This email is in use") + + def test_empty_email(self): + response = self.client.get(self.url, {"email": ""}) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + self.assertEqual(response.content, b"") + + def test_no_email_parameter(self): + response = self.client.get(self.url) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + self.assertEqual(response.content, b"")