|
5 | 5 |
|
6 | 6 | import configparser |
7 | 7 |
|
| 8 | +from cli115.cmds.account import AccountCommand |
8 | 9 | from cli115.cmds.auth import AuthCommand, _parse_cookie_string |
9 | 10 | from cli115.cmds.config_cmd import ConfigCommand |
10 | 11 | from cli115.cmds.cp import CpCommand |
|
26 | 27 | from cli115.cmds.upload import UploadCommand |
27 | 28 | from cli115.cli import build_parser, main |
28 | 29 | from cli115.client.base import ( |
| 30 | + AccountInfo, |
29 | 31 | CloudTask, |
30 | 32 | Directory, |
31 | 33 | DownloadInfo, |
@@ -1092,5 +1094,66 @@ def test_config_command_registered_in_cli(self, mock_load): |
1092 | 1094 | self.assertIn("[general]", mock_out.getvalue()) |
1093 | 1095 |
|
1094 | 1096 |
|
| 1097 | +class TestAccountCommand(unittest.TestCase): |
| 1098 | + |
| 1099 | + @patch("cli115.cmds.account.BaseCommand._create_client") |
| 1100 | + def test_execute_prints_account_info(self, mock_create): |
| 1101 | + mock_client = MagicMock() |
| 1102 | + mock_client.account.info.return_value = AccountInfo( |
| 1103 | + user_name="testuser", |
| 1104 | + user_id=12345, |
| 1105 | + vip=True, |
| 1106 | + expire=datetime(2025, 1, 1), |
| 1107 | + ) |
| 1108 | + mock_create.return_value = mock_client |
| 1109 | + |
| 1110 | + cmd = AccountCommand() |
| 1111 | + import argparse |
| 1112 | + |
| 1113 | + args = argparse.Namespace(format="plain") |
| 1114 | + with patch("builtins.print") as mock_print: |
| 1115 | + cmd.execute(args) |
| 1116 | + output = mock_print.call_args[0][0] |
| 1117 | + self.assertIn("testuser", output) |
| 1118 | + self.assertIn("12345", output) |
| 1119 | + |
| 1120 | + @patch("cli115.cmds.account.BaseCommand._create_client") |
| 1121 | + def test_execute_json_format(self, mock_create): |
| 1122 | + mock_client = MagicMock() |
| 1123 | + mock_client.account.info.return_value = AccountInfo( |
| 1124 | + user_name="jsonuser", |
| 1125 | + user_id=99, |
| 1126 | + vip=False, |
| 1127 | + expire=None, |
| 1128 | + ) |
| 1129 | + mock_create.return_value = mock_client |
| 1130 | + |
| 1131 | + cmd = AccountCommand() |
| 1132 | + import argparse |
| 1133 | + |
| 1134 | + args = argparse.Namespace(format="json") |
| 1135 | + with patch("builtins.print") as mock_print: |
| 1136 | + cmd.execute(args) |
| 1137 | + import json |
| 1138 | + |
| 1139 | + output = json.loads(mock_print.call_args[0][0]) |
| 1140 | + self.assertEqual(output["Username"], "jsonuser") |
| 1141 | + self.assertEqual(output["User ID"], 99) |
| 1142 | + |
| 1143 | + @patch("cli115.cmds.account.BaseCommand._create_client") |
| 1144 | + def test_execute_error_exits(self, mock_create): |
| 1145 | + mock_client = MagicMock() |
| 1146 | + mock_client.account.info.side_effect = RuntimeError("API error") |
| 1147 | + mock_create.return_value = mock_client |
| 1148 | + |
| 1149 | + cmd = AccountCommand() |
| 1150 | + import argparse |
| 1151 | + |
| 1152 | + args = argparse.Namespace(format="plain") |
| 1153 | + with self.assertRaises(SystemExit) as ctx: |
| 1154 | + cmd.execute(args) |
| 1155 | + self.assertEqual(ctx.exception.code, 1) |
| 1156 | + |
| 1157 | + |
1095 | 1158 | if __name__ == "__main__": |
1096 | 1159 | unittest.main() |
0 commit comments