def testEmptyUsername(self):
		"""
		Test that you can't create user with empty username; must fill out username field
		"""
		form_data = {'username': '', 'email': '*****@*****.**', 'password1':'pw1', 'password2': 'pw1'}
		form = UserSignUpForm(data=form_data)

		self.assertFalse(form.is_valid())

		self.assertEqual(['username'], form.errors.keys())
		self.assertTrue('This field is required.' in form.errors.get('username'))
	def testEmptyPassword1(self):
		"""
		Test that you can't create user with empty password; must fill out password field
		"""
		try:
			if User.objects.get(username="******"):
				User.objects.get(username="******").delete()
		except:
			pass

		form_data = {'username': '******', 'email': '*****@*****.**', 'password1':'', 'password2': 'pw1'}
		form = UserSignUpForm(data=form_data)

		self.assertFalse(form.is_valid())

		self.assertEqual(['password1'], form.errors.keys())
		self.assertTrue('This field is required.' in form.errors.get('password1'))
	def testUnmatchedPasswords(self):
		"""
		Tests that password (password1) and confirm password (password2) should error if not the same
		"""
		try:
			if User.objects.get(username="******"):
				User.objects.get(username="******").delete()
		except:
			pass

		form_data = {'username': '******', 'email': '*****@*****.**', 'password1':'pw1', 'password2': 'pw2'}
		form = UserSignUpForm(data=form_data)

		self.assertFalse(form.is_valid())

		self.assertEqual(['password2'], form.errors.keys())
		self.assertTrue("The two password fields didn't match." in form.errors.get('password2'))
	def testInvalidEmail2(self):
		"""
		Test that email must be valid email that needs to include a domain after "@" sign; if not, error
		"""
		try:
			if User.objects.get(username="******"):
				User.objects.get(username="******").delete()
		except:
			pass

		form_data = {'username': '******', 'email': 'emailtest@', 'password1':'pw1', 'password2': 'pw1'}
		form = UserSignUpForm(data=form_data)

		self.assertFalse(form.is_valid())

		self.assertEqual(['email'], form.errors.keys())
		self.assertTrue('Enter a valid email address.' in form.errors.get('email'))
Beispiel #5
0
def userSignUp(request):
    if request.method == 'POST':
        form = UserSignUpForm(request.POST)
        if form.is_valid():
            # return successful registration, should login user and redirect to club directory
            user = form.save()
            auth.login(request, user)
            return redirect("/clubs")
        else:
            # find out which fields were invalid and return error
            return userFormsRender(request, signUpForm=form)

    else:
        if (request.user.is_authenticated()):
            # if user is already authenticated then redirect to club directory
            return redirect("/clubs")
        else:
            return userFormsRender(request)
	def testNewUser(self):
		try:
			if User.objects.get(username="******"):
				User.objects.get(username="******").delete()
		except:
			pass

		form_data = {'username': '******', 'email': '*****@*****.**', 'password1':'pw1', 'password2': 'pw1'}
		form = UserSignUpForm(data=form_data)

		self.assertTrue(form.is_valid())

		form.save()

		self.assertTrue(User.objects.get(username="******") in User.objects.all())

		u = User.objects.get(username="******")
		self.assertEquals(u.username, "newuser")
		self.assertEquals(u.email, "*****@*****.**")
	def testDuplicateUsername(self):
		"""
		Test that creating a user with an existing username errors
		"""
		try:
			if User.objects.get(username="******"):
				User.objects.get(username="******").delete()
		except:
			pass

		u = User(username="******", email="*****@*****.**", password='******')
		u.save()

		form_data = {'username': '******', 'email': '*****@*****.**', 'password1':'pw2', 'password2': 'pw2'}
		form = UserSignUpForm(data=form_data)

		self.assertFalse(form.is_valid())

		#check errors
		self.assertEqual(['username'], form.errors.keys())
		self.assertTrue('A user with that username already exists.' in form.errors.get('username'))