Search code examples
djangoseleniumdjango-unittest

Test Image Upload Always Fails


I cannot get any of my image upload unit tests to work. The following test fails with AssertionError: <ImageFieldFile: profile_pics/default.jpg> != 'test_image.jpg' (Ignoring the fact that my test would fail regardless because it's comparing an ImageFieldFile to my string, more importantly the image doesn't update. I'll fix the assertion later).

def test_upload_image_works(self):
    user = User.objects.create(username='testuser', email='[email protected]')
    self.client.force_login(user)

    with open(settings.MEDIA_ROOT+'\profile_pics\\default_test.jpg', 'rb') as infile:
        self.client.post(
            reverse('update_profile', args=('testuser',)),
            content_type='multipart/form-data',
            #data = {'image': infile},
            data={'image': infile.read()},
            follow=True
        )
    user.refresh_from_db()

    self.assertEqual(user.profile.image, 'default_test.jpg')

However this similar test passes

def test_info_update_works(self):
    user = User.objects.create(username='username', email='[email protected]')
    self.client.force_login(user)

    self.client.post(reverse('update_profile', args=('username',)), {
        'email': '[email protected]'
    })
    user.refresh_from_db()

    self.assertEqual(user.email, '[email protected]')

EDIT:

@login_required
@allowed_users(allowed_roles=['admin', 'registered_user'])
def update_profile(request, username):

    # refuse access if logged in user does not match username
    if not request.user == User.objects.get(username=username):
        response = HttpResponse()
        response.status_code = 403
        return response

    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)

        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated')
           
            return redirect('update_profile', username=User.objects.get(username=username))
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)
    
    context = {
        'u_form' : u_form,
        'p_form' : p_form
    }

    return render(request, 'users/update_profile.html', context)

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='profile_pics/default.jpg', upload_to='profile_pics')

Thank you.


Solution

  • So this is how I would write the test, so you don't have to debug the test when it's failing:

    import os
    
    def test_upload_image_works(self):
        user = User.objects.create(username='testuser', email='[email protected]')
        self.client.force_login(user)
        self.assertTrue(user.is_authenticated)
        # This should be whatever you have implemented to check if a user
        # has a role.
        self.assertTrue(user.wears_hat('registered_user'))
        # Avoid uploading the file to itself and use an image in a subdirectory of the
        # test. This also allows it to be in the repository so CI pipelines and
        # colleagues do not need special steps.
        test_dir = os.path.join(os.path.dirname(__file__), 'data')
        test_img_path = os.path.join(test_dir, 'default_test.jpg')
        expected_path = os.path.join(settings.MEDIA_ROOT, 'profile_pics', 'default_test.jpg')
        self.assertTrue(os.path.exists(test_img_path))
    
        with open(test_img_path, 'rb') as infile:
            r = self.client.post(
                reverse('update_profile', args=('testuser',)),
                content_type='multipart/form-data',
                data = {'image': infile},
                follow=True
            )
            self.assertTrue(r.ok)
        user.refresh_from_db()
    
        self.assertEqual(user.profile.image.path, expected_path)