def test_put_review_no_approved(self): """Test PUT review with no approved field.""" odlc = Odlc(user=self.team, odlc_type=OdlcType.standard) odlc.save() response = self.client.put(odlcs_review_id_url(args=[odlc.pk])) self.assertEqual(400, response.status_code)
def post(self, request): odlc_proto = interop_api_pb2.Odlc() try: json_format.Parse(request.body, odlc_proto) except Exception as e: return HttpResponseBadRequest( 'Failed to parse request. Error: %s' % str(e)) # Validate ODLC proto fields. try: validate_odlc_proto(odlc_proto) except ValueError as e: return HttpResponseBadRequest(str(e)) # Cannot set ODLC ID on a post. if odlc_proto.HasField('id'): return HttpResponseBadRequest( 'Cannot specify ID for POST request.') # Build the ODLC object from the request. odlc = Odlc() odlc.user = request.user update_odlc_from_proto(odlc, odlc_proto) odlc.save() return HttpResponse(json_format.MessageToJson(odlc_to_proto(odlc)), content_type="application/json")
def post(self, request): odlc_proto = interop_api_pb2.Odlc() try: json_format.Parse(request.body, odlc_proto) except Exception as e: return HttpResponseBadRequest( 'Failed to parse request. Error: %s' % str(e)) # Validate ODLC proto fields. try: validate_odlc_proto(odlc_proto) except ValueError as e: return HttpResponseBadRequest(str(e)) # Cannot set ODLC ID on a post. if odlc_proto.HasField('id'): return HttpResponseBadRequest( 'Cannot specify ID for POST request.') # Check that there aren't too many ODLCs uploaded already. odlc_count = Odlc.objects.filter(user=request.user).filter( mission=odlc_proto.mission).count() if odlc_count >= ODLC_UPLOAD_LIMIT: return HttpResponseBadRequest( 'Reached upload limit for ODLCs for mission.') # Build the ODLC object from the request. odlc = Odlc() odlc.user = request.user update_odlc_from_proto(odlc, odlc_proto) odlc.save() return HttpResponse(json_format.MessageToJson(odlc_to_proto(odlc)), content_type="application/json")
def test_put_review_no_approved(self): """Test PUT review with no approved field.""" odlc = Odlc(mission=self.mission, user=self.team, odlc_type=interop_api_pb2.Odlc.STANDARD) odlc.save() response = self.client.put(odlcs_review_id_url(args=[odlc.pk])) self.assertEqual(400, response.status_code)
def test_get_own(self): """Test GETting a odlc owned by the correct user.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() response = self.client.get(odlcs_id_url(args=[t.pk])) self.assertEqual(200, response.status_code) self.assertEqual(t.json(), json.loads(response.content))
def test_get_without_thumbnail(self): """Test GET when there are odlcs without thumbnail.""" odlc = Odlc(user=self.team, odlc_type=OdlcType.standard) odlc.save() response = self.client.get(odlcs_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(0, len(data))
def test_delete_other(self): """Test DELETEing a odlc owned by another user.""" user2 = User.objects.create_user('testuser2', '*****@*****.**', 'testpass') t = Odlc(user=user2, odlc_type=OdlcType.standard) t.save() response = self.client.delete(odlcs_id_url(args=[t.pk])) self.assertEqual(403, response.status_code)
def test_get_other_user(self): """Test GETting a thumbnail owned by a different user.""" user2 = User.objects.create_user('testuser2', '*****@*****.**', 'testpass') t = Odlc(user=user2, odlc_type=OdlcType.standard) t.save() response = self.client.get(odlcs_id_image_url(args=[t.pk])) self.assertEqual(403, response.status_code)
def test_put_location_missing_one(self): """PUTting new location requires both latitude and longitude.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() data = {'latitude': 38} response = self.client.put( odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(400, response.status_code)
def test_put_change_actionable_override(self): """PUT fails if non-admin user tries to change actionable_override.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() data = {'actionable_override': True} response = self.client.put( odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(403, response.status_code)
def test_put_clear_type(self): """PUT type may not be cleared.""" t = Odlc( user=self.user, odlc_type=OdlcType.standard, shape=Shape.square) t.save() data = {'type': None} response = self.client.put( odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(400, response.status_code)
def test_delete_other(self): """Test DELETEing a odlc owned by another user.""" user2 = User.objects.create_user('testuser2', '*****@*****.**', 'testpass') t = Odlc(mission=self.mission, user=user2, odlc_type=interop_api_pb2.Odlc.STANDARD) t.save() response = self.client.delete(odlcs_id_url(args=[t.pk])) self.assertEqual(403, response.status_code)
def test_get_without_thumbnail(self): """Test GET when there are odlcs without thumbnail.""" odlc = Odlc(mission=self.mission, user=self.team, odlc_type=interop_api_pb2.Odlc.STANDARD) odlc.save() response = self.client.get(odlcs_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(0, len(data))
def test_put_invalid_json(self): """PUT request body must be valid JSON.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l) t.save() response = self.client.put(odlcs_id_url(args=[t.pk]), data="latitude=76", content_type='multipart/form-data') self.assertEqual(400, response.status_code)
def test_get_after_delete_own(self): """Test GETting a odlc after DELETE.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() pk = t.pk response = self.client.delete(odlcs_id_url(args=[pk])) self.assertEqual(200, response.status_code) response = self.client.get(odlcs_id_url(args=[pk])) self.assertEqual(404, response.status_code)
def test_put_partial_clear_location(self): """PUT can't clear location with only one of lat/lon.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l) t.save() data = {'latitude': None} response = self.client.put( odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(400, response.status_code)
def test_delete_own(self): """Test DELETEing a odlc owned by the correct user.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() pk = t.pk self.assertTrue(Odlc.objects.get(pk=pk)) response = self.client.delete(odlcs_id_url(args=[pk])) self.assertEqual(200, response.status_code) with self.assertRaises(Odlc.DoesNotExist): Odlc.objects.get(pk=pk)
def test_get_after_delete_own(self): """Test GETting a odlc after DELETE.""" t = Odlc(mission=self.mission, user=self.user, odlc_type=interop_api_pb2.Odlc.STANDARD) t.save() pk = t.pk response = self.client.delete(odlcs_id_url(args=[pk])) self.assertEqual(200, response.status_code) response = self.client.get(odlcs_id_url(args=[pk])) self.assertEqual(404, response.status_code)
def test_get_own(self): """Test GETting a odlc owned by the correct user.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() response = self.client.get(odlcs_id_url(args=[t.pk])) self.assertEqual(200, response.status_code) self.assertEqual( { 'id': t.pk, 'type': 'STANDARD', 'autonomous': False, }, json.loads(response.content))
def test_get_odlcs(self): """We get back the odlcs we own.""" t1 = Odlc(user=self.user, odlc_type=OdlcType.standard) t1.save() t2 = Odlc(user=self.user, odlc_type=OdlcType.standard) t2.save() response = self.client.get(odlcs_url) self.assertEqual(200, response.status_code) d = json.loads(response.content) self.assertEqual([t2.json(), t1.json()], d)
def test_not_others(self): """We don't get odlcs owned by other users.""" user2 = User.objects.create_user('testuser2', '*****@*****.**', 'testpass') mine = Odlc( mission=self.mission, user=self.user, odlc_type=interop_api_pb2.Odlc.STANDARD) mine.save() theirs = Odlc( mission=self.mission, user=user2, odlc_type=interop_api_pb2.Odlc.STANDARD) theirs.save() response = self.client.get(odlcs_url) self.assertEqual(200, response.status_code) self.assertEqual([ { 'id': mine.pk, 'mission': self.mission.pk, 'type': 'STANDARD', 'autonomous': False, }, ], json.loads(response.content))
def test_get_noneditable_without_thumbnail_odlcs(self): """Test GET when there are non-editable odlcs without thumbnail.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() MissionClockEvent(user=self.team, team_on_clock=False, team_on_timeout=False).save() odlc = Odlc(user=self.team, odlc_type=OdlcType.standard) odlc.save() response = self.client.get(odlcs_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(0, len(data))
def test_last_modified_time(self): """Last modified time is set on creation and changes every update.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() orig = t.last_modified_time self.assertIsNotNone(orig) t.alphanumeric = 'A' t.update_last_modified() t.save() self.assertGreater(t.last_modified_time, orig)
def test_get_own(self): """Test GETting a odlc owned by the correct user.""" t = Odlc(mission=self.mission, user=self.user, odlc_type=interop_api_pb2.Odlc.STANDARD) t.save() response = self.client.get(odlcs_id_url(args=[t.pk])) self.assertEqual(200, response.status_code) self.assertEqual( { 'mission': self.mission.pk, 'id': t.pk, 'type': 'STANDARD', 'autonomous': False, }, json.loads(response.content))
def test_last_modified_time(self): """Last modified time is set on creation and changes every update.""" t = Odlc(mission=self.mission, user=self.user, odlc_type=interop_api_pb2.Odlc.STANDARD) t.save() orig = t.last_modified_time self.assertIsNotNone(orig) t.alphanumeric = 'A' t.update_last_modified() t.save() self.assertGreater(t.last_modified_time, orig)
def test_minimal_json(self): """Test odlc JSON with minimal data.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() d = t.json() self.assertIn('id', d) self.assertEqual(self.user.pk, d['user']) self.assertEqual('standard', d['type']) self.assertEqual(None, d['latitude']) self.assertEqual(None, d['longitude']) self.assertEqual(None, d['orientation']) self.assertEqual(None, d['shape']) self.assertEqual(None, d['background_color']) self.assertEqual(None, d['alphanumeric']) self.assertEqual(None, d['alphanumeric_color']) self.assertEqual(None, d['description']) self.assertEqual(False, d['autonomous'])
def test_get_editable_odlcs(self): """Test GET when there are odlcs but are still in editable window.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() Odlc(user=self.team, odlc_type=OdlcType.standard).save() response = self.client.get(odlcs_review_url) self.assertEqual(200, response.status_code) self.assertEqual([], json.loads(response.content))
def test_not_others(self): """We don't get odlcs owned by other users.""" user2 = User.objects.create_user('testuser2', '*****@*****.**', 'testpass') mine = Odlc(user=self.user, odlc_type=OdlcType.standard) mine.save() theirs = Odlc(user=user2, odlc_type=OdlcType.standard) theirs.save() response = self.client.get(odlcs_url) self.assertEqual(200, response.status_code) d = json.loads(response.content) self.assertEqual([mine.json()], d)
def test_valid(self): """Test creating a valid odlc.""" with open(os.path.join(settings.BASE_DIR, 'auvsi_suas/testdata/S.jpg'), 'rb') as f: thumb = SimpleUploadedFile('thumb.jpg', f.read()) l = GpsPosition(latitude=38, longitude=-76) l.save() t = Odlc(mission=self.mission, user=self.user, odlc_type=interop_api_pb2.Odlc.STANDARD, location=l, orientation=interop_api_pb2.Odlc.S, shape=interop_api_pb2.Odlc.SQUARE, shape_color=interop_api_pb2.Odlc.WHITE, alphanumeric='ABC', alphanumeric_color=interop_api_pb2.Odlc.BLACK, description='Test odlc', thumbnail=thumb) t.save()
def test_valid(self): """Test creating a valid odlc.""" with open( os.path.join(settings.BASE_DIR, 'auvsi_suas/fixtures/testdata/S.jpg')) as f: thumb = SimpleUploadedFile('thumb.jpg', f.read()) l = GpsPosition(latitude=38, longitude=-76) l.save() t = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test odlc', thumbnail=thumb) t.save()