class TestUserPasswordUpdateForm(TestCase): def setUp(self): random_name = str(uuid1()) email = random_name + '@m.ru' self.user = User(username=random_name, email=email) self.user.set_password('12345678') self.user.firstname = 'user firstname' self.user.lastname = 'user lastname' self.user.patronymic = 'user patronymic' self.user.birth_date = timezone.datetime(year=1986, month=4, day=10) self.user.save() def test_user_password_update_form(self): newpass = '******' update_form = UserPasswordUpdateForm({'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)) def test_user_password_update_form_when_passwords_are_differ(self): newpass = '******' update_form = UserPasswordUpdateForm({'password': newpass, 'password_again': newpass + "occasional symbols"}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Passwords mismatch') def test_user_password_update_form_when_password_is_short(self): newpass = '******' * 7 update_form = UserPasswordUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Password length must be at least 8 symbols') def test_user_password_update_form_when_password_is_too_large(self): newpass = '******' * (MAX_PAGE_SIZE + 1) update_form = UserPasswordUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!')
class TestUserUpdateForm(TestCase): def setUp(self): random_name = str(uuid1()) img_file, content_type = create_test_image() avatar = SimpleUploadedFile('myavatar.bmp', img_file.read(), content_type) self.user = User(username=random_name, email=random_name + '@m.ru', avatar=avatar) self.user.set_password('12345678') self.user.firstname = 'user firstname' self.user.lastname = 'user lastname' self.user.patronymic = 'user patronymic' self.user.birth_date = timezone.datetime(year=1986, month=4, day=10) self.user.save() 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) 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) def test_update_user_avatar_when_pic_is_too_large(self): img_file, content_type = create_test_image(3000) avatar = {'avatar': SimpleUploadedFile('newavatar.bmp', img_file.read(), content_type)} update_form = UserUpdateForm({}, avatar, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!') 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)) def test_update_user_password_when_passwords_are_differ(self): newpass = '******' update_form = UserUpdateForm({'password': newpass, 'password_again': newpass + "occasional symbols"}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Passwords mismatch') def test_update_user_password_when_password_is_short(self): newpass = '******' * 7 update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Password length must be at least 8 symbols') def test_update_user_password_when_password_is_too_large(self): newpass = '******' * (MAX_PAGE_SIZE + 1) update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!')
class TestUserProfileViews(TestCase): def setUp(self): random_name = str(uuid1()) self.user = User(username=random_name, email=random_name + '@m.ru', is_active=True) self.user.set_password('12345678') self.user.save() random_name = str(uuid1()) self.friend = User(username=random_name, email=random_name + '@m.ru', is_active=True) self.friend.set_password('12345678') self.friend.save() def test_login_view(self): client = Client() response = client.post(reverse('profile:login'), {'username': self.user.username, 'password': '******'}, follow=True) # check that user redirected to mainpage path, code = response.redirect_chain[0] self.assertEqual(path, '/') self.assertEqual(code, 302) # compare logged user with user in DB self.assertEqual(response.status_code, HTTP_OK) user_from_db = User.objects.get(pk=self.user.pk) self.assertEqual(user_from_db.id, response.context['user'].id) def test_signup_view(self): random_name = str(uuid1()) email_addr = random_name + '@m.ru' client = Client() response = client.post(reverse('profile:signup'), {'username': random_name, 'password': '******', 'password_again': '12345678', 'email': email_addr }, follow=True) self.assertEqual(response.status_code, HTTP_OK) self.assertIn(b'A confirmation code link has been sent to your email address.', response.content) # check email for activation code activation_mail = mail.outbox[-1] recipient_addr = activation_mail.to self.assertEqual(recipient_addr[0], email_addr) activation_link = re.search(r'\s(http[^\s]*)\s', activation_mail.body).group(1) activation_response = client.get(activation_link, follow=True) # check after activation self.assertEqual(activation_response.status_code, HTTP_OK) self.assertIn(b'<title>My page</title>', activation_response.content) def test_signup_view_when_confirmation_code_is_incorrect(self): random_name = str(uuid1()) email_addr = random_name + '@m.ru' client = Client() response = client.post(reverse('profile:signup'), {'username': random_name, 'password': '******', 'password_again': '12345678', 'email': email_addr }, follow=True) self.assertEqual(response.status_code, HTTP_OK) self.assertIn(b'A confirmation code link has been sent to your email address.', response.content) # check email for activation code activation_mail = mail.outbox[-1] recipient_addr = activation_mail.to self.assertEqual(recipient_addr[0], email_addr) activation_link = re.search(r'\s(http[^\s]*)\s', activation_mail.body).group(1) activation_response = client.get(activation_link + 'wrong') # check after activation self.assertEqual(activation_response.status_code, HTTP_NOT_FOUND) def test_profile_view_when_update_existing_user(self): client = Client() client.login(username=self.user.username, password='******') response = client.post(reverse('profile:my_profile'), {'firstname': 'My First Name', 'lastname': 'My Last Name', 'patronymic': 'My Patronymic', 'password': '******', 'password_again': '87654321', }) self.assertEqual(response.status_code, HTTP_OK) user_from_db = User.objects.get(username=self.user.username) self.assertEqual(user_from_db.firstname, 'My First Name') self.assertEqual(user_from_db.lastname, 'My Last Name') self.assertEqual(user_from_db.patronymic, 'My Patronymic') self.assertTrue(user_from_db.check_password('87654321')) def test_profile_remove_avatar(self): img_file, content_type = create_test_image() random_name = str(uuid1()) email_addr = random_name + '@m.ru' user = User(username=random_name, email=email_addr, avatar=SimpleUploadedFile('myimage.bmp', img_file.read(), content_type), is_active=True) user.set_password('12345678') user.save() # check before deletion self.assertTrue(os.path.exists(user.avatar.path)) client = Client() client.login(username=user.username, password='******') response = client.post(reverse('profile:my_profile'), {'action': 'remove_avatar'}) self.assertEqual(response.status_code, HTTP_OK) # check after deletion self.assertFalse(os.path.exists(user.avatar.path)) def test_password_reset(self): client = Client() response = client.post(reverse('profile:reset_password'), {'email': self.user.email}, follow=True) self.assertEqual(response.status_code, HTTP_OK) self.assertIn(b'We\'ve emailed you instructions for setting your password', response.content) # check email for reset code reset_request_mail = mail.outbox[-1] reset_link = re.search(r'\s(?:http|https):\/\/testserver([^\s]*)\s', reset_request_mail.body).group(1) reset_response = client.get(reset_link, follow=True) path, code = response.redirect_chain[0] # check after resetting password self.assertEqual(reset_response.status_code, HTTP_OK) response = client.post(reset_link, follow=True) self.assertNotContains(response, "The password reset link was invalid") # type new password upd_pass_link = response.redirect_chain[0][0] response = client.post(upd_pass_link, data={'password': '******', 'password_again': 'qwertyuio'}) self.user.refresh_from_db() self.assertFalse(self.user.check_password('12345678')) self.assertTrue(self.user.check_password('qwertyuio')) self.assertTrue(client.login(username=self.user.username, password='******'))