I've got a test that I'm trying to run where I login to my django application:
class FirefoxTestCases(StaticLiveServerTestCase):
def setUp(self):
data_setup.basic_apps_setup(self)
self.browser = webdriver.Firefox()
def tearDown(self):
self.browser.quit()
def test_log_in_with_no_apps_displays_firefox(self):
# Opening the link we want to test
self.browser.get(self.live_server_url)
assert "log in" in self.browser.page_source
time.sleep(2)
self.browser.find_element_by_id("id_username").send_keys("userone")
self.browser.find_element_by_id("id_password").send_keys("test")
self.browser.find_element_by_id("log_in").click()
time.sleep(2)
# Check the returned result
assert "Something appears to be missing" in self.browser.page_source
In doing this - it doesn't actually allow the login because in my settings I have specific ALLOWED_HOSTS
setup.
Is there a way to access the ALLOWED_HOSTS settings when running this test so that it will allow login when testing?
The issue is that in settings.py
the SESSION_COOKIE_DOMAIN
was being set.
For example:
SESSION_COOKIE_DOMAIN = ".company.com"
Django LiveServerTestCase only does localhost
(and as far as I can tell is unchangeable). So because of this the logged in users cookie wasn't being recognized by the site at localhost
In order to fix this - for tests requiring interactivity (such as logging in) you can override that setting like this:
from django.test.utils import override_settings
...
...
@override_settings(SESSION_COOKIE_DOMAIN="")
def test_log_in_with_no_apps_displays_firefox(self):
# Opening the link we want to test
self.browser.get(self.live_server_url)
assert "log in" in self.browser.page_source
time.sleep(2)
self.browser.find_element_by_id("id_username").send_keys("userone")
self.browser.find_element_by_id("id_password").send_keys("test")
self.browser.find_element_by_id("log_in").click()
time.sleep(2)
# Check the returned result
assert "Something appears to be missing" in self.browser.page_source
This test will override that setting and the user will be able to successfully login without issue.
Any additional functionality the site would usually have that was unavailable because of cookie issues is now resolved.