Fix tests

This commit is contained in:
Jan Dittberner 2023-04-29 13:10:25 +02:00
parent 866f6c8083
commit d88745f46b
11 changed files with 35 additions and 116 deletions

View file

@ -1,11 +0,0 @@
{% extends "base.html" %}
{% load i18n %}
{% block title %}{{ block.super }} - {% translate "Welcome" %}{% endblock title %}
{% block page_title %}{% translate "Welcome to our customer self service" %}{% endblock page_title %}
{% block content %}
<p>{% url 'customer_dashboard' slug=user.username as dashboard_url %}
{% blocktranslate with full_name=user.get_full_name trimmed %}
Hello {{ full_name }},<br/>
You can visit your <a href="{{ dashboard_url }}">Dashboard</a> to view and modify your hosting options.
{% endblocktranslate %}</p>
{% endblock content %}

View file

@ -1,9 +1,9 @@
{% extends "base.html" %}
{% load i18n %}
{% block title %}{{ block.super }} - {% blocktranslate with full_name=dashboard_user.get_full_name trimmed %}
{% block title %}{{ block.super }} - {% blocktranslate with full_name=request.user.get_full_name trimmed %}
Dashboard for {{ full_name }}
{% endblocktranslate %}{% endblock title %}
{% block page_title %}{% blocktranslate with full_name=dashboard_user.get_full_name trimmed %}
{% block page_title %}{% blocktranslate with full_name=request.user.get_full_name trimmed %}
Dashboard for {{ full_name }}
{% endblocktranslate %}{% endblock page_title %}
{% block content %}
@ -39,7 +39,7 @@
{% translate "This user has no hosting packages assigned yet." %}{% endif %}</p>
{% endif %}
{% if user.is_staff %}
<a href="{% url "create_customer_hosting_package" user=object.username %}"
<a href="{% url "create_customer_hosting_package" user=request.user.username %}"
class="btn btn-primary">{% translate "Add hosting package" %}</a>
{% endif %}
</div>

View file

@ -2,7 +2,7 @@
Tests for :py:mod:`dashboard.views`.
"""
from django import http
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
@ -13,65 +13,35 @@ TEST_USER = "test"
TEST_PASSWORD = "secret"
class IndexViewTest(TestCase):
def test_index_view_anonymous(self):
response = self.client.get(reverse("dashboard"))
self.assertRedirects(response, "/accounts/login/?next=/")
def test_index_view(self):
user = User.objects.create(username=TEST_USER)
user.set_password(TEST_PASSWORD)
user.save()
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
response = self.client.get(reverse("dashboard"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "dashboard/index.html")
class UserDashboardViewTest(TestCase):
def _create_test_user(self):
self.user = User.objects.create(username=TEST_USER)
self.user.set_password(TEST_PASSWORD)
self.user.save()
def test_user_dashboard_view_no_user(self):
response = self.client.get(
reverse("customer_dashboard", kwargs={"slug": TEST_USER})
)
self.assertEqual(response.status_code, 404)
def test_user_dashboard_view_anonymous(self):
User.objects.create(username=TEST_USER)
response = self.client.get(
reverse("customer_dashboard", kwargs={"slug": TEST_USER})
)
self.assertEqual(response.status_code, 403)
response = self.client.get(reverse("customer_dashboard"))
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, "/accounts/login/?next=/")
def test_user_dashboard_view_logged_in_ok(self):
self._create_test_user()
self.assertTrue(self.client.login(username=TEST_USER, password=TEST_PASSWORD))
response = self.client.get(
reverse("customer_dashboard", kwargs={"slug": TEST_USER})
)
response = self.client.get(reverse("customer_dashboard"))
self.assertEqual(response.status_code, 200)
def test_user_dashboard_view_logged_in_template(self):
self._create_test_user()
self.assertTrue(self.client.login(username=TEST_USER, password=TEST_PASSWORD))
response = self.client.get(
reverse("customer_dashboard", kwargs={"slug": TEST_USER})
reverse("customer_dashboard")
)
self.assertTemplateUsed(response, "dashboard/user_dashboard.html")
def test_user_dashboard_view_logged_in_context_fresh(self):
self._create_test_user()
self.assertTrue(self.client.login(username=TEST_USER, password=TEST_PASSWORD))
response = self.client.get(
reverse("customer_dashboard", kwargs={"slug": TEST_USER})
)
self.assertIn("dashboard_user", response.context)
self.assertEqual(self.user, response.context["dashboard_user"])
response = self.client.get(reverse("customer_dashboard"))
self.assertIn("hosting_packages", response.context)
self.assertEqual(len(response.context["hosting_packages"]), 0)

View file

@ -1,14 +1,9 @@
from __future__ import absolute_import
from django.urls import re_path
from django.urls import path
from .views import IndexView, UserDashboardView
from .views import UserDashboardView
urlpatterns = [
re_path(r"^$", IndexView.as_view(), name="dashboard"),
re_path(
r"^user/(?P<slug>[\w0-9@.+-_]+)/$",
UserDashboardView.as_view(),
name="customer_dashboard",
),
path("", UserDashboardView.as_view(), name="customer_dashboard"),
]

View file

@ -11,41 +11,17 @@ from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
from hostingpackages.models import CustomerHostingPackage
class IndexView(LoginRequiredMixin, TemplateView):
"""
This is the dashboard view.
"""
template_name = "dashboard/index.html"
def dispatch(self, request, *args, **kwargs):
if not request.user.is_anonymous:
return redirect("customer_dashboard", slug=request.user.username)
return super().dispatch(request, *args, **kwargs)
class UserDashboardView(StaffOrSelfLoginRequiredMixin, DetailView):
class UserDashboardView(LoginRequiredMixin, TemplateView):
"""
This is the user dashboard view.
"""
model = get_user_model()
context_object_name = "dashboard_user"
slug_field = "username"
template_name = "dashboard/user_dashboard.html"
def get_context_data(self, **kwargs):
context = super(UserDashboardView, self).get_context_data(**kwargs)
context["hosting_packages"] = CustomerHostingPackage.objects.filter(
customer=self.object
customer=self.request.user
)
return context
def get_customer_object(self):
"""
Returns the customer object.
"""
return self.get_object()

View file

@ -56,15 +56,6 @@ class NavigationContextProcessorTest(TestCase):
self._check_static_urls(response.context)
self.assertEqual(response.context["active_item"], "contact")
def test_hostingpackage_page_context(self):
User.objects.create_user("test", password="test")
self.client.login(username="test", password="test")
response = self.client.get(reverse("hosting_packages", kwargs={"user": "test"}))
for item in self.EXPECTED_ITEMS:
self.assertIn(item, response.context)
self._check_static_urls(response.context)
self.assertEqual(response.context["active_item"], "hostingpackage")
def _test_page_context_by_viewmodule(self, viewmodule, expecteditem):
request = HttpRequest()
request.resolver_match = MagicMock()

View file

@ -21,9 +21,8 @@
{% for package in customerhostingpackage_list %}
<tr>
<td><a href="{{ package.get_absolute_url }}">{{ package.name }}</a></td>
<td>
<a href="{% url 'customer_dashboard' slug=package.customer.username %}">{{ package.customer }}</a>
</td>
<td>{{ package.customer }}</td>
<td>{{ package.osuser.username }}</td>
<td>
{% with diskspace=package.get_disk_space %}
<span title="{% blocktranslate trimmed %}
@ -40,7 +39,6 @@
{% include "userdbs/snippets/db_type.html" with db_type=dbtype.db_type %}
{% if not forloop.last %} / {% endif %}
{% endfor %}</td>
<td>{{ package.osuser.username }}</td>
<td>{{ package.created }}</td>
</tr>
{% endfor %}

View file

@ -212,7 +212,7 @@ class GroupTest(TestCaseWithCeleryTasks):
group.delete()
self.assertEqual(len(Group.objects.all()), 0)
self.assertEqual(len(TaskResult.objects.all()), 2)
tr = TaskResult.objects.first()
tr = TaskResult.objects.order_by("created").first()
self.assertEqual(tr.creator, "handle_group_created")

View file

@ -32,7 +32,7 @@ class FetchTaskResultsCommandTest(TestCase):
aresult.state = "PENDING"
aresult.ready.return_value = False
Command().handle()
Command().handle(verbosity=0)
tr = TaskResult.objects.get(task_id=TEST_TASK_UUID)
self.assertTrue(asyncresult.called_with(TEST_TASK_UUID))
@ -55,7 +55,7 @@ class FetchTaskResultsCommandTest(TestCase):
aresult.ready.return_value = True
aresult.get.return_value = TEST_TASK_RESULT
Command().handle()
Command().handle(verbosity=0)
tr = TaskResult.objects.get(task_id=TEST_TASK_UUID)
self.assertTrue(asyncresult.called_with(TEST_TASK_UUID))

View file

@ -23,7 +23,7 @@
<body>
<nav class="navbar navbar-expand-lg sticky-top navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="{% url 'dashboard' %}" title="{% translate "Dashboard" %}">gnuviechadmin</a>
<a class="navbar-brand" href="{% url "customer_dashboard" %}" title="{% translate "Dashboard" %}">gnuviechadmin</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarContent"
aria-controls="navbarContent" aria-expanded="false" aria-label="{% translate "Toggle navigation" %}">
<span class="navbar-toggler-icon"></span>
@ -66,7 +66,7 @@
</ul>
{% if user.is_authenticated %}
{% user_display user as user_display %}
{% url 'customer_dashboard' slug=user.username as profile_url %}
{% url "customer_dashboard" as profile_url %}
{% if user.is_impersonate %}
{% user_display user.impersonator as impersonator_display %}
{% url 'impersonate-stop' as stop_impersonation_url %}

View file

@ -94,7 +94,7 @@ class DatabaseUserManagerTest(TestCaseWithCeleryTasks):
self.assertEqual(dbu.name, "{user}db01".format(user=self.osuser.username))
self.assertEqual(dbu.osuser, self.osuser)
self.assertEqual(dbu.db_type, DB_TYPES.mysql)
taskres = TaskResult.objects.all()
taskres = TaskResult.objects.order_by("created").all()
self.assertEqual(len(taskres), 4)
self.assertEqual(taskres[0].creator, "handle_dbuser_created")
self.assertEqual(taskres[0].notes, "mysql user creation")
@ -154,7 +154,7 @@ class DatabaseUserTest(TestCaseWithCeleryTasks):
dbid = db.id
self.dbu.delete()
self.assertFalse(UserDatabase.objects.filter(id=dbid).exists())
taskres = TaskResult.objects.all()
taskres = TaskResult.objects.order_by("created").all()
self.assertEqual(len(taskres), 3)
self.assertEqual(taskres[0].creator, "handle_userdb_created")
self.assertEqual(taskres[0].notes, "pgsql database creation")
@ -187,7 +187,7 @@ class UserDatabaseManagerTest(TestCaseWithCeleryTasks):
self.assertEqual(
db.db_user.name, "{user}db01".format(user=self.osuser.username)
)
taskres = TaskResult.objects.all()
taskres = TaskResult.objects.order_by("created").all()
self.assertEqual(len(taskres), 2)
self.assertEqual(taskres[0].creator, "handle_dbuser_created")
self.assertEqual(taskres[0].notes, "mysql user creation")
@ -202,7 +202,7 @@ class UserDatabaseManagerTest(TestCaseWithCeleryTasks):
self.assertEqual(
db.db_user.name, "{user}db01".format(user=self.osuser.username)
)
taskres = TaskResult.objects.all()
taskres = TaskResult.objects.order_by("created").all()
self.assertEqual(len(taskres), 2)
self.assertEqual(taskres[0].creator, "handle_dbuser_created")
self.assertEqual(taskres[0].notes, "pgsql user creation")
@ -267,20 +267,20 @@ class UserDatabaseTest(TestCaseWithCeleryTasks):
def test___str__(self):
customer = Customer.objects.create_user(username="testcustomer")
osuser = User.objects.create_user(customer=customer)
db = UserDatabase.objects.create_userdatabase_with_user(DB_TYPES.pgsql, osuser)
os_user = User.objects.create_user(customer=customer)
db = UserDatabase.objects.create_userdatabase_with_user(DB_TYPES.pgsql, os_user)
self.assertEqual(
str(db),
"{user}db01 ({dbuser})".format(user=osuser.username, dbuser=db.db_user),
"{user}db01 ({dbuser})".format(user=os_user.username, dbuser=db.db_user),
)
def test_delete_mysql_db(self):
customer = Customer.objects.create_user(username="testcustomer")
osuser = User.objects.create_user(customer=customer)
os_user = User.objects.create_user(customer=customer)
TaskResult.objects.all().delete()
db = UserDatabase.objects.create_userdatabase_with_user(DB_TYPES.mysql, osuser)
db = UserDatabase.objects.create_userdatabase_with_user(DB_TYPES.mysql, os_user)
db.delete()
taskres = TaskResult.objects.all()
self.assertEqual(len(taskres), 3)
self.assertEqual(taskres[2].creator, "handle_userdb_deleted")
self.assertEqual(taskres[2].notes, "mysql database deletion")
task_result = TaskResult.objects.order_by("created").all()
self.assertEqual(len(task_result), 3)
self.assertEqual(task_result[2].creator, "handle_userdb_deleted")
self.assertEqual(task_result[2].notes, "mysql database deletion")