Skip to content

Commit

Permalink
Add setting for enable_auth
Browse files Browse the repository at this point in the history
  • Loading branch information
davismr committed Jan 9, 2025
1 parent 1c6234e commit 3a0b503
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use = egg:wfrp.character

wfrp.character.secret = secret
wfrp.character.enable_auth = true

pyramid.reload_templates = true
pyramid.debug_authorization = false
Expand Down
18 changes: 11 additions & 7 deletions src/wfrp/character/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ def configure_app(global_config, **settings):
engine = engine_from_config(settings, "sqlalchemy.")
DBSession.configure(bind=engine)
Base.metadata.bind = engine
if "wfrp.character.enable_auth" not in settings:
settings["wfrp.character.enable_auth"] = False
enable_auth = settings.get("wfrp.character.enable_auth")
config = Configurator(settings=settings)
# to prevent circular imports
from wfrp.character.security import SecurityPolicy

config.set_security_policy(
SecurityPolicy(
secret=settings["wfrp.character.secret"],
),
)
if enable_auth is True:
from wfrp.character.security import SecurityPolicy

config.set_security_policy(
SecurityPolicy(
secret=settings["wfrp.character.secret"],
),
)
config.include("wfrp.character.routes")
config.add_static_view("static", "wfrp.character:static")
config.add_static_view("static_deform", "deform:static")
Expand Down
2 changes: 1 addition & 1 deletion src/wfrp/character/templates/layout.pt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<div><a href="/">Homepage</a></div>
<div><a href="/character/new">Create new character</a></div>
<div><a href="/links">Links</a></div>
<div>
<div tal:condition="request.registry.settings['wfrp.character.enable_auth']">
<a tal:condition="view.logged_in is None"
href="${request.application_url}/login">Log In</a>
<br tal:condition="view.logged_in is None" />
Expand Down
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import pytest
from factories import CharacterFactory
from pyramid import testing
from pyramid.paster import get_appsettings
from pytest_factoryboy import register
from sqlalchemy import engine_from_config
from webtest import TestApp

from wfrp.character.application import Base
from wfrp.character.application import DBSession
from wfrp.character.application import dbsession
from wfrp.character.application import main
from wfrp.character.models.character import Character
from wfrp.character.security import SecurityPolicy

Expand All @@ -19,7 +21,8 @@
@pytest.fixture(scope="session")
def testapp():
engine = engine_from_config({"sqlalchemy.url": "sqlite:///:memory:"}, "sqlalchemy.")
config = testing.setUp()
settings = {"wfrp.character.enable_auth": False}
config = testing.setUp(settings=settings)
config.add_request_method(dbsession, reify=True)
config.set_security_policy(
SecurityPolicy(
Expand All @@ -37,6 +40,13 @@ def testapp():
return TestApp(config.make_wsgi_app())


@pytest.fixture(scope="session")
def testapp_auth():
settings = get_appsettings("development.ini", name="main")
testapp = TestApp(main(None, **settings))
return testapp


@pytest.fixture
def new_character(testapp):
new_uuid = str(uuid.uuid4())
Expand Down
6 changes: 6 additions & 0 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def test_home_view():
@pytest.mark.package
def test_home_template(testapp):
response = testapp.get("/", status=200)
assert "Warhammer Fantasy Roleplay Character generator" in response.ubody


@pytest.mark.package
def test_home_template_login(testapp_auth):
response = testapp_auth.get("/", status=200)
assert 'href="http://localhost/login">Log In</a>' in response.ubody


Expand Down

0 comments on commit 3a0b503

Please sign in to comment.