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, tag
2from django.urls import reverse
3from django.contrib.auth.models import AnonymousUser
4from django.core.serializers.json import DjangoJSONEncoder
5import json
6from policorp.models import Availability, Location, Task, Booking
7from policorp.views import *
8from policorp.tests import aux
9from datetime import datetime, timezone, time, timedelta
12class TestCreateAvailabilityViews(TestCase):
14 fixtures = ['testsdata.json']
16 task1 = {'id': 1, 'name': 'Device Installation', 'duration': 120}
17 task2 = {'id': 2, 'name': 'Repair', 'duration': 60}
19 def setUp(self):
20 # Every test needs access to the request factory.
21 self.factory = RequestFactory()
23 @tag('createavailabilities')
24 def test_view_createavailabilities_only_post_allowed(self):
25 """ GIVEN ; WHEN GET /policorp/createavailabilities; THEN code 400 should be returned """
26 request = self.factory.get(reverse('policorp:createavailabilities'))
27 response = createavailabilities(request)
28 self.assertEqual(response.status_code, 400)
30 @tag('createavailabilities')
31 def test_view_createavailabilities_only_logged_in_requests_allowed(self):
32 """ GIVEN ; WHEN POST /policorp/createavailabilities logged out; THEN code 401 (unauthorized) should be returned """
33 request = self.factory.post(reverse('policorp:createavailabilities'))
34 request.user = AnonymousUser()
35 response = createavailabilities(request)
36 self.assertEqual(response.status_code, 401)
38 @tag('createavailabilities')
39 def test_view_createavailabilities_only_logged_in_supervisor_requests_allowed(self):
40 """ GIVEN ; WHEN POST /policorp/createavailabilities logged in with non supervisor user; THEN code 401 (unauthorized) should be returned """
41 request = self.factory.post(reverse('policorp:createavailabilities'))
42 request.user = aux.createUser('foo', 'foo@example.com', 'example')
43 response = createavailabilities(request)
44 self.assertEqual(response.status_code, 401)
46 @tag('createavailabilities')
47 def test_view_createavailabilities_returns_201_and_created_2_availabilities(self):
48 """ GIVEN ; WHEN POST /policorp/createavailabilities with json content (location 1; task 1; (today + 1) at 14:00 utc time) + (location 1; task 1; (today + 1) at 16:00 utc time); THEN code 201 should be returned """
49 user = User.objects.create_supervisor('foo', 'foo@example.com', 'example')
50 location = Location.objects.get(pk=1).assign_supervisor(user)
51 tomorrow = datetime.now(timezone.utc) + timedelta(days=2)
52 time1 = time(hour=14, tzinfo=timezone.utc)
53 time2 = time(hour=16, tzinfo=timezone.utc)
54 body = [
55 {
56 "locationid": 1,
57 "taskid": 1,
58 "when": datetime.combine(tomorrow, time1)
59 },
60 {
61 "locationid": 1,
62 "taskid": 1,
63 "when": datetime.combine(tomorrow, time2)
64 }
65 ]
66 request = self.factory.post( reverse('policorp:createavailabilities'),
67 data=json.dumps(body, cls=aux.DateTimeEncoder),
68 content_type='application/json')
69 request.user = user
70 response = createavailabilities(request)
71 self.assertEqual(response.status_code, 201)
72 expected_json = [
73 Availability.objects.all().order_by("-id")[1].json(),
74 Availability.objects.all().order_by("-id")[0].json()
75 ]
76 self.assertJSONEqual(str(response.content, encoding='utf8'), expected_json)
78 @tag('createavailabilities')
79 def test_view_createavailabilities_returns_201_when_not_supervising_1_location(self):
80 """ GIVEN ; WHEN POST /policorp/createavailabilities with json content (location 1; task 1; (today + 1) at 14:00 utc time) + (location 2; task 1; (today + 1) at 16:00 utc time), not supervising location 2; THEN code 401 should be returned """
81 user = User.objects.create_supervisor('foo', 'foo@example.com', 'example')
82 location = Location.objects.get(pk=1).assign_supervisor(user)
83 tomorrow = datetime.now(timezone.utc) + timedelta(days=2)
84 time1 = time(hour=14, tzinfo=timezone.utc)
85 time2 = time(hour=16, tzinfo=timezone.utc)
86 body = [
87 {
88 "locationid": 1,
89 "taskid": 1,
90 "when": datetime.combine(tomorrow, time1)
91 },
92 {
93 "locationid": 2,
94 "taskid": 1,
95 "when": datetime.combine(tomorrow, time2)
96 }
97 ]
98 request = self.factory.post( reverse('policorp:createavailabilities'),
99 data=json.dumps(body, cls=aux.DateTimeEncoder),
100 content_type='application/json')
101 request.user = user
102 response = createavailabilities(request)
103 self.assertEqual(response.status_code, 201)
104 expected_json = [
105 Availability.objects.all().order_by("-id")[0].json(),
106 {
107 "locationid": 2,
108 "taskid": 1,
109 "when": datetime.combine(tomorrow, time2),
110 "error": "Unauthorized"
111 }
112 ]
113 self.assertJSONEqual(str(response.content, encoding='utf8'), json.dumps(expected_json, cls=aux.DateTimeEncoder))
115if __name__ == "__main__":
116 unittest.main()