def setUpClass(cls): cls._outputDirectory = TemporaryDirectory() configuration = Configuration(cls._outputDirectory.name, 'https://ancestry.example.com') cls.site = Site(configuration) place1 = Place('PLACE1', 'one') event1 = Event('EVENT1', Event.Type.BIRTH) event1.place = place1 event1_person_1_presence = Presence(Presence.Role.SUBJECT) event1_person_1_presence.event = event1 person1 = Person('PERSON1', 'Janet', 'Dough') person1.presences.add(event1_person_1_presence) source1 = Source('SOURCE1', 'A Little Birdie') places = [place1] cls.site.ancestry.places.update({place.id: place for place in places}) events = [event1] cls.site.ancestry.events.update({event.id: event for event in events}) people = [person1] cls.site.ancestry.people.update( {person.id: person for person in people}) sources = [source1] cls.site.ancestry.sources.update( {source.id: source for source in sources}) render(cls.site)
def test_post_render_config_with_clean_urls(self): with TemporaryDirectory() as output_directory_path: configuration = Configuration( output_directory_path, 'http://example.com') configuration.plugins[Nginx] = {} configuration.clean_urls = True site = Site(configuration) render(site) expected = '''server { listen 80; server_name example.com; root %s; add_header Cache-Control "max-age=86400"; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_types text/css application/javascript application/json application/xml; set $content_type_extension html; index index.$content_type_extension; location / { # Handle HTTP error responses. error_page 401 /.error/401.$content_type_extension; error_page 403 /.error/403.$content_type_extension; error_page 404 /.error/404.$content_type_extension; location /.error { internal; } try_files $uri $uri/ =404; } }''' % configuration.www_directory_path # noqa: E101 W191 with open(join(configuration.output_directory_path, 'nginx', 'nginx.conf')) as f: # noqa: E101 self.assertEquals(expected, f.read())
def test_validate(self): render(self.site) with open(path.join(path.dirname(__file__), 'resources', 'sitemap.xsd')) as f: schema_doc = etree.parse(f) schema = etree.XMLSchema(schema_doc) with open( path.join(self.site.configuration.www_directory_path, 'sitemap.xml')) as f: sitemap_doc = etree.parse(f) schema.validate(sitemap_doc)
def test_post_render_event(self): with TemporaryDirectory() as output_directory_path: configuration = Configuration( output_directory_path, 'https://ancestry.example.com') configuration.mode = 'development' configuration.plugins[Maps] = {} site = Site(configuration) render(site) with open(join(configuration.www_directory_path, 'betty.js')) as f: betty_js = f.read() self.assertIn('maps.js', betty_js) self.assertIn('maps.css', betty_js) with open(join(configuration.www_directory_path, 'betty.css')) as f: betty_css = f.read() self.assertIn('.map', betty_css)
def test_post_render_config_multilingual(self): with TemporaryDirectory() as output_directory_path: configuration = Configuration( output_directory_path, 'http://example.com') configuration.plugins[Nginx] = {} configuration.locales.clear() configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en') configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl') site = Site(configuration) render(site) expected = '''server { listen 80; server_name example.com; root %s; add_header Cache-Control "max-age=86400"; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_types text/css application/javascript application/json application/xml; set $content_type_extension html; index index.$content_type_extension; location ~ ^/(en|nl)(/|$) { set $locale $1; add_header Content-Language "$locale" always; # Handle HTTP error responses. error_page 401 /$locale/.error/401.$content_type_extension; error_page 403 /$locale/.error/403.$content_type_extension; error_page 404 /$locale/.error/404.$content_type_extension; location ~ ^/$locale/\.error { internal; } try_files $uri $uri/ =404; } location @localized_redirect { set $locale_alias en; return 301 /$locale_alias$uri; } location / { try_files $uri @localized_redirect; } }''' % configuration.www_directory_path # noqa: E101 W191 with open(join(configuration.output_directory_path, 'nginx', 'nginx.conf')) as f: # noqa: E101 self.assertEquals(expected, f.read())
def test_resource_override(self): with TemporaryDirectory() as output_directory_path: with TemporaryDirectory() as resources_directory_path: makedirs(join(resources_directory_path, 'public')) with open( join(resources_directory_path, 'public', 'index.html.j2'), 'w') as f: f.write('{% block content %}Betty was here{% endblock %}') configuration = Configuration(output_directory_path, 'https://ancestry.example.com') configuration.resources_directory_path = resources_directory_path site = Site(configuration) render(site) with open(join(configuration.www_directory_path, 'index.html')) as f: self.assertIn('Betty was here', f.read())
def test_post_render_config_with_clean_urls(self): self.maxDiff = None with TemporaryDirectory() as output_directory_path: configuration = Configuration(output_directory_path, 'https://example.com') configuration.plugins[Nginx] = {} configuration.clean_urls = True site = Site(configuration) render(site) expected = '''server { # The port to listen to. listen 80; # The publicly visible hostname. server_name example.com; # The path to the local web root. root %s; # The cache lifetime. add_header Cache-Control "max-age=86400"; # Handle HTTP error responses. error_page 401 /.error/401.html; error_page 403 /.error/403.html; error_page 404 /.error/404.html; location /.error { internal; } # Redirect */index.html to their parent directories for clean URLs. if ($request_uri ~ "^(.*)/index\.html$") { return 301 $1; } # When directories are requested, serve their index.html contents. location / { if ($request_method = OPTIONS) { add_header Allow "OPTIONS, GET"; return 200; } index index.html; try_files $uri $uri/ =404; } }''' % configuration.www_directory_path # noqa: E101 W191 with open(join(configuration.output_directory_path, 'nginx.conf')) as f: # noqa: E101 self.assertEquals(expected, f.read())
def test_post_render_config_with_content_negotiation(self): with TemporaryDirectory() as output_directory_path: configuration = Configuration( output_directory_path, 'http://example.com') configuration.content_negotiation = True configuration.plugins[Nginx] = {} site = Site(configuration) render(site) expected = '''server { listen 80; server_name example.com; root %s; add_header Cache-Control "max-age=86400"; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_types text/css application/javascript application/json application/xml; set_by_lua_block $content_type_extension { local available_content_types = {'text/html', 'application/json'} local content_type_extensions = {} content_type_extensions['text/html'] = 'html' content_type_extensions['application/json'] = 'json' local content_type = require('cone').negotiate(ngx.req.get_headers()['Accept'], available_content_types) return content_type_extensions[content_type] } index index.$content_type_extension; location / { # Handle HTTP error responses. error_page 401 /.error/401.$content_type_extension; error_page 403 /.error/403.$content_type_extension; error_page 404 /.error/404.$content_type_extension; location /.error { internal; } try_files $uri $uri/ =404; } }''' % configuration.www_directory_path # noqa: E101 W191 with open(join(configuration.output_directory_path, 'nginx', 'nginx.conf')) as f: # noqa: E101 self.assertEquals(expected, f.read())
def run(self): parse.parse(self._site) render.render(self._site)
def test_post_render_config_multilingual_with_content_negotiation(self): with TemporaryDirectory() as output_directory_path: configuration = Configuration( output_directory_path, 'http://example.com') configuration.content_negotiation = True configuration.plugins[Nginx] = {} configuration.locales.clear() configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en') configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl') site = Site(configuration) render(site) expected = '''server { listen 80; server_name example.com; root %s; add_header Cache-Control "max-age=86400"; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_types text/css application/javascript application/json application/xml; set_by_lua_block $content_type_extension { local available_content_types = {'text/html', 'application/json'} local content_type_extensions = {} content_type_extensions['text/html'] = 'html' content_type_extensions['application/json'] = 'json' local content_type = require('cone').negotiate(ngx.req.get_headers()['Accept'], available_content_types) return content_type_extensions[content_type] } index index.$content_type_extension; location ~ ^/(en|nl)(/|$) { set $locale $1; add_header Content-Language "$locale" always; # Handle HTTP error responses. error_page 401 /$locale/.error/401.$content_type_extension; error_page 403 /$locale/.error/403.$content_type_extension; error_page 404 /$locale/.error/404.$content_type_extension; location ~ ^/$locale/\.error { internal; } try_files $uri $uri/ =404; } location @localized_redirect { set_by_lua_block $locale_alias { local available_locales = {'en-US', 'nl-NL'} local locale_aliases = {} locale_aliases['en-US'] = 'en' locale_aliases['nl-NL'] = 'nl' local locale = require('cone').negotiate(ngx.req.get_headers()['Accept-Language'], available_locales) return locale_aliases[locale] } return 301 /$locale_alias$uri; } location / { try_files $uri @localized_redirect; } }''' % configuration.www_directory_path # noqa: E101 W191 with open(join(configuration.output_directory_path, 'nginx', 'nginx.conf')) as f: # noqa: E101 self.assertEquals(expected, f.read())