def test_json(self): """Test target JSON.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test target') t.save() d = t.json() self.assertIn('id', d) self.assertEqual(self.user.pk, d['user']) self.assertEqual('standard', d['type']) self.assertEqual(38, d['latitude']) self.assertEqual(-76, d['longitude']) self.assertEqual('s', d['orientation']) self.assertEqual('square', d['shape']) self.assertEqual('white', d['background_color']) self.assertEqual('ABC', d['alphanumeric']) self.assertEqual('black', d['alphanumeric_color']) self.assertEqual('Test target', d['description'])
def test_put_one(self): """PUT update one field without affecting others.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test target') t.save() data = {'shape': 'circle'} response = self.client.put( targets_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(TargetType.standard, t.target_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 target', t.description)
def test_put_one(self): """PUT update one field without affecting others.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target( user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test target') t.save() data = {'shape': 'circle'} response = self.client.put(targets_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(TargetType.standard, t.target_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 target', t.description)
def test_json(self): """Test target JSON.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test target', autonomous=True) t.save() d = t.json() self.assertIn('id', d) self.assertEqual(self.user.pk, d['user']) self.assertEqual('standard', d['type']) self.assertEqual(38, d['latitude']) self.assertEqual(-76, d['longitude']) self.assertEqual('s', d['orientation']) self.assertEqual('square', d['shape']) self.assertEqual('white', d['background_color']) self.assertEqual('ABC', d['alphanumeric']) self.assertEqual('black', d['alphanumeric_color']) self.assertEqual('Test target', d['description']) self.assertEqual(True, d['autonomous'])
def test_put_review_no_approved(self): """Test PUT review with no approved field.""" target = Target(user=self.team, target_type=TargetType.qrc) target.save() response = self.client.put(targets_review_id_url(args=[target.pk])) self.assertEqual(400, response.status_code)
def test_delete_other(self): """Test DELETEing a target owned by another user.""" user2 = User.objects.create_user('testuser2', '*****@*****.**', 'testpass') t = Target(user=user2, target_type=TargetType.standard) t.save() response = self.client.delete(targets_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 = Target(user=user2, target_type=TargetType.standard) t.save() response = self.client.get(targets_id_image_url(args=[t.pk])) self.assertEqual(403, response.status_code)
def test_get_own(self): """Test GETting a target owned by the correct user.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() response = self.client.get(targets_id_url(args=[t.pk])) self.assertEqual(200, response.status_code) self.assertEqual(t.json(), json.loads(response.content))
def test_put_location_missing_one(self): """PUTting new location requires both latitude and longitude.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'latitude': 38} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(400, response.status_code)
def test_put_invalid_json(self): """PUT request body must be valid JSON.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l) t.save() response = self.client.put(targets_id_url(args=[t.pk]), data="latitude=76", content_type='multipart/form-data') self.assertEqual(400, response.status_code)
def test_put_clear_type(self): """PUT type may not be cleared.""" t = Target(user=self.user, target_type=TargetType.standard, shape=Shape.square) t.save() data = {'type': None} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(400, response.status_code)
def test_last_modified_time(self): """Last modified time is set on creation and changes every update.""" t = Target(user=self.user, target_type=TargetType.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_creation_time(self): """Creation time is set on creation and doesn't change on update.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() orig = t.creation_time self.assertIsNotNone(orig) t.alphanumeric = 'A' t.save() self.assertEqual(orig, t.creation_time)
def test_get_after_delete_own(self): """Test GETting a target after DELETE.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() pk = t.pk response = self.client.delete(targets_id_url(args=[pk])) self.assertEqual(200, response.status_code) response = self.client.get(targets_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 = Target(user=self.user, target_type=TargetType.standard, location=l) t.save() data = {'latitude': None} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(400, response.status_code)
def test_put_invalid_json(self): """PUT request body must be valid JSON.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l) t.save() response = self.client.put( targets_id_url(args=[t.pk]), data="latitude=76", content_type='multipart/form-data') self.assertEqual(400, response.status_code)
def test_put_change_autonomous(self): """Change autonomous with PUT""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'autonomous': True} response = self.client.put(targets_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_delete_own(self): """Test DELETEing a target owned by the correct user.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() pk = t.pk self.assertTrue(Target.objects.get(pk=pk)) response = self.client.delete(targets_id_url(args=[pk])) self.assertEqual(200, response.status_code) with self.assertRaises(Target.DoesNotExist): Target.objects.get(pk=pk)
def test_put_location(self): """PUT new location""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'latitude': 38, 'longitude': -76} response = self.client.put(targets_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_targets(self): """We get back the targets we own.""" t1 = Target(user=self.user, target_type=TargetType.standard) t1.save() t2 = Target(user=self.user, target_type=TargetType.qrc) t2.save() response = self.client.get(targets_url) self.assertEqual(200, response.status_code) d = json.loads(response.content) self.assertItemsEqual([t1.json(), t2.json()], d)
def test_put_change_autonomous(self): """Change autonomous with PUT""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'autonomous': True} response = self.client.put( targets_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_clear_shape(self): """PUT clear a field with None.""" t = Target(user=self.user, target_type=TargetType.standard, shape=Shape.square) t.save() data = {'shape': None} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(None, t.shape)
def test_get_noneditable_without_thumbnail_targets(self): """Test GET when there are non-editable targets 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() target = Target(user=self.team, target_type=TargetType.qrc) target.save() response = self.client.get(targets_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(0, len(data))
def test_put_clear_location(self): """PUT clear location by clearing lat and lon.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l) t.save() data = {'latitude': None, 'longitude': None} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual(None, t.location)
def test_put_append(self): """PUT sets a new field that wasn't set before.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() data = {'description': 'Hello'} response = self.client.put(targets_id_url(args=[t.pk]), data=json.dumps(data)) self.assertEqual(200, response.status_code) t.refresh_from_db() self.assertEqual('Hello', t.description) # Response also matches self.assertEqual(t.json(), json.loads(response.content))
def test_not_others(self): """We don't get targets owned by other users.""" user2 = User.objects.create_user('testuser2', '*****@*****.**', 'testpass') mine = Target(user=self.user, target_type=TargetType.standard) mine.save() theirs = Target(user=user2, target_type=TargetType.qrc) theirs.save() response = self.client.get(targets_url) self.assertEqual(200, response.status_code) d = json.loads(response.content) self.assertItemsEqual([mine.json()], d)
def post(self, request): try: data = json.loads(request.body) except ValueError: return HttpResponseBadRequest('Request body is not valid JSON.') # Target type is required. if 'type' not in data: return HttpResponseBadRequest('Target type required.') latitude = data.get('latitude') longitude = data.get('longitude') # Require zero or both of latitude and longitude. if (latitude is not None and longitude is None) or \ (latitude is None and longitude is not None): return HttpResponseBadRequest( 'Either none or both of latitude and longitude required.') try: data = normalize_data(data) except ValueError as e: return HttpResponseBadRequest(str(e)) l = None if latitude is not None and longitude is not None: l = GpsPosition(latitude=data['latitude'], longitude=data['longitude']) l.save() # Use the dictionary get() method to default non-existent values to None. t = Target(user=request.user, target_type=data['type'], location=l, orientation=data.get('orientation'), shape=data.get('shape'), background_color=data.get('background_color'), alphanumeric=data.get('alphanumeric', ''), alphanumeric_color=data.get('alphanumeric_color'), description=data.get('description', ''), autonomous=data.get('autonomous', False)) t.save() return JsonResponse( t.json(is_superuser=request.user.is_superuser), status=201)
def test_put_review(self): """Test PUT review is saved.""" target = Target(user=self.team, target_type=TargetType.qrc) target.save() response = self.client.put( targets_review_id_url(args=[target.pk]), data=json.dumps({'thumbnail_approved': False})) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertIn('id', data) self.assertEqual(target.pk, data['id']) self.assertIn('thumbnail_approved', data) self.assertFalse(data['thumbnail_approved']) target.refresh_from_db() self.assertFalse(target.thumbnail_approved)
def post(self, request): try: data = json.loads(request.body) except ValueError: return HttpResponseBadRequest('Request body is not valid JSON.') # Target type is required. if 'type' not in data: return HttpResponseBadRequest('Target type required.') latitude = data.get('latitude') longitude = data.get('longitude') # Require zero or both of latitude and longitude. if (latitude is not None and longitude is None) or \ (latitude is None and longitude is not None): return HttpResponseBadRequest( 'Either none or both of latitude and longitude required.') try: data = normalize_data(data) except ValueError as e: return HttpResponseBadRequest(str(e)) l = None if latitude is not None and longitude is not None: l = GpsPosition(latitude=data['latitude'], longitude=data['longitude']) l.save() # Use the dictionary get() method to default non-existent values to None. t = Target(user=request.user, target_type=data['type'], location=l, orientation=data.get('orientation'), shape=data.get('shape'), background_color=data.get('background_color'), alphanumeric=data.get('alphanumeric', ''), alphanumeric_color=data.get('alphanumeric_color'), description=data.get('description', ''), autonomous=data.get('autonomous', False)) t.save() return JsonResponse(t.json(is_superuser=request.user.is_superuser), status=201)
def test_minimal_json(self): """Test target JSON with minimal data.""" t = Target(user=self.user, target_type=TargetType.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'])
def test_put_update_location(self): """PUT updating location only requires one of lat/lon.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target(user=self.user, target_type=TargetType.standard, location=l) t.save() data = {'latitude': 39} response = self.client.put(targets_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(39, t.location.latitude) self.assertEqual(-76, t.location.longitude)
def test_json(self): """Test target JSON.""" l = GpsPosition(latitude=38, longitude=-76) l.save() t = Target( user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric="ABC", alphanumeric_color=Color.black, description="Test target", autonomous=True, ) t.save() d = t.json() self.assertIn("id", d) self.assertEqual(self.user.pk, d["user"]) self.assertEqual("standard", d["type"]) self.assertEqual(38, d["latitude"]) self.assertEqual(-76, d["longitude"]) self.assertEqual("s", d["orientation"]) self.assertEqual("square", d["shape"]) self.assertEqual("white", d["background_color"]) self.assertEqual("ABC", d["alphanumeric"]) self.assertEqual("black", d["alphanumeric_color"]) self.assertEqual("Test target", d["description"]) self.assertEqual(True, d["autonomous"]) self.assertNotIn("thumbnail_approved", d) d = t.json(is_superuser=True) self.assertIn("thumbnail_approved", d) t.thumbnail_approved = True t.save() d = t.json(is_superuser=True) self.assertEqual(None, d["thumbnail"]) self.assertEqual(True, d["thumbnail_approved"])
def test_minimal_json(self): """Test target JSON with minimal data.""" t = Target(user=self.user, target_type=TargetType.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_minimal_json(self): """Test target JSON with minimal data.""" t = Target(user=self.user, target_type=TargetType.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_valid(self): """Test creating a valid target.""" 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 = Target(user=self.user, target_type=TargetType.standard, location=l, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Test target', thumbnail=thumb) t.save()
def test_get_noneditable_targets(self): """Test GET when there are non-editable targets.""" 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() target = Target(user=self.team, target_type=TargetType.qrc) target.save() with open(test_image('A.jpg')) as f: target.thumbnail.save('%d.%s' % (target.pk, 'jpg'), ImageFile(f)) target.save() response = self.client.get(targets_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(1, len(data)) self.assertIn('type', data[0]) self.assertEqual('qrc', data[0]['type'])
def test_delete_thumb(self): """Test DELETEing a target with thumbnail.""" t = Target(user=self.user, target_type=TargetType.standard) t.save() pk = t.pk with open(test_image("A.jpg")) as f: response = self.client.post(targets_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.name self.assertTrue(os.path.exists(absolute_media_path(thumb))) response = self.client.delete(targets_id_url(args=[pk])) self.assertEqual(200, response.status_code) self.assertFalse(os.path.exists(absolute_media_path(thumb)))
def test_null_fields(self): """Only user and target type.""" t = Target(user=self.user, target_type=TargetType.standard) t.save()