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

tests: basic login test #24

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
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest
import webtest
from pyramid.scripting import prepare
Expand All @@ -8,7 +10,12 @@
@pytest.fixture(scope="session")
def settings():
"""Fixture that returns the Pyramid settings dict."""
return sambal.SETTINGS
test_settings = dict(sambal.SETTINGS)
test_settings["samba.host"] = os.getenv("SAMBAL_SAMBA_HOST")
test_settings["samba.username"] = os.getenv("SAMBAL_SAMBA_USERNAME")
test_settings["samba.password"] = os.getenv("SAMBAL_SAMBA_PASSWORD")
test_settings["samba.realm"] = os.getenv("SAMBAL_SAMBA_REALM")
return test_settings


@pytest.fixture(scope="session")
Expand Down
45 changes: 44 additions & 1 deletion tests/test_login.py
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
Loading