Beispiel #1
0
 def test_update_user_avatar(self):
     img_file, content_type = create_test_image(100)
     avatar = {'avatar': SimpleUploadedFile('newavatar.bmp', img_file.read(), content_type)}
     update_form = UserUpdateForm({}, avatar, instance=self.user)
     self.assertTrue(update_form.is_valid())
     update_form.save()
     self.assertEqual(avatar['avatar'].size, update_form.cleaned_data['avatar'].size)
     self.assertEqual('newavatar.bmp', update_form.cleaned_data['avatar'].name)
Beispiel #2
0
 def test_update_user_password(self):
     newpass = '******'
     update_form = UserUpdateForm({'password': newpass,
                                   'password_again': newpass},
                                  instance=self.user)
     self.assertTrue(update_form.is_valid())
     update_form.save()
     self.user.refresh_from_db()
     self.assertTrue(self.user.check_password(newpass))
Beispiel #3
0
    def test_update_basic_user_text_data(self):
        newfirstname = 'new firstname'
        newlastname = 'new lastname'
        newpatronymic = 'new patronymic'
        newbirth_date = '10.04.1986'

        oldpass = self.user.password

        update_form = UserUpdateForm({'firstname': newfirstname,
                                      'lastname': newlastname,
                                      'patronymic': newpatronymic,
                                      'birth_date': newbirth_date},
                                     instance=self.user)
        update_form.is_valid()
        self.assertTrue(update_form.is_valid())
        update_form.save()
        self.user.refresh_from_db()
        self.assertEqual(self.user.firstname, newfirstname)
        self.assertEqual(self.user.lastname, newlastname)
        self.assertEqual(self.user.patronymic, newpatronymic)
        self.assertEqual(self.user.password, oldpass)
        self.assertEqual(timezone.datetime.strftime(self.user.birth_date, DATE_FORMAT), newbirth_date)
Beispiel #4
0
def update_user_by_form(request, context):
    user = context['user']
    valid = False
    user_update_form = UserUpdateForm(request.POST,
                                      request.FILES,
                                      instance=user)
    if user_update_form.is_valid():
        user_update = user_update_form.save(commit=False)
        if user_update.avatar:
            user_update.avatar_size = user_update.avatar.size
        user_update.save()
        context['user'] = user_update
        valid = True
    else:
        # This is strange but user instance is changed after form validation
        context['user'].refresh_from_db()
    return user_update_form, valid