Skip to content
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
8 changes: 5 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
// Make sure SELinux does not disable with access to host filesystems like tmp
"--security-opt=label=disable"
],
"forwardPorts": [
5173
],
"portsAttributes": {
"5173": {
"onAutoForward": "ignore"
}
},
"mounts": [
// Mount in the user terminal config folder so it can be edited
{
Expand Down
12 changes: 12 additions & 0 deletions notes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# refresh_token: dict[str, str] = (
# # pyright: ignore[reportUnknownVariableType]
# keycloak_openid.refresh_token( # pyright: ignore[reportUnknownMemberType]
# token["refresh_token"]
# )
# )
# print(refresh_token)

# save token and refresh token to file
# if they're present, check if access token is valid, if no but refresh token is
# , just refresh token
# if invalid, run whole thing
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ dependencies = [
"hera",
"requests",
"PyYAML",
"python-keycloak",
"pillow",
"numpy",
"h5py",
"dotenv",
] # Add project dependencies here, e.g. ["click", "numpy"]
dynamic = ["version"]
license.file = "LICENSE"
Expand All @@ -39,6 +44,7 @@ dev = [
"pillow",
"numpy",
"h5py",
"dotenv",
]

[project.scripts]
Expand Down
40 changes: 40 additions & 0 deletions src/copier_template/tests/test_keycloak_checker.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
from unittest.mock import MagicMock, patch

from pytest import mark

from {{project_name}}.auth.keycloak_checker import set_token_env_variable


@mark.parametrize("dev,port", [(True, 5173), (False, 8000)])
@patch("{{project_name}}.auth.keycloak_checker.KeycloakOpenID")
@patch("{{project_name}}.auth.keycloak_checker.generate_code_verifier")
@patch("{{project_name}}.auth.keycloak_checker.generate_code_challenge")
@patch("{{project_name}}.auth.keycloak_checker.open_auth_url")
def test_return_key(
mock_open_auth_url: MagicMock,
mock_gen_code_challenge: MagicMock,
mock_gen_code_verifier: MagicMock,
mock_gen_keycloak_id: MagicMock,
dev: bool,
port: int,
):
mock_gen_code_verifier.return_value = "verifier"
mock_gen_code_challenge.return_value = ("challenge", "S256")
os.environ["AUTH"] = "auth_url_code"
keycloak = MagicMock()
mock_gen_keycloak_id.return_value = keycloak

keycloak.auth_url.return_value = "https://mock.site"
keycloak.token.return_value = {
"access_token": "fake_token",
"refresh_token": "fake_refresh",
}
assert set_token_env_variable(dev) == "fake_token"
mock_open_auth_url.assert_called_once_with("https://mock.site", port)
keycloak.token.assert_called_once_with(
grant_type="authorization_code",
code="auth_url_code",
redirect_uri=f"http://localhost:{port}/",
code_verifier="verifier",
)
80 changes: 80 additions & 0 deletions src/copier_template/tests/test_open_auth_url.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import os
from unittest.mock import MagicMock, patch

from {{project_name}}.auth.open_auth_url import (
CallbackHandler,
open_auth_url,
)


@patch("{{project_name}}.auth.open_auth_url.webbrowser.open")
@patch("{{project_name}}.auth.open_auth_url._ReusingHTTPServer")
def test_open_auth_url_normal_function(
mock_http_server: MagicMock,
mock_open_browser: MagicMock,
):
server = mock_http_server.return_value
server.auth_code = "this_is_your_code"
open_auth_url("url", 5173)
server.handle_request.assert_called_once()
mock_open_browser.assert_called_once_with("url")
server.socket.shutdown.assert_called_once()
server.server_close.assert_called_once()
assert os.environ["AUTH"] == "this_is_your_code"


@patch("{{project_name}}.auth.open_auth_url.exit")
@patch("{{project_name}}.auth.open_auth_url.webbrowser.open")
@patch("{{project_name}}.auth.open_auth_url._ReusingHTTPServer")
def test_open_auth_url_raises_error(
mock_http_server: MagicMock,
mock_open_browser: MagicMock,
mock_exit: MagicMock,
):
server = mock_http_server.return_value
server.auth_code = "this_is_your_code"
server.handle_request.side_effect = OSError
open_auth_url("url", 5173)
mock_open_browser.assert_called_once_with("url")
assert os.environ["AUTH"] == ""
mock_exit.assert_called_once_with(1)
server.socket.shutdown.assert_called_once()
server.server_close.assert_called_once()


def test_handler_normal_function():
handler = CallbackHandler.__new__(CallbackHandler)
handler.path = "/?code=this_is_your_code"

handler.server = MagicMock()

handler.send_response = MagicMock()
handler.end_headers = MagicMock()
handler.wfile = MagicMock()
handler.wfile.write = MagicMock()

handler.do_GET()

assert handler.server.auth_code == "this_is_your_code"
handler.send_response.assert_called_once_with(200)
handler.wfile.write.assert_called_once_with(
b"Authorization successful. You can close this window."
)


def test_handler_error_response():
handler = CallbackHandler.__new__(CallbackHandler)
handler.path = "/"

handler.server = MagicMock()

handler.send_response = MagicMock()
handler.end_headers = MagicMock()
handler.wfile = MagicMock()
handler.wfile.write = MagicMock()

handler.do_GET()

handler.send_response.assert_called_once_with(400)
handler.end_headers.assert_called_once()
handler.wfile.write.assert_called_once_with(b"Missing authorization code.")
8 changes: 5 additions & 3 deletions src/copier_template/tests/test_submit_to_graphql.py.jinja
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from unittest.mock import MagicMock, patch

from {{ project_name }}.submit_to_graphql import submit_to_graphql
from {{project_name}}.submit_to_graphql import submit_to_graphql


@patch("{{ project_name }}.submit_to_graphql.Client")
def test_submit_to_graphql(mock_client: MagicMock):
@patch("{{project_name}}.submit_to_graphql.set_token_env_variable")
@patch("{{project_name}}.submit_to_graphql.Client")
def test_submit_to_graphql(mock_client: MagicMock, mock_key: MagicMock):
mock_key.return_value = "fake_token"
mock_instance = MagicMock()
mock_client.return_value = mock_instance
mock_instance.execute.return_value = {
Expand Down
181 changes: 0 additions & 181 deletions src/python_interface_to_workflows/auth/get_token.py

This file was deleted.

Loading
Loading