How to load a unit test in Django REST Framework
I just started with Django REST Framework and I am trying to test a simple file upload point.
I wrote barebones code like this:
models.py
class Picture(models.Model):
some_field = models.ForeignKey(SomeModel)
image = models.ImageField()
views.py
class PhotoUpload(APIView):
def post(self, request, filename, format='multipart'):
image_file = request.FILES['file']
print(image_file)
return Response(status=204)
urls.py
url(r'^api/photos/', PhotoUpload.as_view()),
Looking through the docs, I wrote an example test case like this:
tests.py
class PhotoUploadTests(TestCase):
def setUp(self):
self.oauth_header = _get_oauth2_header()
def test_photo_upload(self):
c = Client()
response = c.get('/api/photos/', **self.oauth_header)
However, I am stuck here and I have no idea how to do this.
I have also tried using curl like this:
curl --form image=@test.jpg http://localhost:8000/api/photos
but it gives me
"Authentication credentials were not provided."
... How do I check the endpoint?
+3
source to share
1 answer
Here's an example of testing when you need to login (in which case it uses the superuser):
# available to those with superuser permissions
self.superuser = User.objects.create_superuser(
username='superuser',
password='password'
)
self.client.login(username='superuser', password='password')
url = reverse('myurl', kwargs={})
request = self.client.get(url, format='json')
0
source to share