Exemple #1
0
    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))
Exemple #2
0
    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))
Exemple #3
0
    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)
Exemple #4
0
    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")
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    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")
Exemple #8
0
    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)
Exemple #9
0
    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)
Exemple #10
0
    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)
Exemple #11
0
    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))
Exemple #12
0
    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))
Exemple #13
0
    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)
Exemple #14
0
    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)
Exemple #15
0
    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)
Exemple #16
0
    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)
Exemple #17
0
    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)
Exemple #18
0
    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))
Exemple #19
0
    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)
Exemple #20
0
    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)
Exemple #21
0
    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)
Exemple #22
0
    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)
Exemple #23
0
    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)
Exemple #24
0
    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)
Exemple #25
0
    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)
Exemple #26
0
    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)
Exemple #27
0
    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)
Exemple #28
0
    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)
Exemple #29
0
    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))
Exemple #30
0
    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)