Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with remote authentication

I have a suite of tests that I wrote while my app was using Django's default authentication, but now I've added Atlassian Crowd as the authentication method and those tests now fail, mainly because the Crowd server isn't there when I want to run my tests from home.

Each app has this in it's Setup() method

def setUp(self):
    """Set up the shared test data."""
    self.client.login(username='admin', password='letmein')

I'm working around it at the moment by commenting out the AUTHENTICATION_BACKENDS, but that isn't going to work on the CI server.

I don't think the error I'm getting is important, but for completeness:

URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>

I've tried adding both auth backends into AUTHENTICATION_BACKENDS and I still get the same results.

What are my options for getting these tests to pass?

Is there any way to force the user to be logged in? Can I mock the auth object somehow?

Could I put some check around the AUTHENTICATION_BACKENDS section in setting to check if it's running in test mode? but then I'm writing special cases for my tests and that kind of defeats the object.

like image 716
Stuart Grimshaw Avatar asked Feb 16 '26 21:02

Stuart Grimshaw


1 Answers

You could change the AUTHENTICATION_BACKENDS setting in the setUp method, then change it back in tearDown. This question's accepted answer has an example just that, but with a different setting.

like image 146
Evan Porter Avatar answered Feb 18 '26 19:02

Evan Porter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!