-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_api.py
2815 lines (2477 loc) · 151 KB
/
test_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from observation_portal.requestgroups.models import (RequestGroup, Request, DraftRequestGroup, Window, Target,
Configuration, Location, Constraints, InstrumentConfig,
AcquisitionConfig, GuidingConfig)
from observation_portal.proposals.models import Proposal, Membership, TimeAllocation, Semester
from observation_portal.observations.models import Observation, ConfigurationStatus
from observation_portal.common.test_helpers import SetTimeMixin, create_simple_requestgroup
import observation_portal.requestgroups.signals.handlers # noqa
# imports for cache based tests
import observation_portal.observations.signals.handlers # noqa
from observation_portal.requestgroups import serializers
from observation_portal.requestgroups import views
from observation_portal.common import state_changes
from observation_portal.requestgroups.contention import Pressure
from observation_portal.accounts.models import Profile
from observation_portal.accounts.test_utils import blend_user
from django.urls import reverse
from django.contrib.auth.models import User
from django.core import cache
from dateutil.parser import parse as datetime_parser
from rest_framework.test import APITestCase
from mixer.backend.django import mixer
from django.utils import timezone
from datetime import datetime, timedelta
import copy
import random
from unittest import skip
from unittest.mock import patch
generic_payload = {
'proposal': 'temp',
'name': 'test group',
'operator': 'SINGLE',
'ipp_value': 1.0,
'observation_type': 'NORMAL',
'requests': [{
'configurations': [{
'type': 'EXPOSE',
'instrument_type': '1M0-SCICAM-SBIG',
'target': {
'name': 'fake target',
'type': 'ICRS',
'dec': 20,
'ra': 34.4,
},
'instrument_configs': [{
'exposure_time': 100,
'exposure_count': 1,
'mode': '',
'bin_x': 1,
'bin_y': 1,
'optical_elements': {
'filter': 'air'
}
}],
'guiding_config': {
'extra_params': {}
},
'acquisition_config': {
'extra_params': {}
},
'constraints': {
'max_airmass': 2.0,
'min_lunar_distance': 30.0,
}
}],
'windows': [{
'start': '2016-09-29T21:12:18Z',
'end': '2016-10-29T21:12:19Z'
}],
'location': {
'telescope_class': '1m0',
},
}]
}
class TestUserGetRequestApi(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user(user_params={'is_staff': False, 'is_superuser': False})
self.other_user = blend_user(user_params={'is_staff': False, 'is_superuser': False})
mixer.blend(Membership, user=self.user, proposal=self.proposal)
self.staff_user = blend_user(user_params={'is_staff': True})
def test_get_request_group_detail_unauthenticated(self):
self.client.force_login(self.other_user)
request_group = mixer.blend(RequestGroup, submitter=self.user, proposal=self.proposal, name="testgroup",
observation_type=RequestGroup.NORMAL)
result = self.client.get(reverse('api:request_groups-detail', args=(request_group.id,)))
self.assertEqual(result.status_code, 404)
def test_get_request_group_detail_authenticated(self):
request_group = mixer.blend(RequestGroup, submitter=self.user, proposal=self.proposal, name="testgroup",
observation_type=RequestGroup.NORMAL)
self.client.force_login(self.user)
result = self.client.get(reverse('api:request_groups-detail', args=(request_group.id,)))
self.assertContains(result, request_group.name)
def test_get_request_group_list_unauthenticated(self):
self.client.force_login(self.other_user)
mixer.blend(RequestGroup, submitter=self.user, proposal=self.proposal, name="testgroup",
observation_type=RequestGroup.NORMAL)
result = self.client.get(reverse('api:request_groups-list'))
self.assertEqual(result.status_code, 200)
self.assertEqual(result.json()['results'], [])
def test_get_request_group_list_authenticated(self):
request_group = mixer.blend(RequestGroup, submitter=self.user, proposal=self.proposal, name="testgroup",
observation_type=RequestGroup.NORMAL)
self.client.force_login(self.user)
result = self.client.get(reverse('api:request_groups-list'))
self.assertContains(result, request_group.name)
def test_get_request_group_list_staff(self):
request_group = mixer.blend(RequestGroup, submitter=self.user, proposal=self.proposal, name="testgroup2",
observation_type=RequestGroup.NORMAL)
self.client.force_login(self.staff_user)
result = self.client.get(reverse('api:request_groups-list'))
self.assertContains(result, request_group.name)
def test_get_request_group_detail_public(self):
proposal = mixer.blend(Proposal, public=True)
request_group = mixer.blend(RequestGroup, submitter=self.user, proposal=proposal, name="publicgroup",
observation_type=RequestGroup.NORMAL)
result = self.client.get(reverse('api:request_groups-detail', args=(request_group.id,)))
self.assertContains(result, request_group.name)
class TestUserPostRequestApi(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user()
self.client.force_login(self.user)
self.semester = mixer.blend(
Semester, id='2016B', start=datetime(2016, 9, 1, tzinfo=timezone.utc),
end=datetime(2016, 12, 31, tzinfo=timezone.utc)
)
self.time_allocation_1m0_sbig = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=self.semester,
instrument_type='1M0-SCICAM-SBIG', std_allocation=100.0, std_time_used=0.0,
rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0, ipp_time_available=5.0
)
self.time_allocation_2m0_floyds = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=self.semester,
instrument_type='2M0-FLOYDS-SCICAM', std_allocation=100.0, std_time_used=0.0,
rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0, ipp_time_available=5.0
)
self.membership = mixer.blend(Membership, user=self.user, proposal=self.proposal)
self.generic_payload = copy.deepcopy(generic_payload)
self.generic_payload['proposal'] = self.proposal.id
def test_post_requestgroup_unauthenticated(self):
self.other_user = blend_user()
self.client.force_login(self.other_user)
response = self.client.post(reverse('api:request_groups-list'), data=self.generic_payload)
self.assertEqual(response.status_code, 400)
def test_post_requestgroup_authenticated(self):
response = self.client.post(reverse('api:request_groups-list'), data=self.generic_payload)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['name'], self.generic_payload['name'])
def test_post_requestgroup_wrong_proposal(self):
bad_data = self.generic_payload.copy()
bad_data['proposal'] = 'DoesNotExist'
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
def test_post_requestgroup_no_membership(self):
proposal = mixer.blend(Proposal)
bad_data = self.generic_payload.copy()
bad_data['proposal'] = proposal.id
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertIn('You do not belong', str(response.content))
self.assertEqual(response.status_code, 400)
def test_post_requestgroup_missing_data(self):
bad_data = self.generic_payload.copy()
del bad_data['requests']
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
def test_post_requestgroup_no_configurations(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['configurations'] = []
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
def test_post_requestgroup_no_requests(self):
bad_data = self.generic_payload.copy()
bad_data['requests'] = []
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
def test_post_requestgroup_no_time_allocation_for_instrument(self):
self.time_allocation_2m0_floyds.delete()
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['location']['telescope_class'] = '2m0'
bad_data['requests'][0]['configurations'][0]['instrument_type'] = '2M0-FLOYDS-SCICAM'
bad_data['requests'][0]['configurations'][0]['type'] = 'SPECTRUM'
del bad_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['filter']
bad_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['slit'] = 'slit_6.0as'
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('You do not have sufficient NORMAL time', str(response.content))
def test_post_requestgroup_manual_instrument_not_allowed(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['location']['site'] = 'tst'
bad_data['requests'][0]['location']['enclosure'] = 'domc'
bad_data['requests'][0]['location']['telescope'] = '1m0a'
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('Invalid instrument type', str(response.content))
def test_post_requestgroup_manual_instrument_not_allowed_with_staff_without_full_location(self):
eng_user = blend_user(user_params={'username': 'eng', 'is_staff': True})
mixer.blend(Membership, user=eng_user, proposal=self.proposal)
self.client.force_login(eng_user)
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['location']['site'] = 'tst'
bad_data['requests'][0]['location']['enclosure'] = 'domc'
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('Invalid instrument type', str(response.content))
def test_post_requestgroup_manual_instrument_allowed_for_staff(self):
good_data = self.generic_payload.copy()
eng_user = blend_user(user_params={'username': 'eng', 'is_staff': True})
mixer.blend(Membership, user=eng_user, proposal=self.proposal)
self.client.force_login(eng_user)
good_data['requests'][0]['location']['site'] = 'tst'
good_data['requests'][0]['location']['enclosure'] = 'domc'
good_data['requests'][0]['location']['telescope'] = '1m0a'
response = self.client.post(reverse('api:request_groups-list'), data=good_data)
self.assertEqual(response.status_code, 201)
def test_post_requestgroup_not_enough_time_allocation_for_instrument(self):
bad_data = self.generic_payload.copy()
self.time_allocation_1m0_sbig.std_time_used = 99.99
self.time_allocation_1m0_sbig.save()
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('does not have enough NORMAL time allocated', str(response.content))
def test_post_requestgroup_not_enough_rr_time_allocation_for_instrument(self):
bad_data = self.generic_payload.copy()
bad_data['observation_type'] = RequestGroup.RAPID_RESPONSE
bad_data['requests'][0]['windows'][0]['start'] = '2016-09-01T00:00:00Z'
bad_data['requests'][0]['windows'][0]['end'] = '2016-09-01T05:59:59Z'
self.time_allocation_1m0_sbig.rr_time_used = 9.99
self.time_allocation_1m0_sbig.save()
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('does not have enough RAPID_RESPONSE time allocated', str(response.content))
def test_post_requestgroup_rr_future_start_time(self):
bad_data = self.generic_payload.copy()
bad_data['observation_type'] = RequestGroup.RAPID_RESPONSE
bad_data['requests'][0]['windows'][0]['start'] = timezone.now() + timedelta(0, 60)
bad_data['requests'][0]['windows'][0]['end'] = timezone.now() + timedelta(0, 18000)
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('cannot be in the future.', str(response.content))
def test_post_requestgroup_rr_within_24_hours(self):
data = self.generic_payload.copy()
data['observation_type'] = RequestGroup.RAPID_RESPONSE
data['requests'][0]['windows'][0]['start'] = timezone.now() + timedelta(0)
data['requests'][0]['windows'][0]['end'] = timezone.now() + timedelta(0, 82800) # 23 hours
response = self.client.post(reverse('api:request_groups-list'), data=data)
self.assertEqual(response.status_code, 201)
def test_post_requestgroup_rr_not_within_24_hours(self):
bad_data = self.generic_payload.copy()
bad_data['observation_type'] = RequestGroup.RAPID_RESPONSE
bad_data['requests'][0]['windows'][0]['start'] = timezone.now() + timedelta(0)
bad_data['requests'][0]['windows'][0]['end'] = timezone.now() + timedelta(0, 90000) # 25 hours
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('must start within the next 24 hours', str(response.content))
def test_post_requestgroup_not_have_any_time_left(self):
bad_data = self.generic_payload.copy()
self.time_allocation_1m0_sbig.std_time_used = 120
self.time_allocation_1m0_sbig.save()
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('does not have any NORMAL time left allocated', str(response.content))
def test_post_requestgroup_time_limit_reached(self):
self.membership.time_limit = 0
self.membership.save()
response = self.client.post(reverse('api:request_groups-list'), data=self.generic_payload)
self.assertEqual(response.status_code, 400)
self.assertIn('duration will exceed the time limit set for your account ', str(response.content))
def test_post_requestgroup_time_limit_not_reached(self):
self.membership.time_limit = 1000
self.membership.save()
response = self.client.post(reverse('api:request_groups-list'), data=self.generic_payload)
self.assertEqual(response.status_code, 201)
def test_post_requestgroup_bad_ipp(self):
bad_data = self.generic_payload.copy()
bad_data['ipp_value'] = 0.0
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
def test_post_requestgroup_default_acquire_mode(self):
good_data = self.generic_payload.copy()
# verify default acquire mode is 'off' for non-floyds
response = self.client.post(reverse('api:request_groups-list'), data=good_data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['requests'][0]['configurations'][0]['acquisition_config']['mode'], 'OFF')
# check that default acquire mode is 'wcs' for floyds
good_data['requests'][0]['location']['telescope_class'] = '2m0'
good_data['requests'][0]['configurations'][0]['instrument_type'] = '2M0-FLOYDS-SCICAM'
del good_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['filter']
good_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['slit'] = 'slit_6.0as'
good_data['requests'][0]['configurations'][0]['type'] = 'SPECTRUM'
response = self.client.post(reverse('api:request_groups-list'), data=good_data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['requests'][0]['configurations'][0]['acquisition_config']['mode'], 'WCS')
# Check that default acquire mode is OFF for floyds non-spectrum
good_data['requests'][0]['configurations'][0]['type'] = 'LAMP_FLAT'
response = self.client.post(reverse('api:request_groups-list'), data=good_data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['requests'][0]['configurations'][0]['acquisition_config']['mode'], 'OFF')
def test_post_requestgroup_acquire_mode_brightest_no_radius(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['configurations'][0]['instrument_type'] = '2M0-FLOYDS-SCICAM'
bad_data['requests'][0]['configurations'][0]['acquisition_config']['mode'] = 'BRIGHTEST'
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('requires [acquire_radius] set in extra param', str(response.content))
def test_post_requestgroup_acquire_mode_brightest(self):
good_data = self.generic_payload.copy()
good_data['requests'][0]['location']['telescope_class'] = '2m0'
good_data['requests'][0]['configurations'][0]['instrument_type'] = '2M0-FLOYDS-SCICAM'
good_data['requests'][0]['configurations'][0]['type'] = 'SPECTRUM'
del good_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['filter']
good_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['slit'] = 'slit_6.0as'
good_data['requests'][0]['configurations'][0]['acquisition_config']['mode'] = 'BRIGHTEST'
good_data['requests'][0]['configurations'][0]['acquisition_config']['extra_params']['acquire_radius'] = 2
response = self.client.post(reverse('api:request_groups-list'), data=good_data)
self.assertEqual(response.status_code, 201)
def test_post_requestgroup_single_must_have_one_request(self):
bad_data = self.generic_payload.copy()
bad_data['requests'].append(bad_data['requests'][0].copy())
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('must have exactly one child request', str(response.content))
# Removed AND operator for now, so commenting out this test
# def test_post_requestgroup_and_must_have_greater_than_one_request(self):
# bad_data = self.generic_payload.copy()
# bad_data['operator'] = 'AND'
# response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
# self.assertEqual(response.status_code, 400)
# self.assertIn('must have more than one child request', str(response.content))
def test_post_requestgroup_constraints_optional(self):
good_data = self.generic_payload.copy()
good_data['requests'][0]['configurations'][0]['target']['dec'] = -30.0
good_data['requests'][0]['configurations'][0]['target']['ra'] = 50.0
del good_data['requests'][0]['configurations'][0]['constraints']['max_airmass']
del good_data['requests'][0]['configurations'][0]['constraints']['min_lunar_distance']
response = self.client.post(reverse('api:request_groups-list'), data=good_data)
self.assertEqual(response.status_code, 201)
def test_validation(self):
good_data = self.generic_payload.copy()
response = self.client.post(reverse('api:request_groups-validate'), data=good_data)
self.assertEqual(response.status_code, 200)
self.assertFalse(response.json()['errors'])
def test_validation_fail(self):
bad_data = self.generic_payload.copy()
del bad_data['operator']
response = self.client.post(reverse('api:request_groups-validate'), data=bad_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['errors']['operator'][0], 'This field is required.')
def test_post_requestgroup_duration_too_long(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['configurations'][0]['instrument_configs'][0]['exposure_time'] = 999999999999
response = self.client.post(reverse('api:request_groups-list'), data=self.generic_payload)
self.assertEqual(response.status_code, 400)
self.assertIn('the target is visible for a maximum of', str(response.content))
def test_post_requestgroup_default_acceptability_threshold(self):
data = self.generic_payload.copy()
# Test that default threshold for non-floyds is 90
response = self.client.post(reverse('api:request_groups-list'), data=data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['requests'][0]['acceptability_threshold'], 90)
# Test that default threshold for floyds is 100
data['requests'][0]['location']['telescope_class'] = '2m0'
data['requests'][0]['configurations'][0]['instrument_type'] = '2M0-FLOYDS-SCICAM'
data['requests'][0]['configurations'][0]['type'] = 'SPECTRUM'
del data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['filter']
data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['slit'] = 'slit_6.0as'
response = self.client.post(reverse('api:request_groups-list'), data=data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['requests'][0]['acceptability_threshold'], 100)
class TestDisallowedMethods(APITestCase):
def setUp(self):
self.user = blend_user()
self.proposal = mixer.blend(Proposal)
mixer.blend(Membership, proposal=self.proposal, user=self.user)
self.rg = mixer.blend(RequestGroup, proposal=self.proposal, observation_type=RequestGroup.NORMAL)
self.client.force_login(self.user)
def test_cannot_delete_rg(self):
response = self.client.delete(reverse('api:request_groups-detail', args=(self.rg.id,)))
self.assertEqual(response.status_code, 405)
def test_cannot_update_rg(self):
response = self.client.put(reverse('api:request_groups-detail', args=(self.rg.id,)))
self.assertEqual(response.status_code, 405)
class TestRequestGroupIPP(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user()
self.client.force_login(self.user)
mixer.blend(Membership, user=self.user, proposal=self.proposal)
semester = mixer.blend(
Semester,
id='2016B',
start=datetime(2016, 9, 1, tzinfo=timezone.utc),
end=datetime(2016, 12, 31, tzinfo=timezone.utc)
)
self.time_allocation_1m0_sbig = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=semester, instrument_type='1M0-SCICAM-SBIG',
std_allocation=100.0, std_time_used=0.0, rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0,
ipp_time_available=5.0, tc_allocation=10, tc_time_used=0
)
self.time_allocation_2m0_floyds = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=semester, instrument_type='2M0-FLOYDS-SCICAM',
std_allocation=100.0, std_time_used=0.0, rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0,
ipp_time_available=5.0, tc_allocation=10, tc_time_used=0
)
self.generic_payload = copy.deepcopy(generic_payload)
self.generic_payload['ipp_value'] = 1.5
self.generic_payload['proposal'] = self.proposal.id
self.generic_payload['name'] = 'ipp_request'
self.generic_multi_payload = copy.deepcopy(self.generic_payload)
self.second_request = copy.deepcopy(generic_payload['requests'][0])
self.second_request['configurations'][0]['instrument_type'] = '2M0-FLOYDS-SCICAM'
self.second_request['configurations'][0]['type'] = 'SPECTRUM'
del self.second_request['configurations'][0]['instrument_configs'][0]['optical_elements']['filter']
self.second_request['configurations'][0]['instrument_configs'][0]['optical_elements']['slit'] = 'slit_6.0as'
self.second_request['location']['telescope_class'] = '2m0'
self.generic_multi_payload['requests'].append(self.second_request)
def _build_request_group(self, rg_dict):
response = self.client.post(reverse('api:request_groups-list'), data=rg_dict)
self.assertEqual(response.status_code, 201)
return RequestGroup.objects.get(name=rg_dict['name'])
def test_request_group_debit_ipp_on_creation(self):
self.assertEqual(self.time_allocation_1m0_sbig.ipp_time_available, 5.0)
rg = self.generic_payload.copy()
response = self.client.post(reverse('api:request_groups-list'), data=rg)
self.assertEqual(response.status_code, 201)
# verify that now that the object is saved, ipp has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
duration = Request.objects.get(request_group=response.json()['id']).duration / 3600.0
self.assertAlmostEqual(time_allocation.ipp_time_available, 5.0 - (duration * (rg['ipp_value'] - 1.0)), 5)
def test_request_group_multi_debit_ipp_on_creation(self):
self.assertEqual(self.time_allocation_1m0_sbig.ipp_time_available, 5.0)
rg = self.generic_payload.copy()
rg['operator'] = 'MANY'
rg['requests'].append(copy.deepcopy(rg['requests'][0]))
response = self.client.post(reverse('api:request_groups-list'), data=rg)
self.assertEqual(response.status_code, 201)
# verify that now that the object is saved, ipp has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
requests = Request.objects.filter(request_group=response.json()['id'])
duration = 0.0
for req in requests:
duration += req.duration
duration /= 3600.0
self.assertAlmostEqual(time_allocation.ipp_time_available, 5.0 - (duration * (rg['ipp_value'] - 1.0)), 5)
def test_tc_request_group_no_debit_ipp_on_creation(self):
self.assertEqual(self.time_allocation_1m0_sbig.ipp_time_available, 5.0)
rg = self.generic_payload.copy()
rg['observation_type'] = RequestGroup.TIME_CRITICAL
response = self.client.post(reverse('api:request_groups-list'), data=rg)
self.assertEqual(response.status_code, 201)
# verify that now that the object is saved, ipp has not been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
def test_request_group_credit_ipp_on_cancelation(self):
request_group = self._build_request_group(self.generic_payload.copy())
# verify that now that the TimeAllocation has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertLess(time_allocation.ipp_time_available, 5.0)
request_group.state = 'CANCELED'
request_group.save()
# verify that now that the TimeAllocation has its original ipp value
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
# also verify that the child request state has changed to window_expired as well
self.assertEqual(request_group.requests.first().state, 'CANCELED')
def test_tc_request_group_no_credit_ipp_on_cancelation(self):
rg = self.generic_payload.copy()
rg['observation_type'] = RequestGroup.TIME_CRITICAL
response = self.client.post(reverse('api:request_groups-list'), data=rg)
request_group = RequestGroup.objects.get(id=response.json()['id'])
# verify that now that the TimeAllocation has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
self.assertEqual(time_allocation.rr_time_used, 0.0)
request_group.state = 'CANCELED'
request_group.save()
# verify that now that the TimeAllocation has its original ipp value
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
self.assertEqual(time_allocation.rr_time_used, 0.0)
# also verify that the child request state has changed to window_expired as well
self.assertEqual(request_group.requests.first().state, 'CANCELED')
def test_request_group_credit_ipp_on_expiration(self):
request_group = self._build_request_group(self.generic_payload.copy())
# verify that now that the TimeAllocation has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertLess(time_allocation.ipp_time_available, 5.0)
request_group.state = 'WINDOW_EXPIRED'
request_group.save()
# verify that now that the TimeAllocation has its original ipp value
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
# also verify that the child request state has changed to window_expired as well
self.assertEqual(request_group.requests.first().state, 'WINDOW_EXPIRED')
def test_request_group_debit_ipp_on_creation_fail(self):
self.time_allocation_1m0_sbig.ipp_time_available = 0
self.time_allocation_1m0_sbig.save()
rg = self.generic_payload.copy()
# ipp duration that is too high, will be rejected
rg['ipp_value'] = 2.0
response = self.client.post(reverse('api:request_groups-list'), data=rg)
self.assertEqual(response.status_code, 400)
self.assertIn('TimeAllocationError', str(response.content))
self.assertIn('An IPP Value of 2.0 requires more IPP time than you have available', str(response.content))
# verify that objects were not created by the send
self.assertFalse(RequestGroup.objects.filter(name='ipp_request').exists())
def test_request_group_multi_credit_ipp_back_on_cancelation(self):
rg = self.generic_multi_payload
rg['operator'] = 'MANY'
request_group = self._build_request_group(rg)
# verify that now that both the TimeAllocation has been debited
time_allocation_1m0 = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertLess(time_allocation_1m0.ipp_time_available, 5.0)
time_allocation_2m0 = TimeAllocation.objects.get(pk=self.time_allocation_2m0_floyds.id)
self.assertLess(time_allocation_2m0.ipp_time_available, 5.0)
# now set one request to completed, then set the user request to unschedulable
request = request_group.requests.first()
request.state = 'COMPLETED'
request.save()
request_group.state = 'WINDOW_EXPIRED'
request_group.save()
# now verify that time allocation 1 is still debited, but time allocation 2 has been credited back its time
time_allocation_1m0 = TimeAllocation.objects.get(pk=self.time_allocation_1m0_sbig.id)
self.assertLess(time_allocation_1m0.ipp_time_available, 5.0)
time_allocation_2m0 = TimeAllocation.objects.get(pk=self.time_allocation_2m0_floyds.id)
self.assertEqual(time_allocation_2m0.ipp_time_available, 5.0)
class TestRequestIPP(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user()
self.client.force_login(self.user)
mixer.blend(Membership, user=self.user, proposal=self.proposal)
semester = mixer.blend(
Semester,
id='2016B',
start=datetime(2016, 9, 1, tzinfo=timezone.utc),
end=datetime(2016, 12, 31, tzinfo=timezone.utc),
)
self.time_allocation_1m0 = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=semester,
instrument_type='1M0-SCICAM-SBIG', std_allocation=100.0, std_time_used=0.0,
rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0, ipp_time_available=5.0
)
self.generic_payload = copy.deepcopy(generic_payload)
self.generic_payload['ipp_value'] = 1.5
self.generic_payload['proposal'] = self.proposal.id
self.generic_payload['name'] = 'ipp_request'
def _build_request_group(self, rg_dict):
response = self.client.post(reverse('api:request_groups-list'), data=rg_dict)
self.assertEqual(response.status_code, 201)
return RequestGroup.objects.get(name=rg_dict['name'])
def test_request_debit_on_completion_after_expired(self):
request_group = self._build_request_group(self.generic_payload.copy())
# verify that now that the TimeAllocation has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
debitted_ipp_value = time_allocation.ipp_time_available
self.assertLess(debitted_ipp_value, 5.0)
# now change requests state to expired
request = request_group.requests.first()
request.state = 'WINDOW_EXPIRED'
request.save()
# verify that now that the TimeAllocation has its original ipp value
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
# now set request to completed and see that ipp is debited once more
request.state = 'COMPLETED'
request.save()
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertEqual(time_allocation.ipp_time_available, debitted_ipp_value)
@patch('observation_portal.common.state_changes.logger')
def test_request_debit_on_completion_after_expired_not_enough_time(self, mock_logger):
request_group = self._build_request_group(self.generic_payload.copy())
# verify that now that the TimeAllocation has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
debitted_ipp_value = time_allocation.ipp_time_available
self.assertLess(debitted_ipp_value, 5.0)
# now change requests state to expired
request = request_group.requests.first()
request.state = 'WINDOW_EXPIRED'
request.save()
# verify that now that the TimeAllocation has its original ipp value
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
# set the time allocation available to 0.01, then set to completed
time_allocation.ipp_time_available = 0.01
time_allocation.save()
# now set request to completed and see that ipp debitted to 0
request.state = 'COMPLETED'
request.save()
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertEqual(time_allocation.ipp_time_available, 0)
# test that the log message was generated
self.assertIn('Time available after debiting will be capped at 0',
mock_logger.warning.call_args[0][0])
def test_request_credit_back_on_cancelation(self):
request_group = self._build_request_group(self.generic_payload.copy())
# verify that now that the TimeAllocation has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertLess(time_allocation.ipp_time_available, 5.0)
# now change requests state to canceled
request = request_group.requests.first()
request.state = 'CANCELED'
request.save()
# verify that now that the TimeAllocation has its original ipp value
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
def test_request_credit_on_completion(self):
payload = self.generic_payload.copy()
payload['ipp_value'] = 0.5
request_group = self._build_request_group(payload)
# verify that now that the TimeAllocation has been debited
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertEqual(time_allocation.ipp_time_available, 5.0)
# now change requests state to canceled
request = request_group.requests.first()
request.state = 'COMPLETED'
request.save()
# verify that now that the TimeAllocation has its original ipp value
time_allocation = TimeAllocation.objects.get(pk=self.time_allocation_1m0.id)
self.assertGreater(time_allocation.ipp_time_available, 5.0)
class TestWindowApi(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user()
self.client.force_login(self.user)
mixer.blend(Membership, user=self.user, proposal=self.proposal)
self.semester = mixer.blend(
Semester,
id='2016B',
start=datetime(2016, 9, 1, tzinfo=timezone.utc),
end=datetime(2016, 12, 31, tzinfo=timezone.utc),
)
self.time_allocation_1m0 = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=self.semester,
telescope_class='1m0', std_allocation=100.0, std_time_used=0.0,
rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0,
ipp_time_available=5.0
)
self.generic_payload = copy.deepcopy(generic_payload)
self.generic_payload['proposal'] = self.proposal.id
def test_post_requestgroup_window_end_before_start(self):
bad_data = self.generic_payload.copy()
end = bad_data['requests'][0]['windows'][0]['end']
start = bad_data['requests'][0]['windows'][0]['start']
bad_data['requests'][0]['windows'][0]['end'] = start
bad_data['requests'][0]['windows'][0]['start'] = end
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('cannot be earlier than window start', str(response.content))
def test_post_requestgroup_no_windows_invalid(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['windows'] = []
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
bad_data = self.generic_payload.copy()
del bad_data['requests'][0]['windows']
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
def test_window_does_not_fit_in_any_semester(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['windows'][0]['start'] = '2015-01-01 00:00:00'
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('The observation window does not fit within any defined semester', str(response.content))
@patch('observation_portal.requestgroups.duration_utils.get_semesters')
def test_request_windows_are_in_different_semesters(self, mock_get_semesters):
# Patch get_semesters so that we can add on a window that is in a different valid semester, and still ensure
# that the semesters that are returned are predictable in this test - the semesters variable used in
# get_semesters in duration_utils is a global, so it can cause unpredictable results across tests
another_semester = mixer.blend(
Semester,
id='2017A',
start=datetime(2017, 1, 1, tzinfo=timezone.utc),
end=datetime(2017, 6, 30, tzinfo=timezone.utc),
)
mock_get_semesters.return_value = [self.semester, another_semester]
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['windows'].append({'start': '2017-02-01 00:00:00', 'end': '2017-02-02 00:00:00'})
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('The observation windows must all be in the same semester', str(response.content))
class TestCadenceApi(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user()
mixer.blend(Membership, user=self.user, proposal=self.proposal)
semester = mixer.blend(
Semester, id='2016B', start=datetime(2016, 9, 1, tzinfo=timezone.utc),
end=datetime(2016, 12, 31, tzinfo=timezone.utc)
)
self.time_allocation_1m0 = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=semester,
telescope_class='1m0', std_allocation=100.0, std_time_used=0.0,
rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0,
ipp_time_available=5.0, instrument_type='1M0-SCICAM-SBIG'
)
self.client.force_login(self.user)
self.generic_payload = copy.deepcopy(generic_payload)
self.generic_payload['proposal'] = self.proposal.id
del self.generic_payload['requests'][0]['windows']
self.generic_payload['requests'][0]['cadence'] = {
'start': '2016-09-01T21:12:18Z',
'end': '2016-09-03T22:12:19Z',
'period': 24.0,
'jitter': 12.0
}
def test_post_requestgroup_cadence_and_windows_is_invalid(self):
bad_data = self.generic_payload.copy()
bad_data['windows'] = [{'start': '2016-09-29T21:12:18Z', 'end': '2016-10-29T21:12:19Z'}]
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertTrue(response.json()['requests'][0]['cadence'])
def test_post_requestgroup_cadence_is_invalid(self):
bad_data = self.generic_payload.copy()
bad_data['windows'] = []
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
del bad_data['windows']
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
def test_post_cadence_end_before_start_invalid(self):
bad_data = self.generic_payload.copy()
end = bad_data['requests'][0]['cadence']['end']
bad_data['requests'][0]['cadence']['end'] = bad_data['requests'][0]['cadence']['start']
bad_data['requests'][0]['cadence']['start'] = end
response = self.client.post(reverse('api:request_groups-cadence'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('cannot be earlier than cadence start', str(response.content))
def test_post_cadence_end_in_the_past_invalid(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['cadence']['end'] = datetime(1901, 1, 1)
bad_data['requests'][0]['cadence']['start'] = datetime(1900, 1, 1)
response = self.client.post(reverse('api:request_groups-cadence'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('End time must be in the future', str(response.content))
def test_post_cadence_valid(self):
response = self.client.post(reverse('api:request_groups-cadence'), data=self.generic_payload)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()['requests']), 2)
def test_cadence_invalid(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['cadence']['jitter'] = 'bug'
response = self.client.post(reverse('api:request_groups-cadence'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['cadence']['jitter'], ['A valid number is required.'])
def test_cadence_with_windows_invalid(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['windows'] = [{'start': '2016-09-29T21:12:18Z', 'end': '2016-10-29T21:12:19Z'}]
response = self.client.post(reverse('api:request_groups-cadence'), data=bad_data)
self.assertIn('requests may not contain windows', str(response.content))
def test_cadence_invalid_period(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['cadence']['period'] = -666
response = self.client.post(reverse('api:request_groups-cadence'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['cadence']['period'], ['Ensure this value is greater than or equal to 0.02.'])
def test_post_requestgroup_after_valid_cadence(self):
response = self.client.post(reverse('api:request_groups-cadence'), data=self.generic_payload)
second_response = self.client.post(reverse('api:request_groups-list'), data=response.json())
self.assertEqual(second_response.status_code, 201)
self.assertGreater(self.user.requestgroup_set.all().count(), 0)
def test_post_cadence_with_invalid_request(self):
bad_data = self.generic_payload.copy()
bad_data['requests'].append('invalid_request')
response = self.client.post(reverse('api:request_groups-cadence'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('Invalid data. Expected a dictionary, but got str.', str(response.content))
def test_post_cadence_with_no_visible_requests(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['cadence']['end'] = '2016-09-01T21:13:18Z'
bad_data['requests'][0]['configurations'][0]['instrument_configs'][0]['exposure_count'] = 100
bad_data['requests'][0]['cadence']['jitter'] = 0.02
bad_data['requests'][0]['cadence']['period'] = 0.02
response = self.client.post(reverse('api:request_groups-cadence'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('No visible requests within cadence window parameters', str(response.content))
class TestICRSTarget(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user()
self.client.force_login(self.user)
semester = mixer.blend(Semester, id='2016B', start=datetime(2016, 9, 1, tzinfo=timezone.utc),
end=datetime(2016, 12, 31, tzinfo=timezone.utc)
)
self.time_allocation_1m0_sbig = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=semester,
instrument_type='1M0-SCICAM-SBIG', std_allocation=100.0,
std_time_used=0.0, rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0,
ipp_time_available=5.0
)
self.time_allocation_2m0_floyds = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=semester,
instrument_type='2M0-FLOYDS-SCICAM', std_allocation=100.0, std_time_used=0.0,
rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0, ipp_time_available=5.0
)
mixer.blend(Membership, user=self.user, proposal=self.proposal)
self.generic_payload = copy.deepcopy(generic_payload)
self.generic_payload['proposal'] = self.proposal.id
def test_post_requestgroup_no_ra(self):
bad_data = self.generic_payload.copy()
del bad_data['requests'][0]['configurations'][0]['target']['ra']
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 400)
self.assertIn('ra', str(response.content))
def test_post_requestgroup_extra_ns_field(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['configurations'][0]['target']['longascnode'] = 4.0
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 201)
target = response.json()['requests'][0]['configurations'][0]['target']
self.assertNotIn('longascnode', target)
def test_post_requestgroup_test_defaults(self):
bad_data = self.generic_payload.copy()
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 201)
target = response.json()['requests'][0]['configurations'][0]['target']
self.assertEqual(target['proper_motion_ra'], 0.0)
self.assertEqual(target['proper_motion_dec'], 0.0)
self.assertEqual(target['parallax'], 0.0)
self.assertEqual(target['epoch'], 2000.0)
def test_post_requestgroup_test_proper_motion_no_epoch(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['configurations'][0]['target']['proper_motion_ra'] = 1.0
bad_data['requests'][0]['configurations'][0]['target']['proper_motion_dec'] = 1.0
# epoch defaults to 2000 so we should pass
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 201)
def test_post_requestgroup_test_proper_motion_with_epoch(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['configurations'][0]['target']['proper_motion_ra'] = 1.0
bad_data['requests'][0]['configurations'][0]['target']['proper_motion_dec'] = 1.0
bad_data['requests'][0]['configurations'][0]['target']['epoch'] = 2001.0
response = self.client.post(reverse('api:request_groups-list'), data=bad_data)
self.assertEqual(response.status_code, 201)
def test_floyds_gets_vfloat_default(self):
good_data = self.generic_payload.copy()
good_data['requests'][0]['location']['telescope_class'] = '2m0'
good_data['requests'][0]['configurations'][0]['instrument_type'] = '2M0-FLOYDS-SCICAM'
good_data['requests'][0]['configurations'][0]['type'] = 'SPECTRUM'
del good_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['filter']
good_data['requests'][0]['configurations'][0]['instrument_configs'][0]['optical_elements']['slit'] = 'slit_6.0as'
response = self.client.post(reverse('api:request_groups-list'), data=good_data, follow=True)
self.assertEqual(response.json()['requests'][0]['configurations'][0]['instrument_configs'][0]['rotator_mode'], 'VFLOAT')
def test_target_name_max_length(self):
bad_data = self.generic_payload.copy()
bad_data['requests'][0]['configurations'][0]['target']['name'] = 'x' * 51
response = self.client.post(reverse('api:request_groups-list'), data=bad_data, follow=True)
self.assertEqual(response.status_code, 400)
self.assertIn('50 characters', str(response.content))
class TestOrbitalElementsTarget(SetTimeMixin, APITestCase):
def setUp(self):
super().setUp()
self.proposal = mixer.blend(Proposal)
self.user = blend_user()
self.client.force_login(self.user)
semester = mixer.blend(
Semester, id='2016B', start=datetime(2016, 9, 1, tzinfo=timezone.utc),
end=datetime(2016, 12, 31, tzinfo=timezone.utc)
)
self.time_allocation_1m0 = mixer.blend(
TimeAllocation, proposal=self.proposal, semester=semester, std_allocation=100.0, std_time_used=0.0,
instrument_type='1M0-SCICAM-SBIG', rr_allocation=10, rr_time_used=0.0, ipp_limit=10.0,
ipp_time_available=5.0
)
mixer.blend(Membership, user=self.user, proposal=self.proposal)
self.generic_payload = copy.deepcopy(generic_payload)
self.generic_payload['proposal'] = self.proposal.id
self.generic_payload['requests'][0]['configurations'][0]['target'] = {
'name': 'fake target',