-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_unit.py
More file actions
139 lines (102 loc) · 3.67 KB
/
test_unit.py
File metadata and controls
139 lines (102 loc) · 3.67 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
134
135
136
137
138
139
import pytest
import main
@pytest.fixture(scope="session", autouse=True)
def init_boot():
main.BOX = {"port": 8000, "repo": {}}
yield
@pytest.fixture
def mock_fix(monkeypatch, mocker):
def mock_lumos(cmd, warning=True):
return 0
# monkeypatch.setattr("main.lumos", mock_lumos)
mocker.patch("main.lumos", return_value=0)
yield
def test_pull_success(mock_fix, tmp_path):
repo_path = tmp_path / "repo"
repo_path.mkdir()
(repo_path / ".git").mkdir()
assert main.pull(repo_path) is True
assert main.pull(repo_path, "git fetch && git rebase && make reload") is True
def test_pull_no_repo(mock_fix, tmp_path):
repo_empty_path = tmp_path / "repo_empty"
repo_nogit_path = tmp_path / "repo_nogit"
repo_nogit_path.mkdir()
assert main.pull(repo_empty_path) is None
assert main.pull(repo_nogit_path) is None
def test_pull_git_fetch_fail(mocker, tmp_path):
repo_path = tmp_path / "repo"
repo_path.mkdir()
(repo_path / ".git").mkdir()
mocker.patch("main.lumos", return_value=1)
assert main.pull(repo_path) is None
@pytest.fixture
def client(mocker):
"""创建一个 Flask 测试客户端"""
main.app.config["TESTING"] = True
client = main.app.test_client()
main.BOX["repo"] = {
"repo1": {"path": "/path/to/repo1", "secret": "secret1"},
"repo2": {"path": "/path/to/repo2", "secret": "secret2"},
"repo3": {"path": "/path/to/repo3", "secret": "secret3", "active": False},
}
mocker.patch("main.pull", return_value=True)
yield client
def test_main_get(client):
"""测试 GET 请求"""
response = client.get("/repo1")
assert response.data == b"Hello World!"
def test_main_post_success(client):
"""测试 POST 请求且签名验证成功"""
import hashlib
import hmac
repo = "repo1"
secret = main.BOX["repo"][repo]["secret"]
data = b"some data"
signature = (
"sha256="
+ hmac.new(
secret.encode("utf-8"), msg=data, digestmod=hashlib.sha256
).hexdigest()
)
response = client.post(
f"/{repo}", data=data, headers={"X-Hub-Signature-256": signature}
)
assert response.data == b"Pull executed successfully."
assert response.status_code == 200
def test_main_post_invalid_signature(client):
"""测试 POST 请求且签名验证失败"""
repo = "repo1"
secret = main.BOX["repo"][repo]["secret"]
data = b"some data"
invalid_signature = "sha256=invalidsignature"
response = client.post(
f"/{repo}", data=data, headers={"X-Hub-Signature-256": invalid_signature}
)
assert response.data == b"Invalid signature"
assert response.status_code == 400
def test_main_repo_inactive(client):
"""测试 config.toml active = false"""
repo = "repo3"
secret = main.BOX["repo"][repo]["secret"]
data = b"some data"
invalid_signature = "sha256=invalidsignature"
response = client.post(
f"/{repo}", data=data, headers={"X-Hub-Signature-256": invalid_signature}
)
assert response.data == b"No Active Repo [repo3] local."
assert response.status_code == 401
def test_main_repo_404(client):
"""测试 config.toml active = false"""
repo = "repo3"
secret = main.BOX["repo"][repo]["secret"]
data = b"some data"
invalid_signature = "sha256=invalidsignature"
response = client.post(
f"/", data=data, headers={"X-Hub-Signature-256": invalid_signature}
)
assert response.status_code == 404
response = client.post(
f"/empty", data=data, headers={"X-Hub-Signature-256": invalid_signature}
)
assert response.data == b"Not Found Repo [empty] local."
assert response.status_code == 404