Пример #1
0
    def test_save_url_as_static_file_200(self):
        output_dir = "/output"
        url = "/licenses/metadata.yaml"
        file_content = b"xxxxx"
        relpath = "licenses/metadata.yaml"

        class MockResponse:
            content = file_content
            status_code = 200

        class MockResolverMatch:
            def __init__(self, func):
                self.func = func
                self.args = []
                self.kwargs = {}

        mock_metadata_view = MagicMock()
        mock_metadata_view.return_value = MockResponse()

        with mock.patch("licenses.utils.save_bytes_to_file") as mock_save:
            with mock.patch.object(URLResolver, "resolve") as mock_resolve:
                mock_resolve.return_value = MockResolverMatch(
                    func=mock_metadata_view)
                with mock.patch("sys.stdout", new=StringIO()) as mock_out:
                    save_url_as_static_file(output_dir, url, relpath)
                    self.assertEqual(mock_out.getvalue().strip(),
                                     "licenses/metadata.yaml")

        self.assertEqual([call(url)], mock_resolve.call_args_list)
        self.assertEqual([call(request=mock.ANY)],
                         mock_metadata_view.call_args_list)
        self.assertEqual(
            [call(file_content, "/output/licenses/metadata.yaml")],
            mock_save.call_args_list,
        )
Пример #2
0
    def test_save_url_as_static_file_500(self):
        output_dir = "/output"
        url = "/licenses/metadata.yaml"
        file_content = b"xxxxx"

        class MockResponse:
            content = file_content
            status_code = 500

        class MockResolverMatch:
            def __init__(self, func):
                self.func = func
                self.args = []
                self.kwargs = {}

        mock_metadata_view = MagicMock()
        mock_metadata_view.return_value = MockResponse()

        with mock.patch("licenses.utils.save_bytes_to_file"):
            with mock.patch.object(URLResolver, "resolve") as mock_resolve:
                mock_resolve.return_value = MockResolverMatch(
                    func=mock_metadata_view)
                with self.assertRaisesMessage(
                        ValueError,
                        "ERROR: Status 500 for url /licenses/metadata.yaml",
                ):
                    save_url_as_static_file(output_dir, url,
                                            "/output/licenses/metadata.yaml")
Пример #3
0
    def test_save_url_as_static_file_home(self):
        """
        home is a TemplateView, which needs to be rendered before
        we can look at its content?
        """
        output_dir = "/output"
        url = "/"

        with mock.patch("licenses.utils.save_bytes_to_file"):
            save_url_as_static_file(output_dir, url, "/output/home.html")
Пример #4
0
    def test_save_url_as_static_file_home(self):
        """
        home is a TemplateView, which needs to be rendered before
        we can look at its content?
        """
        output_dir = "/output"
        url = "/"

        with mock.patch("licenses.utils.save_bytes_to_file"):
            with mock.patch("sys.stdout", new=StringIO()) as mock_out:
                save_url_as_static_file(output_dir, url, "/output/home.html")
                self.assertEqual(mock_out.getvalue().strip(),
                                 "/output/home.html")
Пример #5
0
    def run_django_distill(self):
        """Outputs static files into the output dir."""
        if not os.path.isdir(settings.STATIC_ROOT):
            e = "Static source directory does not exist, run collectstatic"
            raise CommandError(e)
        output_dir = self.output_dir
        if os.path.isdir(output_dir):
            rmtree(output_dir)
        os.makedirs(output_dir)

        self.stdout.write(f"\n{self.output_dir}")
        save_url_as_static_file(output_dir, "/status/", "status/index.html")
        tbranches = TranslationBranch.objects.filter(complete=False)
        for tbranch_id in tbranches.values_list("id", flat=True):
            save_url_as_static_file(
                output_dir,
                f"/status/{tbranch_id}/",
                f"status/{tbranch_id}.html",
            )

        legalcodes = LegalCode.objects.validgroups()
        for group in legalcodes.keys():
            self.stdout.write(f"\n{self.output_dir}")
            for legalcode in legalcodes[group]:
                # deed
                filepath, symlinks = legalcode.get_file_and_links("deed")
                save_url_as_static_file(
                    output_dir,
                    legalcode.deed_url,
                    filepath,
                )
                for symlink in symlinks:
                    relative_symlink(output_dir, filepath, symlink)
                # legalcode
                filepath, symlinks = legalcode.get_file_and_links("legalcode")
                save_url_as_static_file(
                    output_dir,
                    legalcode.license_url,
                    filepath,
                )
                for symlink in symlinks:
                    relative_symlink(output_dir, filepath, symlink)

        self.stdout.write(f"\n{self.output_dir}")
        save_url_as_static_file(output_dir, reverse("metadata"),
                                "licenses/metadata.yaml")
Пример #6
0
    def run_django_distill(self):
        """Outputs static files into the output dir."""
        if not os.path.isdir(settings.STATIC_ROOT):
            e = "Static source directory does not exist, run collectstatic"
            raise CommandError(e)
        output_dir = self.output_dir
        if os.path.isdir(output_dir):
            rmtree(output_dir)
        os.makedirs(output_dir)

        save_url_as_static_file(output_dir, "/status/", "status/index.html")
        tbranches = TranslationBranch.objects.filter(complete=False)
        for tbranch_id in tbranches.values_list("id", flat=True):
            save_url_as_static_file(output_dir, f"/status/{tbranch_id}/",
                                    f"status/{tbranch_id}.html")

        for legalcode in LegalCode.objects.valid():
            save_url_as_static_file(output_dir, legalcode.license_url,
                                    legalcode.get_license_path())
            save_url_as_static_file(output_dir, legalcode.deed_url,
                                    legalcode.get_deed_path())
        save_url_as_static_file(output_dir, reverse("metadata"),
                                "licenses/metadata.yaml")
Пример #7
0
 def test_save_url_as_static_file_not_200(self):
     output_dir = "/output"
     url = "/some/url/"
     with self.assertRaises(Resolver404):
         save_url_as_static_file(output_dir, url, "")