Example #1
0
    def test_content_encoding_gzip(self):
        kwargs = {'message': 'hello'}

        message = json.dumps(kwargs)

        fp = StringIO()

        try:
            f = GzipFile(fileobj=fp, mode='w')
            f.write(message)
        finally:
            f.close()

        key = self.projectkey.public_key
        secret = self.projectkey.secret_key

        resp = self.client.post(
            self.path, fp.getvalue(),
            content_type='application/octet-stream',
            HTTP_CONTENT_ENCODING='gzip',
            HTTP_X_SENTRY_AUTH=get_auth_header('_postWithHeader', key, secret),
        )

        assert resp.status_code == 200, resp.content

        event_id = json.loads(resp.content)['id']
        instance = Event.objects.get(event_id=event_id)

        assert instance.message == 'hello'
Example #2
0
    def test_content_encoding_gzip(self):
        kwargs = {"message": "hello"}

        message = json.dumps(kwargs)

        fp = StringIO()

        try:
            f = GzipFile(fileobj=fp, mode="w")
            f.write(message)
        finally:
            f.close()

        key = self.projectkey.public_key
        secret = self.projectkey.secret_key

        with self.tasks():
            resp = self.client.post(
                self.path,
                fp.getvalue(),
                content_type="application/octet-stream",
                HTTP_CONTENT_ENCODING="gzip",
                HTTP_X_SENTRY_AUTH=get_auth_header("_postWithHeader", key, secret),
            )

        assert resp.status_code == 200, resp.content

        event_id = json.loads(resp.content)["id"]
        instance = Event.objects.get(event_id=event_id)

        assert instance.message == "hello"
Example #3
0
    def test_content_encoding_gzip(self):
        kwargs = {'message': 'hello'}

        message = json.dumps(kwargs)

        fp = StringIO()

        try:
            f = GzipFile(fileobj=fp, mode='w')
            f.write(message)
        finally:
            f.close()

        key = self.projectkey.public_key
        secret = self.projectkey.secret_key

        resp = self.client.post(
            self.path,
            fp.getvalue(),
            content_type='application/octet-stream',
            HTTP_CONTENT_ENCODING='gzip',
            HTTP_X_SENTRY_AUTH=get_auth_header('_postWithHeader', key, secret),
        )

        assert resp.status_code == 200, resp.content

        event_id = json.loads(resp.content)['id']
        instance = Event.objects.get(event_id=event_id)

        assert instance.message == 'hello'
Example #4
0
    def put(self, request, user):
        if user != request.user:
            return Response(status=status.HTTP_403_FORBIDDEN)

        photo_string = request.DATA.get('avatar_photo')
        photo = None
        if photo_string:
            photo_string = photo_string.decode('base64')
            if len(photo_string) > settings.SENTRY_MAX_AVATAR_SIZE:
                return Response(
                    {'error': 'Image too large.'},
                    status=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE)
            try:
                with Image.open(StringIO(photo_string)) as img:
                    width, height = img.size
                    if not self.is_valid_size(width, height):
                        return Response({'error': 'Image invalid size.'},
                                        status=status.HTTP_400_BAD_REQUEST)
            except IOError:
                return Response({'error': 'Invalid image format.'},
                                status=status.HTTP_400_BAD_REQUEST)
            file_name = '%s.png' % user.id
            photo = File.objects.create(name=file_name, type=self.FILE_TYPE)
            photo.putfile(StringIO(photo_string))

        avatar, _ = UserAvatar.objects.get_or_create(user=user)
        if avatar.file and photo:
            avatar.file.delete()
            avatar.clear_cached_photos()
        if photo:
            avatar.file = photo
            avatar.ident = uuid4().hex

        avatar_type = request.DATA.get('avatar_type')

        if not avatar.file and avatar_type == 'upload':
            return Response(status=status.HTTP_400_BAD_REQUEST)

        if avatar_type:
            try:
                avatar.avatar_type = [
                    i for i, n in UserAvatar.AVATAR_TYPES if n == avatar_type
                ][0]
            except IndexError:
                return Response(status=status.HTTP_400_BAD_REQUEST)

        avatar.save()
        return Response(serialize(user, request.user))
Example #5
0
 def get_cached_photo(self, size):
     if not self.file:
         return
     if size not in self.ALLOWED_SIZES:
         size = min(self.ALLOWED_SIZES, key=lambda x: abs(x - size))
     cache_key = self.get_cache_key(size)
     photo = cache.get(cache_key)
     if photo is None:
         photo_file = self.file.getfile()
         with Image.open(photo_file) as image:
             image = image.resize((size, size))
             image_file = StringIO()
             image.save(image_file, "PNG")
             photo = image_file.getvalue()
             cache.set(cache_key, photo)
     return photo
Example #6
0
 def get_cached_photo(self, size):
     if not self.file:
         return
     if size not in self.ALLOWED_SIZES:
         size = min(self.ALLOWED_SIZES, key=lambda x: abs(x - size))
     cache_key = self.get_cache_key(size)
     photo = cache.get(cache_key)
     if photo is None:
         photo_file = self.file.getfile()
         with Image.open(photo_file) as image:
             image = image.resize((size, size))
             image_file = StringIO()
             image.save(image_file, 'PNG')
             photo = image_file.getvalue()
             cache.set(cache_key, photo)
     return photo
Example #7
0
 def test_headers(self):
     user = self.create_user(email='*****@*****.**')
     photo = File.objects.create(name='test.png', type='avatar.file')
     photo.putfile(StringIO('test'))
     avatar = UserAvatar.objects.create(user=user, file=photo)
     url = reverse('sentry-user-avatar-url',
                   kwargs={'avatar_id': avatar.ident})
     response = self.client.get(url)
     assert response.status_code == 200
     assert response['Cache-Control'] == FOREVER_CACHE
     assert response.get('Vary') is None
     assert response.get('Set-Cookie') is None
Example #8
0
 def decompress_gzip(self, encoded_data):
     try:
         fp = StringIO(encoded_data)
         try:
             f = GzipFile(fileobj=fp)
             return f.read()
         finally:
             f.close()
     except Exception as e:
         # This error should be caught as it suggests that there's a
         # bug somewhere in the client's code.
         self.log.info(unicode(e), exc_info=True)
         raise APIError('Bad data decoding request (%s, %s)' %
                        (type(e).__name__, e))
Example #9
0
def decompress_gzip(encoded_data):
    try:
        fp = StringIO(encoded_data)
        try:
            f = GzipFile(fileobj=fp)
            return f.read()
        finally:
            f.close()
    except Exception as e:
        # This error should be caught as it suggests that there's a
        # bug somewhere in the client's code.
        logger.info(e, **client_metadata(exception=e))
        raise APIForbidden('Bad data decoding request (%s, %s)' % (
            e.__class__.__name__, e))