Coverage for ivatar/test_views.py: 90%
41 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-04 00:07 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-04 00:07 +0000
1"""
2Test our views in ivatar.ivataraccount.views and ivatar.views
3"""
5import contextlib
7# pylint: disable=too-many-lines
8import os
9import django
10from django.urls import reverse
11from django.test import TestCase
12from django.test import Client
13from django.contrib.auth.models import User
14from ivatar.utils import random_string, Bluesky
16BLUESKY_APP_PASSWORD = None
17BLUESKY_IDENTIFIER = None
18with contextlib.suppress(Exception):
19 from settings import BLUESKY_APP_PASSWORD, BLUESKY_IDENTIFIER
20os.environ["DJANGO_SETTINGS_MODULE"] = "ivatar.settings"
21django.setup()
24class Tester(TestCase): # pylint: disable=too-many-public-methods
25 """
26 Main test class
27 """
29 client = Client()
30 user = None
31 username = random_string()
32 password = random_string()
33 email = "{}@{}.{}".format(username, random_string(), random_string(2))
34 # Dunno why random tld doesn't work, but I'm too lazy now to investigate
35 openid = "http://{}.{}.{}/".format(username, random_string(), "org")
37 def login(self):
38 """
39 Login as user
40 """
41 self.client.login(username=self.username, password=self.password)
43 def setUp(self):
44 """
45 Prepare for tests.
46 - Create user
47 """
48 self.user = User.objects.create_user(
49 username=self.username,
50 password=self.password,
51 )
53 def test_incorrect_digest(self):
54 """
55 Test incorrect digest
56 """
57 response = self.client.get("/avatar/" + "x" * 65, follow=True)
58 self.assertEqual(
59 response.redirect_chain[2][0],
60 "/static/img/nobody/80.png",
61 "Doesn't redirect to static?",
62 )
63 # self.assertRedirects(
64 # response=response,
65 # expected_url="/static/img/nobody/80.png",
66 # msg_prefix="Why does an invalid hash not redirect to deadbeef?",
67 # )
69 def test_logout(self):
70 """
71 Test if logout works correctly
72 """
73 self.login()
74 response = self.client.get(reverse("logout"), follow=True)
75 self.assertEqual(
76 response.status_code, 405, "logout with get should lead to http error 405"
77 )
78 response = self.client.post(reverse("logout"), follow=True)
79 self.assertEqual(response.status_code, 200, "logout with post should logout")
81 def test_Bluesky_client(self):
82 """
83 Bluesky client needs credentials, so it's limited with testing here now
84 """
86 if BLUESKY_APP_PASSWORD and BLUESKY_IDENTIFIER:
87 b = Bluesky()
88 profile = b.get_profile("libravatar.org")
89 self.assertEqual(profile["handle"], "libravata.org")
90 # As long as I don't change my avatar, this should stay the same
91 self.assertEqual(
92 profile["avatar"],
93 "https://cdn.bsky.app/img/avatar/plain/did:plc:35jdu26cjgsc5vdbsaqiuw4a/bafkreidgtubihcdwcr72s5nag2ohcnwhhbg2zabw4jtxlhmtekrm6t5f4y@jpeg",
94 )
95 self.assertEqual(True, True)