|
1 | | -from os import makedirs |
2 | | -from tempfile import TemporaryDirectory |
3 | | -from unittest.mock import patch |
| 1 | +from io import BytesIO |
| 2 | +from textwrap import dedent |
| 3 | +from zipfile import ZipFile |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
7 | | -from django.conf import settings |
8 | 7 | from django.test import RequestFactory |
9 | 8 |
|
10 | | -from pontoon.base.models import Project, Resource |
| 9 | +from pontoon.base.models import Project |
11 | 10 | from pontoon.base.views import download_translations |
12 | | -from pontoon.sync.tests.test_checkouts import MockVersionControl |
13 | | -from pontoon.sync.tests.utils import build_file_tree |
14 | 11 | from pontoon.test.factories import ( |
| 12 | + EntityFactory, |
15 | 13 | LocaleFactory, |
16 | 14 | ProjectFactory, |
17 | | - RepositoryFactory, |
18 | 15 | ResourceFactory, |
| 16 | + SectionFactory, |
19 | 17 | TranslatedResourceFactory, |
| 18 | + TranslationFactory, |
20 | 19 | UserFactory, |
21 | 20 | ) |
22 | 21 |
|
23 | 22 |
|
24 | 23 | @pytest.mark.django_db |
25 | | -@pytest.mark.parametrize("two_repos", [True, False]) |
26 | | -@pytest.mark.parametrize( |
27 | | - "repo_url,expected_location", |
28 | | - [ |
29 | | - ( |
30 | | - "https://github.com:gh-org/gh-repo.git", |
31 | | - "https://raw.githubusercontent.com/gh-org/gh-repo/HEAD/de-Test/a.ftl", |
32 | | - ), |
33 | | - ( |
34 | | - "git@gitlab.com:gl-org/gl-repo.git", |
35 | | - "https://gitlab.com/gl-org/gl-repo/-/raw/HEAD/de-Test/a.ftl?inline=false", |
36 | | - ), |
37 | | - ("http://example.com/tgt-repo", "https://example.com/tgt-repo"), |
38 | | - ], |
39 | | -) |
40 | | -def test_download(two_repos, repo_url, expected_location): |
41 | | - with ( |
42 | | - TemporaryDirectory() as root, |
43 | | - patch("pontoon.sync.core.checkout.get_repo", return_value=MockVersionControl()), |
44 | | - ): |
45 | | - settings.MEDIA_ROOT = root |
46 | | - locale = LocaleFactory.create(code="de-Test") |
47 | | - if two_repos: |
48 | | - repo_src = RepositoryFactory( |
49 | | - url="http://example.com/src-repo", source_repo=True |
50 | | - ) |
51 | | - repo_tgt = RepositoryFactory(url=repo_url) |
52 | | - project = ProjectFactory.create( |
53 | | - name="test-dl", |
54 | | - locales=[locale], |
55 | | - repositories=[repo_src, repo_tgt], |
56 | | - visibility=Project.Visibility.PUBLIC, |
57 | | - ) |
58 | | - src_root = repo_src.checkout_path |
59 | | - tgt_root = repo_tgt.checkout_path |
60 | | - makedirs(src_root) |
61 | | - build_file_tree(src_root, {"en-US": {"a.ftl": ""}}) |
62 | | - makedirs(tgt_root) |
63 | | - build_file_tree(tgt_root, {"de-Test": {"a.ftl": ""}}) |
64 | | - else: |
65 | | - repo = RepositoryFactory(url=repo_url) |
66 | | - project = ProjectFactory.create( |
67 | | - name="test-dl", |
68 | | - locales=[locale], |
69 | | - repositories=[repo], |
70 | | - visibility=Project.Visibility.PUBLIC, |
71 | | - ) |
72 | | - repo_root = repo.checkout_path |
73 | | - makedirs(repo_root) |
74 | | - build_file_tree( |
75 | | - repo_root, {"en-US": {"a.ftl": ""}, "de-Test": {"a.ftl": ""}} |
76 | | - ) |
77 | | - res = ResourceFactory.create( |
78 | | - project=project, path="a.ftl", format=Resource.Format.FLUENT |
79 | | - ) |
80 | | - TranslatedResourceFactory.create(locale=locale, resource=res) |
| 24 | +def test_download_fluent(): |
| 25 | + locale = LocaleFactory.create(code="de-Test") |
| 26 | + project = ProjectFactory.create( |
| 27 | + name="test-dl", |
| 28 | + locales=[locale], |
| 29 | + visibility=Project.Visibility.PUBLIC, |
| 30 | + ) |
| 31 | + res = ResourceFactory.create( |
| 32 | + project=project, format="fluent", path="path/to/file.ftl" |
| 33 | + ) |
| 34 | + TranslatedResourceFactory.create(locale=locale, resource=res) |
| 35 | + section = SectionFactory.create(resource=res, key=[], comment="Group") |
| 36 | + e1 = EntityFactory.create(resource=res, section=section, key=["e1"], value=["E1"]) |
| 37 | + e2 = EntityFactory.create( |
| 38 | + resource=res, |
| 39 | + section=section, |
| 40 | + key=["e2"], |
| 41 | + value=[], |
| 42 | + properties={"attr": ["E2"]}, |
| 43 | + ) |
| 44 | + EntityFactory.create(resource=res, section=section, key=["e3"], value=["E3"]) |
| 45 | + TranslationFactory.create( |
| 46 | + locale=locale, entity=e1, value=["T1"], active=True, approved=True |
| 47 | + ) |
| 48 | + TranslationFactory.create( |
| 49 | + locale=locale, |
| 50 | + entity=e2, |
| 51 | + value=[], |
| 52 | + properties={"attr": ["T2"]}, |
| 53 | + active=True, |
| 54 | + approved=True, |
| 55 | + ) |
| 56 | + |
| 57 | + request = RequestFactory().get( |
| 58 | + "/translations/?code=de-Test&slug=test-dl&part=path/to/file.ftl" |
| 59 | + ) |
| 60 | + request.user = UserFactory() |
| 61 | + response = download_translations(request) |
| 62 | + assert response.status_code == 200 |
| 63 | + assert response["Content-Type"] == "application/zip" |
| 64 | + assert ( |
| 65 | + response["Content-Disposition"] |
| 66 | + == "attachment; filename=de-Test_test-dl_path_to_file.zip" |
| 67 | + ) |
| 68 | + bytes_io = BytesIO(response.content) |
| 69 | + with ZipFile(bytes_io, "r") as zipfile: |
| 70 | + assert zipfile.namelist() == ["de-Test_test-dl_path_to_file.ftl"] |
| 71 | + raw = zipfile.read("de-Test_test-dl_path_to_file.ftl") |
| 72 | + assert raw.decode("utf-8") == dedent("""\ |
| 73 | + ## Group |
| 74 | +
|
| 75 | + e1 = T1 |
| 76 | + e2 = |
| 77 | + .attr = T2 |
| 78 | + """) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.django_db |
| 82 | +def test_download_xliff(): |
| 83 | + locale = LocaleFactory.create(code="de-Test") |
| 84 | + project = ProjectFactory.create( |
| 85 | + name="test-dlx", |
| 86 | + locales=[locale], |
| 87 | + visibility=Project.Visibility.PUBLIC, |
| 88 | + ) |
| 89 | + res = ResourceFactory.create(project=project, format="xliff", path="file.xlf") |
| 90 | + TranslatedResourceFactory.create(locale=locale, resource=res) |
| 91 | + section = SectionFactory.create(resource=res, key=["file.foo"], comment="Group") |
| 92 | + e1 = EntityFactory.create( |
| 93 | + resource=res, section=section, key=["file.foo", "e1"], value=["E1"] |
| 94 | + ) |
| 95 | + e2 = EntityFactory.create( |
| 96 | + resource=res, section=section, key=["file.foo", "e2"], value=["E2"] |
| 97 | + ) |
| 98 | + EntityFactory.create( |
| 99 | + resource=res, section=section, key=["file.foo", "e3"], value=["E3"] |
| 100 | + ) |
| 101 | + TranslationFactory.create( |
| 102 | + locale=locale, entity=e1, value=["T1"], active=True, approved=True |
| 103 | + ) |
| 104 | + TranslationFactory.create( |
| 105 | + locale=locale, entity=e2, value=["T2"], active=True, approved=True |
| 106 | + ) |
81 | 107 |
|
82 | | - request = RequestFactory().get( |
83 | | - "/translations/?code=de-Test&slug=test-dl&part=a.ftl" |
84 | | - ) |
85 | | - request.user = UserFactory() |
86 | | - response = download_translations(request) |
87 | | - assert response.status_code == 302 |
88 | | - assert response.get("Location") == expected_location |
| 108 | + request = RequestFactory().get( |
| 109 | + "/translations/?code=de-Test&slug=test-dlx&part=file.xlf" |
| 110 | + ) |
| 111 | + request.user = UserFactory() |
| 112 | + response = download_translations(request) |
| 113 | + assert response.status_code == 200 |
| 114 | + assert response["Content-Type"] == "application/zip" |
| 115 | + assert ( |
| 116 | + response["Content-Disposition"] |
| 117 | + == "attachment; filename=de-Test_test-dlx_file.zip" |
| 118 | + ) |
| 119 | + bytes_io = BytesIO(response.content) |
| 120 | + with ZipFile(bytes_io, "r") as zipfile: |
| 121 | + assert zipfile.namelist() == ["de-Test_test-dlx_file.xlf"] |
| 122 | + raw = zipfile.read("de-Test_test-dlx_file.xlf") |
| 123 | + assert raw.decode("utf-8") == dedent("""\ |
| 124 | + <?xml version="1.0" encoding="utf-8"?> |
| 125 | + <xliff> |
| 126 | + <file original="file.foo"> |
| 127 | + <!-- Group --> |
| 128 | + <body> |
| 129 | + <trans-unit id="e1"> |
| 130 | + <source>E1</source> |
| 131 | + <target>T1</target> |
| 132 | + </trans-unit> |
| 133 | + <trans-unit id="e2"> |
| 134 | + <source>E2</source> |
| 135 | + <target>T2</target> |
| 136 | + </trans-unit> |
| 137 | + <trans-unit id="e3"> |
| 138 | + <source>E3</source> |
| 139 | + </trans-unit> |
| 140 | + </body> |
| 141 | + </file> |
| 142 | + </xliff> |
| 143 | + """) |
0 commit comments