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