Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:44:30+00:00 2026-06-16T22:44:30+00:00

I’m having trouble posting data to some views that use Django REST framework in

  • 0

I’m having trouble posting data to some views that use Django REST framework in my tests. I’m using django_webtest to test my user API. I’m running into a problem with the following code:

class UserApiTest(WebTest):

    def setUp(self):
        AdminFactory()

    def test_accessing_user_list_shows_one_user(self):
        user_list = self.app.get('/quickstart/users/', user='admin')
        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, user='admin')
        url = 'http://localhost:80/quickstart/users/2/'
        assert_that(user_create.json, has_entry('url', url))

The problem is that I get a CSRF error when the second test runs. Looking at the Django REST Framework documentation I read that the CSRF error is triggered only when using session-based authentication. So, I figured I’d try basic authentication, which according to Django’s documentation only requires setting the REMOTE_USER environment variable:

class UserApiTest(WebTest):

    extra_environ = {'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))

This worked worse, as the user wasn’t even authorized to view these pages (i.e. accessing the URL’s returned 403’s).

My question is: How do I properly setup basic authentication with django_webtest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:44:31+00:00Added an answer on June 16, 2026 at 10:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am using jsonparser to parse data and images obtained from json response. When
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.