test_storage.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import os
  2. import tempfile
  3. import unittest
  4. from unittest.mock import patch
  5. import storage
  6. class TrialAccountStorageTests(unittest.TestCase):
  7. def setUp(self):
  8. self.tempdir = tempfile.TemporaryDirectory()
  9. self.addCleanup(self.tempdir.cleanup)
  10. self.data_dir_patch = patch.object(storage, "DATA_DIR", self.tempdir.name)
  11. self.db_path_patch = patch.object(storage, "DB_PATH", os.path.join(self.tempdir.name, "accounts.db"))
  12. self.data_dir_patch.start()
  13. self.db_path_patch.start()
  14. self.addCleanup(self.data_dir_patch.stop)
  15. self.addCleanup(self.db_path_patch.stop)
  16. storage.init_db()
  17. def test_trial_eligibility_remains_after_final_status_changes(self):
  18. storage.upsert_account(
  19. "trial@example.com",
  20. "pw",
  21. fields={
  22. "final_status": "paid",
  23. "initial_session": {
  24. "accessToken": "tok-trial",
  25. "account": {"planType": "free"},
  26. },
  27. "notes": storage.TRIAL_ELIGIBLE_NOTE,
  28. },
  29. )
  30. storage.upsert_account(
  31. "trial@example.com",
  32. "pw",
  33. fields={
  34. "final_status": "cpa_uploaded",
  35. "plus_session": {
  36. "accessToken": "tok-trial",
  37. "account": {"planType": "plus"},
  38. },
  39. },
  40. )
  41. account = storage.get_account("trial@example.com")
  42. self.assertTrue(account["is_trial_account"])
  43. self.assertEqual(account["trial_state"], "eligible")
  44. self.assertTrue(account["can_retry_payment"])
  45. def test_get_account_keeps_manual_payment_trial_separate_from_trial_account(self):
  46. storage.upsert_account(
  47. "manual@example.com",
  48. "pw",
  49. fields={
  50. "final_status": "trial",
  51. "last_error": "非免费金额需手动付款: 金额为 $20.00",
  52. "initial_session": {
  53. "accessToken": "tok-manual",
  54. "account": {"planType": "free"},
  55. },
  56. "notes": storage.TRIAL_INELIGIBLE_NOTE,
  57. },
  58. )
  59. account = storage.get_account("manual@example.com")
  60. self.assertFalse(account["is_trial_account"])
  61. self.assertEqual(account["trial_state"], "ineligible")
  62. self.assertTrue(account["can_retry_payment"])
  63. def test_list_accounts_trial_filter_includes_trial_accounts_and_pending_payment_accounts(self):
  64. storage.upsert_account(
  65. "trial@example.com",
  66. "pw",
  67. fields={
  68. "final_status": "cpa_uploaded",
  69. "initial_session": {
  70. "accessToken": "tok-trial",
  71. "account": {"planType": "free"},
  72. },
  73. "notes": storage.TRIAL_ELIGIBLE_NOTE,
  74. },
  75. )
  76. storage.upsert_account(
  77. "manual@example.com",
  78. "pw",
  79. fields={
  80. "final_status": "trial",
  81. "last_error": "非免费金额需手动付款: 金额为 $20.00",
  82. "initial_session": {
  83. "accessToken": "tok-manual",
  84. "account": {"planType": "free"},
  85. },
  86. "notes": storage.TRIAL_INELIGIBLE_NOTE,
  87. },
  88. )
  89. storage.upsert_account(
  90. "plain@example.com",
  91. "pw",
  92. fields={
  93. "final_status": "cpa_uploaded",
  94. "initial_session": {
  95. "accessToken": "tok-plain",
  96. "account": {"planType": "free"},
  97. },
  98. },
  99. )
  100. accounts = storage.list_accounts(status="trial")
  101. self.assertEqual(
  102. [account["email"] for account in accounts],
  103. ["manual@example.com", "trial@example.com"],
  104. )
  105. if __name__ == "__main__":
  106. unittest.main()