def test_username_already_in_db(self): """test form with username already in DB""" data = {'username':'******', 'email':'*****@*****.**', 'password1':'t3stpassword', 'password2':'t3stpassword'} form = RegistrationForm(data) self.assertFalse(form.is_valid()) self.assertRaises(ValidationError)
def test_email_already_in_db(self): """test form with email already in DB""" self.assertTrue(User.objects.filter(email='*****@*****.**')) data = {'username':'******', 'email':'*****@*****.**', 'password1':'t3stpassword', 'password2':'t3stpassword'} form = RegistrationForm(data) self.assertFalse(form.is_valid()) self.assertRaises(ValidationError)
def test_blank_data(self): """test form with empty field""" form = RegistrationForm({}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, { 'username': ['Ce champ est obligatoire.'], 'email': ['Ce champ est obligatoire.'], 'password1': ['Ce champ est obligatoire.'], 'password2': ['Ce champ est obligatoire.'], })
def test_valid_data(self): """test succes form""" data = {'username':'******', 'email':'*****@*****.**', 'password1':'t3stpassword', 'password2':'t3stpassword'} form = RegistrationForm(data) self.assertTrue(form.is_valid()) user = form.save() self.assertEqual(user.username, 'brandnewuser') self.assertEqual(user.email, "*****@*****.**") self.assertEqual(user.check_password('t3stpassword'), True) self.assertTrue(user.is_authenticated) self.assertTrue(User.objects.filter(username='******').exists())
def register_view(request): """Registration view creating a user""" form = RegistrationForm() if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) return redirect('QuitSoonApp:profile') return render(request, 'registration/register.html', {'form': form})