From 994b489c637c7be1615861b7974ef3d07a0e66be Mon Sep 17 00:00:00 2001 From: Levi Date: Sat, 14 May 2016 02:12:42 -0430 Subject: [PATCH] Added test to order detail view, Added test to customer orders view, Added test to virtual machine detail view, Added test to customer booked virtual machines view --- hosting/admin.py | 3 +- hosting/models.py | 14 +- hosting/templates/hosting/order_detail.html | 24 ++-- hosting/test_views.py | 135 +++++++++++++++++++- hosting/urls.py | 4 +- hosting/views.py | 5 +- 6 files changed, 157 insertions(+), 28 deletions(-) diff --git a/hosting/admin.py b/hosting/admin.py index 84067ff9..70392113 100644 --- a/hosting/admin.py +++ b/hosting/admin.py @@ -1,6 +1,5 @@ from django.contrib import admin -from .models import RailsBetaUser, VirtualMachineType +from .models import VirtualMachineType -admin.site.register(RailsBetaUser) admin.site.register(VirtualMachineType) diff --git a/hosting/models.py b/hosting/models.py index 131a51d8..8b26189a 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -10,14 +10,6 @@ from utils.models import BillingAddress from .managers import VMPlansManager -class RailsBetaUser(models.Model): - email = models.EmailField(unique=True) - received_date = models.DateTimeField('date received') - - def __str__(self): - return "%s - %s" % (self.email, self.received_date) - - class VirtualMachineType(models.Model): HETZNER_NUG = 'hetzner_nug' @@ -86,6 +78,9 @@ class VirtualMachinePlan(models.Model): objects = VMPlansManager() + def __str__(self): + return "%s" % (self.id) + @cached_property def hosting_company_name(self): return self.vm_type.get_hosting_company_display() @@ -115,6 +110,9 @@ class HostingOrder(models.Model): cc_brand = models.CharField(max_length=10) stripe_charge_id = models.CharField(max_length=100, null=True) + def __str__(self): + return "%s" % (self.id) + @cached_property def status(self): return self.ORDER_APPROVED_STATUS if self.approved else self.ORDER_DECLINED_STATUS diff --git a/hosting/templates/hosting/order_detail.html b/hosting/templates/hosting/order_detail.html index f7c31eff..8e1ff691 100644 --- a/hosting/templates/hosting/order_detail.html +++ b/hosting/templates/hosting/order_detail.html @@ -6,7 +6,7 @@
-

Invoice

Order # {{object.id}}

+

Invoice

Order # {{order.id}}


@@ -14,18 +14,18 @@

Billed To:

{{user.name}}
- {{object.billing_address.street_address}},{{order.billing_address.postal_code}}
- {{object.billing_address.city}}, {{object.billing_address.country}}. + {{order.billing_address.street_address}},{{order.billing_address.postal_code}}
+ {{order.billing_address.city}}, {{order.billing_address.country}}.
Order Date:
- {{object.created_at}}

+ {{order.created_at}}

Status:
- {{object.status}} + {% endif %}">{{order.status}}

@@ -35,7 +35,7 @@
Payment Method:
- {{object.cc_brand}} ending **** {{object.last4}}
+ {{order.cc_brand}} ending **** {{order.last4}}
{{user.email}}
@@ -48,15 +48,15 @@

Order summary


-

Type {{object.vm_plan.hosting_company_name}}

+

Type {{order.vm_plan.hosting_company_name}}


-

Cores {{object.vm_plan.cores}}

+

Cores {{order.vm_plan.cores}}


-

Memory {{object.vm_plan.memory}} GiB

+

Memory {{order.vm_plan.memory}} GiB


-

Disk space {{object.vm_plan.disk_size}} GiB

+

Disk space {{order.vm_plan.disk_size}} GiB


-

Total

{{object.vm_plan.price}} CHF

+

Total

{{order.vm_plan.price}} CHF

diff --git a/hosting/test_views.py b/hosting/test_views.py index 161e95a5..0e9c7626 100644 --- a/hosting/test_views.py +++ b/hosting/test_views.py @@ -8,9 +8,10 @@ from model_mommy import mommy from membership.models import CustomUser, StripeCustomer -from .models import VirtualMachineType, HostingOrder -from .views import DjangoHostingView, RailsHostingView, NodeJSHostingView, LoginView, SignupView,\ - PaymentVMView +from .models import VirtualMachineType, HostingOrder, VirtualMachinePlan +from .views import DjangoHostingView, RailsHostingView, NodeJSHostingView, LoginView, SignupView, \ + PaymentVMView, OrdersHostingDetailView, OrdersHostingListView, VirtualMachineDetailView, \ + VirtualMachinesPlanListView from utils.tests import BaseTestCase @@ -171,6 +172,134 @@ class PaymentVMViewTest(BaseTestCase): settings.STRIPE_API_PUBLIC_KEY) +class VirtualMachineDetailViewTest(BaseTestCase): + + def setUp(self): + super(VirtualMachineDetailViewTest, self).setUp() + + self.stripe_customer = mommy.make(StripeCustomer, user=self.customer) + self.vm = mommy.make(VirtualMachinePlan) + 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 = VirtualMachineDetailView() + 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(BaseTestCase): + + def setUp(self): + super(VirtualMachinesPlanListViewTest, self).setUp() + + self.stripe_customer = mommy.make(StripeCustomer, user=self.customer) + mommy.make(HostingOrder, customer=self.stripe_customer, approved=True, _quantity=20) + _vms = VirtualMachinePlan.objects.all() + self.vms = sorted(_vms, key=lambda vm: vm.id, reverse=True) + self.url = reverse('hosting:virtual_machines') + self.view = VirtualMachinesPlanListView() + self.expected_template = 'hosting/virtual_machines.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')) + 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['vms']), self.vms[:10]) + self.assertTemplateUsed(response, self.expected_template) + + +class OrderHostingDetailViewTest(BaseTestCase): + + def setUp(self): + super(OrderHostingDetailViewTest, self).setUp() + + self.stripe_customer = mommy.make(StripeCustomer, user=self.customer) + self.order = mommy.make(HostingOrder, customer=self.stripe_customer) + self.url = reverse('hosting:orders', kwargs={'pk': self.order.id}) + self.view = OrdersHostingDetailView() + self.expected_template = 'hosting/order_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:orders', kwargs={'pk': self.order.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['order'], self.order) + 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): diff --git a/hosting/urls.py b/hosting/urls.py index bb195b79..b313859d 100644 --- a/hosting/urls.py +++ b/hosting/urls.py @@ -3,7 +3,7 @@ from django.conf.urls import url from .views import DjangoHostingView, RailsHostingView, PaymentVMView, \ NodeJSHostingView, LoginView, SignupView, IndexView, \ OrdersHostingListView, OrdersHostingDetailView, VirtualMachinesPlanListView,\ - VirtualMachineDetailListView + VirtualMachineDetailView urlpatterns = [ # url(r'pricing/?$', VMPricingView.as_view(), name='pricing'), @@ -15,7 +15,7 @@ urlpatterns = [ url(r'orders/?$', OrdersHostingListView.as_view(), name='orders'), url(r'orders/(?P\d+)/?$', OrdersHostingDetailView.as_view(), name='orders'), url(r'my-virtual-machines/?$', VirtualMachinesPlanListView.as_view(), name='virtual_machines'), - url(r'my-virtual-machines/(?P\d+)/?$', VirtualMachineDetailListView.as_view(), + url(r'my-virtual-machines/(?P\d+)/?$', VirtualMachineDetailView.as_view(), name='virtual_machines'), url(r'login/?$', LoginView.as_view(), name='login'), url(r'signup/?$', SignupView.as_view(), name='signup'), diff --git a/hosting/views.py b/hosting/views.py index 2819104f..1004563c 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -220,6 +220,7 @@ class PaymentVMView(LoginRequiredMixin, FormView): class OrdersHostingDetailView(LoginRequiredMixin, DetailView): template_name = "hosting/order_detail.html" + context_object_name = "order" login_url = reverse_lazy('hosting:login') model = HostingOrder @@ -230,6 +231,7 @@ class OrdersHostingListView(LoginRequiredMixin, ListView): context_object_name = "orders" model = HostingOrder paginate_by = 10 + ordering = '-id' def get_queryset(self): user = self.request.user @@ -243,6 +245,7 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView): context_object_name = "vms" model = VirtualMachinePlan paginate_by = 10 + ordering = '-id' def get_queryset(self): user = self.request.user @@ -250,7 +253,7 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView): return super(VirtualMachinesPlanListView, self).get_queryset() -class VirtualMachineDetailListView(LoginRequiredMixin, DetailView): +class VirtualMachineDetailView(LoginRequiredMixin, DetailView): template_name = "hosting/virtual_machine_detail.html" login_url = reverse_lazy('hosting:login') model = VirtualMachinePlan