Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from django.test import TestCase, RequestFactory
2from django.urls import reverse
3from rest_framework import status
4from rest_framework.test import APIRequestFactory, force_authenticate
5from datetime import datetime, timezone, timedelta
6import json
7from policorp.tests import aux
8from policorp.models import Availability, Location, Task, User, Booking
9from policorp.classViews.bookOnTheFlyView import BookOnTheFlyView
11import sys
13class TestBookingOnTheFlyView(TestCase):
15 fixtures = ['testsdata.json']
17 def setUp(self):
18 # Every test needs access to the request factory.
19 self.factory = APIRequestFactory()
21 def test_bookOnTheFlyView_location_supervisor_creates_new_availability_and_booking(self):
23 u1 = User.objects.create_supervisor('foo', 'foo@example.com', 'example')
25 now = datetime.now(tz=timezone.utc)
26 locationid = 1
27 taskid = 1
28 cancelled = False
29 note = 'A note for my schedule'
31 location = Location.objects.get(pk=locationid)
32 location.assign_supervisor(u1)
34 booking = {
35 'availability': {
36 'when': now.isoformat(),
37 'where': locationid,
38 'what': taskid
39 },
40 'cancelled': cancelled,
41 'note': note,
42 'user': u1.username
43 }
45 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json')
47 force_authenticate(request, user=u1)
48 response = BookOnTheFlyView.as_view()(request)
50 self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
52 booking = Booking.objects.get(user=u1)
53 self.assertIsNotNone(booking)
55 def test_bookOnTheFlyView_only_authenticated_users_can_post(self):
57 now = datetime.now(tz=timezone.utc)
58 locationid = 1
59 taskid = 1
60 note = 'A note for my schedule'
62 booking = {
63 'availability': {
64 'when': now.isoformat(),
65 'where': locationid,
66 'what': taskid
67 },
68 'note': note,
69 'user': 'zoe'
70 }
72 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json')
74 response = BookOnTheFlyView.as_view()(request)
76 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
78 def test_bookOnTheFlyView_only_authenticated_supervisors_users_can_post(self):
80 u1 = aux.createUser('foo', 'foo@example.com', 'example')
82 now = datetime.now(tz=timezone.utc)
83 locationid = 1
84 taskid = 1
85 note = 'A note for my schedule'
87 booking = {
88 'availability': {
89 'when': now.isoformat(),
90 'where': locationid,
91 'what': taskid
92 },
93 'note': note,
94 'user': u1.username
95 }
97 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json')
99 force_authenticate(request, user=u1)
100 response = BookOnTheFlyView.as_view()(request)
102 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
104 def test_bookOnTheFlyView_only_an_authenticated_location_supervisor_at_that_location_can_post(self):
106 u1 = User.objects.create_supervisor('foo', 'foo@example.com', 'example')
108 now = datetime.now(tz=timezone.utc)
109 locationid = 1
110 taskid = 1
111 cancelled = False
112 note = 'A note for my schedule'
114 booking = {
115 'availability': {
116 'when': now.isoformat(),
117 'where': locationid,
118 'what': taskid
119 },
120 'cancelled': cancelled,
121 'note': note,
122 'user': u1.username
123 }
125 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json')
127 force_authenticate(request, user=u1)
128 response = BookOnTheFlyView.as_view()(request)
130 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)