def test_login(self): user = User.objects.create(username='******') user.backend = '' view = LoginView() request = RequestFactory() request.GET = {} request.META = {} request.user = None request.session = self.client.session request.session.create() view.request = request form = self.moxx.CreateMock(UsernameLoginForm) form.get_user().AndReturn(user) self.moxx.ReplayAll() view.form_valid(form) self.moxx.VerifyAll() self.assertTrue(request.user)
def test_form_valid(self): """ Check if the user is authenticated. """ # add test User account user = UserFactory() user.set_password('123') user.save() # create LoginView view = LoginView() view.request = self.generate_request() # create LoginForm form = LoginForm() form.data = {'username': user.username, 'password': '******'} form.cleaned_data = form.clean() # test now view.form_valid(form) self.assertEqual(view.request.user.username, user.username) self.assertTrue(view.request.user.is_authenticated())