Coverage for website/tests/test_authorview_more.py: 100%
36 statements
« prev ^ index » next coverage.py v7.5.0, created at 2025-09-13 15:29 -0300
« prev ^ index » next coverage.py v7.5.0, created at 2025-09-13 15:29 -0300
1from django.contrib.auth import get_user_model
2from django.test import RequestFactory, TestCase
4from website.models.AuthorModel import Author
5from website.views.AuthorView import check_author_form, check_user_form, edit_author
7User = get_user_model()
10class AuthorViewMoreTests(TestCase):
11 def setUp(self):
12 self.factory = RequestFactory()
13 self.user = User.objects.create_user(
14 email="au1@example.com", password="pw", username="au1"
15 )
16 self.author = Author.objects.create(
17 user=self.user, author_name="Orig", author_url_slug="orig"
18 )
19 # create another user+author to simulate username target
20 self.other_user = User.objects.create_user(
21 email="other@example.com", password="pw", username="newname"
22 )
23 self.other_author = Author.objects.create(
24 user=self.other_user, author_name="Other", author_url_slug="other"
25 )
27 def test_check_author_form_updates_name_when_valid(self):
28 data = {
29 "author_name": "Updated Name",
30 "username": self.user.username,
31 "social_media_profile": [],
32 "social_media": [],
33 "exclude-social": [],
34 }
35 req = self.factory.post("/fake", data)
36 req.user = self.user
38 ok = check_author_form(req, self.author)
39 assert ok
40 self.author.refresh_from_db()
41 assert self.author.author_name == "Updated Name"
43 def test_check_user_form_updates_username_when_free(self):
44 # attempt to change current author's username to an existing
45 # username (other_user)
46 data = {
47 "username": "newname",
48 "author_name": "X",
49 "email": "new@example.com",
50 "password": "",
51 "confirm_pass": "",
52 }
53 req = self.factory.post("/fake", data)
54 req.user = self.user
56 free = check_user_form(req, self.author)
57 # should report username free (update may happen only when form valid)
58 assert free is True
60 def test_edit_author_with_user_author_returns_response(self):
61 # patch module render to avoid template parsing during unit test
62 import website.views.AuthorView as av
64 old_render = av.render
65 av.render = lambda *a, **k: type("R", (), {"status_code": 200})()
66 try:
67 req = self.factory.get("/fake")
68 req.user = self.user
69 resp = edit_author(req, author_slug=None)
70 assert getattr(resp, "status_code", None) == 200
71 finally:
72 av.render = old_render