Skip to content

Commit 7b27a72

Browse files
committed
Improve the facets test.
1 parent 0945305 commit 7b27a72

File tree

3 files changed

+137
-36
lines changed

3 files changed

+137
-36
lines changed

python/example-pytest-selfie/tests/app_account_test.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ def client():
1212
with app.test_client() as client:
1313
yield client
1414

15+
def test_homepage_v1(client):
16+
expect_selfie(client.get("/").data.decode()).to_be("""
17+
<html><body>
18+
<h1>Please login</h1>
19+
<form action="/login" method="post">
20+
<input type="text" name="email" placeholder="email">
21+
<input type="submit" value="login">
22+
</form>
23+
</body></html>""")
1524

16-
def test_homepage(client):
17-
web_selfie(client.get("/")).to_be("""<html>
25+
def test_homepage_v2(client):
26+
web_selfie(client.get("/")).to_be("""<html>
1827
<body>
1928
<h1>
2029
Please login
@@ -30,51 +39,40 @@ def test_homepage(client):
3039
Please login
3140
╔═ [status] ═╗
3241
200 OK""")
42+
43+
def test_login_flow(client):
44+
web_selfie(client.get("/")).to_match_disk("1. not logged in") \
45+
.facet("md").to_be("Please login")
3346

47+
web_selfie(client.post("/login", data={"email": "[email protected]"})) \
48+
.to_match_disk("2. post login form") \
49+
.facet("md").to_be("""Email sent!
3450
35-
def test_T01_not_logged_in(client):
36-
response = client.get("/")
37-
expect_selfie(response.data.decode()).to_be("""
38-
<html><body>
39-
<h1>Please login</h1>
40-
<form action="/login" method="post">
41-
<input type="text" name="email" placeholder="email">
42-
<input type="submit" value="login">
43-
</form>
44-
</body></html>""")
45-
51+
Check your email for your login link.""")
4652

47-
def test_T02_login(client):
48-
response = client.post("/login", data={"email": "[email protected]"})
49-
expect_selfie(response.data.decode()).to_be("""
50-
<html><body>
51-
<h1>Email sent!</h1>
52-
<p>Check your email for your login link.</p>
53-
</body></html>""")
54-
55-
email = wait_for_incoming_email()
56-
expect_selfie(email).to_be(
53+
email = wait_for_incoming_email()
54+
expect_selfie(email).to_be(
5755
{
5856
5957
"subject": "Login to example.com",
6058
"html_content": 'Click <a href="http://localhost/login-confirm/2Yw4aCQ">here</a> to login.',
6159
}
6260
)
61+
62+
web_selfie(client.get("/login-confirm/2Yw4aCQ")).to_be("""REDIRECT 302 Found to /
63+
╔═ [cookies] ═╗
64+
[email protected]|29Xwa32OsHUoHm4TRitwQMWpuynz3r1aw3BcB5pPGdY=; Path=/""")
6365

66+
client.set_cookie('login', '[email protected]|29Xwa32OsHUoHm4TRitwQMWpuynz3r1aw3BcB5pPGdY=')
67+
web_selfie(client.get("/")).to_match_disk("3. log in works with cookies") \
68+
.facet("md").to_be("Welcome back [email protected]")
6469

65-
def test_T03_login_confirm(client):
66-
response = client.get("/login-confirm/erjchFY=", follow_redirects=False)
67-
expect_selfie(headers_to_string(response)).to_be("""200 OK
68-
Content-Type=text/html; charset=utf-8""")
69-
70+
client.set_cookie('login', '[email protected]|ABCDEF')
71+
web_selfie(client.get("/")).to_match_disk("4. log in fails with fake cookies") \
72+
.facet("status").to_be("401 UNAUTHORIZED")
7073

71-
def headers_to_string(response):
72-
headers = [f"{response.status}"]
73-
for name, value in response.headers.items():
74-
if name.lower() not in ["server", "date", "content-length"]:
75-
headers.append(f"{name}={value}")
76-
return "\n".join(headers)
7774

75+
#SELFIEWRITE
7876

7977
if __name__ == "__main__":
8078
pytest.main()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
╔═ test_login_flow/1. not logged in ═╗
2+
<html>
3+
<body>
4+
<h1>
5+
Please login
6+
</h1>
7+
<form action="/login" method="post">
8+
<input name="email" placeholder="email" type="text"/>
9+
<input type="submit" value="login"/>
10+
</form>
11+
</body>
12+
</html>
13+
14+
╔═ test_login_flow/1. not logged in[md] ═╗
15+
Please login
16+
╔═ test_login_flow/1. not logged in[status] ═╗
17+
200 OK
18+
╔═ test_login_flow/2. post login form ═╗
19+
<html>
20+
<body>
21+
<h1>
22+
Email sent!
23+
</h1>
24+
<p>
25+
Check your email for your login link.
26+
</p>
27+
</body>
28+
</html>
29+
30+
╔═ test_login_flow/2. post login form[md] ═╗
31+
Email sent!
32+
33+
Check your email for your login link.
34+
╔═ test_login_flow/2. post login form[status] ═╗
35+
200 OK
36+
╔═ test_login_flow/3. log in works with cookies ═╗
37+
<html>
38+
<body>
39+
<h1>
40+
Welcome back [email protected]
41+
</h1>
42+
</body>
43+
</html>
44+
45+
╔═ test_login_flow/3. log in works with cookies[md] ═╗
46+
Welcome back [email protected]
47+
╔═ test_login_flow/3. log in works with cookies[status] ═╗
48+
200 OK
49+
╔═ test_login_flow/3. log in works with those cookies ═╗
50+
<html>
51+
<body>
52+
<h1>
53+
Welcome back [email protected]
54+
</h1>
55+
</body>
56+
</html>
57+
58+
╔═ test_login_flow/3. log in works with those cookies[md] ═╗
59+
Welcome back [email protected]
60+
╔═ test_login_flow/3. log in works with those cookies[status] ═╗
61+
200 OK
62+
╔═ test_login_flow/4. log in blocked without cookies ═╗
63+
<html>
64+
<body>
65+
<h1>
66+
Welcome back [email protected]
67+
</h1>
68+
</body>
69+
</html>
70+
71+
╔═ test_login_flow/4. log in blocked without cookies[md] ═╗
72+
Welcome back [email protected]
73+
╔═ test_login_flow/4. log in blocked without cookies[status] ═╗
74+
200 OK
75+
╔═ test_login_flow/4. log in fails with fake cookies ═╗
76+
<!DOCTYPE html>
77+
<html lang="en">
78+
<title>
79+
401 Unauthorized
80+
</title>
81+
<h1>
82+
Unauthorized
83+
</h1>
84+
<p>
85+
The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.
86+
</p>
87+
</html>
88+
89+
╔═ test_login_flow/4. log in fails with fake cookies[md] ═╗
90+
401 Unauthorized
91+
92+
Unauthorized
93+
94+
The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.
95+
╔═ test_login_flow/4. log in fails with fake cookies[status] ═╗
96+
401 UNAUTHORIZED
97+
╔═ [end of file] ═╗

python/example-pytest-selfie/tests/selfie_settings.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
def _web_camera(response: TestResponse) -> Snapshot:
1717
redirect_reason = REDIRECTS.get(response.status_code)
1818
if redirect_reason is not None:
19-
return Snapshot.of(
19+
snapshot = Snapshot.of(
2020
f"REDIRECT {response.status_code} {redirect_reason} to "
2121
+ response.headers.get("Location", "<unknown>")
2222
)
2323
else:
24-
return Snapshot.of(response.data.decode()).plus_facet("status", response.status)
24+
snapshot = Snapshot.of(response.data.decode()).plus_facet(
25+
"status", response.status
26+
)
27+
28+
if response.headers.get("Set-Cookie"):
29+
snapshot = snapshot.plus_facet("cookies", response.headers.get("Set-Cookie"))
30+
return snapshot
2531

2632

2733
def _pretty_print_html(html: str):

0 commit comments

Comments
 (0)