-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_github_actions_setup.py
More file actions
133 lines (102 loc) · 4.39 KB
/
Copy pathtest_github_actions_setup.py
File metadata and controls
133 lines (102 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import base64
import importlib.util
import json
import pytest
from pathlib import Path
from unittest.mock import patch, MagicMock
def load_setup_module():
spec = importlib.util.spec_from_file_location(
"github_actions_setup",
"cortexapps_cli/solutions/github-actions-deploy/setup.py",
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
@pytest.fixture
def mod():
return load_setup_module()
@pytest.fixture
def setup(mod, tmp_path):
instance = mod.GitHubActionsSetup(state_dir=tmp_path)
instance._answers = {
"github_token": "ghp_test",
"github_owner": "test-org",
"repo_name": "cortex-deploy-demo",
"cortex_api_key": "crt_testkey",
"cortex_base_url": "https://api.getcortexapp.com",
}
return instance
def test_get_authenticated_user(setup):
resp = MagicMock(status_code=200)
resp.json.return_value = {"login": "test-user"}
with patch("requests.get", return_value=resp):
assert setup._get_authenticated_user() == "test-user"
def test_create_repo_skips_if_exists(setup):
resp = MagicMock(status_code=200)
with patch("requests.get", return_value=resp) as mock_get, \
patch("requests.post") as mock_post:
setup._create_repo()
mock_get.assert_called_once()
mock_post.assert_not_called()
def test_create_repo_creates_when_missing(setup):
user_resp = MagicMock(status_code=200)
user_resp.json.return_value = {"login": "test-org"}
check_resp = MagicMock(status_code=404)
post_resp = MagicMock(status_code=201)
post_resp.json.return_value = {"html_url": "https://github.com/test-org/cortex-deploy-demo"}
get_responses = [check_resp, user_resp]
with patch("requests.get", side_effect=get_responses), \
patch("requests.post", return_value=post_resp) as mock_post:
setup._create_repo()
mock_post.assert_called_once()
def test_create_repo_raises_on_unexpected_status(setup):
resp = MagicMock(status_code=403)
resp.text = "Forbidden"
with patch("requests.get", return_value=resp), \
patch("requests.post") as mock_post:
with pytest.raises(RuntimeError, match="Unexpected status checking repo existence: 403"):
setup._create_repo()
mock_post.assert_not_called()
def test_seed_workflow_skips_if_unchanged(setup, tmp_path):
# Read the actual template to simulate matching content
template_path = Path("cortexapps_cli/solutions/github-actions-deploy/_templates/cortex-deploy.yml")
content = template_path.read_text()
content_b64 = base64.b64encode(content.encode()).decode()
resp = MagicMock(status_code=200)
resp.json.return_value = {"content": content_b64, "sha": "abc123"}
with patch("requests.get", return_value=resp), \
patch("requests.put") as mock_put:
setup._seed_workflow()
mock_put.assert_not_called()
def test_seed_workflow_creates_when_missing(setup):
get_resp = MagicMock(status_code=404)
put_resp = MagicMock(status_code=201)
put_resp.json.return_value = {"content": {"sha": "abc123"}}
with patch("requests.get", return_value=get_resp), \
patch("requests.put", return_value=put_resp) as mock_put:
setup._seed_workflow()
mock_put.assert_called_once()
def test_set_secret(setup):
# Valid Curve25519 public key (generated via nacl.public.PrivateKey.generate())
from nacl.public import PrivateKey
dummy_key = base64.b64encode(bytes(PrivateKey.generate().public_key)).decode()
key_resp = MagicMock(status_code=200)
key_resp.json.return_value = {"key_id": "key123", "key": dummy_key}
key_resp.raise_for_status = MagicMock()
put_resp = MagicMock(status_code=204)
with patch("requests.get", return_value=key_resp), \
patch("requests.put", return_value=put_resp) as mock_put:
setup._set_secret("CORTEX_API_KEY", "crt_testkey")
mock_put.assert_called_once()
call_json = mock_put.call_args.kwargs["json"]
assert "encrypted_value" in call_json
assert call_json["key_id"] == "key123"
def test_trigger_workflow(setup):
resp = MagicMock(status_code=204)
with patch("requests.post", return_value=resp) as mock_post:
setup._trigger_direct()
url = mock_post.call_args.args[0]
assert "dispatches" in url
assert mock_post.call_args.kwargs["json"] == {"ref": "main"}
def test_main_callable(mod):
assert callable(mod.main)