Best Python code snippet using pytest-django_python
test_forms.py
Source:test_forms.py
...25 def test_login_form_raises_locked(self):26 user = self.initialize_user(is_active=False)27 form = LoginForm(data={'username': 'test_user', 'password': 'rrrr'})28 self.assertFalse(form.is_valid())29 self.assertWarnsMessage('Your account is locked out - please contact support.', form.errors.as_json())30 def test_login_form_raises_email_unverified(self):31 self.initialize_user(email_verified=False)32 form = LoginForm(data={'username': 'test_user', 'password': 'rrrr'})33 self.assertFalse(form.is_valid())34 self.assertWarnsMessage('Please verify your email', form.errors.as_json())35 def test_login_invalid(self):36 self.initialize_user()37 form = LoginForm(data={'username': 'test_user', 'password': 'rrr'})38 self.assertFalse(form.is_valid())39 self.assertWarnsMessage('Sorry, that login was invalid. Please try again.', form.errors.as_json())40class TestPhoneActivationForms(AccountsTestCase):41 def setUp(self) -> None:42 self.client = Client(enforce_csrf_checks=False)43 self.rf = RequestFactory()44 def set_up_for_phone_verification(self):45 user = self.initialize_user(phone='+5571981265131')46 self.client.force_login(user)47 request = self.rf.post(reverse('auth:verify_phone'), data={'phone': '+5571981265131'}, follow=True)48 self.process_requests(request)49 response = verify_phone(request)50 response.client = self.client51 return request, user, response52 # @override_settings(TWILIO_PHONE_NUMBER='+15005550006')53 def test_phone_verification_form_pass(self):54 request, user, response = self.set_up_for_phone_verification()55 form = PhoneVerificationForm(data=request.POST)56 self.assertTrue(form.is_valid())57 self.assertRedirects(response, reverse('auth:activate_phone'))58 user = User.objects.get(phone=form.cleaned_data['phone'])59 self.assertTrue(user.profile.temp_token != '')60 session_phone = request.session.get('user_phone')61 self.assertEquals(user.phone, session_phone)62 self.assertTrue(len(user.profile.temp_token), 4)63 def test_phone_verification_form_fails(self):64 form = PhoneVerificationForm(data={'phone': '+16469061922'})65 self.assertFalse(form.is_valid())66 def test_phone_verification_form_valid(self):67 user = self.initialize_user(phone='+5571981265131')68 self.client.force_login(user)69 user.profile.temp_token = 123470 user.save()71 request = self.rf.post(reverse('auth:activate_phone'),72 data={'token': 1234}, follow=True)73 self.process_requests(request)74 request.session['user_phone'] = str(user.__dict__['phone']) # force valid75 response = activate_phone(request)76 response.client = self.client77 self.assertRedirects(response, reverse('auth:profile'))78 def test_phone_verification_form_invalid_token(self):79 user = self.initialize_user(phone='+5571981265131')80 self.client.force_login(user)81 user.profile.temp_token = 123482 user.save()83 request = self.rf.post(reverse('auth:activate_phone'),84 data={'token': 1111}, follow=True)85 self.process_requests(request)86 request.session['user_phone'] = str(user.__dict__['phone']) # force valid87 response = activate_phone(request)88 response.client = self.client89 self.assertWarnsMessage('Invalid token', response.content.decode('utf-8'))90 def test_phone_verification_form_expired_token(self):91 user = self.initialize_user(phone='+5571981265131')92 self.client.force_login(user)93 user.profile.temp_token = 123494 user.save()95 request = self.rf.post(reverse('auth:activate_phone'),96 data={'token': 1234}, follow=True)97 self.process_requests(request)98 response = activate_phone(request)99 response.client = self.client100 self.assertWarnsMessage('Expired request', response.content.decode('utf-8'))101 def test_phone_verification_form_invalid_request(self):102 user = self.initialize_user(phone='+5571981265131')103 self.client.force_login(user)104 user.profile.temp_token = 1234105 user.save()106 request = self.rf.post(reverse('auth:activate_phone'),107 data={'tok': 1234}, follow=True)108 self.process_requests(request)109 request.user = user110 request.session['user_phone'] = str(user.__dict__['phone']) # force valid111 response = activate_phone(request) # first request112 self.assertWarnsMessage('Invalid request, please request another code', response.content.decode('utf-8'))113 def test_phone_verification_form_for_non_existed_user(self):114 user = self.initialize_user(phone='+18704945574')115 self.client.force_login(user)116 form = PhoneVerificationForm(data={'phone': user.phone})117 response = self.client.post(reverse('auth:verify_phone'), data={'phone': '+18704945566'}, follow=True)118 self.assertContains(response, form.errors)119 def test_phone_verification_get(self):120 user = self.initialize_user(phone='+18704945574')121 response = self.client.get(reverse('auth:verify_phone'))122 self.assertIsInstance(response.context['form'], PhoneVerificationForm)123 self.client.force_login(user)124 response = self.client.get(reverse('auth:verify_phone'))125 self.assertIsInstance(response.context['form'], PhoneVerificationForm)126 def test_verificator_raises(self):...
tests.py
Source:tests.py
1from django.test import TestCase2"""3https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.TestCase.assertWarnsMessage4assertWarnsMessage(expected_warning, expected_message, callable, *args, **kwargs)5assertWarnsMessage(expected_warning, expected_message)6"""7class Test(TestCase):8 def test(self):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!