48 lines
2.5 KiB
Python
48 lines
2.5 KiB
Python
|
|
import importlib.util
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
import sys
|
||
|
|
|
||
|
|
p = Path(__file__).resolve().parents[1] / 'scripts' / 'export_wordpress_url_baseline.py'
|
||
|
|
spec = importlib.util.spec_from_file_location('url_baseline', p)
|
||
|
|
a = importlib.util.module_from_spec(spec)
|
||
|
|
sys.modules[spec.name] = a
|
||
|
|
spec.loader.exec_module(a)
|
||
|
|
|
||
|
|
|
||
|
|
class Tests(unittest.TestCase):
|
||
|
|
def test_legacy_url_route(self):
|
||
|
|
self.assertEqual(a.route_from_url('https://example.com/pickup-truck/', {'example.com'}), '/pickup-truck/')
|
||
|
|
def test_query_legacy_requires_manual_mapping(self):
|
||
|
|
self.assertIsNone(a.route_from_url('https://example.com/?p=22', {'example.com'}))
|
||
|
|
def test_foreign_host_excluded(self):
|
||
|
|
self.assertIsNone(a.route_from_url('https://bad.example.com/x/', {'example.com'}))
|
||
|
|
def test_wxr_only_published_not_attachments(self):
|
||
|
|
with tempfile.TemporaryDirectory() as d:
|
||
|
|
f = Path(d)/'wp.xml'
|
||
|
|
f.write_text('''<?xml version="1.0" encoding="UTF-8"?><rss xmlns:wp="http://wordpress.org/export/1.2/"><channel>
|
||
|
|
<item><link>https://example.com/p1/</link><wp:post_type>page</wp:post_type><wp:status>publish</wp:status></item>
|
||
|
|
<item><link>https://example.com/p2/</link><wp:post_type>page</wp:post_type><wp:status>draft</wp:status></item>
|
||
|
|
<item><link>https://example.com/upload/</link><wp:post_type>attachment</wp:post_type><wp:status>publish</wp:status></item>
|
||
|
|
</channel></rss>''')
|
||
|
|
out=Path(d)/'old.txt'
|
||
|
|
self.assertEqual(a.main([str(f),'--host','example.com','--output',str(out)]),0)
|
||
|
|
self.assertIn('/p1/',out.read_text())
|
||
|
|
self.assertNotIn('/p2/',out.read_text())
|
||
|
|
self.assertNotIn('/upload/',out.read_text())
|
||
|
|
def test_sitemap_support(self):
|
||
|
|
with tempfile.TemporaryDirectory() as d:
|
||
|
|
f=Path(d)/'s.xml'
|
||
|
|
f.write_text('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://example.com/about/</loc></url></urlset>')
|
||
|
|
out=Path(d)/'urls.txt'
|
||
|
|
self.assertEqual(a.main([str(f),'--host','example.com','--output',str(out)]),0)
|
||
|
|
self.assertIn('/about/',out.read_text())
|
||
|
|
def test_dtd_rejected(self):
|
||
|
|
with tempfile.TemporaryDirectory() as d:
|
||
|
|
f=Path(d)/'s.xml'
|
||
|
|
f.write_text('<!DOCTYPE urlset [<!ENTITY hacked "HACK">]><urlset>&hacked;</urlset>')
|
||
|
|
self.assertNotEqual(a.main([str(f),'--host','example.com','--output',str(Path(d)/'urls.txt')]),0)
|
||
|
|
|
||
|
|
if __name__=='__main__': unittest.main()
|