Coverage for src/meshadmin/cli/tests/test_template.py: 100%
57 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-07 19:26 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-07 19:26 +0200
1import httpx
2import pytest
3from typer.testing import CliRunner
5from meshadmin.cli.main import app
7runner = CliRunner()
9MOCK_TEMPLATE_CREATE = {"id": 1, "name": "test-template", "enrollment_key": "abc123"}
11MOCK_TEMPLATE_TOKEN = "1234567890"
14@pytest.fixture
15def mock_template_access_token(mocker):
16 return mocker.patch(
17 "meshadmin.cli.commands.template.get_access_token", return_value="fake-token"
18 )
21@pytest.fixture
22def mock_template_context_config(mocker):
23 return mocker.patch(
24 "meshadmin.cli.commands.template.get_context_config",
25 return_value={"endpoint": "http://testserver"},
26 )
29def test_create_template_success(
30 mocker, mock_template_access_token, mock_template_context_config
31):
32 mock_response = httpx.Response(
33 status_code=200,
34 json=MOCK_TEMPLATE_CREATE,
35 request=httpx.Request("POST", "http://testserver/api/v1/templates"),
36 )
37 mock_post = mocker.patch("httpx.post", return_value=mock_response)
38 result = runner.invoke(
39 app,
40 [
41 "template",
42 "create",
43 "test-network",
44 "test-template",
45 "true",
46 "false",
47 "false",
48 ],
49 )
50 assert result.exit_code == 0
51 mock_post.assert_called_once()
52 assert "test-template" in result.stdout
53 assert "abc123" in result.stdout
56def test_create_template_auth_failure(
57 mock_template_access_token, mock_template_context_config
58):
59 mock_template_access_token.side_effect = Exception("Auth failed")
60 result = runner.invoke(
61 app,
62 [
63 "template",
64 "create",
65 "test-network",
66 "test-template",
67 "true",
68 "false",
69 "false",
70 ],
71 )
72 assert result.exit_code == 1
73 assert "failed to get access token" in result.stdout
76def test_get_template_token_success(
77 mocker, mock_template_access_token, mock_template_context_config
78):
79 mock_response = httpx.Response(
80 status_code=200,
81 text=MOCK_TEMPLATE_TOKEN,
82 request=httpx.Request(
83 "GET", "http://testserver/api/v1/templates/test-template/token"
84 ),
85 )
86 mock_get = mocker.patch("httpx.get", return_value=mock_response)
87 result = runner.invoke(app, ["template", "get-token", "test-template"])
88 assert result.exit_code == 0
89 mock_get.assert_called_once_with(
90 "http://testserver/api/v1/templates/test-template/token",
91 params=None,
92 headers={"Authorization": "Bearer fake-token"},
93 )
94 assert MOCK_TEMPLATE_TOKEN in result.stdout
97def test_get_template_token_ttl(
98 mocker, mock_template_access_token, mock_template_context_config
99):
100 mock_response = httpx.Response(
101 status_code=200,
102 text=MOCK_TEMPLATE_TOKEN,
103 request=httpx.Request(
104 "GET", "http://testserver/api/v1/templates/test-template/token"
105 ),
106 )
107 mock_get = mocker.patch("httpx.get", return_value=mock_response)
108 result = runner.invoke(
109 app, ["template", "get-token", "test-template", "--ttl", "1000"]
110 )
111 assert result.exit_code == 0
112 mock_get.assert_called_once_with(
113 "http://testserver/api/v1/templates/test-template/token",
114 params={"ttl": 1000},
115 headers={"Authorization": "Bearer fake-token"},
116 )
117 assert MOCK_TEMPLATE_TOKEN in result.stdout
120def test_get_template_token_auth_failure(
121 mock_template_access_token, mock_template_context_config
122):
123 mock_template_access_token.side_effect = Exception("Auth failed")
124 result = runner.invoke(app, ["template", "get-token", "test-template"])
125 assert result.exit_code == 1
126 assert "failed to get access token" in result.stdout
129def test_delete_template_success(
130 mocker, mock_template_access_token, mock_template_context_config
131):
132 mock_response = httpx.Response(
133 status_code=200,
134 json={"message": "Template test-template deleted"},
135 request=httpx.Request(
136 "DELETE", "http://testserver/api/v1/templates/test-template"
137 ),
138 )
139 mock_delete = mocker.patch("httpx.delete", return_value=mock_response)
140 result = runner.invoke(app, ["template", "delete", "test-template"])
141 assert result.exit_code == 0
142 mock_delete.assert_called_once_with(
143 "http://testserver/api/v1/templates/test-template",
144 headers={"Authorization": "Bearer fake-token"},
145 )
146 assert "deleted" in result.stdout.lower()
149def test_delete_template_auth_failure(
150 mock_template_access_token, mock_template_context_config
151):
152 mock_template_access_token.side_effect = Exception("Auth failed")
153 result = runner.invoke(app, ["template", "delete", "test-template"])
154 assert result.exit_code == 1
155 assert "failed to get access token" in result.stdout