From e1cc6641698255d4ae0b70738877990bb8e78d2e Mon Sep 17 00:00:00 2001 From: emil farisan Date: Thu, 28 Nov 2019 10:39:51 +0700 Subject: [PATCH 1/2] test_my_lists: precreate sessions, move login checks into base --- functional_tests/base.py | 0 functional_tests/test_login.py | 0 functional_tests/test_my_lists.py | 23 +++++++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 functional_tests/base.py create mode 100644 functional_tests/test_login.py create mode 100644 functional_tests/test_my_lists.py diff --git a/functional_tests/base.py b/functional_tests/base.py new file mode 100644 index 0000000..e69de29 diff --git a/functional_tests/test_login.py b/functional_tests/test_login.py new file mode 100644 index 0000000..e69de29 diff --git a/functional_tests/test_my_lists.py b/functional_tests/test_my_lists.py new file mode 100644 index 0000000..6eab652 --- /dev/null +++ b/functional_tests/test_my_lists.py @@ -0,0 +1,23 @@ +from django.conf import settings +from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model +from django.contrib.sessions.backends.db import SessionStore +from .base import FunctionalTest +User = get_user_model() + + +class MyListsTest(FunctionalTest): + + def create_pre_authenticated_session(self, email): + user = User.objects.create(email=email) + session = SessionStore() + session[SESSION_KEY] = user.pk + session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] + session.save() + ## to set a cookie we need to first visit the domain. + ## 404 pages load the quickest! + self.browser.get(self.live_server_url + "/404_no_such_url/") + self.browser.add_cookie(dict( + name=settings.SESSION_COOKIE_NAME, + value=session.session_key, + path='/', + )) \ No newline at end of file -- GitLab From 38c887d9f237c87044398875df28a1264b482821 Mon Sep 17 00:00:00 2001 From: emil farisan Date: Thu, 28 Nov 2019 11:28:44 +0700 Subject: [PATCH 2/2] Exercise 9 --- functional_tests/base.py | 77 +++++++++++++++++++++++++++++++ functional_tests/test_login.py | 10 ++++ functional_tests/test_my_lists.py | 14 +++++- 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/functional_tests/base.py b/functional_tests/base.py index e69de29..36d09fa 100644 --- a/functional_tests/base.py +++ b/functional_tests/base.py @@ -0,0 +1,77 @@ +import sys +import time + +from django.contrib.staticfiles.testing import StaticLiveServerTestCase +from selenium import webdriver +from selenium.common.exceptions import WebDriverException +from selenium.webdriver.chrome.options import Options + +MAX_WAIT = 10 + + +def wait(fn): + def modified_fn(*args, **kwargs): + start_time = time.time() + while True: + try: + return fn(*args, **kwargs) + except (AssertionError, WebDriverException) as e: + if time.time() - start_time > MAX_WAIT: + raise e + time.sleep(0.5) + + return modified_fn + + +class FunctionalTest(StaticLiveServerTestCase): + + @classmethod + def setUpClass(cls): + for arg in sys.argv: + if 'liveserver' in arg: + cls.server_url = 'http://' + arg.split('=')[1] + return + super().setUpClass() + cls.server_url = cls.live_server_url + + def setUp(self): + chrome_options = Options() + chrome_options.add_argument('--dns-prefetch-disable') + chrome_options.add_argument('--no-sandbox') + chrome_options.add_argument('--headless') + chrome_options.add_argument('disable-gpu') + self.browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options) + self.browser.implicitly_wait(3) + + def tearDown(self): + self.browser.quit() + + def check_for_row_in_list_table(self, row_text): + table = self.browser.find_element_by_id('id_list_table') + rows = table.find_elements_by_tag_name('tr') + self.assertIn(row_text, [row.text for row in rows]) + + def get_item_input_box(self): + return self.browser.find_element_by_id('id_new_item') + + @wait + def wait_for(self, fn): + return fn() + + @wait + ​def wait_for_row_in_list_table(self, row_text): + ​table = self.browser.find_element_by_id('id_list_table') + ​rows = table.find_elements_by_tag_name('tr') + ​self.assertIn(row_text, [row.text for row in rows]) + + + ​@wait + ​def wait_to_be_logged_in(self, email): + ​self.browser.find_element_by_link_text('Log out') + + + ​@wait + ​def wait_to_be_logged_out(self, email): + ​self.browser.find_element_by_name('email') + ​navbar = self.browser.find_element_by_css_selector('.navbar') + ​self.assertNotIn(email, navbar.text) diff --git a/functional_tests/test_login.py b/functional_tests/test_login.py index e69de29..64235c5 100644 --- a/functional_tests/test_login.py +++ b/functional_tests/test_login.py @@ -0,0 +1,10 @@ +def test_can_get_email_link_to_log_in(self): + [...] + # she is logged in! + self.wait_to_be_logged_in(email=TEST_EMAIL) + + # Now she logs out + self.browser.find_element_by_link_text('Log out').click() + + # She is logged out + self.wait_to_be_logged_out(email=TEST_EMAIL) \ No newline at end of file diff --git a/functional_tests/test_my_lists.py b/functional_tests/test_my_lists.py index 6eab652..05d3309 100644 --- a/functional_tests/test_my_lists.py +++ b/functional_tests/test_my_lists.py @@ -20,4 +20,16 @@ class MyListsTest(FunctionalTest): name=settings.SESSION_COOKIE_NAME, value=session.session_key, path='/', - )) \ No newline at end of file + )) + + def test_logged_in_users_lists_are_saved_as_my_lists(self): + ​email = 'edith@example.com' + ​self.browser.get(self.live_server_url) + ​self.wait_to_be_logged_out(email) + + ​# Edith is a logged-in user + ​self.create_pre_authenticated_session(email) + ​self.browser.get(self.live_server_url) + ​self.wait_to_be_logged_in(email) + + \ No newline at end of file -- GitLab