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

This commit is contained in:
Levi 2016-05-14 02:12:42 -04:30
parent 2a1bcbc904
commit 994b489c63
6 changed files with 157 additions and 28 deletions

View file

@ -1,6 +1,5 @@
from django.contrib import admin from django.contrib import admin
from .models import RailsBetaUser, VirtualMachineType from .models import VirtualMachineType
admin.site.register(RailsBetaUser)
admin.site.register(VirtualMachineType) admin.site.register(VirtualMachineType)

View file

@ -10,14 +10,6 @@ from utils.models import BillingAddress
from .managers import VMPlansManager 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): class VirtualMachineType(models.Model):
HETZNER_NUG = 'hetzner_nug' HETZNER_NUG = 'hetzner_nug'
@ -86,6 +78,9 @@ class VirtualMachinePlan(models.Model):
objects = VMPlansManager() objects = VMPlansManager()
def __str__(self):
return "%s" % (self.id)
@cached_property @cached_property
def hosting_company_name(self): def hosting_company_name(self):
return self.vm_type.get_hosting_company_display() return self.vm_type.get_hosting_company_display()
@ -115,6 +110,9 @@ class HostingOrder(models.Model):
cc_brand = models.CharField(max_length=10) cc_brand = models.CharField(max_length=10)
stripe_charge_id = models.CharField(max_length=100, null=True) stripe_charge_id = models.CharField(max_length=100, null=True)
def __str__(self):
return "%s" % (self.id)
@cached_property @cached_property
def status(self): def status(self):
return self.ORDER_APPROVED_STATUS if self.approved else self.ORDER_DECLINED_STATUS return self.ORDER_APPROVED_STATUS if self.approved else self.ORDER_DECLINED_STATUS

View file

@ -6,7 +6,7 @@
<div class="row"> <div class="row">
<div class="col-xs-8 col-xs-offset-2"> <div class="col-xs-8 col-xs-offset-2">
<div class="invoice-title"> <div class="invoice-title">
<h2>Invoice</h2><h3 class="pull-right">Order # {{object.id}}</h3> <h2>Invoice</h2><h3 class="pull-right">Order # {{order.id}}</h3>
</div> </div>
<hr> <hr>
<div class="row"> <div class="row">
@ -14,18 +14,18 @@
<address> <address>
<h3><b>Billed To:</b></h3> <h3><b>Billed To:</b></h3>
{{user.name}}<br> {{user.name}}<br>
{{object.billing_address.street_address}},{{order.billing_address.postal_code}}<br> {{order.billing_address.street_address}},{{order.billing_address.postal_code}}<br>
{{object.billing_address.city}}, {{object.billing_address.country}}. {{order.billing_address.city}}, {{order.billing_address.country}}.
</address> </address>
</div> </div>
<div class="col-xs-6 text-right"> <div class="col-xs-6 text-right">
<address> <address>
<strong>Order Date:</strong><br> <strong>Order Date:</strong><br>
{{object.created_at}}<br><br> {{order.created_at}}<br><br>
<strong>Status:</strong><br> <strong>Status:</strong><br>
<strong class="{% if object.status == 'Approved' %}text-success <strong class="{% if order.status == 'Approved' %}text-success
{%else%} text-danger {%else%} text-danger
{% endif %}">{{object.status}}</strong> {% endif %}">{{order.status}}</strong>
<br><br> <br><br>
</address> </address>
@ -35,7 +35,7 @@
<div class="col-xs-6"> <div class="col-xs-6">
<address> <address>
<strong>Payment Method:</strong><br> <strong>Payment Method:</strong><br>
{{object.cc_brand}} ending **** {{object.last4}}<br> {{order.cc_brand}} ending **** {{order.last4}}<br>
{{user.email}} {{user.email}}
</address> </address>
</div> </div>
@ -48,15 +48,15 @@
<h3><b>Order summary</b></h3> <h3><b>Order summary</b></h3>
<hr> <hr>
<div class="content"> <div class="content">
<p><b>Type</b> <span class="pull-right">{{object.vm_plan.hosting_company_name}}</span></p> <p><b>Type</b> <span class="pull-right">{{order.vm_plan.hosting_company_name}}</span></p>
<hr> <hr>
<p><b>Cores</b> <span class="pull-right">{{object.vm_plan.cores}}</span></p> <p><b>Cores</b> <span class="pull-right">{{order.vm_plan.cores}}</span></p>
<hr> <hr>
<p><b>Memory</b> <span class="pull-right">{{object.vm_plan.memory}} GiB</span></p> <p><b>Memory</b> <span class="pull-right">{{order.vm_plan.memory}} GiB</span></p>
<hr> <hr>
<p><b>Disk space</b> <span class="pull-right">{{object.vm_plan.disk_size}} GiB</span></p> <p><b>Disk space</b> <span class="pull-right">{{order.vm_plan.disk_size}} GiB</span></p>
<hr> <hr>
<h4>Total<p class="pull-right"><b>{{object.vm_plan.price}} CHF</b></p></h4> <h4>Total<p class="pull-right"><b>{{order.vm_plan.price}} CHF</b></p></h4>
</div> </div>
</div> </div>
</div> </div>

View file

@ -8,9 +8,10 @@ from model_mommy import mommy
from membership.models import CustomUser, StripeCustomer from membership.models import CustomUser, StripeCustomer
from .models import VirtualMachineType, HostingOrder from .models import VirtualMachineType, HostingOrder, VirtualMachinePlan
from .views import DjangoHostingView, RailsHostingView, NodeJSHostingView, LoginView, SignupView,\ from .views import DjangoHostingView, RailsHostingView, NodeJSHostingView, LoginView, SignupView, \
PaymentVMView PaymentVMView, OrdersHostingDetailView, OrdersHostingListView, VirtualMachineDetailView, \
VirtualMachinesPlanListView
from utils.tests import BaseTestCase from utils.tests import BaseTestCase
@ -171,6 +172,134 @@ class PaymentVMViewTest(BaseTestCase):
settings.STRIPE_API_PUBLIC_KEY) 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): class LoginViewTest(TestCase):
def setUp(self): def setUp(self):

View file

@ -3,7 +3,7 @@ from django.conf.urls import url
from .views import DjangoHostingView, RailsHostingView, PaymentVMView, \ from .views import DjangoHostingView, RailsHostingView, PaymentVMView, \
NodeJSHostingView, LoginView, SignupView, IndexView, \ NodeJSHostingView, LoginView, SignupView, IndexView, \
OrdersHostingListView, OrdersHostingDetailView, VirtualMachinesPlanListView,\ OrdersHostingListView, OrdersHostingDetailView, VirtualMachinesPlanListView,\
VirtualMachineDetailListView VirtualMachineDetailView
urlpatterns = [ urlpatterns = [
# url(r'pricing/?$', VMPricingView.as_view(), name='pricing'), # url(r'pricing/?$', VMPricingView.as_view(), name='pricing'),
@ -15,7 +15,7 @@ urlpatterns = [
url(r'orders/?$', OrdersHostingListView.as_view(), name='orders'), url(r'orders/?$', OrdersHostingListView.as_view(), name='orders'),
url(r'orders/(?P<pk>\d+)/?$', OrdersHostingDetailView.as_view(), name='orders'), url(r'orders/(?P<pk>\d+)/?$', OrdersHostingDetailView.as_view(), name='orders'),
url(r'my-virtual-machines/?$', VirtualMachinesPlanListView.as_view(), name='virtual_machines'), url(r'my-virtual-machines/?$', VirtualMachinesPlanListView.as_view(), name='virtual_machines'),
url(r'my-virtual-machines/(?P<pk>\d+)/?$', VirtualMachineDetailListView.as_view(), url(r'my-virtual-machines/(?P<pk>\d+)/?$', VirtualMachineDetailView.as_view(),
name='virtual_machines'), name='virtual_machines'),
url(r'login/?$', LoginView.as_view(), name='login'), url(r'login/?$', LoginView.as_view(), name='login'),
url(r'signup/?$', SignupView.as_view(), name='signup'), url(r'signup/?$', SignupView.as_view(), name='signup'),

View file

@ -220,6 +220,7 @@ class PaymentVMView(LoginRequiredMixin, FormView):
class OrdersHostingDetailView(LoginRequiredMixin, DetailView): class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
template_name = "hosting/order_detail.html" template_name = "hosting/order_detail.html"
context_object_name = "order"
login_url = reverse_lazy('hosting:login') login_url = reverse_lazy('hosting:login')
model = HostingOrder model = HostingOrder
@ -230,6 +231,7 @@ class OrdersHostingListView(LoginRequiredMixin, ListView):
context_object_name = "orders" context_object_name = "orders"
model = HostingOrder model = HostingOrder
paginate_by = 10 paginate_by = 10
ordering = '-id'
def get_queryset(self): def get_queryset(self):
user = self.request.user user = self.request.user
@ -243,6 +245,7 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView):
context_object_name = "vms" context_object_name = "vms"
model = VirtualMachinePlan model = VirtualMachinePlan
paginate_by = 10 paginate_by = 10
ordering = '-id'
def get_queryset(self): def get_queryset(self):
user = self.request.user user = self.request.user
@ -250,7 +253,7 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView):
return super(VirtualMachinesPlanListView, self).get_queryset() return super(VirtualMachinesPlanListView, self).get_queryset()
class VirtualMachineDetailListView(LoginRequiredMixin, DetailView): class VirtualMachineDetailView(LoginRequiredMixin, DetailView):
template_name = "hosting/virtual_machine_detail.html" template_name = "hosting/virtual_machine_detail.html"
login_url = reverse_lazy('hosting:login') login_url = reverse_lazy('hosting:login')
model = VirtualMachinePlan model = VirtualMachinePlan