Fix tests
This commit is contained in:
parent
866f6c8083
commit
d88745f46b
11 changed files with 35 additions and 116 deletions
|
@ -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 %}
|
|
@ -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>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"),
|
||||
]
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue