I'm trying to write test cases for my class views that are secure behind django-two-factor-auth OTPRequiredMixin. I'm not sure how to write the setUp
function to fully authenticate the user through OTP. I've tried self.client.force_login()
but when I try to browse to that url in my test function, I am landing on the "Permission Denied" page that prompts to enable two-factor authentication for the user, instead of the expected url.
Permission Denied
The page you requested, enforces users to verify using two-factor authentication for security reasons. You need to enable these security features in order to access this page.
Two-factor authentication is not enabled for your account. Enable two-factor authentication for enhanced account security.
Here's an example of one of the class views:
class ProjectCreateView(OTPRequiredMixin, CreateView):
model = Project
template_name = 'project_create.html'
fields = ['name', 'description']
And here's an example of my setup and a test:
class ProjectTestCase(TestCase):
def setUp(self):
self.user = get_user_model().objects.create(
username='jsmith', first_name='John', last_name='Smith', email='johnsmith@test.com', password='secure'
)
[...]
self.client.force_login(self.user)
def test_project_create(self):
response = self.client.post(
'/project/create/', {'name': 'Test Project', 'description': 'A basic test project'}
)
self.assertEqual(response.status_code, 200)