def test_ended_at_before_started_at(self): user = User.objects.first() job = Job.objects.first() data = { 'task': job.tasks.first().pk, 'started_at': make_aware(datetime(2019, 4, 1, 9, 0, 0)), 'ended_at': make_aware(datetime(2019, 4, 1, 8, 0, 0)), 'user': user.pk } serializer = TimeEntrySerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, {'ended_at': ['Ended at must be after Started at']} )
def test_time_span_multiple_days(self): user = User.objects.first() job = Job.objects.first() data = { 'task': job.tasks.first().pk, 'started_at': make_aware(datetime(2019, 4, 1, 9, 0, 0)), 'ended_at': make_aware(datetime(2019, 4, 2, 9, 0, 0)), 'user': user.pk } serializer = TimeEntrySerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, {'non_field_errors': ['A time entry cannot span multiple days']} )
def test_serialized_data(self): user = User.objects.first() job = Job.objects.first() instance = TimeEntry.objects.create( task=job.tasks.first(), started_at=make_aware(datetime(2019, 4, 1, 9, 0, 0)), ended_at=make_aware(datetime(2019, 4, 1, 9, 15, 0)), user=user, signed_off=True, signed_off_date=make_aware(datetime(2019, 4, 1, 9, 15, 0)), ) serializer = TimeEntrySerializer(instance=instance) self.assertEqual( serializer.data, { 'id': instance.pk, 'started_at': localtime(instance.started_at).isoformat(), 'ended_at': localtime(instance.ended_at).isoformat(), 'comments': instance.comments, 'task': instance.task.pk, 'user': instance.user.pk, 'duration': '00:15:00', 'signed_off': instance.signed_off, 'signed_off_date': localtime(instance.signed_off_date).isoformat() } )
def test_logged_in_user_can_save_their_own(self): request = RequestFactory().get('/') request.user = self.create_user() context_instance = RequestContext(request) job = Job.objects.first() data = { 'task': job.tasks.first().pk, 'started_at': make_aware(datetime(2019, 4, 1, 9, 0, 0)), 'ended_at': make_aware(datetime(2019, 4, 1, 9, 15, 0)), 'user': request.user.pk } serializer = TimeEntrySerializer(data=data, context=context_instance) self.assertTrue(serializer.is_valid())
def signoff(self, serializer): data = serializer.validated_data entries = TimeEntry.objects.filter(started_at__date=data['date'], user=data['user'], signed_off=False) entry_pks = [e.pk for e in entries] entries.update(signed_off=True) updates_entries = TimeEntry.objects.filter(pk__in=entry_pks) serializer = TimeEntrySerializer(instance=updates_entries, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def test_user_with_correct_perm_can_save_for_another_user(self): request = RequestFactory().get('/') force_authenticate(request, user=self.create_user()) context_instance = {'request': Request(request)} permission = Permission.objects.get(codename='manage_time_entry') context_instance['request'].user.user_permissions.add(permission) user = User.objects.first() job = Job.objects.first() data = { 'task': job.tasks.first().pk, 'started_at': make_aware(datetime(2019, 4, 1, 9, 0, 0)), 'ended_at': make_aware(datetime(2019, 4, 1, 9, 15, 0)), 'user': user.pk } serializer = TimeEntrySerializer(data=data, context=context_instance) self.assertTrue(serializer.is_valid())
def _create_test_object(self): job = Job.objects.first() self.test_object = TimeEntry.objects.create( task=job.tasks.first(), started_at=make_aware(datetime(2019, 4, 1, 9, 0, 0)), ended_at=make_aware(datetime(2019, 4, 1, 9, 15, 0)), user=self.user, comments='some comments' ) self.test_object_data = TimeEntrySerializer(instance=self.test_object).data self.test_object_url = self.base_url + str(self.test_object.pk) + '/'
def test_user_without_correct_perm_cannot_save_for_another_user(self): request = APIRequestFactory().get('/') force_authenticate(request, user=self.create_user()) context_instance = {'request': Request(request)} user = User.objects.first() job = Job.objects.first() data = { 'task': job.tasks.first().pk, 'started_at': make_aware(datetime(2019, 4, 1, 9, 0, 0)), 'ended_at': make_aware(datetime(2019, 4, 1, 9, 15, 0)), 'user': user.pk } serializer = TimeEntrySerializer(data=data, context=context_instance) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, {'non_field_errors': ['You dont have permission to save a time entry for another user']} )