Development Override Compose File
[laas.git] / src / booking / tests / test_views.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from datetime import timedelta
12
13 from django.test import Client
14 from django.test import TestCase
15 from django.urls import reverse
16 from django.utils import timezone
17 from django.utils.encoding import force_text
18 from registration.forms import User
19
20 from account.models import UserProfile
21 from booking.models import Booking
22 from dashboard.models import Resource
23 from jenkins.models import JenkinsSlave
24
25
26 class BookingViewTestCase(TestCase):
27     def setUp(self):
28         self.client = Client()
29         self.slave = JenkinsSlave.objects.create(name='test', url='test')
30         self.owner = User.objects.create(username='owner')
31         self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x',
32                                             url='x',owner=self.owner)
33         self.user1 = User.objects.create(username='user1')
34         self.user1.set_password('user1')
35         self.user1profile = UserProfile.objects.create(user=self.user1)
36         self.user1.save()
37
38         self.user1 = User.objects.get(pk=self.user1.id)
39
40
41     def test_resource_bookings_json(self):
42         url = reverse('booking:bookings_json', kwargs={'resource_id': 0})
43         self.assertEqual(self.client.get(url).status_code, 404)
44
45         url = reverse('booking:bookings_json', kwargs={'resource_id': self.res1.id})
46         response = self.client.get(url)
47         self.assertEqual(response.status_code, 200)
48         self.assertJSONEqual(force_text(response.content), {"bookings": []})
49         booking1 = Booking.objects.create(start=timezone.now(),
50                                           end=timezone.now() + timedelta(weeks=1), user=self.user1,
51                                           resource=self.res1)
52         response = self.client.get(url)
53         json = response.json()
54         self.assertEqual(response.status_code, 200)
55         self.assertIn('bookings', json)
56         self.assertEqual(len(json['bookings']), 1)
57         self.assertIn('start', json['bookings'][0])
58         self.assertIn('end', json['bookings'][0])
59         self.assertIn('id', json['bookings'][0])
60         self.assertIn('purpose', json['bookings'][0])
61
62     def test_booking_form_view(self):
63         url = reverse('booking:create', kwargs={'resource_id': 0})
64         self.assertEqual(self.client.get(url).status_code, 404)
65
66         # authenticated user
67         url = reverse('booking:create', kwargs={'resource_id': self.res1.id})
68         self.client.login(username='user1',password='user1')
69         response = self.client.get(url)
70         self.assertEqual(response.status_code, 200)
71         self.assertTemplateUsed('booking/booking_calendar.html')
72         self.assertTemplateUsed('booking/booking_form.html')
73         self.assertIn('resource', response.context)
74
75
76     def test_booking_view(self):
77         start = timezone.now()
78         end = start + timedelta(weeks=1)
79         booking = Booking.objects.create(start=start, end=end, user=self.user1, resource=self.res1)
80
81         url = reverse('booking:detail', kwargs={'booking_id':0})
82         response = self.client.get(url)
83         self.assertEqual(response.status_code, 404)
84
85         url = reverse('booking:detail', kwargs={'booking_id':booking.id})
86         response = self.client.get(url)
87         self.assertEqual(response.status_code, 200)
88         self.assertTemplateUsed('booking/booking_detail.html')
89         self.assertIn('booking', response.context)
90
91     def test_booking_list_view(self):
92         start = timezone.now() - timedelta(weeks=2)
93         end = start + timedelta(weeks=1)
94         Booking.objects.create(start=start, end=end, user=self.user1, resource=self.res1)
95
96         url = reverse('booking:list')
97         response = self.client.get(url)
98         self.assertEqual(response.status_code, 200)
99         self.assertTemplateUsed('booking/booking_list.html')
100         self.assertTrue(len(response.context['bookings']) == 0)
101
102         start = timezone.now()
103         end = start + timedelta(weeks=1)
104         Booking.objects.create(start=start, end=end, user=self.user1, resource=self.res1)
105         response = self.client.get(url)
106         self.assertTrue(len(response.context['bookings']) == 1)