dynamicweb/hosting/test_views.py
2026-10-04 18:36:54 +01:00

487 lines
18 KiB
Python

from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from django.urls import reverse, resolve
from django.contrib.auth.tokens import default_token_generator
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
from unittest import skipIf
from model_mommy import mommy
from stored_messages.models import Inbox
from factories import CustomUserFactory
from membership.models import CustomUser, StripeCustomer
from .models import HostingOrder
from .views import (
DjangoHostingView, RailsHostingView, NodeJSHostingView, LoginView,
SignupView, PaymentVMView, OrdersHostingDetailView, OrdersHostingListView,
VirtualMachinesPlanListView, PasswordResetView, PasswordResetConfirmView,
HostingPricingView, NotificationsView, MarkAsReadNotificationView
)
from utils.tests import BaseTestCase, create_customer_client
from unittest.mock import patch, MagicMock
@skipIf(
(settings.OPENNEBULA_DOMAIN is None or
settings.OPENNEBULA_DOMAIN == "test_domain"),
"""OpenNebula details unavailable, so skipping
ProcessVMSelectionTestMixin"""
)
class ProcessVMSelectionTestMixin(object):
def url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(self.view.get_context_data(), self.expected_context)
self.assertEqual(response.context['hosting'], self.expected_context['hosting'])
self.assertTemplateUsed(response, self.expected_template)
# def test_anonymous_post(self):
# params = {'vm_template_id': 1, 'configuration': 1}
# response = self.client.post(self.url, params)
# self.assertRedirects(response, expected_url=reverse('hosting:login'),
# status_code=302, target_status_code=200)
class DjangoHostingViewTest(TestCase, ProcessVMSelectionTestMixin):
def setUp(self):
self.url = reverse("django.hosting")
self.view = DjangoHostingView()
self.expected_template = "hosting/django.html"
self.expected_context = {"hosting": "django"}
class RailsHostingViewTest(TestCase, ProcessVMSelectionTestMixin):
def setUp(self):
self.url = reverse("rails.hosting")
self.view = RailsHostingView()
self.expected_template = "hosting/rails.html"
self.expected_context = {"hosting": "rails"}
class HostingPricingViewTest(TestCase):
def setUp(self):
self.url = reverse("hosting:pricing")
self.view = HostingPricingView()
self.expected_template = "hosting/hosting_pricing.html"
def test_get(self):
with patch("hosting.views.OpenNebulaManager") as manager:
manager.return_value.get_templates.return_value = []
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
@skipIf(
(settings.OPENNEBULA_DOMAIN is None or
settings.OPENNEBULA_DOMAIN == "test_domain"),
"""OpenNebula details unavailable, so skipping
HostingPricingViewTest.test_get"""
)
class HostingPricingViewTest(TestCase):
def setUp(self):
self.url = reverse('hosting:pricing')
self.view = HostingPricingView()
self.expected_template = 'hosting/hosting_pricing.html'
# configuration_options = dict(VirtualMachinePlan.VM_CONFIGURATION)
self.expected_context = {
# 'configuration_options': configuration_options,
'email': "info@django-hosting.ch",
# 'vm_types': VirtualMachineType.get_serialized_vm_types(),
}
def url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(self.view.get_context_data(), self.expected_context)
self.assertTemplateUsed(response, self.expected_template)
# def test_anonymous_post(self):
# response = self.client.post(self.url)
# self.assertRedirects(response, expected_url=reverse('hosting:login'),
# status_code=302, target_status_code=200)
class NotificationsViewTest(TestCase):
def setUp(self):
self.customer, self.customer_client = create_customer_client()
self.url = reverse("hosting:notifications")
self.view = NotificationsView
self.expected_template = "hosting/notifications.html"
self.inboxes = mommy.make(
Inbox, user=self.customer, _quantity=2
)
self.messages = [inbox.message for inbox in self.inboxes]
def test_get(self):
response = self.client.get(self.url)
expected_url = (
f"{reverse('hosting:login')}?next={self.url}"
)
self.assertRedirects(response, expected_url)
response = self.customer_client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.context["all_notifications"],
self.messages,
)
self.assertTemplateUsed(response, self.expected_template)
class MarkAsReadNotificationViewTest(TestCase):
def setUp(self):
self.customer, self.customer_client = create_customer_client()
self.view = MarkAsReadNotificationView
self.expected_template = "hosting/notifications.html"
self.inbox = mommy.make(Inbox, user=self.customer)
self.message = self.inbox.message
self.url = reverse(
"hosting:read_notification",
kwargs={"pk": self.message.id},
)
def test_post(self):
response = self.client.post(self.url)
expected_url = (
f"{reverse('hosting:login')}?next={self.url}"
)
self.assertRedirects(response, expected_url)
response = self.customer_client.post(self.url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertFalse(
Inbox.objects.filter(user=self.customer).exists()
)
self.assertTemplateUsed(response, self.expected_template)
# class GenerateVMSSHKeysViewTest(BaseTestCase):
#
# def setUp(self):
# super(GenerateVMSSHKeysViewTest, self).setUp()
#
# # self.view = GenerateVMSSHKeysView
# # self.vm = mommy.make(VirtualMachinePlan)
# self.expected_template = 'hosting/virtual_machine_key.html'
# self.url = reverse('hosting:virtual_machine_key', kwargs={'pk': self.vm.id})
#
# def test_url_resolve_to_view_correctly(self):
# found = resolve(self.url)
# self.assertEqual(found.func.__name__, self.view.__name__)
#
# def test_get(self):
#
# # Anonymous user should get redirect to login
# response = self.client.get(self.url)
# expected_url = "%s?next=%s" % (reverse('hosting:login'),
# reverse('hosting:virtual_machine_key',
# kwargs={'pk': self.vm.id}))
# self.assertRedirects(response, expected_url=expected_url,
# status_code=302, target_status_code=200)
#
# # Logged user should get the page
# response = self.customer_client.get(self.url, follow=True)
# self.assertEqual(response.status_code, 200)
# #updated_vm = VirtualMachinePlan.objects.get(id=self.vm.id)
# #self.assertEqual(response.context['public_key'].decode("utf-8"), updated_vm.public_key)
# self.assertTrue(response.context['private_key'] is not None)
# self.assertEqual(len(response.context['public_key']), 380)
# self.assertTrue(len(response.context['private_key']) is 1678 or 1674)
# self.assertTemplateUsed(response, self.expected_template)
# class VirtualMachineViewTest(BaseTestCase):
#
# def setUp(self):
# super(VirtualMachineViewTest, self).setUp()
#
# self.stripe_customer = mommy.make(StripeCustomer, user=self.customer)
# #self.vm = mommy.make(VirtualMachinePlan)
# self.vm.assign_permissions(self.customer)
# self.order = mommy.make(HostingOrder, customer=self.stripe_customer, vm_plan=self.vm)
# self.url = reverse('hosting:virtual_machines', kwargs={'pk': self.vm.id})
# self.view = VirtualMachineView()
# self.expected_template = 'hosting/virtual_machine_detail.html'
#
# def url_resolve_to_view_correctly(self):
# found = resolve(self.url)
# self.assertEqual(found.func.__name__, self.view.__name__)
#
# def test_get(self):
#
# # Anonymous user should get redirect to login
# response = self.client.get(self.url)
# expected_url = "%s?next=%s" % (reverse('hosting:login'),
# reverse('hosting:virtual_machines',
# kwargs={'pk': self.vm.id}))
# self.assertRedirects(response, expected_url=expected_url,
# status_code=302, target_status_code=200)
#
# # Customer should be able to get data
# response = self.customer_client.get(self.url, follow=True)
# self.assertEqual(response.status_code, 200)
# self.assertEqual(response.context['virtual_machine'], self.vm)
# self.assertTemplateUsed(response, self.expected_template)
class VirtualMachinesPlanListViewTest(TestCase):
def setUp(self):
self.customer, self.customer_client = create_customer_client()
self.stripe_customer = mommy.make(
StripeCustomer, user=self.customer
)
mommy.make(
HostingOrder,
customer=self.stripe_customer,
approved=True,
_quantity=20,
)
self.url = reverse("hosting:virtual_machines")
self.view = VirtualMachinesPlanListView()
self.expected_template = "hosting/virtual_machines.html"
def test_get(self):
response = self.client.get(self.url)
expected_url = (
f"{reverse('hosting:login')}?next={self.url}"
)
self.assertRedirects(response, expected_url)
# The view and its serializer instantiate their own managers.
with (
patch("hosting.views.OpenNebulaManager") as view_manager,
patch(
"opennebula_api.serializers.OpenNebulaManager"
) as serializer_manager,
):
view_manager.return_value.get_vms.return_value = []
view_manager.return_value.try_get_templates.return_value = []
serializer_manager.return_value.try_get_templates.return_value = []
response = self.customer_client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
class OrdersHostingListViewTest(TestCase):
def setUp(self):
self.customer, self.customer_client = create_customer_client()
self.stripe_customer = mommy.make(
StripeCustomer, user=self.customer
)
orders = mommy.make(
HostingOrder,
customer=self.stripe_customer,
_quantity=20,
)
self.orders = sorted(
orders, key=lambda order: order.id, reverse=True
)
self.url = reverse("hosting:orders")
self.view = OrdersHostingListView()
self.expected_template = "hosting/orders.html"
def test_get(self):
response = self.client.get(self.url)
expected_url = (
f"{reverse('hosting:login')}?next={self.url}"
)
self.assertRedirects(response, expected_url)
response = self.customer_client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
list(response.context["orders"]),
self.orders[:10],
)
self.assertTemplateUsed(response, self.expected_template)
class OrdersHostingListViewTest(BaseTestCase):
def setUp(self):
super(OrdersHostingListViewTest, self).setUp()
self.stripe_customer = mommy.make(StripeCustomer, user=self.customer)
_orders = mommy.make(HostingOrder, customer=self.stripe_customer, _quantity=20)
self.orders = sorted(_orders, key=lambda order: order.id, reverse=True)
self.url = reverse('hosting:orders')
self.view = OrdersHostingListView()
self.expected_template = 'hosting/orders.html'
def url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
# Anonymous user should get redirect to login
response = self.client.get(self.url)
expected_url = "%s?next=%s" % (reverse('hosting:login'), reverse('hosting:orders'))
self.assertRedirects(response, expected_url=expected_url,
status_code=302, target_status_code=200)
# Customer should be able to get his orders
response = self.customer_client.get(self.url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(list(response.context['orders']), self.orders[:10])
self.assertTemplateUsed(response, self.expected_template)
class LoginViewTest(TestCase):
def setUp(self):
self.url = reverse('hosting:login')
self.view = LoginView
self.expected_template = 'hosting/login.html'
self.user = mommy.make('membership.CustomUser', validated=1)
self.password = 'fake_password'
self.user.set_password(self.password)
self.user.save()
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
def test_anonymous_user_can_login(self):
data = {
'email': self.user.email,
'password': self.password
}
response = self.client.post(self.url, data=data, follow=True)
self.assertEqual(response.context['user'], self.user)
self.assertEqual(response.status_code, 200)
class SignupViewTest(TestCase):
def setUp(self):
self.url = reverse("hosting:signup")
self.expected_template = "hosting/signup.html"
self.view = SignupView
self.signup_data = {
"name": "ungleich",
"email": "test@ungleich.ch",
"password": "fake_password",
"confirm_password": "fake_password",
}
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
@patch("membership.models.LdapManager")
def test_anonymous_user_can_signup(self, mock_ldap_manager):
ldap_manager = mock_ldap_manager.return_value
# assign_username() expects:
# exist, entries = ldap_manager.check_user_exists(...)
ldap_manager.check_user_exists.return_value = (False, [])
response = self.client.post(
self.url,
data=self.signup_data,
follow=True,
)
self.assertEqual(response.status_code, 200)
user = CustomUser.objects.get(
email=self.signup_data["email"]
)
self.assertTrue(
user.check_password("fake_password")
)
class PasswordResetViewTest(BaseTestCase):
def setUp(self):
super(PasswordResetViewTest, self).setUp()
self.url = reverse('hosting:reset_password')
self.view = PasswordResetView
self.expected_template = 'hosting/reset_password.html'
self.user = mommy.make('membership.CustomUser')
self.password = 'fake_password'
self.user.set_password(self.password)
self.user.save()
self.post_data = {
'email': self.user.email
}
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
def test_post(self):
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertEqual(response.status_code, 200)
def test_test_generate_email_context(self):
context = self.setup_view(self.view()).test_generate_email_context(
self.user
)
self.assertEqual(context.get('user'), self.user)
self.assertEqual(context.get('site_name'), settings.DCL_TEXT)
class PasswordResetConfirmViewTest(BaseTestCase):
def setUp(self):
super(PasswordResetConfirmViewTest, self).setUp()
self.view = PasswordResetConfirmView
self.expected_template = 'hosting/confirm_reset_password.html'
self.user = mommy.make('membership.CustomUser')
self.password = 'fake_password'
self.user.set_password(self.password)
self.user.save()
self.token = default_token_generator.make_token(self.user)
self.uid = urlsafe_base64_encode(force_bytes(self.user.pk))
self.url = reverse('hosting:reset_password_confirm',
kwargs={'token': self.token, 'uidb64': self.uid})
self.post_data = {
'new_password1': 'new_password',
'new_password2': 'new_password'
}
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
# def test_post(self):
# response = self.client.post(
# self.url, data=self.post_data, follow=True
# )
# self.assertEqual(response.status_code, 200)
# self.assertTrue(not response.context['form'].errors)