Files
xieyi-weixin-/test_native_verification.py
T
2026-09-21 10:56:48 +08:00

107 lines
5.5 KiB
Python

"""Manual verification must stay bound to the selected build, account and contact."""
import contextlib
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest import mock
import wecom_native_verification as verification
import wecom_6018_validation as utility
class SendVerificationTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.path = Path(self.temp.name) / 'report.json'
self.live = dict(accountId='100', pid=1, clientVersion='5.0.11.6018', clientSha256='fixture')
self.enterContext(mock.patch.object(verification, 'discover', return_value=self.live))
self.database = self.enterContext(mock.patch('reply_database.LiveReplyDatabase._open_database'))
self.database.return_value._matching_conversations.return_value = [('100', 'S:100_200', '测试')]
self.database.return_value.get_conversation_context_by_id.return_value = {'messages': []}
self.sender = self.enterContext(mock.patch.object(verification, 'NativeSender'))
self.sender.return_value.send.return_value = {'status': 'confirmed', 'serverId': 123}
def run_test(self, *extra):
result = verification.run_send_test(['--protocol-send-test', '测试', '--report', str(self.path), *extra])
return result, json.loads(self.path.read_text(encoding='utf-8'))
def test_wrong_build_or_changed_account_cannot_open_database_or_send(self):
for argument in (('--expected-client', '5.0.10.6023'), ('--expected-account', '999')):
with self.subTest(argument=argument):
status, report = self.run_test(*argument)
self.assertEqual(status, 2)
self.assertEqual(report['status'], 'failed')
self.database.assert_not_called()
self.sender.assert_not_called()
def test_ambiguous_or_other_account_contact_is_rejected(self):
for matches in ([], [('999', 'S:999_200', '测试')],
[('100', 'S:100_200', '测试'), ('100', 'S:100_201', '测试')]):
self.database.return_value._matching_conversations.return_value = matches
status, _ = self.run_test()
self.assertEqual(status, 2)
self.sender.assert_not_called()
def test_contact_verification_notice_prevents_test_send(self):
self.database.return_value.get_conversation_context_by_id.return_value = {'messages': [
{'content_type': 1011, 'content': '开启了联系人验证,请先发送联系人验证请求'}]}
status, _ = self.run_test()
self.assertEqual(status, 2)
self.sender.assert_not_called()
def test_request_is_bound_to_client_build_and_guard_rechecks_contact(self):
status, report = self.run_test('--expected-client', '5.0.11.6018', '--expected-account', '100')
self.assertEqual(status, 0)
self.assertFalse(report['automaticListenerStarted'])
call = self.sender.return_value.send.call_args
self.assertIn(':5.0.11.6018:100:S:100_200', call.args[0])
self.assertEqual(call.args[4], '协议通道测试,请忽略')
self.assertTrue(call.kwargs['guard']())
self.database.return_value.get_conversation_context_by_id.return_value = None
self.assertFalse(call.kwargs['guard']())
self.database.assert_called_once_with(account='100')
class UtilityTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.output = Path(self.temp.name)
self.report = dict(clientVersion='5.0.11.6018', nativeAccount='100', status='native_validation_required')
self.probe = self.enterContext(mock.patch('wxwork_protocol_probe.probe_installed_client', return_value=self.report))
self.send = self.enterContext(mock.patch.object(verification, 'run_send_test'))
self.enterContext(contextlib.redirect_stdout(io.StringIO()))
def test_check_only_never_initializes_keys_or_prompts_or_sends(self):
ask = mock.Mock(side_effect=AssertionError('unexpected prompt'))
self.assertEqual(utility.run_validation(check_only=True, output=self.output, ask=ask), 0)
self.probe.assert_called_once_with(initialize_database=False)
self.send.assert_not_called()
def test_other_client_version_never_prompts_or_sends(self):
self.report['clientVersion'] = '5.0.10.6023'
ask = mock.Mock(side_effect=AssertionError('unexpected prompt'))
self.assertEqual(utility.run_validation(output=self.output, ask=ask), 2)
self.send.assert_not_called()
def test_cancelled_contact_or_confirmation_never_sends(self):
for answers in ([''], ['测试', '取消']):
self.assertEqual(utility.run_validation(output=self.output, ask=mock.Mock(side_effect=answers)), 2)
self.send.assert_not_called()
def test_explicit_send_keeps_account_and_build_checks(self):
def send(args):
path = Path(args[args.index('--report') + 1])
path.write_text(json.dumps({'status': 'confirmed'}), encoding='utf-8')
return 0
self.send.side_effect = send
self.assertEqual(utility.run_validation(output=self.output, ask=mock.Mock(side_effect=['测试', '发送'])), 0)
args = self.send.call_args.args[0]
self.assertEqual(args[args.index('--expected-client') + 1], '5.0.11.6018')
self.assertEqual(args[args.index('--expected-account') + 1], '100')
if __name__ == '__main__': unittest.main()