diff --git a/tests/test_env.py b/tests/test_env.py index ed79c37..1db85dc 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -24,6 +24,15 @@ class EnvTests(unittest.TestCase): "hello world", env.render_str({"name": "world"}, "hello {{ name }}") ) + def test_common_domain(self): + self.assertEqual( + "mydomain.com", + env.render_str( + {"d1": "d1.mydomain.com", "d2": "d2.mydomain.com"}, + "{{ d1|common_domain(d2) }}", + ), + ) + def test_render_str_missing_configuration(self): self.assertRaises(exceptions.TutorError, env.render_str, {}, "hello {{ name }}") @@ -41,6 +50,12 @@ class EnvTests(unittest.TestCase): ) def test_render_full(self): + defaults = tutor_config.load_defaults() + with tempfile.TemporaryDirectory() as root: + env.render_full(root, defaults) + + def test_render_full_with_https(self): + defaults = tutor_config.load_defaults() + defaults["ACTIVATE_HTTPS"] = True with tempfile.TemporaryDirectory() as root: - defaults = tutor_config.load_defaults() env.render_full(root, defaults) diff --git a/tutor/env.py b/tutor/env.py index e89aff9..eb05121 100644 --- a/tutor/env.py +++ b/tutor/env.py @@ -25,7 +25,7 @@ class Renderer: undefined=jinja2.StrictUndefined, ) environment.filters["random_string"] = utils.random_string - environment.filters["common_domain"] = utils.random_string + environment.filters["common_domain"] = utils.common_domain environment.filters["reverse_host"] = utils.reverse_host environment.globals["TUTOR_VERSION"] = __version__ cls.ENVIRONMENT = environment @@ -68,22 +68,29 @@ def render_full(root, config): Render the full environment, including version information. """ for subdir in ["android", "apps", "k8s", "local", "webui"]: - render_subdir(subdir, root, config) + save_subdir(subdir, root, config) copy_subdir("build", root) - render_file(config, VERSION_FILENAME) + save_file(VERSION_FILENAME, root, config) -def render_subdir(subdir, root, config): +def save_subdir(subdir, root, config): """ Render the templates located in `subdir` and store them with the same hierarchy at `root`. """ for path in walk_templates(subdir): - dst = pathjoin(root, path) - rendered = render_file(config, path) - utils.ensure_file_directory_exists(dst) - with open(dst, "w") as of: - of.write(rendered) + save_file(path, root, config) + + +def save_file(path, root, config): + """ + Render the template located in `path` and store it with the same hierarchy at `root`. + """ + dst = pathjoin(root, path) + rendered = render_file(config, path) + utils.ensure_file_directory_exists(dst) + with open(dst, "w") as of: + of.write(rendered) def render_file(config, *path): diff --git a/tutor/templates/apps/openedx/config/cms.env.json b/tutor/templates/apps/openedx/config/cms.env.json index 50e108e..649e17a 100644 --- a/tutor/templates/apps/openedx/config/cms.env.json +++ b/tutor/templates/apps/openedx/config/cms.env.json @@ -33,7 +33,7 @@ "EMAIL_PORT": {{ SMTP_PORT }}, "HTTPS": "{{ "on" if ACTIVATE_HTTPS else "off" }}", "LANGUAGE_CODE": "{{ LANGUAGE_CODE }}", - {% if ACTIVATE_HTTPS %}"SESSION_COOKIE_DOMAIN": ".{{ lms_cms_common_domain }}",{% endif %} + {% if ACTIVATE_HTTPS %}"SESSION_COOKIE_DOMAIN": ".{{ LMS_HOST|common_domain(CMS_HOST) }}",{% endif %} "CACHES": { "default": { "KEY_PREFIX": "default", diff --git a/tutor/templates/apps/openedx/config/lms.env.json b/tutor/templates/apps/openedx/config/lms.env.json index 544c573..644fab8 100644 --- a/tutor/templates/apps/openedx/config/lms.env.json +++ b/tutor/templates/apps/openedx/config/lms.env.json @@ -44,7 +44,7 @@ {% endif %} "LANGUAGE_CODE": "{{ LANGUAGE_CODE }}", "LOGIN_REDIRECT_WHITELIST": ["{{ CMS_HOST }}", "studio.localhost"], - {% if ACTIVATE_HTTPS %}"SESSION_COOKIE_DOMAIN": ".{{ lms_cms_common_domain }}",{% endif %} + {% if ACTIVATE_HTTPS %}"SESSION_COOKIE_DOMAIN": ".{{ LMS_HOST|common_domain(CMS_HOST) }}",{% endif %} "CACHES": { "default": { "KEY_PREFIX": "default",