Fakultas Ilmu Komputer UI

Commit 3490009c authored by Rani Lasma Uli's avatar Rani Lasma Uli
Browse files

done chapter 20.1

parent 59021c39
Pipeline #26348 passed with stages
in 9 minutes and 9 seconds
...@@ -309,4 +309,4 @@ Also, using library for mocking is pretty simple - in this case just using @patc ...@@ -309,4 +309,4 @@ Also, using library for mocking is pretty simple - in this case just using @patc
> >
> Mocking with Library: > Mocking with Library:
> 1. Readable code, no object creation in testing class > 1. Readable code, no object creation in testing class
> 2. Fully-package! No need to write the construct the object (fake) to mock real object > 2. Fully-package! No need to write the constructor of the object (fake) to mock real object
...@@ -10,6 +10,21 @@ from unittest import skip ...@@ -10,6 +10,21 @@ from unittest import skip
MAX_WAIT = 10 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): class FunctionalTest(StaticLiveServerTestCase):
def setUp(self): def setUp(self):
self.BASE_URL = os.environ.get("HEROKU_HOST", self.live_server_url) self.BASE_URL = os.environ.get("HEROKU_HOST", self.live_server_url)
...@@ -23,8 +38,8 @@ class FunctionalTest(StaticLiveServerTestCase): ...@@ -23,8 +38,8 @@ class FunctionalTest(StaticLiveServerTestCase):
def createBrowserInstance(self): def createBrowserInstance(self):
try: try:
self.browser = webdriver.Chrome(options=self.options) # self.browser = webdriver.Chrome(options=self.options)
# self.browser = webdriver.Firefox() self.browser = webdriver.Firefox()
except WebDriverException: except WebDriverException:
# linux # linux
self.browser = webdriver.Chrome('./chromedriver', self.browser = webdriver.Chrome('./chromedriver',
...@@ -36,6 +51,7 @@ class FunctionalTest(StaticLiveServerTestCase): ...@@ -36,6 +51,7 @@ class FunctionalTest(StaticLiveServerTestCase):
def tearDown(self): def tearDown(self):
self.browser.quit() self.browser.quit()
@wait
def wait_for_row_in_list_table(self, row_text): def wait_for_row_in_list_table(self, row_text):
start_time = time.time() start_time = time.time()
while True: while True:
...@@ -49,6 +65,7 @@ class FunctionalTest(StaticLiveServerTestCase): ...@@ -49,6 +65,7 @@ class FunctionalTest(StaticLiveServerTestCase):
raise e raise e
time.sleep(0.5) time.sleep(0.5)
@wait
def wait_for(self, fn): def wait_for(self, fn):
start_time = time.time() start_time = time.time()
while True: while True:
...@@ -57,4 +74,17 @@ class FunctionalTest(StaticLiveServerTestCase): ...@@ -57,4 +74,17 @@ class FunctionalTest(StaticLiveServerTestCase):
except (AssertionError, WebDriverException) as e: except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT: if time.time() - start_time > MAX_WAIT:
raise e raise e
time.sleep(0.5) time.sleep(0.5)
\ No newline at end of file
@wait
def wait_to_be_logged_in(self, email):
self.browser.find_element_by_link_text('Log out')
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertIn(email, navbar.text)
@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)
...@@ -42,15 +42,10 @@ class LoginTest(FunctionalTest): ...@@ -42,15 +42,10 @@ class LoginTest(FunctionalTest):
self.browser.get(url) self.browser.get(url)
# she is logged in! # she is logged in!
self.wait_for( self.wait_to_be_logged_in(email=TEST_EMAIL)
lambda: self.browser.find_element_by_link_text('Log out'))
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertIn(TEST_EMAIL, navbar.text)
# Now she logs out # Now she logs out
self.browser.find_element_by_link_text('Log out').click() self.browser.find_element_by_link_text('Log out').click()
# She is logged out # She is logged out
self.wait_for(lambda: self.browser.find_element_by_name('email')) self.wait_to_be_logged_out(email=TEST_EMAIL)
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertNotIn(TEST_EMAIL, navbar.text)
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='/',
))
def test_logged_in_users_lists_are_saved_as_my_lists(self):
email = 'edith@example.com'
self.browser.get(self.BASE_URL)
self.wait_to_be_logged_out(email)
# Edith is a logged-in user
self.create_pre_authenticated_session(email)
self.browser.get(self.BASE_URL)
self.wait_to_be_logged_in(email)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment