Fakultas Ilmu Komputer UI

Commit 01a6fbc9 authored by Rahmania Astrid Mochtar's avatar Rahmania Astrid Mochtar
Browse files

Testinggoat/ch20

parent 2562e745
......@@ -235,4 +235,10 @@ Implementasinya:
messages.SUCCESS,
"Check your email, we've sent you a link you can use to log in."
```
Dalam test nonmock akan pass, namun dalam test mock akan fail. Ini terjadi karena object yang di-mock adalah `accounts.views.messages` dan yang digunakan untuk assert adalah `messages.success`. Sementara dalam implementasi digunakan `messages.add_messages`. Sehingga meskipun hasil yang didapatkan sama, test yang dijalankan akan terus fail.
\ No newline at end of file
Dalam test nonmock akan pass, namun dalam test mock akan fail. Ini terjadi karena object yang di-mock adalah `accounts.views.messages` dan yang digunakan untuk assert adalah `messages.success`. Sementara dalam implementasi digunakan `messages.add_messages`. Sehingga meskipun hasil yang didapatkan sama, test yang dijalankan akan terus fail.
## Exercise 9
### Perbandingan Functional Test Login 20.1 vs 18.3
Functional test login pada 20.1 lebih baik dari 18.3 karena terdapat separation of concerns. Di setiap FT diperlukan user yang sudah logged-in. Daripada di setiap FT terdapat process login yang berulang, lebih baik jika step tersebut dapat di-skip.
\ No newline at end of file
......@@ -11,6 +11,7 @@ import time
MAX_WAIT = 10
class FunctionalTest(StaticLiveServerTestCase):
def setUp(self):
chrome_options = Options()
......@@ -25,12 +26,35 @@ class FunctionalTest(StaticLiveServerTestCase):
def tearDown(self):
self.browser.quit()
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)
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)
\ No newline at end of file
......@@ -41,18 +41,10 @@ class LoginTest(FunctionalTest):
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)
# 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