def test_form_valid(self, mock_get_auth_user, mock_auth, mock_remote_login, mock_get_user_id, mock_get_and_update_user): # Fake user and required attributes user = self.user user.backend = 'accounts.authentication.WocatCMSAuthenticationBackend' mock_get_auth_user.return_value = user mock_auth.return_value = user mock_remote_login.return_value = 1 mock_get_user_id.return_value = 1 mock_get_and_update_user.return_value = self.user form = WocatAuthenticationForm form.get_user = lambda x: user # Add message store request = RequestFactory().post( reverse('login'), data={'form': form(initial=self.invalid_login_credentials)}) request.user = user request.session = MagicMock() request._messages = FallbackStorage view = self.setup_view(LoginView(), request) response = view.form_valid( form(initial=self.invalid_login_credentials)) self.assertEqual(request.user, self.user) self.assertEqual(response.url, reverse('home'))
def test_get(self): lv = LoginView() request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'PATH_INFO': 'accounts:login', 'wsgi.input': StringIO()}) mock_wsgi_session_context() expect = render( request, 'accounts/login.html', {'form': LoginForm()} ) result = lv.get(request) self.assertEqual( remove_csrf(result.content.decode('utf-8')), remove_csrf(expect.content.decode('utf-8')) )
def test_post__when_form_is_valid(self, mock_form): mock_form.return_value.is_valid.return_value = True def getitem(name): return COR_APPUSER_DATA_1st['username'] mock_form.return_value.data = MagicMock() mock_form.return_value.data.__getitem__.side_effect = getitem lv = LoginView() request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'PATH_INFO': 'accounts:login', 'wsgi.input': StringIO()}) mock_wsgi_session_context() expect = redirect('travel:place_list') result = lv.post(request) self.assertEqual( remove_csrf(result.content.decode('utf-8')), remove_csrf(expect.content.decode('utf-8')) )
def test_dispatch(self): request = self.factory.get(reverse('login')) setattr(request, 'user', self.user) view = self.setup_view(LoginView(), request) response = view.dispatch(view.request, *view.args, **view.kwargs) self.assertEqual(response.status_code, 302)
def setUp(self): self.invalid_login_credentials = {'username': '******', 'password': '******'} self.factory = RequestFactory() self.factory.GET = MagicMock() self.user = create_new_user() self.view = self.setup_view(view=LoginView(), request=self.factory)
def test_class_variable__is_registered_correctly(self): lv = LoginView() self.assertEqual( lv.template_name, 'accounts/login.html' )