TestSite.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import shutil
  2. import os
  3. import pytest
  4. from Site import SiteManager
  5. @pytest.mark.usefixtures("resetSettings")
  6. class TestSite:
  7. def testClone(self, site):
  8. assert site.storage.directory == "src/Test/testdata/1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT"
  9. # Remove old files
  10. if os.path.isdir("src/Test/testdata/159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL"):
  11. shutil.rmtree("src/Test/testdata/159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL")
  12. assert not os.path.isfile("src/Test/testdata/159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL/content.json")
  13. # Clone 1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT to 15E5rhcAUD69WbiYsYARh4YHJ4sLm2JEyc
  14. new_site = site.clone(
  15. "159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL", "5JU2p5h3R7B1WrbaEdEDNZR7YHqRLGcjNcqwqVQzX2H4SuNe2ee", address_index=1
  16. )
  17. # Check if clone was successful
  18. assert new_site.address == "159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL"
  19. assert new_site.storage.isFile("content.json")
  20. assert new_site.storage.isFile("index.html")
  21. assert new_site.storage.isFile("data/users/content.json")
  22. assert new_site.storage.isFile("data/zeroblog.db")
  23. assert new_site.storage.verifyFiles() == [] # No bad files allowed
  24. assert new_site.storage.query("SELECT * FROM keyvalue WHERE key = 'title'").fetchone()["value"] == "MyZeroBlog"
  25. # Test re-cloning (updating)
  26. # Changes in non-data files should be overwritten
  27. new_site.storage.write("index.html", "this will be overwritten")
  28. assert new_site.storage.read("index.html"), "this will be overwritten"
  29. # Changes in data file should be kept after re-cloning
  30. changed_contentjson = new_site.storage.loadJson("content.json")
  31. changed_contentjson["description"] = "Update Description Test"
  32. new_site.storage.writeJson("content.json", changed_contentjson)
  33. changed_data = new_site.storage.loadJson("data/data.json")
  34. changed_data["title"] = "UpdateTest"
  35. new_site.storage.writeJson("data/data.json", changed_data)
  36. # The update should be reflected to database
  37. assert new_site.storage.query("SELECT * FROM keyvalue WHERE key = 'title'").fetchone()["value"] == "UpdateTest"
  38. # Re-clone the site
  39. site.log.debug("Re-cloning")
  40. site.clone("159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL")
  41. assert new_site.storage.loadJson("data/data.json")["title"] == "UpdateTest"
  42. assert new_site.storage.loadJson("content.json")["description"] == "Update Description Test"
  43. assert new_site.storage.read("index.html") != "this will be overwritten"
  44. # Delete created files
  45. new_site.storage.deleteFiles()
  46. assert not os.path.isdir("src/Test/testdata/159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL")
  47. # Delete from site registry
  48. assert new_site.address in SiteManager.site_manager.sites
  49. SiteManager.site_manager.delete(new_site.address)
  50. assert new_site.address not in SiteManager.site_manager.sites