11from django .test import TestCase
2+ from django .contrib .gis .geos import MultiPolygon , Polygon
23from http import HTTPStatus
4+ from publications .models import GlobalRegion
35
46class SitemapTest (TestCase ):
57 def test_index (self ):
@@ -17,3 +19,73 @@ def test_publications(self):
1719 self .assertEqual (response .status_code , HTTPStatus .OK )
1820 self .assertEqual (response ["content-type" ], "application/xml" )
1921 # TODO test content
22+
23+ def test_feeds (self ):
24+ """Test feeds sitemap generation for global regions."""
25+ response = self .client .get ("/sitemap-feeds.xml" )
26+ self .assertEqual (response .status_code , HTTPStatus .OK )
27+ self .assertEqual (response ["content-type" ], "application/xml" )
28+
29+ def test_feeds_content (self ):
30+ """Test feeds sitemap includes regional feed URLs."""
31+ # Create test GlobalRegion instances
32+ test_polygon = MultiPolygon (Polygon (((0 , 0 ), (0 , 1 ), (1 , 1 ), (1 , 0 ), (0 , 0 ))))
33+
34+ continent = GlobalRegion .objects .create (
35+ name = "Test Continent" ,
36+ region_type = GlobalRegion .CONTINENT ,
37+ source_url = "http://example.com" ,
38+ license = "CC BY 4.0" ,
39+ geom = test_polygon
40+ )
41+
42+ ocean = GlobalRegion .objects .create (
43+ name = "Test Ocean" ,
44+ region_type = GlobalRegion .OCEAN ,
45+ source_url = "http://example.com" ,
46+ license = "CC BY 4.0" ,
47+ geom = test_polygon
48+ )
49+
50+ # Get the feeds sitemap
51+ response = self .client .get ("/sitemap-feeds.xml" )
52+ content = response .content .decode ('utf-8' )
53+
54+ # Verify response
55+ self .assertEqual (response .status_code , HTTPStatus .OK )
56+ self .assertIn ('<?xml version="1.0" encoding="UTF-8"?>' , content )
57+ self .assertIn ('<urlset' , content )
58+
59+ # Verify continent feed URL is included
60+ self .assertIn ('/feeds/continent/test-continent/' , content )
61+
62+ # Verify ocean feed URL is included
63+ self .assertIn ('/feeds/ocean/test-ocean/' , content )
64+
65+ # Verify priority and changefreq
66+ self .assertIn ('<priority>0.6</priority>' , content )
67+ self .assertIn ('<changefreq>daily</changefreq>' , content )
68+
69+ def test_feeds_index_reference (self ):
70+ """Test that feeds sitemap is referenced in main sitemap index.
71+
72+ Note: Django's sitemap index only includes sitemaps that have items.
73+ This test creates GlobalRegion objects to ensure the feeds sitemap
74+ appears in the index.
75+ """
76+ # Create at least one GlobalRegion so the feeds sitemap has items
77+ test_polygon = MultiPolygon (Polygon (((0 , 0 ), (0 , 1 ), (1 , 1 ), (1 , 0 ), (0 , 0 ))))
78+ GlobalRegion .objects .create (
79+ name = "Test Region" ,
80+ region_type = GlobalRegion .CONTINENT ,
81+ source_url = "http://example.com" ,
82+ license = "CC BY 4.0" ,
83+ geom = test_polygon
84+ )
85+
86+ response = self .client .get ("/sitemap.xml" )
87+ content = response .content .decode ('utf-8' )
88+
89+ self .assertEqual (response .status_code , HTTPStatus .OK )
90+ # Verify the feeds sitemap is listed in the index
91+ self .assertIn ('sitemap-feeds.xml' , content )
0 commit comments