I am trying to create a simple login test using Django and Selenium, but I get 403 due to CSRF failure. I expect the middleware to add a cookie to the GET request and then parse it into POST.
Here is what I have checked so far:
1. Is the cookie in the GET request set to /accounts/login/ ?
Yes, the cookie is set in the process_response method
2. Is a cookie available for the Selenium driver?
Yes
ipdb> self.selenium.get_cookies() [{u'domain': u'localhost', u'name': u'csrftoken', u'value': u'DzNbEn9kZw0WZQ4OsRLouriFN5MOIQos', u'expiry': 1470691410, u'path': u'/', u'httpOnly': False, u'secure': True}]
3. Is a cookie found during a POST request?
No, this try / except from django.middleware.CsrfViewMiddleware.process_view does not work:
source
try: csrf_token = _sanitize_token( request.COOKIES[settings.CSRF_COOKIE_NAME])
the code
class TestLogin(StaticLiveServerTestCase): @classmethod def setUpClass(cls): cls.selenium = getattr(webdriver, settings.SELENIUM_WEBDRIVER)() cls.selenium.maximize_window() cls.selenium.implicitly_wait(5) super(TestLogin, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() super(TestLogin, cls).tearDownClass() def test_login(self): self.selenium.get('{}{}'.format(self.live_server_url, '/accounts/login/?next=/')) assert "Django" in self.selenium.title un_el = self.selenium.find_element_by_id('id_username').send_keys('the_un') pw_el = self.selenium.find_element_by_id('id_password') pw_el.send_keys('the_pw') pw_el.send_keys(Keys.RETURN) try: WebDriverWait(self.selenium, 5).until(EC.title_contains("New Title")) except TimeoutException as e: msg = "Could not find 'New Title' in title. Current title: {}".format(self.selenium.title) raise TimeoutException(msg) finally: self.selenium.quit()
Question
What can I try to debug this?