TestOptionalManager.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import copy
  2. import pytest
  3. @pytest.mark.usefixtures("resetSettings")
  4. class TestOptionalManager:
  5. def testDbFill(self, site):
  6. contents = site.content_manager.contents
  7. assert len(site.content_manager.hashfield) > 0
  8. assert contents.db.execute("SELECT COUNT(*) FROM file_optional WHERE is_downloaded = 1").fetchone()[0] == len(site.content_manager.hashfield)
  9. def testSetContent(self, site):
  10. contents = site.content_manager.contents
  11. # Add new file
  12. new_content = copy.deepcopy(contents["content.json"])
  13. new_content["files_optional"]["testfile"] = {
  14. "size": 1234,
  15. "sha512": "aaaabbbbcccc"
  16. }
  17. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  18. contents["content.json"] = new_content
  19. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] > num_optional_files_before
  20. # Remove file
  21. new_content = copy.deepcopy(contents["content.json"])
  22. del new_content["files_optional"]["testfile"]
  23. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  24. contents["content.json"] = new_content
  25. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] < num_optional_files_before
  26. def testDeleteContent(self, site):
  27. contents = site.content_manager.contents
  28. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  29. del contents["content.json"]
  30. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] < num_optional_files_before
  31. def testVerifyFiles(self, site):
  32. contents = site.content_manager.contents
  33. # Add new file
  34. new_content = copy.deepcopy(contents["content.json"])
  35. new_content["files_optional"]["testfile"] = {
  36. "size": 1234,
  37. "sha512": "aaaabbbbcccc"
  38. }
  39. contents["content.json"] = new_content
  40. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  41. assert not file_row["is_downloaded"]
  42. # Write file from outside of ZeroNet
  43. site.storage.open("testfile", "wb").write(b"A" * 1234) # For quick check hash does not matter only file size
  44. hashfield_len_before = len(site.content_manager.hashfield)
  45. site.storage.verifyFiles(quick_check=True)
  46. assert len(site.content_manager.hashfield) == hashfield_len_before + 1
  47. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  48. assert file_row["is_downloaded"]
  49. # Delete file outside of ZeroNet
  50. site.storage.delete("testfile")
  51. site.storage.verifyFiles(quick_check=True)
  52. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  53. assert not file_row["is_downloaded"]
  54. def testVerifyFilesSameHashId(self, site):
  55. contents = site.content_manager.contents
  56. new_content = copy.deepcopy(contents["content.json"])
  57. # Add two files with same hashid (first 4 character)
  58. new_content["files_optional"]["testfile1"] = {
  59. "size": 1234,
  60. "sha512": "aaaabbbbcccc"
  61. }
  62. new_content["files_optional"]["testfile2"] = {
  63. "size": 2345,
  64. "sha512": "aaaabbbbdddd"
  65. }
  66. contents["content.json"] = new_content
  67. assert site.content_manager.hashfield.getHashId("aaaabbbbcccc") == site.content_manager.hashfield.getHashId("aaaabbbbdddd")
  68. # Write files from outside of ZeroNet (For quick check hash does not matter only file size)
  69. site.storage.open("testfile1", "wb").write(b"A" * 1234)
  70. site.storage.open("testfile2", "wb").write(b"B" * 2345)
  71. site.storage.verifyFiles(quick_check=True)
  72. # Make sure that both is downloaded
  73. assert site.content_manager.isDownloaded("testfile1")
  74. assert site.content_manager.isDownloaded("testfile2")
  75. assert site.content_manager.hashfield.getHashId("aaaabbbbcccc") in site.content_manager.hashfield
  76. # Delete one of the files
  77. site.storage.delete("testfile1")
  78. site.storage.verifyFiles(quick_check=True)
  79. assert not site.content_manager.isDownloaded("testfile1")
  80. assert site.content_manager.isDownloaded("testfile2")
  81. assert site.content_manager.hashfield.getHashId("aaaabbbbdddd") in site.content_manager.hashfield
  82. def testIsPinned(self, site):
  83. assert not site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  84. site.content_manager.setPin("data/img/zerotalk-upvote.png", True)
  85. assert site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  86. assert len(site.content_manager.cache_is_pinned) == 1
  87. site.content_manager.cache_is_pinned = {}
  88. assert site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  89. def testBigfilePieceReset(self, site):
  90. site.bad_files = {
  91. "data/fake_bigfile.mp4|0-1024": 10,
  92. "data/fake_bigfile.mp4|1024-2048": 10,
  93. "data/fake_bigfile.mp4|2048-3064": 10
  94. }
  95. site.onFileDone("data/fake_bigfile.mp4|0-1024")
  96. assert site.bad_files["data/fake_bigfile.mp4|1024-2048"] == 1
  97. assert site.bad_files["data/fake_bigfile.mp4|2048-3064"] == 1
  98. def testOptionalDelete(self, site):
  99. contents = site.content_manager.contents
  100. site.content_manager.setPin("data/img/zerotalk-upvote.png", True)
  101. site.content_manager.setPin("data/img/zeroid.png", False)
  102. new_content = copy.deepcopy(contents["content.json"])
  103. del new_content["files_optional"]["data/img/zerotalk-upvote.png"]
  104. del new_content["files_optional"]["data/img/zeroid.png"]
  105. assert site.storage.isFile("data/img/zerotalk-upvote.png")
  106. assert site.storage.isFile("data/img/zeroid.png")
  107. site.storage.writeJson("content.json", new_content)
  108. site.content_manager.loadContent("content.json", force=True)
  109. assert not site.storage.isFile("data/img/zeroid.png")
  110. assert site.storage.isFile("data/img/zerotalk-upvote.png")
  111. def testOptionalRename(self, site):
  112. contents = site.content_manager.contents
  113. site.content_manager.setPin("data/img/zerotalk-upvote.png", True)
  114. new_content = copy.deepcopy(contents["content.json"])
  115. new_content["files_optional"]["data/img/zerotalk-upvote-new.png"] = new_content["files_optional"]["data/img/zerotalk-upvote.png"]
  116. del new_content["files_optional"]["data/img/zerotalk-upvote.png"]
  117. assert site.storage.isFile("data/img/zerotalk-upvote.png")
  118. assert site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  119. site.storage.writeJson("content.json", new_content)
  120. site.content_manager.loadContent("content.json", force=True)
  121. assert not site.storage.isFile("data/img/zerotalk-upvote.png")
  122. assert not site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  123. assert site.content_manager.isPinned("data/img/zerotalk-upvote-new.png")
  124. assert site.storage.isFile("data/img/zerotalk-upvote-new.png")