Esempio n. 1
0
    def test_utilities_no_user_or_username(self):
        mock_request = mock.MagicMock()
        mock_request.method = 'POST'
        mock_request.POST = 'login-submit'
        mock_request.user.is_anonymous = True
        mock_request.user.username = '******'
        mock_request.GET = ''

        with self.assertRaises(ValueError) as context:
            utilities.log_user_in(mock_request)

        self.assertTrue('You must provide either the "user" or the "username" arguments.' in str(context.exception))
Esempio n. 2
0
    def test_utilities_no_user_username_does_not_exist(self, mock_redirect):
        mock_request = mock.MagicMock()
        mock_request.method = 'POST'
        mock_request.POST = 'login-submit'
        mock_request.user.is_anonymous = True
        mock_request.user.username = '******'
        mock_request.GET = ''

        utilities.log_user_in(mock_request, username='******')

        # mock redirect after logged in using next parameter or default to user profile
        mock_redirect.assert_called_once_with('accounts:login')
Esempio n. 3
0
def login_view(request):
    """
    Handle login
    """
    # Only allow users to access login page if they are not logged in
    if not request.user.is_anonymous:
        return redirect('user:profile', username=request.user.username)

    # Handle form
    if request.method == 'POST' and 'login-submit' in request.POST:
        # Create login form bound to request data
        form = LoginForm(request.POST)

        # Validate the form
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            # Authenticate
            user = authenticate(request, username=username, password=password)

            # If not authenticated, user will be None
            if user is not None:
                # The password has been verified for the user
                if user.is_active:
                    # Check for multi factor authentication
                    mfa_response = has_mfa(request, user.username)
                    if mfa_response:
                        return mfa_response

                    return log_user_in(request, user)
                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 login form
        form = LoginForm()

    # Determine if signup is disabled or not
    signup_enabled = settings.ENABLE_OPEN_SIGNUP if hasattr(
        settings, 'ENABLE_OPEN_SIGNUP') else False

    context = {'form': form, 'signup_enabled': signup_enabled}

    return render(request, 'tethys_portal/accounts/login.html', context)
Esempio n. 4
0
    def test_utilities_no_user_exist_next(self, mock_redirect, mock_authenticate):
        mock_request = mock.MagicMock()
        mock_request.method = 'POST'
        mock_request.POST = 'login-submit'
        mock_request.user.is_anonymous = True
        mock_request.user.username = '******'
        mock_request.GET = {'next': 'foo'}

        # mock authenticate
        mock_user = mock.MagicMock()
        mock_authenticate.return_value = mock_user

        # mock the password has been verified for the user
        mock_user.is_active = True

        utilities.log_user_in(mock_request, user=mock_user)

        # mock redirect after logged in using next parameter or default to user profile
        mock_redirect.assert_called_once_with('foo')