Typically, Django unit tests do not require a secure connection, however, every once in a while your app tests for a secure connection.  An example of this would be the OAuth app we have that utilizes oauthlib.  oauthlib checks that a request is over a secure connection:

While it is possible to change the “enforce_ssl” attribute to be False during testing, it is safer to avoid having code that can weaken this type of check.

Fortunately, there is an easy way to have your unit test and have it work too:  have the unit test client make a secure connection. The solution is to use a secure client:

OK.  It is not really a secure connection, but it is not really a connection in the first place. 🙂

Let us see the environment of the test client as seen from a unit test.  First, the unit test:

Next, the output:

As you can see, the URL scheme is now https and the port is 443. Changing the port is overkill since oauthlib does not, and should not, check the port number but only whether the connection is secure or not.

Related Posts

Related Topics & Tags: API / Platform

About Sean Farley

I am a Senior Software Engineer at SmartFile. I design parts of the SmartFile API in addition to writing code and unit tests. I also take pride in telling Brian and John how much better Android phones are than 'Snapple' phones.

1 thought on ““Secure” Django Test Client”

Leave a Reply

Your email address will not be published. Required fields are marked *