Coverage for ivatar/test_views.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 11:51 +0000

1""" 

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

3""" 

4 

5# pylint: disable=too-many-lines 

6import os 

7import django 

8from django.urls import reverse 

9from django.test import TestCase 

10from django.test import Client 

11from django.contrib.auth.models import User 

12from ivatar.utils import random_string, Bluesky 

13 

14from django.conf import settings 

15 

16BLUESKY_APP_PASSWORD = getattr(settings, "BLUESKY_APP_PASSWORD", None) 

17BLUESKY_IDENTIFIER = getattr(settings, "BLUESKY_IDENTIFIER", None) 

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

19django.setup() 

20 

21 

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

23 """ 

24 Main test class 

25 """ 

26 

27 client = Client() 

28 user = None 

29 username = random_string() 

30 password = random_string() 

31 email = "{}@{}.{}".format(username, random_string(), random_string(2)) 

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

33 openid = "http://{}.{}.{}/".format(username, random_string(), "org") 

34 

35 def login(self): 

36 """ 

37 Login as user 

38 """ 

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

40 

41 def setUp(self): 

42 """ 

43 Prepare for tests. 

44 - Create user 

45 """ 

46 self.user = User.objects.create_user( 

47 username=self.username, 

48 password=self.password, 

49 ) 

50 

51 def test_incorrect_digest(self): 

52 """ 

53 Test incorrect digest 

54 """ 

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

56 self.assertEqual( 

57 response.redirect_chain[2][0], 

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

59 "Doesn't redirect to static?", 

60 ) 

61 # self.assertRedirects( 

62 # response=response, 

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

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

65 # ) 

66 

67 def test_logout(self): 

68 """ 

69 Test if logout works correctly 

70 """ 

71 self.login() 

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

73 self.assertEqual( 

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

75 ) 

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

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

78 

79 def test_Bluesky_client(self): 

80 """ 

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

82 """ 

83 

84 if BLUESKY_APP_PASSWORD and BLUESKY_IDENTIFIER: 

85 b = Bluesky() 

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

87 self.assertEqual(profile["handle"], "libravatar.org") 

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

89 self.assertEqual( 

90 profile["avatar"], 

91 "https://cdn.bsky.app/img/avatar/plain/did:plc:pnzkvz5q5xg72bzkxrucalje/bafkreic456md3b46sm5sh3rsrgis26njy2d4p6u5yqxpz5bpqa6yk6ceni", 

92 ) 

93 self.assertEqual(True, True)