5👍
✅
You may need to convert the file into uploaded file format before posting to the url,
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.files import File
class HouseImageTest(APITestCase):
def setUp(self):
self.client = APIClient()
self.user = mommy.make(User, username='Pan')
self.house = mommy.make(House, location="100.00, 100.00")
settings.MEDIA_ROOT = tempfile.mkdtemp()
def test_post_image(self):
image = Image.new('RGB', (100, 100))
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
image.save(tmp_file)
file = File(tmp_file)
uploaded_file = SimpleUploadedFile('new_image.jpg', file.read(), content_type='multipart/form-data')
data = {
'image': uploaded_file,
'houses': self.house.id,
}
self.client.force_authenticate(user=self.user)
response = self.client.post(reverse('api:house_images-list'), data, format='multipart')
response.render()
self.assertEqual(status.HTTP_201_CREATED, response.status_code)
Source:stackexchange.com