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_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_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_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_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_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 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_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"])