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