Skip to content

Commit

Permalink
login: error handling on login screen and test
Browse files Browse the repository at this point in the history
Closes #15 but it still needs better styling
  • Loading branch information
robvdl committed Mar 8, 2024
1 parent e528356 commit 23c6ae2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/sambal/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Optional

from ldb import LdbError
from samba.auth import system_session
from samba.credentials import Credentials
from samba.param import LoadParm
Expand All @@ -24,12 +25,15 @@ def connect_samdb(username, password, host, realm=None) -> Optional[SamDB]:
if realm:
creds.set_realm(realm)

return SamDB(
url=url,
session_info=system_session(),
credentials=creds,
lp=lp,
)
try:
return SamDB(
url=url,
session_info=system_session(),
credentials=creds,
lp=lp,
)
except LdbError:
return None


def get_samdb(request) -> Optional[SamDB]:
Expand Down
9 changes: 9 additions & 0 deletions src/sambal/templates/login.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
<body>
<h1>Domain Login</h1>
<form action="{{ request.route_path('login') }}" method="POST">
{% with errors = request.session.pop_flash(queue="error") %}
{% if errors %}
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<div>
{{ form.username.label }}
{{ form.username(autocomplete="username", placeholder="Account name") }}
Expand Down
18 changes: 18 additions & 0 deletions tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ def test_login_logout(testapp, settings):
assert "Sambal Login" in response.text


def test_login_invalid_credentials(testapp, settings):
response = testapp.get("/login/", status=200)
parser = LoginHTMLParser()
parser.feed(response.text)

login_form = {
"host": settings["samba.host"],
"username": "invalid",
"password": "invalid",
"realm": settings["samba.realm"],
"csrf_token": parser.csrf_token,
"return_url": parser.return_url,
}

response = testapp.post("/login/", login_form, status=200)
assert "Login to host failed" in response.text


def test_login_required(testapp):
response = testapp.get("/", status=200)
assert "Sambal Login" in response.text

0 comments on commit 23c6ae2

Please sign in to comment.