Hide keyboard shortcuts

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 

10 

11import sys 

12 

13class TestBookingOnTheFlyView(TestCase): 

14 

15 fixtures = ['testsdata.json'] 

16 

17 def setUp(self): 

18 # Every test needs access to the request factory. 

19 self.factory = APIRequestFactory() 

20 

21 def test_bookOnTheFlyView_location_supervisor_creates_new_availability_and_booking(self): 

22 

23 u1 = User.objects.create_supervisor('foo', 'foo@example.com', 'example') 

24 

25 now = datetime.now(tz=timezone.utc) 

26 locationid = 1 

27 taskid = 1 

28 cancelled = False 

29 note = 'A note for my schedule' 

30 

31 location = Location.objects.get(pk=locationid) 

32 location.assign_supervisor(u1) 

33 

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 } 

44 

45 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json') 

46 

47 force_authenticate(request, user=u1) 

48 response = BookOnTheFlyView.as_view()(request) 

49 

50 self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) 

51 

52 booking = Booking.objects.get(user=u1) 

53 self.assertIsNotNone(booking) 

54 

55 def test_bookOnTheFlyView_only_authenticated_users_can_post(self): 

56 

57 now = datetime.now(tz=timezone.utc) 

58 locationid = 1 

59 taskid = 1 

60 note = 'A note for my schedule' 

61 

62 booking = { 

63 'availability': { 

64 'when': now.isoformat(), 

65 'where': locationid, 

66 'what': taskid 

67 }, 

68 'note': note, 

69 'user': 'zoe' 

70 } 

71 

72 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json') 

73 

74 response = BookOnTheFlyView.as_view()(request) 

75 

76 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 

77 

78 def test_bookOnTheFlyView_only_authenticated_supervisors_users_can_post(self): 

79 

80 u1 = aux.createUser('foo', 'foo@example.com', 'example') 

81 

82 now = datetime.now(tz=timezone.utc) 

83 locationid = 1 

84 taskid = 1 

85 note = 'A note for my schedule' 

86 

87 booking = { 

88 'availability': { 

89 'when': now.isoformat(), 

90 'where': locationid, 

91 'what': taskid 

92 }, 

93 'note': note, 

94 'user': u1.username 

95 } 

96 

97 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json') 

98 

99 force_authenticate(request, user=u1) 

100 response = BookOnTheFlyView.as_view()(request) 

101 

102 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 

103 

104 def test_bookOnTheFlyView_only_an_authenticated_location_supervisor_at_that_location_can_post(self): 

105 

106 u1 = User.objects.create_supervisor('foo', 'foo@example.com', 'example') 

107 

108 now = datetime.now(tz=timezone.utc) 

109 locationid = 1 

110 taskid = 1 

111 cancelled = False 

112 note = 'A note for my schedule' 

113 

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 } 

124 

125 request = self.factory.post(reverse('policorp:bookonthefly'), json.dumps(booking, cls=aux.DateTimeEncoder), content_type='application/json') 

126 

127 force_authenticate(request, user=u1) 

128 response = BookOnTheFlyView.as_view()(request) 

129 

130 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)