def test_login_view_get_method_not_active_user(self, mock_redirect, mock_login_form, mock_authenticate, mock_login, mock_messages, mock_render): 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_form = mock.MagicMock() mock_login_form.return_value = mock_form # mock validate the form mock_form.is_valid.return_value = True mock_username = mock.MagicMock() mock_password = mock.MagicMock() mock_form.cleaned_data('username').return_value = mock_username mock_form.cleaned_data('password').return_value = mock_password # 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 = False # call the login function with mock args login_view(mock_request) mock_login_form.assert_called_with(mock_request.POST) # mock authenticate call mock_authenticate.asset_called_with(username=mock_username, password=mock_password) # mock the user is valid, active, and authenticated, so login in the user mock_login.assert_not_called() # mock redirect after logged in using next parameter or default to user profile mock_redirect.assert_not_called() mock_messages.error.assert_called_once_with( mock_request, "Sorry, but your account has been disabled. " "Please contact the site " "administrator for more details.") context = {'form': mock_login_form(), 'signup_enabled': False} mock_render.assert_called_once_with( mock_request, 'tethys_portal/accounts/login.html', context)
def test_login_view_get_method_user_none(self, mock_redirect, mock_login_form, mock_authenticate, mock_login, mock_messages, mock_render): 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_form = mock.MagicMock() mock_login_form.return_value = mock_form # mock validate the form mock_form.is_valid.return_value = True mock_username = mock.MagicMock() mock_password = mock.MagicMock() mock_form.cleaned_data('username').return_value = mock_username mock_form.cleaned_data('password').return_value = mock_password # mock authenticate mock_user = mock.MagicMock() mock_authenticate.return_value = None # mock the password has been verified for the user mock_user.is_active = False # call the login function with mock args login_view(mock_request) mock_login_form.assert_called_with(mock_request.POST) # mock authenticate call mock_authenticate.asset_called_with(username=mock_username, password=mock_password) # mock the user is valid, active, and authenticated, so login in the user mock_login.assert_not_called() # mock redirect after logged in using next parameter or default to user profile mock_redirect.assert_not_called() mock_messages.warning.assert_called_once_with(mock_request, "Whoops! We were not able to log you in. " "Please check your username and " "password and try again.") context = {'form': mock_login_form(), 'signup_enabled': False} mock_render.assert_called_once_with(mock_request, 'tethys_portal/accounts/login.html', context)
def test_login_view_get_method_next(self, mock_redirect, mock_login_form, mock_authenticate, mock_login): 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_form = mock.MagicMock() mock_login_form.return_value = mock_form # mock validate the form mock_form.is_valid.return_value = True mock_username = mock.MagicMock() mock_password = mock.MagicMock() mock_form.cleaned_data('username').return_value = mock_username mock_form.cleaned_data('password').return_value = mock_password # 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 # call the login function with mock args login_view(mock_request) mock_login_form.assert_called_with(mock_request.POST) # mock authenticate call mock_authenticate.asset_called_with(username=mock_username, password=mock_password) # mock the user is valid, active, and authenticated, so login in the user mock_login.assert_called_with(mock_request, mock_user) # mock redirect after logged in using next parameter or default to user profile mock_redirect.assert_called_once_with(mock_request.GET['next'])
def test_login_view_post_request_with_mfa(self, mock_login_form, mock_authenticate, mock_mfa): 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 = '' mock_mfa.return_value = True mock_form = mock.MagicMock() mock_login_form.return_value = mock_form # mock validate the form mock_form.is_valid.return_value = True mock_username = mock.MagicMock() mock_password = mock.MagicMock() mock_form.cleaned_data('username').return_value = mock_username mock_form.cleaned_data('password').return_value = mock_password # mock authenticate mock_user = mock.MagicMock() mock_user.username = '******' mock_authenticate.return_value = mock_user # mock the password has been verified for the user mock_user.is_active = True # call the login function with mock args login_view(mock_request) mock_login_form.assert_called_with(mock_request.POST) # mock authenticate call mock_authenticate.asset_called_with(username=mock_username, password=mock_password) # mock the user is valid, active, and authenticated, so login in the user mock_mfa.assert_called_with(mock_request, mock_user.username)
def test_login_view_wrong_method(self, mock_redirect, mock_login_form, mock_login, mock_render): mock_request = mock.MagicMock() mock_request.method = 'foo' mock_form = mock.MagicMock() mock_login_form.return_value = mock_form # call the login function with mock args login_view(mock_request) mock_login_form.assert_called_with() # mock the user is valid, active, and authenticated, so login in the user mock_login.assert_not_called() # mock redirect after logged in using next parameter or default to user profile mock_redirect.assert_not_called() context = {'form': mock_login_form(), 'signup_enabled': False} mock_render.assert_called_once_with(mock_request, 'tethys_portal/accounts/login.html', context)
def test_login_view_not_anonymous_user(self, mock_redirect): mock_request = mock.MagicMock() mock_request.user.is_anonymous = False mock_request.user.username = '******' login_view(mock_request) mock_redirect.assert_called_once_with('user:profile', username='******')