Search code examples
pythondjangodjango-testing

How can I post data into test Client post request?


I'm writing a test to see if form data is validating on post request trying to create a Post object.

tests.py

def setUp(self):
    user = User.objects.create_user(email='[email protected]', password='test', name='test')
    self.user = User.objects.get(email='[email protected]')
    self.client.login(email=user.email, password=user.password)

@tag('fast')
def test_index(self):
    client = Client()
    response = client.post(reverse('index'), data={'user': self.user, 'body': 'Test'}, follow=True)
    print(response, self.user)
    self.assertTrue(Post.objects.filter(body='Test').exists())

but the test fails with message False is not true implying that the object with body "Test" was not created. I already tried encoding data with urlencode but it did not help.

Here is what print statement shows: <TemplateResponse status_code=200, "text/html; charset=utf-8"> test

views.py

def index(request):
    posts = Post.objects.all()
    if request.method == 'POST':
        form = NewPostForm(request.POST, request.FILES)
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.user = user
            new_post.save()
            return redirect('index')
    else:
        form = NewPostForm(instance=None)
    context = {
        'form': form,
        'posts': posts
    }

    return render(request, 'index.html', context=context)

Maybe the user isn't being serialized correctly but when I remove it from data attribute (which would mean that the current user should be assigned to Post object) I get basically the same result.


Solution

  • In the end I decided to just test it for valid response and leave the data validation part to the form validation test, here's what it looks like now:

    def test_index(self):
        client = Client()
        get_response = client.get('/home/', {}, True)
        post_response = client.post(reverse('index'), data={'body': 'Test'}, follow=True)
        self.assertEqual(get_response.status_code, 200)
        self.assertEqual(post_response.status_code, 200)
    

    If anyone has any suggestions I would love to see them though because my particaular qestion problem still exists.