Skip to content

Commit

Permalink
Add template test cases and tox
Browse files Browse the repository at this point in the history
  • Loading branch information
rayluo committed Oct 7, 2024
1 parent 0f55267 commit 9c46e23
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
14 changes: 14 additions & 0 deletions tests/test_django.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
from unittest import mock

import pytest

from identity.django import _parse_redirect_uri, Auth


def test_parse_redirect_uri():
with pytest.raises(ValueError):
_parse_redirect_uri("https://example.com")
Expand All @@ -21,3 +24,14 @@ def test_configuration_errors_should_be_delivered_to_log_in():
mock_render_auth_error.assert_called_once_with(
mock.ANY, error="configuration_error", error_description=mock.ANY)

# I don't know how to create a dummy Django app on-the-fly, here we do this instead
def test_the_installed_package_contains_builtin_templates():
import identity
templates_needed = {"login.html", "auth_error.html"}
templates_found = set()
for path in identity.__path__:
for t in templates_needed:
if os.path.exists(os.path.join(path, "templates", "identity", t)):
templates_found.add(t)
assert templates_needed == templates_found

21 changes: 21 additions & 0 deletions tests/test_flask.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from unittest.mock import patch, Mock

from flask import Flask

from identity.flask import Auth


Expand All @@ -11,3 +14,21 @@ def test_logout():
assert auth._request.host_url in auth.logout().get_data(as_text=True), (
"The host_url should be in the logout URL. There was a bug in 0.9.0.")

@patch("msal.authority.tenant_discovery", new=Mock(return_value={
"authorization_endpoint": "https://example.com/placeholder",
"token_endpoint": "https://example.com/placeholder",
}))
def test_login_should_locate_its_template():
app = Flask(__name__)
app.config["SESSION_TYPE"] = "filesystem" # Required for Flask-session,
# see also https://stackoverflow.com/questions/26080872
client_id = str(hash(app))
auth = Auth(
app,
client_id=client_id,
redirect_uri="http://localhost:5000/redirect", # To use auth code flow
oidc_authority="https://example.com/foo",
)
with app.test_request_context("/", method="GET"):
assert client_id in auth.login() # Proper template output contains client_id

2 changes: 1 addition & 1 deletion tests/test_quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from identity.quart import Auth


@pytest.mark.asyncio
@pytest.mark.asyncio(loop_scope="session")
async def test_login(monkeypatch):
app = Quart(__name__)
app.config["SESSION_TYPE"] = "redis"
Expand Down
15 changes: 15 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tox]
env_list =
py3
minversion = 4.21.2

[testenv]
description = run the tests with pytest
package = wheel
wheel_build_env = .pkg
deps =
pytest>=6
-r requirements.txt
commands =
pip list
pytest {tty:--color=yes} -o asyncio_default_fixture_loop_scope=function {posargs}

0 comments on commit 9c46e23

Please sign in to comment.