From 61f00f4110b1d33827e3bd19ffef34f10be8273a Mon Sep 17 00:00:00 2001 From: maxbr Date: Mon, 17 Oct 2016 14:56:11 +0200 Subject: Add unit tests JIRA: RELENG-12 This commit increases test statement coverage to 84%. It also fixes the bugs that emerged while testing. Change-Id: I696091f1a07f8b7647872c7cb15f4368a4690619 Signed-off-by: maxbr --- pharos-dashboard/src/booking/models.py | 3 +- pharos-dashboard/src/booking/tests/test_models.py | 29 +++++------------- pharos-dashboard/src/booking/tests/test_views.py | 37 +++++++++++++++++++---- pharos-dashboard/src/booking/views.py | 13 ++++---- 4 files changed, 48 insertions(+), 34 deletions(-) (limited to 'pharos-dashboard/src/booking') diff --git a/pharos-dashboard/src/booking/models.py b/pharos-dashboard/src/booking/models.py index 88ab558..0b3fa3b 100644 --- a/pharos-dashboard/src/booking/models.py +++ b/pharos-dashboard/src/booking/models.py @@ -8,13 +8,14 @@ ############################################################################## +from django.conf import settings from django.contrib.auth.models import User from django.db import models from jira import JIRA from jira import JIRAError from dashboard.models import Resource -from django.conf import settings + class Installer(models.Model): id = models.AutoField(primary_key=True) diff --git a/pharos-dashboard/src/booking/tests/test_models.py b/pharos-dashboard/src/booking/tests/test_models.py index 612b35c..b4cd113 100644 --- a/pharos-dashboard/src/booking/tests/test_models.py +++ b/pharos-dashboard/src/booking/tests/test_models.py @@ -10,12 +10,11 @@ from datetime import timedelta -from django.contrib.auth.models import User, Permission +from django.contrib.auth.models import Permission from django.test import TestCase from django.utils import timezone -from account.models import UserProfile -from booking.models import Booking +from booking.models import * from dashboard.models import Resource from jenkins.models import JenkinsSlave @@ -37,7 +36,10 @@ class BookingModelTestCase(TestCase): self.user1 = User.objects.get(pk=self.user1.id) - def test_start__end(self): + self.installer = Installer.objects.create(name='TestInstaller') + self.scenario = Scenario.objects.create(name='TestScenario') + + def test_start_end(self): """ if the start of a booking is greater or equal then the end, saving should raise a ValueException @@ -88,20 +90,5 @@ class BookingModelTestCase(TestCase): user=self.user1, resource=self.res1)) self.assertTrue( Booking.objects.create(start=start, end=end, - user=self.user1, resource=self.res2)) - - def test_authorization(self): - user = User.objects.create(username='user') - user.userprofile = UserProfile.objects.create(user=user) - self.assertRaises(PermissionError, Booking.objects.create, start=timezone.now(), - end=timezone.now() + timedelta(days=1), resource=self.res1, user=user) - self.res1.owner = user - self.assertTrue( - Booking.objects.create(start=timezone.now(), end=timezone.now() + timedelta(days=1), - resource=self.res1, user=user)) - self.res1.owner = self.owner - user.user_permissions.add(self.add_booking_perm) - user = User.objects.get(pk=user.id) - self.assertTrue( - Booking.objects.create(start=timezone.now(), end=timezone.now() + timedelta(days=1), - resource=self.res2, user=user)) + user=self.user1, resource=self.res2, scenario=self.scenario, + installer=self.installer)) \ No newline at end of file diff --git a/pharos-dashboard/src/booking/tests/test_views.py b/pharos-dashboard/src/booking/tests/test_views.py index e568c15..c1da013 100644 --- a/pharos-dashboard/src/booking/tests/test_views.py +++ b/pharos-dashboard/src/booking/tests/test_views.py @@ -10,12 +10,10 @@ from datetime import timedelta -from django.contrib import auth from django.test import Client -from django.utils import timezone -from django.contrib.auth.models import Permission from django.test import TestCase from django.urls import reverse +from django.utils import timezone from django.utils.encoding import force_text from registration.forms import User @@ -37,9 +35,6 @@ class BookingViewTestCase(TestCase): self.user1profile = UserProfile.objects.create(user=self.user1) self.user1.save() - self.add_booking_perm = Permission.objects.get(codename='add_booking') - self.user1.user_permissions.add(self.add_booking_perm) - self.user1 = User.objects.get(pk=self.user1.id) @@ -78,4 +73,34 @@ class BookingViewTestCase(TestCase): self.assertIn('resource', response.context) + def test_booking_view(self): + start = timezone.now() + end = start + timedelta(weeks=1) + booking = Booking.objects.create(start=start, end=end, user=self.user1, resource=self.res1) + url = reverse('booking:detail', kwargs={'booking_id':0}) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + + url = reverse('booking:detail', kwargs={'booking_id':booking.id}) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed('booking/booking_detail.html') + self.assertIn('booking', response.context) + + def test_booking_list_view(self): + start = timezone.now() - timedelta(weeks=2) + end = start + timedelta(weeks=1) + Booking.objects.create(start=start, end=end, user=self.user1, resource=self.res1) + + url = reverse('booking:list') + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed('booking/booking_list.html') + self.assertTrue(len(response.context['bookings']) == 0) + + start = timezone.now() + end = start + timedelta(weeks=1) + Booking.objects.create(start=start, end=end, user=self.user1, resource=self.res1) + response = self.client.get(url) + self.assertTrue(len(response.context['bookings']) == 1) \ No newline at end of file diff --git a/pharos-dashboard/src/booking/views.py b/pharos-dashboard/src/booking/views.py index 413573d..6fdca0e 100644 --- a/pharos-dashboard/src/booking/views.py +++ b/pharos-dashboard/src/booking/views.py @@ -8,8 +8,6 @@ ############################################################################## -from datetime import timedelta - from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin @@ -45,7 +43,7 @@ def create_jira_ticket(user, booking): booking.save() -class BookingFormView(LoginRequiredMixin, FormView): +class BookingFormView(FormView): template_name = "booking/booking_calendar.html" form_class = BookingForm @@ -63,6 +61,11 @@ class BookingFormView(LoginRequiredMixin, FormView): return reverse('booking:create', kwargs=self.kwargs) def form_valid(self, form): + if not self.request.user.is_authenticated: + messages.add_message(self.request, messages.ERROR, + 'You need to be logged in to book a Pod.') + return super(BookingFormView, self).form_invalid(form) + user = self.request.user booking = Booking(start=form.cleaned_data['start'], end=form.cleaned_data['end'], @@ -75,9 +78,6 @@ class BookingFormView(LoginRequiredMixin, FormView): except ValueError as err: messages.add_message(self.request, messages.ERROR, err) return super(BookingFormView, self).form_invalid(form) - except PermissionError as err: - messages.add_message(self.request, messages.ERROR, err) - return super(BookingFormView, self).form_invalid(form) try: if settings.CREATE_JIRA_TICKET: create_jira_ticket(user, booking) @@ -101,6 +101,7 @@ class BookingView(TemplateView): context.update({'title': title, 'booking': booking}) return context + class BookingListView(TemplateView): template_name = "booking/booking_list.html" -- cgit 1.2.3-korg