After working on the problem, I found that I need to pass full basic authentication headers, instead of just the REMOTE_USER
. This is because Django REST Framework parses the basic authentication in their BasicAuthentication
backend.
class UserApiTest(WebTest):
auth_password = base64.encodestring('admin:default').strip()
extra_environ = {
'AUTH_TYPE': 'Basic',
'HTTP_AUTHORIZATION': 'Basic {}'.format(auth_password),
'REMOTE_USER': 'admin'}
def setUp(self):
AdminFactory()
def test_accessing_user_list_shows_one_user(self):
user_list = self.app.get('/quickstart/users/')
assert_that(user_list.json, has_entry('count', 1))
def test_posting_new_user_returns_url_for_user_detail(self):
post_data = {'username': 'john', 'email': 'john.doe@example.com'}
user_create = self.app.post('/quickstart/users/', post_data)
url = 'http://localhost:80/quickstart/users/2/'
assert_that(user_create.json, has_entry('url', url))
Also, by default, you should make sure any user you create in your tests has the is_staff
flag set to yes. Of course, if you have set Django REST to have different permissions than you should make sure that those permissions are correctly set.
I hope this helps someone.