-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff806e9
commit 605d09b
Showing
18 changed files
with
316 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ docs/source/_build/ | |
example_projects/token_auth/ | ||
.env/ | ||
.venv/ | ||
|
||
# Playwright | ||
videos/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import os | ||
import time | ||
from http.client import HTTPConnection | ||
from subprocess import Popen | ||
|
||
import pytest | ||
|
||
HOST = "localhost" | ||
PORT = 8000 | ||
BASE_URL = f"http://{HOST}:{PORT}" | ||
|
||
|
||
@pytest.fixture | ||
def browser_context_args(): | ||
return {"record_video_dir": "videos/"} | ||
|
||
|
||
@pytest.fixture | ||
def context(context): | ||
# We don't need a really long timeout. | ||
# The timeout determines how long Playwright waits for a HTML element to | ||
# become available. | ||
# By default it's 30 seconds, which is way too long when testing an app | ||
# locally. | ||
context.set_default_timeout(10000) | ||
yield context | ||
|
||
|
||
@pytest.fixture | ||
def mfa_app(): | ||
""" | ||
Running dev server and Playwright test in parallel. | ||
More info https://til.simonwillison.net/pytest/playwright-pytest | ||
""" | ||
path = os.path.join( | ||
os.path.dirname(os.path.dirname(__file__)), | ||
"example_projects", | ||
"mfa_demo", | ||
) | ||
|
||
process = Popen( | ||
["python", "-m", "main", "--reset-db"], | ||
cwd=path, | ||
) | ||
retries = 5 | ||
while retries > 0: | ||
conn = HTTPConnection(f"{HOST}:{PORT}") | ||
try: | ||
conn.request("HEAD", "/") | ||
response = conn.getresponse() | ||
if response is not None: | ||
yield process | ||
break | ||
except ConnectionRefusedError: | ||
time.sleep(1) | ||
retries -= 1 | ||
|
||
if not retries: | ||
raise RuntimeError("Failed to start http server") | ||
else: | ||
process.terminate() | ||
process.wait() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
By using pages we can make out test more scalable. | ||
https://playwright.dev/docs/pom | ||
""" | ||
|
||
from playwright.sync_api import Page | ||
|
||
|
||
class LoginPage: | ||
url = "http://localhost:8000/login/" | ||
|
||
def __init__(self, page: Page): | ||
self.page = page | ||
self.username_input = page.locator('input[name="username"]') | ||
self.password_input = page.locator('input[name="password"]') | ||
self.button = page.locator("button") | ||
|
||
def reset(self): | ||
self.page.goto(self.url) | ||
|
||
def login(self): | ||
self.username_input.fill("piccolo") | ||
self.password_input.fill("piccolo123") | ||
self.button.click() | ||
|
||
|
||
class RegisterPage: | ||
url = "http://localhost:8000/register/" | ||
|
||
def __init__(self, page: Page): | ||
self.page = page | ||
self.username_input = page.locator("[name=username]") | ||
self.email_input = page.locator("[name=email]") | ||
self.password_input = page.locator("[name=password]") | ||
self.confirm_password_input = page.locator("[name=confirm_password]") | ||
self.button = page.locator("button") | ||
|
||
def reset(self): | ||
self.page.goto(self.url) | ||
|
||
def login(self): | ||
self.username_input.fill("piccolo") | ||
self.email_input.fill("[email protected]") | ||
self.password_input.fill("piccolo123") | ||
self.confirm_password_input.fill("piccolo123") | ||
self.button.click() | ||
|
||
|
||
class MFARegisterPage: | ||
url = "http://localhost:8000/private/mfa-register/" | ||
|
||
def __init__(self, page: Page): | ||
self.page = page | ||
|
||
def reset(self): | ||
self.page.goto(self.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from playwright.async_api import Page | ||
|
||
from .pages import LoginPage, MFARegisterPage, RegisterPage | ||
|
||
|
||
def test_login(page: Page, mfa_app): | ||
""" | ||
Make sure we can register, sign up for MFA. | ||
""" | ||
register_page = RegisterPage(page=page) | ||
register_page.reset() | ||
register_page.login() | ||
|
||
login_page = LoginPage(page=page) | ||
login_page.reset() | ||
login_page.login() | ||
|
||
mfa_register_page = MFARegisterPage(page=page) | ||
mfa_register_page.reset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Home</title> | ||
|
||
<style> | ||
body { | ||
font-family: sans-serif; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<h1>MFA Demo</h1> | ||
<p>First <a href="/register/">register</a></p> | ||
<p>Then <a href="/login/">login</a></p> | ||
<p>Then <a href="/private/mfa-register/">sign up for MFA</a></p> | ||
<p>Then try the <a href="/private/">private page</a></p> | ||
<p>And <a href="/private/logout/">logout</a></p> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.