Example #1
0
    def test_uploads(self):
        """Simply tests the backend functions in `lib.uploads`.

        Also tests `posts.get_upload` since this is only a simple wrapper
        around the backend function.

        """
        test_upload_dir = 'tests/upload_test_files/'
        test_upload_files = [
            join(test_upload_dir, f) for f in listdir(test_upload_dir)
            if isfile(join(test_upload_dir, f))
        ]

        # Create a GridFS object to test image deletion
        grid = gridfs.GridFS(m.db, collection='uploads')

        # Test each file in the upload directory
        for f in test_upload_files:
            # Don't read non image files in the directory
            _, ext = splitext(f)
            if ext not in ('.gif', '.jpg', '.jpeg', '.png'):
                continue

            image = io.BytesIO(
                open(f).read()
            )
            filename, animated = process_upload(image)

            # Get the upload these are designed for being served directly by
            # Flask. This is a Flask/Werkzeug response object
            image = get_upload(filename)
            self.assertTrue(grid.exists({'filename': filename}))
            self.assertEqual(image.headers['Content-Type'], 'image/png')

            if animated:
                image = get_upload(animated)
                self.assertTrue(grid.exists({'filename': animated}))
                self.assertEqual(image.headers['Content-Type'], 'image/gif')

            # Test deletion
            # Ensure file is present (it will be)
            self.assertTrue(grid.exists({'filename': filename}))
            # Delete the file and ensure it is not there through GridFS
            delete_upload(filename)
            # Ensure the file has gone
            self.assertFalse(grid.exists({'filename': filename}))

        # Ensure that if we load a non-image file a None value is returned
        image = io.BytesIO()
        self.assertEqual(process_upload(image), (None, None))
Example #2
0
def avatar(username):
    """Return the users avatar image or the dafault."""
    # Get the user
    user = get_user(get_uid_username(username))

    # If the user has an avatar set then get it from GridFS
    if user.get('avatar') is not None:
        return get_upload(user.get('avatar'), cache_for=0,
                          collection='avatars')

    # The user doesn't have one send them the default
    return send_file('static/img/otter_avatar.png', cache_timeout=0)
Example #3
0
    def test_uploads(self):
        """Simply tests the backend functions in `lib.uploads`.

        Also tests `posts.get_upload` since this is only a simple wrapper
        around the backend function.

        """
        test_upload_dir = 'tests/upload_test_files/'
        test_upload_files = [
            join(test_upload_dir, f) for f in listdir(test_upload_dir)
            if isfile(join(test_upload_dir, f))
        ]

        # Create a GridFS object to test image deletion
        grid = gridfs.GridFS(m.db, collection='uploads')

        # Test each file in the upload directory
        for f in test_upload_files:
            image = io.BytesIO(open(f).read())
            filename = process_upload(image)

            # Get the upload these are designed for being served directly by
            # Flask. This is a Flask/Werkzeug response object
            image = get_upload(filename)
            self.assertTrue(grid.exists({'filename': filename}))
            self.assertEqual(image.headers['Content-Type'], 'image/png')

            # Test deletion
            # Ensure file is present (it will be)
            self.assertTrue(grid.exists({'filename': filename}))
            # Delete the file and ensure it is not there through GridFS
            delete_upload(filename)
            # Ensure the file has gone
            self.assertFalse(grid.exists({'filename': filename}))

        # Ensure that if we load a non-image file a None value is returned
        image = io.BytesIO()
        self.assertIsNone(process_upload(image))