From 1f902169926e61c8383109b391699ea8419674b0 Mon Sep 17 00:00:00 2001 From: Rob van der Linde Date: Sat, 9 Mar 2024 09:42:19 +1300 Subject: [PATCH] login: error handling on login screen and test Closes #15 but it still needs better styling --- src/sambal/client.py | 16 ++++++++++------ src/sambal/templates/login.jinja2 | 9 +++++++++ tests/test_login.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/sambal/client.py b/src/sambal/client.py index 5663d6f..63e763f 100644 --- a/src/sambal/client.py +++ b/src/sambal/client.py @@ -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 @@ -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]: diff --git a/src/sambal/templates/login.jinja2 b/src/sambal/templates/login.jinja2 index 6772332..4b1139a 100644 --- a/src/sambal/templates/login.jinja2 +++ b/src/sambal/templates/login.jinja2 @@ -13,6 +13,15 @@

Domain Login

+ {% with errors = request.session.pop_flash(queue="error") %} + {% if errors %} + + {% endif %} + {% endwith %}
{{ form.username.label }} {{ form.username(autocomplete="username", placeholder="Account name") }} diff --git a/tests/test_login.py b/tests/test_login.py index f1fa7e1..fa4ed43 100644 --- a/tests/test_login.py +++ b/tests/test_login.py @@ -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