Add fn to ensure a url matches an app url pattern
In some functions, we redirect a user based on a parameter in a query string. This commit adds a function that checks to see if a given url matches a url pattern of a view function. This will help us ensure that the url passed as the next parameter isn't malicious.
This commit is contained in:
@@ -1,7 +1,36 @@
|
||||
from tests.factories import UserFactory
|
||||
from tests.factories import UserFactory, PortfolioFactory
|
||||
from atst.routes import match_url_pattern
|
||||
|
||||
|
||||
def test_root_redirects_if_user_is_logged_in(client, user_session):
|
||||
user_session(UserFactory.create())
|
||||
response = client.get("/", follow_redirects=False)
|
||||
assert "home" in response.location
|
||||
|
||||
|
||||
def test_match_url_pattern(client):
|
||||
|
||||
assert not match_url_pattern(None)
|
||||
assert match_url_pattern("/home") == "/home"
|
||||
|
||||
portfolio = PortfolioFactory()
|
||||
# matches a URL with an argument
|
||||
assert (
|
||||
match_url_pattern(f"/portfolios/{portfolio.id}") # /portfolios/<portfolio_id>
|
||||
== f"/portfolios/{portfolio.id}"
|
||||
)
|
||||
# matches a url with a query string
|
||||
assert (
|
||||
match_url_pattern(f"/portfolios/{portfolio.id}?foo=bar")
|
||||
== f"/portfolios/{portfolio.id}?foo=bar"
|
||||
)
|
||||
# matches a URL only with a valid method
|
||||
assert not match_url_pattern(f"/portfolios/{portfolio.id}/edit")
|
||||
assert (
|
||||
match_url_pattern(f"/portfolios/{portfolio.id}/edit", method="POST")
|
||||
== f"/portfolios/{portfolio.id}/edit"
|
||||
)
|
||||
|
||||
# returns None for URL that doesn't match a view function
|
||||
assert not match_url_pattern("/pwned")
|
||||
assert not match_url_pattern("http://www.hackersite.com/pwned")
|
||||
|
@@ -28,7 +28,7 @@ def test_user_can_update_profile(user_session, client):
|
||||
def test_user_is_redirected_when_updating_profile(user_session, client):
|
||||
user = UserFactory.create()
|
||||
user_session(user)
|
||||
next_url = "/requests"
|
||||
next_url = "/home"
|
||||
|
||||
user_data = user.to_dictionary()
|
||||
user_data["date_latest_training"] = user_data["date_latest_training"].strftime(
|
||||
|
Reference in New Issue
Block a user