1

I have this code in my views.py

@otp_required
def decrypt(request):
   pass

This decorator makes sure that the user needs to be logged in with 2FA to view this page. However how can i simulate a logged in user with 2FA in my tests? I couldn't find anything in the docs regarding this 2FA module that explains how to run tests on a view with this decorator. Is there a specific test package i could use that will ignore this @otp_required or get around it somehow?

Something like this?

@patch(views.decrypt)
def test(self):
        response = self.client.get('/decrypt')
        self.assertEqual(response.status_code, 200)
Kevin
  • 37
  • 10
  • Can't `mock.patch` handle this? – Jonathon Reinhart May 07 '17 at 17:01
  • Better than my previous comment (check if you're in a test environment) would be override the code that enforces that attribute at the start of your test run. – Basic May 07 '17 at 17:11
  • @JonathonReinhart im read the docs of mock.patch and i don't understand it very well atm. Ive updated the post does it look right ? did you mean something like that ? – Kevin May 07 '17 at 17:14
  • @Basic Hmm not sure on how to check if im in a test environment i need to do some research on that as well – Kevin May 07 '17 at 17:18
  • @Kevin Apologies, I'd actually edited my comment with a better solution by the time you replied. When your tests start up, use [monkey patching](http://stackoverflow.com/a/27466499/156755) to replace the method that validates 2FA with a stub that does `return True` or similar. – Basic May 07 '17 at 18:06

0 Answers0