-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtest_antminer_change_password.py
More file actions
111 lines (84 loc) · 3.63 KB
/
test_antminer_change_password.py
File metadata and controls
111 lines (84 loc) · 3.63 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
"""Tests for Antminer stock firmware password change."""
import unittest
from unittest.mock import AsyncMock, patch
from pyasic.web.antminer import AntminerModernWebAPI
class TestAntminerModernChangePassword(unittest.IsolatedAsyncioTestCase):
"""Test AntminerModernWebAPI.change_password and the AntminerModern backend."""
async def test_web_api_success_updates_stored_password(self):
"""Successful passwd.cgi response updates self.pwd."""
# Arrange
api = AntminerModernWebAPI("192.168.1.1")
api.pwd = "old_password" # nosec B105 - test fixture
with patch.object(api, "send_command", new_callable=AsyncMock) as mock_send:
mock_send.return_value = {
"stats": "success",
"code": "M000",
"msg": "",
}
# Act
result = await api.change_password("new_password")
# Assert
mock_send.assert_awaited_once_with(
"passwd",
curPwd="old_password",
newPwd="new_password",
confirmPwd="new_password",
)
self.assertEqual(result["stats"], "success")
self.assertEqual(api.pwd, "new_password")
async def test_web_api_failure_preserves_stored_password(self):
"""Failed passwd.cgi response leaves self.pwd unchanged."""
# Arrange
api = AntminerModernWebAPI("192.168.1.1")
api.pwd = "old_password" # nosec B105 - test fixture
with patch.object(api, "send_command", new_callable=AsyncMock) as mock_send:
mock_send.return_value = {
"stats": "error",
"code": "E000",
"msg": "wrong password",
}
# Act
result = await api.change_password("new_password")
# Assert
self.assertEqual(result["stats"], "error")
self.assertEqual(api.pwd, "old_password")
async def test_backend_returns_true_on_success(self):
"""AntminerModern.change_password returns True when the API succeeds."""
from pyasic.miners.backends.antminer import AntminerModern
# Arrange
miner = AntminerModern("192.168.1.1")
with patch.object(
miner.web, "change_password", new_callable=AsyncMock
) as mock_change:
mock_change.return_value = {"stats": "success", "code": "M000", "msg": ""}
# Act
result = await miner.change_password("new_password")
# Assert
self.assertTrue(result)
mock_change.assert_awaited_once_with("new_password")
async def test_backend_returns_false_on_failure(self):
"""AntminerModern.change_password returns False when the API fails."""
from pyasic.miners.backends.antminer import AntminerModern
# Arrange
miner = AntminerModern("192.168.1.1")
with patch.object(
miner.web, "change_password", new_callable=AsyncMock
) as mock_change:
mock_change.return_value = {
"stats": "error",
"code": "E000",
"msg": "wrong password",
}
# Act
result = await miner.change_password("new_password")
# Assert
self.assertFalse(result)
async def test_base_miner_default_returns_false(self):
"""BaseMiner.change_password returns False by default."""
from pyasic.miners.base import BaseMiner
# Act
result = await BaseMiner.change_password(BaseMiner, "anything")
# Assert
self.assertFalse(result)
if __name__ == "__main__":
unittest.main()