|
16 | 16 | App, |
17 | 17 | ) |
18 | 18 | from txstratum.manager import TxMiningManager |
| 19 | +from txstratum.middleware import VERSION_CHECK_ERROR_MESSAGE |
19 | 20 | from txstratum.utils import tx_or_block_from_bytes |
20 | 21 |
|
21 | 22 | from .tx_examples import INVALID_TX_DATA |
@@ -123,11 +124,9 @@ def get_timestamp(tx_bytes: bytes) -> int: |
123 | 124 | return tx.timestamp |
124 | 125 |
|
125 | 126 |
|
126 | | -class AppTestCase(AioHTTPTestCase): |
127 | | - async def get_application(self): |
128 | | - self.manager = TxMiningManager(backend=None, pubsub=MagicMock(), address=None) |
129 | | - self.myapp = App(self.manager) |
130 | | - return self.myapp.app |
| 127 | +class BaseAppTestCase(AioHTTPTestCase): |
| 128 | + __test__ = False |
| 129 | + version_check: bool |
131 | 130 |
|
132 | 131 | @unittest_run_loop |
133 | 132 | async def test_health_check(self): |
@@ -486,3 +485,197 @@ async def test_submit_job_fail_script_data26_non_standard(self): |
486 | 485 | data = await resp.json() |
487 | 486 | self.assertEqual(400, resp.status) |
488 | 487 | self.assertEqual({"error": "non-standard-tx"}, data) |
| 488 | + |
| 489 | + @unittest_run_loop |
| 490 | + async def test_check_wallet_version(self): |
| 491 | + tx_hex = update_timestamp(TX_SCRIPT_DATA25).hex() |
| 492 | + |
| 493 | + header_desktop_lower = { |
| 494 | + "User-Agent": ( |
| 495 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" |
| 496 | + "HathorWallet/0.22.1 Chrome/73.0.3683.121 Electron/5.0.13 Safari/537.36 HathorWallet/0.22.1" |
| 497 | + ) |
| 498 | + } |
| 499 | + header_desktop_equal = { |
| 500 | + "User-Agent": ( |
| 501 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" |
| 502 | + "HathorWallet/0.23.0 Chrome/73.0.3683.121 Electron/5.0.13 Safari/537.36 HathorWallet/0.23.0" |
| 503 | + ) |
| 504 | + } |
| 505 | + header_desktop_higher = { |
| 506 | + "User-Agent": ( |
| 507 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" |
| 508 | + "HathorWallet/0.23.1 Chrome/73.0.3683.121 Electron/5.0.13 Safari/537.36 HathorWallet/0.23.1" |
| 509 | + ) |
| 510 | + } |
| 511 | + |
| 512 | + # Success with desktop version higher than the minimum |
| 513 | + resp = await self.client.request( |
| 514 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_desktop_higher |
| 515 | + ) |
| 516 | + await resp.json() |
| 517 | + self.assertEqual(200, resp.status) |
| 518 | + |
| 519 | + # Success with desktop version equal to the minimum |
| 520 | + resp = await self.client.request( |
| 521 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_desktop_equal |
| 522 | + ) |
| 523 | + await resp.json() |
| 524 | + self.assertEqual(200, resp.status) |
| 525 | + |
| 526 | + # Error with desktop version lower than the minimum if the app is validating desktop version |
| 527 | + resp = await self.client.request( |
| 528 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_desktop_lower |
| 529 | + ) |
| 530 | + data = await resp.json() |
| 531 | + if self.version_check: |
| 532 | + self.assertEqual(400, resp.status) |
| 533 | + self.assertEqual( |
| 534 | + { |
| 535 | + "error": VERSION_CHECK_ERROR_MESSAGE, |
| 536 | + "data": {"min_version": "0.23.0", "version": "0.22.1"}, |
| 537 | + }, |
| 538 | + data, |
| 539 | + ) |
| 540 | + else: |
| 541 | + self.assertEqual(200, resp.status) |
| 542 | + |
| 543 | + header_mobile_lower = {"User-Agent": "Hathor Wallet Mobile / 0.18.0"} |
| 544 | + header_mobile_equal = {"User-Agent": "Hathor Wallet Mobile / 1.18.3"} |
| 545 | + header_mobile_higher = {"User-Agent": "Hathor Wallet Mobile / 20.1.0"} |
| 546 | + header_mobile_custom = {"User-Agent": "HathorMobile/1"} |
| 547 | + header_mobile_custom_wrong = {"User-Agent": "HathorMobile/2"} |
| 548 | + |
| 549 | + # Success with mobile version higher than the minimum |
| 550 | + resp = await self.client.request( |
| 551 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_mobile_higher |
| 552 | + ) |
| 553 | + await resp.json() |
| 554 | + self.assertEqual(200, resp.status) |
| 555 | + |
| 556 | + # Success with mobile version equal to the minimum |
| 557 | + resp = await self.client.request( |
| 558 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_mobile_equal |
| 559 | + ) |
| 560 | + await resp.json() |
| 561 | + self.assertEqual(200, resp.status) |
| 562 | + |
| 563 | + # Error with mobile version lower than the minimum if the app is validating mobile version |
| 564 | + resp = await self.client.request( |
| 565 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_mobile_lower |
| 566 | + ) |
| 567 | + data = await resp.json() |
| 568 | + if self.version_check: |
| 569 | + self.assertEqual(400, resp.status) |
| 570 | + self.assertEqual( |
| 571 | + { |
| 572 | + "error": VERSION_CHECK_ERROR_MESSAGE, |
| 573 | + "data": {"min_version": "1.18.3", "version": "0.18.0"}, |
| 574 | + }, |
| 575 | + data, |
| 576 | + ) |
| 577 | + else: |
| 578 | + self.assertEqual(200, resp.status) |
| 579 | + |
| 580 | + # Error with mobile version before v0.18.0, if the app is validating mobile version |
| 581 | + resp = await self.client.request( |
| 582 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_mobile_custom |
| 583 | + ) |
| 584 | + data = await resp.json() |
| 585 | + if self.version_check: |
| 586 | + self.assertEqual(400, resp.status) |
| 587 | + self.assertEqual( |
| 588 | + { |
| 589 | + "error": VERSION_CHECK_ERROR_MESSAGE, |
| 590 | + "data": {"min_version": "1.18.3"}, |
| 591 | + }, |
| 592 | + data, |
| 593 | + ) |
| 594 | + else: |
| 595 | + self.assertEqual(200, resp.status) |
| 596 | + |
| 597 | + # Success if the custom is different than expected |
| 598 | + resp = await self.client.request( |
| 599 | + "POST", |
| 600 | + "/submit-job", |
| 601 | + json={"tx": tx_hex}, |
| 602 | + headers=header_mobile_custom_wrong, |
| 603 | + ) |
| 604 | + await resp.json() |
| 605 | + self.assertEqual(200, resp.status) |
| 606 | + |
| 607 | + header_headless_lower = {"User-Agent": "Hathor Wallet Headless / 0.14.87"} |
| 608 | + header_headless_equal = {"User-Agent": "Hathor Wallet Headless / 0.14.88"} |
| 609 | + header_headless_higher = {"User-Agent": "Hathor Wallet Headless / 0.14.89"} |
| 610 | + |
| 611 | + # Success with headless version higher than the minimum |
| 612 | + resp = await self.client.request( |
| 613 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_headless_higher |
| 614 | + ) |
| 615 | + await resp.json() |
| 616 | + self.assertEqual(200, resp.status) |
| 617 | + |
| 618 | + # Success with headless version equal to the minimum |
| 619 | + resp = await self.client.request( |
| 620 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_headless_equal |
| 621 | + ) |
| 622 | + await resp.json() |
| 623 | + self.assertEqual(200, resp.status) |
| 624 | + |
| 625 | + # Error with headless version lower than the minimum if the app is validating headless version |
| 626 | + resp = await self.client.request( |
| 627 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_headless_lower |
| 628 | + ) |
| 629 | + data = await resp.json() |
| 630 | + if self.version_check: |
| 631 | + self.assertEqual(400, resp.status) |
| 632 | + self.assertEqual( |
| 633 | + { |
| 634 | + "error": VERSION_CHECK_ERROR_MESSAGE, |
| 635 | + "data": {"min_version": "0.14.88", "version": "0.14.87"}, |
| 636 | + }, |
| 637 | + data, |
| 638 | + ) |
| 639 | + else: |
| 640 | + self.assertEqual(200, resp.status) |
| 641 | + |
| 642 | + header_headless_wrong1 = {"User-Agent": "Hathor Wallet Headless / 0.xx.88"} |
| 643 | + header_headless_wrong2 = {"User-Agent": "Hathor Wallet Headless / 0.14.88beta"} |
| 644 | + |
| 645 | + # Success in both cases because the regex shouldn't identify this as headless versions |
| 646 | + resp = await self.client.request( |
| 647 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_headless_wrong1 |
| 648 | + ) |
| 649 | + await resp.json() |
| 650 | + self.assertEqual(200, resp.status) |
| 651 | + |
| 652 | + resp = await self.client.request( |
| 653 | + "POST", "/submit-job", json={"tx": tx_hex}, headers=header_headless_wrong2 |
| 654 | + ) |
| 655 | + await resp.json() |
| 656 | + self.assertEqual(200, resp.status) |
| 657 | + |
| 658 | + |
| 659 | +class AppTestCase(BaseAppTestCase): |
| 660 | + __test__ = True |
| 661 | + |
| 662 | + async def get_application(self): |
| 663 | + self.manager = TxMiningManager(backend=None, pubsub=MagicMock(), address=None) |
| 664 | + self.myapp = App(self.manager) |
| 665 | + self.version_check = False |
| 666 | + return self.myapp.app |
| 667 | + |
| 668 | + |
| 669 | +class AppVersionCheckTestCase(BaseAppTestCase): |
| 670 | + __test__ = True |
| 671 | + |
| 672 | + async def get_application(self): |
| 673 | + self.manager = TxMiningManager(backend=None, pubsub=MagicMock(), address=None) |
| 674 | + self.myapp = App( |
| 675 | + self.manager, |
| 676 | + min_wallet_desktop_version="0.23.0", |
| 677 | + min_wallet_mobile_version="1.18.3", |
| 678 | + min_wallet_headless_version="0.14.88", |
| 679 | + ) |
| 680 | + self.version_check = True |
| 681 | + return self.myapp.app |
0 commit comments