1

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='user@example.com')
    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='user@example.com')
    self.client.force_login(user)

    self.client.post(reverse('update_profile', args=('username',)), {
        'email': 'updated@example.com'
    })
    user.refresh_from_db()

    self.assertEqual(user.email, 'updated@example.com')

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.

horse
  • 479
  • 7
  • 25
  • `data: {'image': infile.read()}` –  Jul 23 '20 at 01:13
  • Thank you, however this still returns the same failure :s. I edited my code above – horse Jul 23 '20 at 01:26
  • Sorry it was late. Your approach should work, let me look in detail. OK, so you're not testing `response.ok()` which means it's possible you're being denied. Are newly created users automatically assigned the role 'registered_user'? –  Jul 23 '20 at 12:01

2 Answers2

1

user.profile.image' type is ImageFieldFile not str. So you can not compare them.

The correct way should be:

self.assertEqual(user.profile.image.name, 'profile_pics/default_test.jpg')
ishak O.
  • 168
  • 1
  • 2
  • 14
  • Thank you, that helped me with the next problem I was going to have, however my real issue is that the image isn't updated, the image is ```default.jpg``` and doesn't update to ```test_image.jpg``` after the test – horse Jul 23 '20 at 00:35
  • Is your endpoint works when you try manual? Can you share your view? – ishak O. Jul 23 '20 at 00:42
  • It works fine manually. I've actually been stuck on this for months. I repeatedly put in a few hrs, get nowhere, then give up and come back a few weeks later. I added my views.py. Thank you. – horse Jul 23 '20 at 00:48
  • I coudn't find the error. It's maybe about the file path. Using `os.path.join(MEDIA_ROOT, 'profile_pics', 'test_image.jpg')` will be better instead of concatenate strings. – ishak O. Jul 23 '20 at 01:54
  • Yeah, it's literally had me stumped for months. I will try your suggestion with the file path, though if I change it so it's wrong (```tet_img``` for example) it throws an error, so I dont think it's that. Thanks though – horse Jul 23 '20 at 02:04
0

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='user@example.com')
    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)
  • Thank you very much. This is very helpful as there is a lot here which I can learn from. My current user is a 'registered_user'. It turns out my other test on the same page (for email) returns a status code 302, while this test is still failing with a 400 error. I must have something somewhere else in my code that's causing the error. I'll go through it thoroughly tomorrow. Thanks again – horse Jul 23 '20 at 13:12