Coverage for website/tests/test_author_view_unit.py: 98%

53 statements  

« 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 

3 

4from website.models.AuthorModel import Author 

5from website.models.AuthorSocialMediaModel import SocialMedia 

6from website.views.AuthorView import ( 

7 check_request_post, 

8 create_social_media, 

9 edit_author, 

10 exclude_social_media, 

11 update_social_media, 

12) 

13 

14User = get_user_model() 

15 

16 

17class AuthorViewUnitTests(TestCase): 

18 def setUp(self): 

19 self.factory = RequestFactory() 

20 # create a user and author 

21 self.user = User.objects.create_user( 

22 email="u1@example.com", password="pw", username="u1" 

23 ) 

24 self.author = Author.objects.create( 

25 user=self.user, author_name="A1", author_url_slug="a1" 

26 ) 

27 

28 def test_check_request_post_none_on_get(self): 

29 req = self.factory.get("/fake") 

30 req.user = self.user 

31 self.assertIsNone(check_request_post(req)) 

32 

33 def test_check_request_post_with_post_lists(self): 

34 # ensure author has no social media 

35 self.author.social_media.clear() 

36 data = { 

37 "username": self.user.username, 

38 "author_name": "New Name", 

39 "social_media_profile": ["p1", "p2"], 

40 "exclude-social": [""], 

41 "social_media": ["1", "2"], 

42 } 

43 req = self.factory.post("/fake", data) 

44 req.user = self.user 

45 # call should return a dict with keys 

46 d = check_request_post(req) 

47 assert d["username"] == self.user.username 

48 assert isinstance(d["new_social_addition"], bool) 

49 

50 def test_update_create_exclude_social_media_flow(self): 

51 # create one existing social media 

52 s1 = SocialMedia.objects.create( 

53 user_social_media=self.author, social_media=1, social_media_profile="pold" 

54 ) 

55 self.author.social_media.add(s1) 

56 

57 # prepare post with updated values and an extra new one 

58 data = { 

59 "social_media": ["1", "2"], 

60 "social_media_profile": ["pnew", "pnew2"], 

61 "exclude-social": [""], 

62 "username": self.user.username, 

63 "author_name": "New", 

64 } 

65 req = self.factory.post("/fake", data) 

66 req.user = self.user 

67 

68 # update_social_media should update existing entry and return True 

69 updated = update_social_media(req, list(self.author.social_media.all())) 

70 assert updated 

71 s1.refresh_from_db() 

72 assert s1.social_media_profile == "pnew" 

73 

74 # create_social_media should add a new SocialMedia for the author 

75 author_request_post = {"username": self.user.username} 

76 create_social_media(req, author_request_post) 

77 assert self.author.social_media.count() >= 2 

78 

79 # now test exclude_social_media: mark first social to exclude 

80 exclude_id = str(s1.social_media) 

81 req2 = self.factory.post( 

82 "/fake", 

83 { 

84 "exclude-social": [exclude_id], 

85 "username": self.user.username, 

86 "author_name": self.author.author_name, 

87 }, 

88 ) 

89 req2.user = self.user 

90 exclude_social_media(req2, self.author) 

91 # ensure deletion occurred (s1 should be deleted) 

92 from django.core.exceptions import ObjectDoesNotExist 

93 

94 try: 

95 s1.refresh_from_db() 

96 still = True 

97 except ObjectDoesNotExist: 

98 still = False 

99 assert not still 

100 

101 def test_edit_author_redirects_when_no_slug_and_no_author(self): 

102 # request.user without author attr 

103 anon = User.objects.create_user( 

104 email="u2@example.com", password="pw", username="u2" 

105 ) 

106 req = self.factory.get("/fake") 

107 req.user = anon 

108 res = edit_author(req, author_slug=None) 

109 # should be an HttpResponseRedirect to '/' 

110 assert res.status_code in (301, 302)