def test_delete_thumb(self): """Test DELETEing a odlc with thumbnail.""" t = Odlc( mission=self.mission, user=self.user, odlc_type=interop_api_pb2.Odlc.STANDARD) t.save() pk = t.pk with open(test_image("A.jpg"), 'rb') as f: response = self.client.post( odlcs_id_image_url(args=[pk]), data=f.read(), content_type="image/jpeg") self.assertEqual(200, response.status_code) t.refresh_from_db() thumb = t.thumbnail.path self.assertTrue(os.path.exists(thumb)) response = self.client.delete(odlcs_id_url(args=[pk])) self.assertEqual(200, response.status_code) self.assertFalse(os.path.exists(thumb))
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_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 test_put_review(self): """Test PUT review is saved.""" 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]), data=json.dumps({ 'thumbnailApproved': True, 'descriptionApproved': True, })) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertIn('odlc', data) self.assertIn('id', data['odlc']) self.assertEqual(odlc.pk, data['odlc']['id']) self.assertIn('thumbnailApproved', data) self.assertTrue(data['thumbnailApproved']) self.assertTrue(data['descriptionApproved']) odlc.refresh_from_db() self.assertTrue(odlc.thumbnail_approved) self.assertTrue(odlc.description_approved)
def test_put_clear_shape(self): """PUT clear a field with None.""" 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') t.save() updated = { 'type': 'STANDARD', } response = self.client.put(odlcs_id_url(args=[t.pk]), data=json.dumps(updated)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(self.user, t.user) self.assertEqual(OdlcType.standard, t.odlc_type) self.assertIsNone(t.location) self.assertIsNone(t.orientation) self.assertIsNone(t.shape) self.assertIsNone(t.background_color) self.assertEqual('', t.alphanumeric) self.assertIsNone(t.alphanumeric_color) self.assertEqual('', t.description)
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_one(self): """PUT update one field without affecting others.""" 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') t.save() data = {'shape': 'circle'} response = self.client.put( odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() t.location.refresh_from_db() self.assertEqual(self.user, t.user) self.assertEqual(OdlcType.standard, t.odlc_type) self.assertEqual(38, t.location.latitude) self.assertEqual(-76, t.location.longitude) self.assertEqual(Orientation.s, t.orientation) self.assertEqual(Shape.circle, t.shape) self.assertEqual(Color.white, t.background_color) self.assertEqual('ABC', t.alphanumeric) self.assertEqual(Color.black, t.alphanumeric_color) self.assertEqual('Test odlc', t.description)
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_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_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_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_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.save() self.assertGreater(t.last_modified_time, orig)
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_creation_time(self): """Creation time is set on creation and doesn't change on update.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() orig = t.creation_time self.assertIsNotNone(orig) t.alphanumeric = 'A' t.save() self.assertEqual(orig, t.creation_time)
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_put_append(self): """PUT sets a new field that wasn't set before.""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() data = {'type': 'STANDARD', 'description': 'Hello'} response = self.client.put(odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual('Hello', t.description)
def test_put_change_autonomous(self): """Change autonomous with PUT""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() data = {'autonomous': True} response = self.client.put( odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(True, t.autonomous)
def test_put_location(self): """PUT new location""" t = Odlc(user=self.user, odlc_type=OdlcType.standard) t.save() data = {'latitude': 38, 'longitude': -76} response = self.client.put( odlcs_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(38, t.location.latitude) self.assertEqual(-76, t.location.longitude)
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_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_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_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)