Fakultas Ilmu Komputer UI

Commit 371abdac authored by Jordan Muhammad Andrianda's avatar Jordan Muhammad Andrianda
Browse files

Latihan9

parent a69f5c90
......@@ -9,6 +9,8 @@ from django.contrib import auth, messages
from django.urls import reverse
from accounts.models import Token
import time
time.sleep(30)
def send_login_email(request):
email = request.POST['email']
......
......@@ -19,13 +19,44 @@ class FunctionalTest(StaticLiveServerTestCase):
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(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
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)
@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)
......@@ -43,14 +43,41 @@ class LoginTest(FunctionalTest):
# 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)
self.wait_to_be_logged_in(email=TEST_EMAIL)
self.browser.find_element_by_link_text('Log out').click()
self.wait_to_be_logged_out(email=TEST_EMAIL)
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
......@@ -4,6 +4,18 @@
### Jordan Muhammad Andrianda <br> 1506722765 <br> This is Practice Repository PMPL Course
## Cerita Latihan 9 Mengapa implementasi Functional Test untuk fitur login lebih baik menggunakan implementasi subbab 20.1
# Penjelasan
Bab 20.1 disini kita menggunakan functionnal authentication system supaya bisa identifkasikan ke user dan akan bisa menampilkan list yang sudah dibuat.
Kemudian dari situ kita membuat sebuah functional test untuk user logged in yang bisa menghemat waktu tak perlu melakukan dari awal log in lgi, hal ini akan melewati bagian kita harus melakukan log in email
Sedangkan di tahap implementasi kita bisa tambah function create pre autenthicated session untuk bisa melakukan login, dengan hal tersebubt kita bbisa memakai session untuk functional test lain yang bisa membuat kita tak perlu melakukan log in lg
## Cerita Latihan8 Perbedaan antara manual mocking dan menggunakan mock library
# Manual Mocking
......
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