Skip to content

Commit 83d5447

Browse files
committed
feat(auth): adds copies of tests to the copier template
1 parent 7f4b8fc commit 83d5447

3 files changed

Lines changed: 125 additions & 3 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
from unittest.mock import MagicMock, patch
3+
4+
from pytest import mark
5+
6+
from {{project_name}}.auth.keycloak_checker import set_token_env_variable
7+
8+
9+
@mark.parametrize("dev,port", [(True, 5173), (False, 8000)])
10+
@patch("{{project_name}}.auth.keycloak_checker.KeycloakOpenID")
11+
@patch("{{project_name}}.auth.keycloak_checker.generate_code_verifier")
12+
@patch("{{project_name}}.auth.keycloak_checker.generate_code_challenge")
13+
@patch("{{project_name}}.auth.keycloak_checker.open_auth_url")
14+
def test_return_key(
15+
mock_open_auth_url: MagicMock,
16+
mock_gen_code_challenge: MagicMock,
17+
mock_gen_code_verifier: MagicMock,
18+
mock_gen_keycloak_id: MagicMock,
19+
dev: bool,
20+
port: int,
21+
):
22+
mock_gen_code_verifier.return_value = "verifier"
23+
mock_gen_code_challenge.return_value = ("challenge", "S256")
24+
os.environ["AUTH"] = "auth_url_code"
25+
keycloak = MagicMock()
26+
mock_gen_keycloak_id.return_value = keycloak
27+
28+
keycloak.auth_url.return_value = "https://mock.site"
29+
keycloak.token.return_value = {
30+
"access_token": "fake_token",
31+
"refresh_token": "fake_refresh",
32+
}
33+
assert set_token_env_variable(dev) == "fake_token"
34+
mock_open_auth_url.assert_called_once_with("https://mock.site", port)
35+
keycloak.token.assert_called_once_with(
36+
grant_type="authorization_code",
37+
code="auth_url_code",
38+
redirect_uri=f"http://localhost:{port}/",
39+
code_verifier="verifier",
40+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
from unittest.mock import MagicMock, patch
3+
4+
from {{project_name}}.auth.open_auth_url import (
5+
CallbackHandler,
6+
open_auth_url,
7+
)
8+
9+
10+
@patch("{{project_name}}.auth.open_auth_url.webbrowser.open")
11+
@patch("{{project_name}}.auth.open_auth_url._ReusingHTTPServer")
12+
def test_open_auth_url_normal_function(
13+
mock_http_server: MagicMock,
14+
mock_open_browser: MagicMock,
15+
):
16+
server = mock_http_server.return_value
17+
server.auth_code = "this_is_your_code"
18+
open_auth_url("url", 5173)
19+
server.handle_request.assert_called_once()
20+
mock_open_browser.assert_called_once_with("url")
21+
server.socket.shutdown.assert_called_once()
22+
server.server_close.assert_called_once()
23+
assert os.environ["AUTH"] == "this_is_your_code"
24+
25+
26+
@patch("{{project_name}}.auth.open_auth_url.exit")
27+
@patch("{{project_name}}.auth.open_auth_url.webbrowser.open")
28+
@patch("{{project_name}}.auth.open_auth_url._ReusingHTTPServer")
29+
def test_open_auth_url_raises_error(
30+
mock_http_server: MagicMock,
31+
mock_open_browser: MagicMock,
32+
mock_exit: MagicMock,
33+
):
34+
server = mock_http_server.return_value
35+
server.auth_code = "this_is_your_code"
36+
server.handle_request.side_effect = OSError
37+
open_auth_url("url", 5173)
38+
mock_open_browser.assert_called_once_with("url")
39+
assert os.environ["AUTH"] == ""
40+
mock_exit.assert_called_once_with(1)
41+
server.socket.shutdown.assert_called_once()
42+
server.server_close.assert_called_once()
43+
44+
45+
def test_handler_normal_function():
46+
handler = CallbackHandler.__new__(CallbackHandler)
47+
handler.path = "/?code=this_is_your_code"
48+
49+
handler.server = MagicMock()
50+
51+
handler.send_response = MagicMock()
52+
handler.end_headers = MagicMock()
53+
handler.wfile = MagicMock()
54+
handler.wfile.write = MagicMock()
55+
56+
handler.do_GET()
57+
58+
assert handler.server.auth_code == "this_is_your_code"
59+
handler.send_response.assert_called_once_with(200)
60+
handler.wfile.write.assert_called_once_with(
61+
b"Authorization successful. You can close this window."
62+
)
63+
64+
65+
def test_handler_error_response():
66+
handler = CallbackHandler.__new__(CallbackHandler)
67+
handler.path = "/"
68+
69+
handler.server = MagicMock()
70+
71+
handler.send_response = MagicMock()
72+
handler.end_headers = MagicMock()
73+
handler.wfile = MagicMock()
74+
handler.wfile.write = MagicMock()
75+
76+
handler.do_GET()
77+
78+
handler.send_response.assert_called_once_with(400)
79+
handler.end_headers.assert_called_once()
80+
handler.wfile.write.assert_called_once_with(b"Missing authorization code.")

src/copier_template/tests/test_submit_to_graphql.py.jinja

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from unittest.mock import MagicMock, patch
22

3-
from {{ project_name }}.submit_to_graphql import submit_to_graphql
3+
from {{project_name}}.submit_to_graphql import submit_to_graphql
44

55

6-
@patch("{{ project_name }}.submit_to_graphql.Client")
7-
def test_submit_to_graphql(mock_client: MagicMock):
6+
@patch("{{project_name}}.submit_to_graphql.set_token_env_variable")
7+
@patch("{{project_name}}.submit_to_graphql.Client")
8+
def test_submit_to_graphql(mock_client: MagicMock, mock_key: MagicMock):
9+
mock_key.return_value = "fake_token"
810
mock_instance = MagicMock()
911
mock_client.return_value = mock_instance
1012
mock_instance.execute.return_value = {

0 commit comments

Comments
 (0)