Skip to content

Modified __init__.py to keep up with password/username validation requirement changes #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2025/x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def register_page():
@check50.check(register_page)
def simple_register():
"""registering user succeeds (and login or portfolio page is displayed)"""
Finance().register("_cs50", "ohHai28!", "ohHai28!").status(200)
Finance().register("cs-50", "ohHai28!", "ohHai28!").status(200)


@check50.check(register_page)
def register_empty_field_fails():
"""registration with an empty field fails"""
for user in [
("", "crimson", "crimson"),
("jharvard", "crimson", ""),
("", "Cr1mson!", "Cr1mson!"),
("jharvard", "Cr1mson!", ""),
("jharvard", "", ""),
]:
Finance().register(*user).status(400)
Expand All @@ -51,6 +51,32 @@ def register_password_mismatch_fails():
Finance().register("check50user1", "thisiscs50", "crimson").status(400)


@check50.check(register_page)
def register_password_validation_fails():
"""registration with invalid passwords fail"""
for user in [
("jharvard", "cr1mson!", "cr1mson!"), # no uppercase letters
("jharvard", "CR1MSON!", "CR1MSON!"), # no lowercase letters
("jharvard", "Crimson!", "Crimson!"), # no digits
("jharvard", "Cr1mson1", "Cr1mson1"), # no special char
("jharvard", "Cr1m!", "Cr!m!"), # len(password) < 8
("jharvard", "Cr1m!"*10, "Cr1m!"*10) # len(password) > 32
]:
Finance().register(*user).status(400)


@check50.check(register_page)
def register_username_validation_fails():
"""registration with invalid usernames fail"""
for user in [
("-jharvard", "Cr1mson!", "Cr1mson!"), # starts with dash
(";.,~", "Cr1mson!", "Cr1mson!"), # non-alphanumeric
("jh", "Cr1mson!", "Cr1mson!"), # len(username) < 3
("j" * 40, "Cr1mson!", "Cr1mson!") # len(username) > 39
]:
Finance().register(*user).status(400)


@check50.check(register_page)
def register_reject_duplicate_username():
"""registration rejects duplicate username"""
Expand All @@ -67,35 +93,35 @@ def login_page():
@check50.check(simple_register)
def can_login():
"""logging in as registered user succceeds"""
Finance().login("_cs50", "ohHai28!").status(200).get(
Finance().login("cs-50", "ohHai28!").status(200).get(
"/", follow_redirects=False
).status(200)


@check50.check(can_login)
def quote_page():
"""quote page has all required elements"""
Finance().login("_cs50", "ohHai28!").validate_form("/quote", "symbol")
Finance().login("cs-50", "ohHai28!").validate_form("/quote", "symbol")


@check50.check(quote_page)
def quote_handles_invalid():
"""quote handles invalid ticker symbol"""
Finance().login("_cs50", "ohHai28!").quote("ZZZ").status(400)
Finance().login("cs-50", "ohHai28!").quote("ZZZ").status(400)


@check50.check(quote_page)
def quote_handles_blank():
"""quote handles blank ticker symbol"""
Finance().login("_cs50", "ohHai28!").quote("").status(400)
Finance().login("cs-50", "ohHai28!").quote("").status(400)


@check50.check(quote_page)
def quote_handles_valid():
"""quote handles valid ticker symbol"""
(
Finance()
.login("_cs50", "ohHai28!")
.login("cs-50", "ohHai28!")
.quote("AAAA")
.status(200)
.content(r"28\.00", "28.00", name="body")
Expand All @@ -105,21 +131,21 @@ def quote_handles_valid():
@check50.check(can_login)
def buy_page():
"""buy page has all required elements"""
Finance().login("_cs50", "ohHai28!").validate_form("/buy", ["shares", "symbol"])
Finance().login("cs-50", "ohHai28!").validate_form("/buy", ["shares", "symbol"])


@check50.check(buy_page)
def buy_handles_invalid():
"""buy handles invalid ticker symbol"""
Finance().login("_cs50", "ohHai28!").transaction("/buy", "ZZZZ", "2").status(400)
Finance().login("cs-50", "ohHai28!").transaction("/buy", "ZZZZ", "2").status(400)


@check50.check(buy_page)
def buy_handles_incorrect_shares():
"""buy handles fractional, negative, and non-numeric shares"""
(
Finance()
.login("_cs50", "ohHai28!")
.login("cs-50", "ohHai28!")
.transaction("/buy", "AAAA", "-1")
.status(400)
.transaction("/buy", "AAAA", "1.5")
Expand All @@ -134,7 +160,7 @@ def buy_handles_valid():
"""buy handles valid purchase"""
(
Finance()
.login("_cs50", "ohHai28!")
.login("cs-50", "ohHai28!")
.transaction("/buy", "AAAA", "1")
.transaction("/buy", "AAAA", "3")
.content(r"112\.00", "112.00")
Expand All @@ -147,7 +173,7 @@ def sell_page():
"""sell page has all required elements"""
(
Finance()
.login("_cs50", "ohHai28!")
.login("cs-50", "ohHai28!")
.validate_form("/sell", ["shares"])
.validate_form("/sell", ["symbol"], field_tag="select")
)
Expand All @@ -156,15 +182,15 @@ def sell_page():
@check50.check(buy_handles_valid)
def sell_handles_invalid():
"""sell handles invalid number of shares"""
Finance().login("_cs50", "ohHai28!").transaction("/sell", "AAAA", "8").status(400)
Finance().login("cs-50", "ohHai28!").transaction("/sell", "AAAA", "8").status(400)


@check50.check(buy_handles_valid)
def sell_handles_valid():
"""sell handles valid sale"""
(
Finance()
.login("_cs50", "ohHai28!")
.login("cs-50", "ohHai28!")
.transaction("/sell", "AAAA", "2")
.content(r"56\.00", "56.00")
.content(r"9,?944\.00", "9,944.00")
Expand All @@ -176,7 +202,7 @@ def history_page():
"""history page shows transactions"""
(
Finance()
.login("_cs50", "ohHai28!")
.login("cs-50", "ohHai28!")
.get("/history")
.content(r"28\.00", "28.00")
.content("AAAA")
Expand Down