def test_format_string(self):
        settings.MULTITENANT_RELATIVE_STATIC_ROOT = "%s/other_dir"
        storage = TenantStaticFilesStorage()

        # location
        path_suffix = "/staticfiles/{}/other_dir".format(
            self.tenant.schema_name)
        self.assertEqual(storage.location[-len(path_suffix):], path_suffix)

        # path
        path_suffix = "/staticfiles/{}/other_dir/foo.txt".format(
            self.tenant.schema_name)
        self.assertEqual(
            storage.path("foo.txt")[-len(path_suffix):], path_suffix)

        # base_url
        self.assertEqual(
            storage.base_url,
            "/static/{}/other_dir/".format(self.tenant.schema_name))

        # url
        self.assertEqual(
            storage.url("foo.txt"),
            "/static/{}/other_dir/foo.txt".format(self.tenant.schema_name),
        )
    def setUp(self):
        super().setUp()
        settings.STATIC_ROOT = "/staticfiles"
        settings.STATIC_URL = "/static/"
        settings.MULTITENANT_RELATIVE_STATIC_ROOT = "%s/other_dir"

        self.storage = TenantStaticFilesStorage()
class TenantStaticFilesStorageTestCase(TenantTestCase):
    def setUp(self):
        super().setUp()
        settings.STATIC_ROOT = "/staticfiles"
        settings.STATIC_URL = "/static/"
        settings.MULTITENANT_RELATIVE_STATIC_ROOT = "%s/other_dir"

        self.storage = TenantStaticFilesStorage()

    def test_relative_static_root_raises_exception_if_no_static_root_configured(
            self):
        with self.assertRaises(ImproperlyConfigured):
            del settings.STATIC_ROOT

            self.storage.relative_static_root  # noqa Lookup static root

    def test_base_location(self):
        self.assertEqual(
            self.storage.base_location,
            "{}/{}/other_dir".format(settings.STATIC_ROOT,
                                     self.tenant.schema_name),
        )

    def test_base_location_defaults_to_appending_tenant_to_static_root(self):
        del settings.MULTITENANT_RELATIVE_STATIC_ROOT

        self.assertEqual(
            self.storage.base_location,
            "{}/{}".format(settings.STATIC_ROOT, self.tenant.schema_name),
        )

    def test_base_url_uses_static_url(self):
        self.assertEqual(self.storage.base_url, "/static/")

    @override_settings(REWRITE_STATIC_URLS=True)
    def test_base_url_rewrites_static_url(self):
        self.assertEqual(
            self.storage.base_url,
            "/static/{}/other_dir/".format(connection.schema_name))

    def test_base_url_defaults_to_static_url(self):
        del settings.MULTITENANT_RELATIVE_STATIC_ROOT

        self.assertEqual(self.storage.base_url, "/static/")

    def test_path_raises_exception_if_no_static_root_configured(self):
        with self.assertRaises(ImproperlyConfigured):
            del settings.STATIC_ROOT

            self.storage.path("test")
 def test_checks_media_root_config_collision(self):
     with self.assertRaises(ImproperlyConfigured):
         settings.MEDIA_ROOT = settings.STATIC_ROOT = "/media/"
         obj = TenantStaticFilesStorage()
         # Since check_settings() is not called at __init__, but each time
         # base_url is accessed, we must trigger the getter of base_url
         getattr(obj, "base_url")
 def test_checks_media_url_config_collision(self):
     with self.assertRaises(ImproperlyConfigured):
         settings.MEDIA_URL = "/static/{}/".format(self.tenant.schema_name)
         obj = TenantStaticFilesStorage()
         # Since check_settings() is not called at __init__, but each time
         # base_url is accessed, we must trigger the getter of base_url
         getattr(obj, "base_url")
Beispiel #6
0
    def storages(self):
        """
        Lazy retrieval of list of storage handlers for the current tenant.
        :return: A ,a[ pf dir paths to an appropriate storage instance.
        """
        if self._storages.get(connection.schema_name, None) is None:
            schema_storages = OrderedDict()

            for prefix, root in self.locations:
                filesystem_storage = TenantStaticFilesStorage(location=root)
                filesystem_storage.prefix = prefix
                schema_storages[root] = filesystem_storage

            self._storages[connection.schema_name] = schema_storages

        return self._storages[connection.schema_name]
    def test_default(self):
        storage = TenantStaticFilesStorage()

        # location
        path_suffix = "/staticfiles/{}".format(self.tenant.schema_name)
        self.assertEqual(storage.location[-len(path_suffix):], path_suffix)

        # path
        path_suffix = "/staticfiles/{}/foo.txt".format(self.tenant.schema_name)
        self.assertEqual(
            storage.path("foo.txt")[-len(path_suffix):], path_suffix)

        # base_url
        self.assertEqual(storage.base_url,
                         "/static/{}/".format(self.tenant.schema_name))

        # url
        self.assertEqual(storage.url("foo.txt"),
                         "/static/{}/foo.txt".format(self.tenant.schema_name))
    def test_default(self):
        storage = TenantStaticFilesStorage()

        # location
        path_suffix = "/staticfiles/{}".format(self.tenant.schema_name)
        self.assertEqual(storage.location[-len(path_suffix):], path_suffix)

        # path
        path_suffix = "/staticfiles/{}/foo.txt".format(self.tenant.schema_name)
        self.assertEqual(storage.path("foo.txt")[-len(path_suffix):], path_suffix)

        # base_url
        self.assertEqual(
            storage.base_url, "/static/{}/".format(self.tenant.schema_name)
        )

        # url
        self.assertEqual(
            storage.url("foo.txt"), "/static/{}/foo.txt".format(self.tenant.schema_name)
        )
    def test_format_string(self):
        settings.MULTITENANT_RELATIVE_STATIC_ROOT = "%s/other_dir"
        storage = TenantStaticFilesStorage()

        # location
        path_suffix = "/staticfiles/{}/other_dir".format(self.tenant.schema_name)
        self.assertEqual(storage.location[-len(path_suffix):], path_suffix)

        # path
        path_suffix = "/staticfiles/{}/other_dir/foo.txt".format(
            self.tenant.schema_name
        )
        self.assertEqual(storage.path("foo.txt")[-len(path_suffix):], path_suffix)

        # base_url
        self.assertEqual(
            storage.base_url, "/static/{}/other_dir/".format(self.tenant.schema_name)
        )

        # url
        self.assertEqual(
            storage.url("foo.txt"),
            "/static/{}/other_dir/foo.txt".format(self.tenant.schema_name),
        )
 def test_checks_media_root_config_collision(self):
     with self.assertRaises(ImproperlyConfigured):
         settings.MEDIA_ROOT = settings.STATIC_ROOT = "/media/"
         TenantStaticFilesStorage()
 def test_checks_media_url_config_collision(self):
     with self.assertRaises(ImproperlyConfigured):
         settings.MEDIA_URL = "/static/{}/".format(self.tenant.schema_name)
         TenantStaticFilesStorage()