Fakultas Ilmu Komputer UI

Commit 5365e976 authored by Farah Alhaniy Efendi's avatar Farah Alhaniy Efendi
Browse files

Merge branch 'exercise9' into 'master'

Exercise9

See merge request !14
parents 2662df42 eeca6c88
Pipeline #26011 passed with stages
in 29 minutes and 41 seconds
......@@ -18,6 +18,8 @@ Herokuapp : https://pmpl-farah.herokuapp.com/
- Spiking & De-Spiking
- Exercise 8 :
- Mocks Can Leave You Tightly Coupled
- Exercise 9 :
- Perbedaan implementasi functional test fitur login
## Exercise 3
**Proses test isolation**
......@@ -176,3 +178,7 @@ Dengan implementasi di **accounts/views.py** seperti berikut ini
Dengan implementasi tersebut, test tanpa mock terpenuhi sedangkan test dengan mock gagal terpenuhi. Dalam kata lain, dengan mock kita harus memiliki detil implementasi yang sesuai pada views, walaupun sebenarnya hasilnya sama sama benar.
## Exercise 8
**Perbedaan implementasi functional test fitur login**
Pada bab 20.1 kita ingin menggunakan functional authentication system agar dapat mengidentifikasikan user dan memperlihatkan list yang sudah mereka buat. Maka dari itu kita membuat FT untuk logged-in user sehingga daripada melakukan seluruh prosedur dari tahap login yang memakan waktu, kita dapat melewati bagian login email (karena sudah logged in).
......@@ -18,34 +18,83 @@ class FunctionalTest(StaticLiveServerTestCase):
self.browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
self.browser.implicitly_wait(10)
def tearDown(self):
self.browser.quit()
def wait_for_row_in_list_table(self, row_text):
start_time = time.time()
while True:
try:
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])
return
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
# def wait_for_row_in_list_table(self, row_text):
# start_time = time.time()
# while True:
# try:
# 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])
# return
# except (AssertionError, WebDriverException) as e:
# if time.time() - start_time > MAX_WAIT:
# raise e
# time.sleep(0.5)
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 wait_for(self, fn):
# start_time = time.time()
# while True:
# try:
# return fn()
# except (AssertionError, WebDriverException) as e:
# if time.time() - start_time > MAX_WAIT:
# raise e
# time.sleep(0.5)
# def wait_to_be_logged_in(self, email):
# self.wait_for(
# lambda: self.browser.find_element_by_link_text('Log out')
# )
# navbar = self.browser.find_element_by_css_selector('.navbar')
# self.assertIn(email, navbar.text)
# def wait_to_be_logged_out(self, email):
# self.wait_for(
# lambda: self.browser.find_element_by_name('email')
# )
# navbar = self.browser.find_element_by_css_selector('.navbar')
# self.assertNotIn(email, navbar.text)
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
@wait
def wait_for(self, fn):
start_time = time.time()
while True:
try:
return fn()
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
\ No newline at end of file
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')
# 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)
\ No newline at end of file
......@@ -40,19 +40,28 @@ class LoginTest(FunctionalTest):
# she clicks it
self.browser.get(url)
# # she is logged in!
# self.wait_for(
# 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
# self.browser.find_element_by_link_text('Log out').click()
# # She is logged out
# self.wait_for(
# lambda: self.browser.find_element_by_name('email')
# )
# navbar = self.browser.find_element_by_css_selector('.navbar')
# self.assertNotIn(TEST_EMAIL, navbar.text)
# she is logged in!
self.wait_for(
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)
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_for(
lambda: self.browser.find_element_by_name('email')
)
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertNotIn(TEST_EMAIL, navbar.text)
\ No newline at end of file
self.wait_to_be_logged_out(email=TEST_EMAIL)
\ No newline at end of file
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.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
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