# Path: trumbowyg/forms.py
# class ImageForm(forms.Form):
#     image = forms.ImageField()


import json
import os
from PIL import Image
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.test.client import Client
from django.test.utils import setup_test_environment
from django.urls import reverse
from django.utils.six import BytesIO
from trumbowyg.forms import ImageForm
        setup_test_environment()    # This creates request context
        image_data = create_image('uploads/image.jpg')
        self.image_to_upload = SimpleUploadedFile(
            'image.jpg', image_data.getvalue())

    def test_upload_post(self):
        self.client.login(username='tmp', password='tmp')
        response = self.client.post(
            reverse('trumbowyg_upload_image'),
            {'image': self.image_to_upload},
            **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
        )
        self.assertEqual(response.status_code, 200)

        if type(response.content) == bytes:
            content = response.content.decode('utf-8')
        else:
            content = response.content
        self.assertJSONEqual(content, {
            'message': 'uploadSuccess',
            'file': 'uploads/thumb_image.jpg'
        })

        # unlink test file
        image_path = 'media/' + json.loads(content)['file']
        if os.path.isfile(image_path):
            os.unlink(image_path)

    def test_form(self):
        form_data = {'image': self.image_to_upload}

# Path: trumbowyg/forms.py
# class ImageForm(forms.Form):
#     image = forms.ImageField()


# Path: tests/tests.py
import json
import os
from PIL import Image
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.test.client import Client
from django.test.utils import setup_test_environment
from django.urls import reverse
from django.utils.six import BytesIO
from trumbowyg.forms import ImageForm

        setup_test_environment()    # This creates request context
        image_data = create_image('uploads/image.jpg')
        self.image_to_upload = SimpleUploadedFile(
            'image.jpg', image_data.getvalue())

    def test_upload_post(self):
        self.client.login(username='tmp', password='tmp')
        response = self.client.post(
            reverse('trumbowyg_upload_image'),
            {'image': self.image_to_upload},
            **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
        )
        self.assertEqual(response.status_code, 200)

        if type(response.content) == bytes:
            content = response.content.decode('utf-8')
        else:
            content = response.content
        self.assertJSONEqual(content, {
            'message': 'uploadSuccess',
            'file': 'uploads/thumb_image.jpg'
        })

        # unlink test file
        image_path = 'media/' + json.loads(content)['file']
        if os.path.isfile(image_path):
            os.unlink(image_path)

    def test_form(self):
        form_data = {'image': self.image_to_upload}

        form = ImageForm(files=form_data)