Django's Test Client

What Django gives you. A TestCase that runs each test against a temporary database inside a transaction, rolled back afterwards, and a test client that makes requests without a server running.

So a test can create a post, request a page, and assert what came back, in a few lines and in a fraction of a second.

Test behavior, not implementation. A test asserting that a function returns a particular dictionary breaks every time you refactor. A test asserting that a draft is not visible to strangers keeps working, and is the one you actually care about.

The best tests read like the promises your application makes.

The tests worth writing first, in order:

That an unpublished post is not publicly visible.

That a stranger cannot edit someone else's post.

That a signed-out visitor is redirected rather than shown the form.

That the list page shows published posts and not drafts.

Every one of those is a security property. If they pass, the most damaging class of bug in this application cannot happen quietly.

◈ Graded Challenge

Write check_access(cases) auditing permission rules in bulk, the way a test suite does. Each case is a dictionary with keys action, user, author, published and expected, where expected is what should be allowed. Decide the real answer using the same rules as module 10: viewing a published post is public; viewing a draft, editing and deleting require being the author; a user of None may only view published posts. Return a list of the actions and authors that disagreed with expected, as strings shaped "action:author", in the order given. Return an empty list when everything agrees.

Name it exactly: check_access(cases)

Python3 visible + 4 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: How Much Is Enough →
← Back