Skip to content
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

login: error handling on login screen and test #25

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
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
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
Loading