Coverage for ivatar/test_views.py: 100%
40 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-26 00:11 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-26 00:11 +0000
1# -*- coding: utf-8 -*-
2"""
3Test our views in ivatar.ivataraccount.views and ivatar.views
4"""
5# pylint: disable=too-many-lines
6import os
7import json
8import django
9from django.urls import reverse
10from django.test import TestCase
11from django.test import Client
12from django.contrib.auth.models import User
14from ivatar.utils import random_string
16os.environ["DJANGO_SETTINGS_MODULE"] = "ivatar.settings"
17django.setup()
20class Tester(TestCase): # pylint: disable=too-many-public-methods
21 """
22 Main test class
23 """
25 client = Client()
26 user = None
27 username = random_string()
28 password = random_string()
29 email = "%s@%s.%s" % (username, random_string(), random_string(2))
30 # Dunno why random tld doesn't work, but I'm too lazy now to investigate
31 openid = "http://%s.%s.%s/" % (username, random_string(), "org")
33 def login(self):
34 """
35 Login as user
36 """
37 self.client.login(username=self.username, password=self.password)
39 def setUp(self):
40 """
41 Prepare for tests.
42 - Create user
43 """
44 self.user = User.objects.create_user(
45 username=self.username,
46 password=self.password,
47 )
49 def test_incorrect_digest(self):
50 """
51 Test incorrect digest
52 """
53 response = self.client.get("/avatar/%s" % "x" * 65, follow=True)
54 self.assertEqual(
55 response.redirect_chain[0][0],
56 "/static/img/deadbeef.png",
57 "Doesn't redirect to static?",
58 )
59 # self.assertRedirects(
60 # response=response,
61 # expected_url="/static/img/deadbeef.png",
62 # msg_prefix="Why does an invalid hash not redirect to deadbeef?",
63 # )
65 def test_stats(self):
66 """
67 Test incorrect digest
68 """
69 response = self.client.get("/stats/", follow=True)
70 self.assertEqual(response.status_code, 200, "unable to fetch stats!")
71 j = json.loads(response.content)
72 self.assertEqual(j["users"], 1, "user count incorrect")
73 self.assertEqual(j["mails"], 0, "mails count incorrect")
74 self.assertEqual(j["openids"], 0, "openids count incorrect")
75 self.assertEqual(j["unconfirmed_mails"], 0, "unconfirmed mails count incorrect")
76 self.assertEqual(
77 j["unconfirmed_openids"], 0, "unconfirmed openids count incorrect"
78 )
79 self.assertEqual(j["avatars"], 0, "avatars count incorrect")
81 def test_logout(self):
82 """
83 Test if logout works correctly
84 """
85 self.login()
86 response = self.client.get(reverse("logout"), follow=True)
87 self.assertEqual(
88 response.status_code, 405, "logout with get should lead to http error 405"
89 )
90 response = self.client.post(reverse("logout"), follow=True)
91 self.assertEqual(response.status_code, 200, "logout with post should logout")