-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Requires new environment variable only needed for testing. * SAMBAL_SAMBA_HOST * SAMBAL_SAMBA_USERNAME * SAMBAL_SAMBA_PASSWORD * SAMBAL_SAMBA_REALM They are loaded from environment variable so no secrets are put into the code. The csrf token is read from the from using HTMLParser which turned out to be a nice and simple solution for extracting these actually, and didn't require any additional libraries.
- Loading branch information
Showing
2 changed files
with
52 additions
and
2 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
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 |
---|---|---|
@@ -1,3 +1,46 @@ | ||
from html.parser import HTMLParser | ||
|
||
|
||
class LoginHTMLParser(HTMLParser): | ||
"""Simple HTML parser to extract csrf token using the standard library.""" | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.csrf_token = None | ||
self.return_url = None | ||
|
||
def handle_starttag(self, tag, attrs): | ||
if tag == "input": | ||
tag_attrs = dict(attrs) | ||
|
||
if tag_attrs["name"] == "csrf_token": | ||
self.csrf_token = tag_attrs["value"] | ||
|
||
if tag_attrs["name"] == "return_url": | ||
self.return_url = tag_attrs["value"] | ||
|
||
|
||
def test_login(testapp, settings): | ||
response = testapp.get("/login/", status=200) | ||
parser = LoginHTMLParser() | ||
parser.feed(response.text) | ||
|
||
login_form = { | ||
"host": settings["samba.host"], | ||
"username": settings["samba.username"], | ||
"password": settings["samba.password"], | ||
"realm": settings["samba.realm"], | ||
"csrf_token": parser.csrf_token, | ||
"return_url": parser.return_url, | ||
} | ||
|
||
response = testapp.post("/login/", login_form, status=302) | ||
assert response.headers["location"] == parser.return_url | ||
|
||
response = testapp.get("/", status=200) | ||
assert "Sambal Login" not in response.text | ||
|
||
|
||
def test_login_required(testapp): | ||
response = testapp.get("/", status=200) | ||
assert b"Sambal Login" in response.body | ||
assert "Sambal Login" in response.text |