Skip to content

Commit ee33612

Browse files
author
Jon
committed
Add request filter on telescope_class to make it safer for adding suspend time on request patches
1 parent aefbb32 commit ee33612

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

observation_portal/requestgroups/filters.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class Meta:
4242

4343

4444
class RequestFilter(django_filters.FilterSet):
45+
telescope_class = django_filters.MultipleChoiceFilter(
46+
choices=lambda: configdb.get_telescope_class_tuples(), field_name='location__telescope_class', distinct=True,
47+
)
4548
class Meta:
4649
model = Request
4750
fields = ('state',)

observation_portal/requestgroups/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def validate(self, data):
658658
return data
659659

660660
def validate_suspend_until(self, value):
661-
if value and value < timezone.now():
661+
if value and value <= timezone.now():
662662
raise serializers.ValidationError(_('The suspend_until value must be in the future'))
663663
return value
664664

observation_portal/requestgroups/test/test_api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ def test_setting_suspend_until_time_succeeds(self):
788788
for r in self.reqs:
789789
self.assertIsNone(r.suspend_until)
790790
# Now set the suspend until time into the future on those requests
791-
response = self.client.patch(reverse('api:requests-detail', args=(r.id,)), data=payload)
791+
response = self.client.patch(reverse('api:requests-detail', args=(r.id,)) + '?telescope_class=1m0', data=payload)
792792
self.assertEqual(response.status_code, 200)
793793
r.refresh_from_db()
794794
self.assertEqual(r.suspend_until, suspend_time)
@@ -810,6 +810,15 @@ def test_removing_suspend_until_time_succeeds(self):
810810
r.refresh_from_db()
811811
self.assertIsNone(r.suspend_until)
812812

813+
def test_setting_suspend_until_time_with_wrong_telescope_class_fails(self):
814+
suspend_time = timezone.now() + timedelta(hours=4)
815+
payload = {'suspend_until': suspend_time.isoformat()}
816+
for r in self.reqs:
817+
self.assertIsNone(r.suspend_until)
818+
# Use a filter parameter telescope_class that doesn't match this request which should fail to find the request
819+
response = self.client.patch(reverse('api:requests-detail', args=(r.id,)) + '?telescope_class=2m0', data=payload)
820+
self.assertContains(response, 'No Request matches', status_code=404)
821+
813822
def test_setting_suspend_fails_with_non_time_fails(self):
814823
payload = {'suspend_until': 'not a time'}
815824
r = self.reqs[0]

0 commit comments

Comments
 (0)