TestContentFilter.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import pytest
  2. from ContentFilter import ContentFilterPlugin
  3. from Site import SiteManager
  4. @pytest.fixture
  5. def filter_storage():
  6. ContentFilterPlugin.filter_storage = ContentFilterPlugin.ContentFilterStorage(SiteManager.site_manager)
  7. return ContentFilterPlugin.filter_storage
  8. @pytest.mark.usefixtures("resetSettings")
  9. @pytest.mark.usefixtures("resetTempSettings")
  10. class TestContentFilter:
  11. def createInclude(self, site):
  12. site.storage.writeJson("filters.json", {
  13. "mutes": {"1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C": {}},
  14. "siteblocks": {site.address: {}}
  15. })
  16. def testIncludeLoad(self, site, filter_storage):
  17. self.createInclude(site)
  18. filter_storage.file_content["includes"]["%s/%s" % (site.address, "filters.json")] = {
  19. "date_added": 1528295893,
  20. }
  21. assert not filter_storage.include_filters["mutes"]
  22. assert not filter_storage.isMuted("1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C")
  23. assert not filter_storage.isSiteblocked(site.address)
  24. filter_storage.includeUpdateAll(update_site_dbs=False)
  25. assert len(filter_storage.include_filters["mutes"]) == 1
  26. assert filter_storage.isMuted("1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C")
  27. assert filter_storage.isSiteblocked(site.address)
  28. def testIncludeAdd(self, site, filter_storage):
  29. self.createInclude(site)
  30. query_num_json = "SELECT COUNT(*) AS num FROM json WHERE directory = 'users/1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C'"
  31. assert not filter_storage.isSiteblocked(site.address)
  32. assert not filter_storage.isMuted("1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C")
  33. assert site.storage.query(query_num_json).fetchone()["num"] == 2
  34. # Add include
  35. filter_storage.includeAdd(site.address, "filters.json")
  36. assert filter_storage.isSiteblocked(site.address)
  37. assert filter_storage.isMuted("1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C")
  38. assert site.storage.query(query_num_json).fetchone()["num"] == 0
  39. # Remove include
  40. filter_storage.includeRemove(site.address, "filters.json")
  41. assert not filter_storage.isSiteblocked(site.address)
  42. assert not filter_storage.isMuted("1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C")
  43. assert site.storage.query(query_num_json).fetchone()["num"] == 2
  44. def testIncludeChange(self, site, filter_storage):
  45. self.createInclude(site)
  46. filter_storage.includeAdd(site.address, "filters.json")
  47. assert filter_storage.isSiteblocked(site.address)
  48. assert filter_storage.isMuted("1J6UrZMkarjVg5ax9W4qThir3BFUikbW6C")
  49. # Add new blocked site
  50. assert not filter_storage.isSiteblocked("1Hello")
  51. filter_content = site.storage.loadJson("filters.json")
  52. filter_content["siteblocks"]["1Hello"] = {}
  53. site.storage.writeJson("filters.json", filter_content)
  54. assert filter_storage.isSiteblocked("1Hello")
  55. # Add new muted user
  56. query_num_json = "SELECT COUNT(*) AS num FROM json WHERE directory = 'users/1C5sgvWaSgfaTpV5kjBCnCiKtENNMYo69q'"
  57. assert not filter_storage.isMuted("1C5sgvWaSgfaTpV5kjBCnCiKtENNMYo69q")
  58. assert site.storage.query(query_num_json).fetchone()["num"] == 2
  59. filter_content["mutes"]["1C5sgvWaSgfaTpV5kjBCnCiKtENNMYo69q"] = {}
  60. site.storage.writeJson("filters.json", filter_content)
  61. assert filter_storage.isMuted("1C5sgvWaSgfaTpV5kjBCnCiKtENNMYo69q")
  62. assert site.storage.query(query_num_json).fetchone()["num"] == 0