Coverage for ivatar/test_views.py: 92%

52 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-12 23:12 +0000

1# -*- coding: utf-8 -*- 

2""" 

3Test our views in ivatar.ivataraccount.views and ivatar.views 

4""" 

5 

6import contextlib 

7 

8# pylint: disable=too-many-lines 

9import os 

10import json 

11import django 

12from django.urls import reverse 

13from django.test import TestCase 

14from django.test import Client 

15from django.contrib.auth.models import User 

16from ivatar.utils import random_string, Bluesky 

17 

18BLUESKY_APP_PASSWORD = None 

19BLUESKY_IDENTIFIER = None 

20with contextlib.suppress(Exception): 

21 from settings import BLUESKY_APP_PASSWORD, BLUESKY_IDENTIFIER 

22os.environ["DJANGO_SETTINGS_MODULE"] = "ivatar.settings" 

23django.setup() 

24 

25 

26class Tester(TestCase): # pylint: disable=too-many-public-methods 

27 """ 

28 Main test class 

29 """ 

30 

31 client = Client() 

32 user = None 

33 username = random_string() 

34 password = random_string() 

35 email = "%s@%s.%s" % (username, random_string(), random_string(2)) 

36 # Dunno why random tld doesn't work, but I'm too lazy now to investigate 

37 openid = "http://%s.%s.%s/" % (username, random_string(), "org") 

38 

39 def login(self): 

40 """ 

41 Login as user 

42 """ 

43 self.client.login(username=self.username, password=self.password) 

44 

45 def setUp(self): 

46 """ 

47 Prepare for tests. 

48 - Create user 

49 """ 

50 self.user = User.objects.create_user( 

51 username=self.username, 

52 password=self.password, 

53 ) 

54 

55 def test_incorrect_digest(self): 

56 """ 

57 Test incorrect digest 

58 """ 

59 response = self.client.get("/avatar/" + "x" * 65, follow=True) 

60 self.assertEqual( 

61 response.redirect_chain[2][0], 

62 "/static/img/nobody/80.png", 

63 "Doesn't redirect to static?", 

64 ) 

65 # self.assertRedirects( 

66 # response=response, 

67 # expected_url="/static/img/nobody/80.png", 

68 # msg_prefix="Why does an invalid hash not redirect to deadbeef?", 

69 # ) 

70 

71 def test_stats(self): 

72 """ 

73 Test incorrect digest 

74 """ 

75 response = self.client.get("/stats/", follow=True) 

76 self.assertEqual(response.status_code, 200, "unable to fetch stats!") 

77 j = json.loads(response.content) 

78 self.assertEqual(j["users"], 1, "user count incorrect") 

79 self.assertEqual(j["mails"], 0, "mails count incorrect") 

80 self.assertEqual(j["openids"], 0, "openids count incorrect") 

81 self.assertEqual(j["unconfirmed_mails"], 0, "unconfirmed mails count incorrect") 

82 self.assertEqual( 

83 j["unconfirmed_openids"], 0, "unconfirmed openids count incorrect" 

84 ) 

85 self.assertEqual(j["avatars"], 0, "avatars count incorrect") 

86 

87 def test_logout(self): 

88 """ 

89 Test if logout works correctly 

90 """ 

91 self.login() 

92 response = self.client.get(reverse("logout"), follow=True) 

93 self.assertEqual( 

94 response.status_code, 405, "logout with get should lead to http error 405" 

95 ) 

96 response = self.client.post(reverse("logout"), follow=True) 

97 self.assertEqual(response.status_code, 200, "logout with post should logout") 

98 

99 def test_Bluesky_client(self): 

100 """ 

101 Bluesky client needs credentials, so it's limited with testing here now 

102 """ 

103 

104 if BLUESKY_APP_PASSWORD and BLUESKY_IDENTIFIER: 

105 b = Bluesky() 

106 profile = b.get_profile("libravatar.org") 

107 self.assertEqual(profile["handle"], "libravata.org") 

108 # As long as I don't change my avatar, this should stay the same 

109 self.assertEqual( 

110 profile["avatar"], 

111 "https://cdn.bsky.app/img/avatar/plain/did:plc:35jdu26cjgsc5vdbsaqiuw4a/bafkreidgtubihcdwcr72s5nag2ohcnwhhbg2zabw4jtxlhmtekrm6t5f4y@jpeg", 

112 ) 

113 self.assertEqual(True, True)