def test_RegisterForm_invalid_user(self): register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123', 'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response} register_form = RegisterForm(data=register_data) err_msg = "This value may contain only letters, numbers and @/./+/-/_ characters." self.assertEqual(register_form.errors['username'], [err_msg]) self.assertFalse(register_form.is_valid())
def register(request): """ Handle new user registration """ # Only allow users to access register page if they are not logged in if not request.user.is_anonymous: return redirect('user:profile', username=request.user.username) # Disallow access to this page if open signup is disabled if not hasattr(settings, 'ENABLE_OPEN_SIGNUP') or not settings.ENABLE_OPEN_SIGNUP: return redirect('accounts:login') # Handle form if request.method == 'POST' and 'register-submit' in request.POST: # Create form bound to request data form = RegisterForm(request.POST) # Validate the form if form.is_valid(): # Validate username and password using form methods username = form.clean_username() email = form.clean_email() # noqa: F841 password = form.clean_password2() # If no exceptions raised to here, then the username is unique and the passwords match. # Commit the new user to database form.save() # Authenticate the new user user = authenticate(request, username=username, password=password) if user is not None: # The password has been verified for the user if user.is_active: # The user is valid, active, and authenticated, so login in the user login(request, user) # Redirect after logged in using next parameter or default to user profile if 'next' in request.GET: return redirect(request.GET['next']) else: return redirect('user:profile', username=user.username) else: # The password is valid, but the user account has been disabled # Return a disabled account 'error' message messages.error(request, "Sorry, but your account has been disabled. Please contact the site " "administrator for more details.") else: # User was not authenticated, return errors messages.warning(request, "Whoops! We were not able to log you in. Please check your username and " "password and try again.") else: # Create new empty form form = RegisterForm() context = {'form': form} return render(request, 'tethys_portal/accounts/register.html', context)
def test_RegisterForm_clean_email(self): register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123', 'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response} register_form = RegisterForm(data=register_data) self.assertTrue(register_form.is_valid()) ret = register_form.clean_email() self.assertEqual('*****@*****.**', ret)
def test_RegisterForm_clean_username_dup(self): register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123', 'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response} register_form = RegisterForm(data=register_data) # validate form, false because duplicated user self.assertFalse(register_form.is_valid()) # user is duplicated so is_valid removed from cleaned_data, we add it back to test register_form.cleaned_data['username'] = '******' self.assertRaises(forms.ValidationError, register_form.clean_username)
def test_RegisterForm_clean_password2_diff(self): register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abcd123', 'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response} register_form = RegisterForm(data=register_data) # use is_valid to get cleaned_data attributes self.assertFalse(register_form.is_valid()) # is_valid removed cleaned_data password2, need to update register_form.cleaned_data['password2'] = 'abc123' self.assertRaises(forms.ValidationError, register_form.clean_password2)
def test_RegisterForm_clean_password2(self, mock_vp): register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123', 'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response} register_form = RegisterForm(data=register_data) # Check if form is valid and to generate cleaned_data self.assertTrue(register_form.is_valid()) ret = register_form.clean_password2() mock_vp.assert_called_with('abc123') self.assertEqual('abc123', ret)
def register(request): """ Handle new user registration """ # Only allow users to access register page if they are not logged in if not request.user.is_anonymous(): return redirect('user:profile', username=request.user.username) # Disallow access to this page if open signup is disabled if not hasattr(settings, 'ENABLE_OPEN_SIGNUP') or not settings.ENABLE_OPEN_SIGNUP: return redirect('accounts:login') # Handle form if request.method == 'POST' and 'register-submit' in request.POST: # Create form bound to request data form = RegisterForm(request.POST) # Validate the form if form.is_valid(): # Validate username and password using form methods username = form.clean_username() email = form.clean_email() password = form.clean_password2() # If no exceptions raised to here, then the username is unique and the passwords match. # Commit the new user to database form.save() # Authenticate the new user user = authenticate(username=username, password=password) if user is not None: # The password has been verified for the user if user.is_active: # The user is valid, active, and authenticated, so login in the user login(request, user) # Redirect after logged in using next parameter or default to user profile if 'next' in request.GET: return redirect(request.GET['next']) else: return redirect('user:profile', username=user.username) else: # The password is valid, but the user account has been disabled # Return a disabled account 'error' message messages.error(request, "Sorry, but your account has been disabled. Please contact the site " "administrator for more details." ) else: # User was not authenticated, return errors messages.warning(request, "Whoops! We were not able to log you in. Please check your username and " "password and try again." ) else: # Create new empty form form = RegisterForm() context = {'form': form} return render(request, 'tethys_portal/accounts/register.html', context)
def test_RegisterForm_clean_email_dup(self): register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123', 'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response} register_form = RegisterForm(data=register_data) register_form.is_valid() # is_valid is removing duplicated email self.assertNotIn('email', register_form.cleaned_data) # To test raise error, we need to put it back in to test register_form.cleaned_data['email'] = '*****@*****.**' self.assertRaises(forms.ValidationError, register_form.clean_email)
def test_RegisterForm_save(self): register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123', 'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response} register_form = RegisterForm(data=register_data) ret = register_form.save() # Also try to get from database after it's saved ret_database = User.objects.get(username='******') # Check result self.assertIsInstance(ret, User) self.assertIsInstance(ret_database, User) self.assertEqual('user1', ret.username) self.assertEqual('user1', ret_database.username)