Trying to write tests for a legacy Django 1.11 project. The project is using unittest package (I don't know if that's standard or a choice).
I'm trying to use django.test.Client.force_login to test a view with LoginRequiredMixin but it does nothing. I have added print(self.request.user) to the mixin's dispatch method. It outputs AnonymousUser whether I use force_login or not. Why is it not working?
The test (simplified for readability's sake):
class CheckoutTest(TestCase):
def test_successful_payment(self):
user = UserAccount.objects.create(email='[email protected]', is_owner=True, is_customer=True)
self.client.force_login(user)
self.client.post('/subscription/buy/' + str(package.id) + '/', {
... redacted ...
})
The view:
class BuyPackageView(LoginRequiredMixin, View):
def post(self, request, *args, **kwargs):
... redacted ...
My test code snippet looks like, I am getting user from table related to allauth app: