Beispiel #1
0
    def test_translation_counts(self):
        """
        Translation memory should aggregate identical translations strings
        from the different entities and count up their occurrences.
        """
        new_locale = LocaleFactory.create()
        memory_entry = TranslationMemoryFactory.create(source="aaaa", target="ccc", locale=new_locale)
        TranslationMemoryFactory.create(source="abaa", target="ccc", locale=new_locale)
        TranslationMemoryFactory.create(source="aaab", target="ccc", locale=new_locale)
        TranslationMemoryFactory.create(source="aaab", target="ccc", locale=new_locale)

        response = self.client.get('/translation-memory/', {
            'text': 'aaaa',
            'pk': memory_entry.entity.pk,
            'locale': memory_entry.locale.code
        })

        result = response.json()
        src_string = result[0].pop('source')

        assert_true(src_string in ('abaa', 'aaab', 'aaab'))
        assert_equal(
            result,
            [{
                u'count': 3,
                u'quality': 75.0,
                u'target': u'ccc',
            }]
        )
Beispiel #2
0
 def test_removed_files(self):
     with patch.object(
         self.vcsrepository, 'execute', side_effect=self.execute_success
     ) as mock_execute:
         removed_files = self.vcsrepository.get_removed_files('/path', '1')
         assert_true(mock_execute.called)
         assert_equal(removed_files, ['removed_file1.properties', 'removed_file2.properties'])
Beispiel #3
0
    def test_invalid_project(self):
        """If the project is invalid, redirect home."""
        LocaleFactory.create(code='fakelocale')

        response = self.client.get('/fakelocale/invalid-project/')
        assert_redirects(response, reverse('pontoon.home'))
        assert_equal(self.client.session['translate_error'], {'none': None})
Beispiel #4
0
def assert_xml(xml_content, expected_xml=None, dtd_path=None):
    """Provided xml_content should be a valid XML string and be equal to expected_xml."""

    def to_xml(string):
        """
        A shortcut function to load xml.
        """
        return etree.fromstring(string)

    def normalize_xml(xml_string):
        """
        Helps to normalize different xml to the same format, indentation etc.
        At the same time, content is validated.
        """
        return etree.tostring(to_xml(xml_content))

    validated_xml = normalize_xml(xml_content)

    if dtd_path:
        dtd = etree.DTD(dtd_path)
        if not dtd.validate(to_xml(xml_content)):
            raise AssertionError(dtd.error_log)

    if expected_xml is not None:
        assert_equal(validated_xml, normalize_xml(expected_xml))
Beispiel #5
0
    def test_resources_parse_error(self):
        """
        If VCSResource() raises a ParseError while loading, log an error
        and skip the resource.
        """
        self.vcs_project.relative_resource_paths = Mock(return_value=['failure', 'success'])

        # Fail only if the path is failure so we can test the ignore.
        def vcs_resource_constructor(project, path, locales=None):
            if path == 'failure':
                raise ParseError('error message')
            else:
                return 'successful resource'

        changed_vcs_resources = {'success': [], 'failure': []}
        with patch('pontoon.sync.vcs.models.VCSResource') as MockVCSResource, \
            patch('pontoon.sync.vcs.models.log') as mock_log, \
            patch.object(
                VCSProject, 'changed_files', new_callable=PropertyMock,
                return_value=changed_vcs_resources
        ):
            MockVCSResource.side_effect = vcs_resource_constructor

            assert_equal(self.vcs_project.resources, {'success': 'successful resource'})
            mock_log.error.assert_called_with(CONTAINS('failure', 'error message'))
Beispiel #6
0
    def test_invalid_project(self):
        """If the project is invalid, redirect home."""
        LocaleFactory.create(code="fakelocale")

        response = self.client.get("/fakelocale/invalid-project/")
        assert_redirects(response, reverse("pontoon.home"))
        assert_equal(self.client.session["translate_error"], {"none": None})
Beispiel #7
0
    def test_pull_multi_locale(self):
        """
        If the repo is multi-locale, pull all of the repos for the
        active locales.
        """
        locale1 = LocaleFactory.create(code="locale1")
        locale2 = LocaleFactory.create(code="locale2")
        repo = RepositoryFactory.create(
            type=Repository.GIT, url="https://example.com/{locale_code}/", project__locales=[locale1, locale2]
        )

        repo.locale_url = lambda locale: "https://example.com/" + locale.code
        repo.locale_checkout_path = lambda locale: "/media/" + locale.code

        with patch("pontoon.base.models.update_from_vcs") as update_from_vcs, patch(
            "pontoon.base.models.get_revision"
        ) as mock_get_revision:
            # Return path as the revision so different locales return
            # different values.
            mock_get_revision.side_effect = lambda type, path: path

            assert_equal(repo.pull(), {"locale1": "/media/locale1", "locale2": "/media/locale2"})
            update_from_vcs.assert_has_calls(
                [
                    call(Repository.GIT, "https://example.com/locale1", "/media/locale1"),
                    call(Repository.GIT, "https://example.com/locale2", "/media/locale2"),
                ]
            )
Beispiel #8
0
 def test_update_vcs_entity_unapproved(self):
     """
     Do not update VCS with unapproved translations. If no approved
     translations exist, delete existing ones.
     """
     self.update_main_vcs_entity(approved=False)
     assert_equal(self.main_vcs_translation.strings, {})
    def test_shove_instances(self):
        instance1 = ShoveInstanceFactory.create(hostname='foo', active=True)
        instance2 = ShoveInstanceFactory.create(hostname='bar', active=True)
        ShoveInstanceFactory.create(hostname='baz', active=False)

        command = ScheduledCommandFactory(hostnames='foo,bar,baz')
        assert_equal(set(command.shove_instances), set([instance1, instance2]))
Beispiel #10
0
    def test_python_new_format_placeables(self):
        """Test detection of the new format string in python strings."""
        assert_equal(
            mark_placeables(u'Hello {name}'),
            u'Hello <mark class="placeable" title="Python format string">{name}</mark>'
        )

        assert_equal(
            mark_placeables(u'Hello {name!s}'),
            u'Hello <mark class="placeable" title="Python format string">{name!s}</mark>'
        )

        assert_equal(
            mark_placeables(u'Hello {someone.name}'),
            u'Hello <mark class="placeable" title="Python format string">{someone.name}</mark>'
        )

        assert_equal(
            mark_placeables(u'Hello {name[0]}'),
            u'Hello <mark class="placeable" title="Python format string">{name[0]}</mark>'
        )

        assert_equal(
            mark_placeables(u'Hello {someone.name[0]}'),
            u'Hello <mark class="placeable" title="Python format string">{someone.name[0]}</mark>'
        )
Beispiel #11
0
    def test_connect_existing_persona_account(self):
        self.log_mock.side_effect = lambda provider: provider == 'persona'

        self.adapter.pre_social_login(MagicMock, self.get_sociallogin('fxa'))

        assert_true(self.sociallogin.account.pk)
        assert_equal(self.sociallogin.user, self.user)
Beispiel #12
0
    def test_locale_checkout_path(self):
        """Append the locale code the the project's checkout_path."""
        repo = RepositoryFactory.create(url="https://example.com/path/{locale_code}/", project__slug="test-project")
        locale = LocaleFactory.create(code="test-locale")

        with self.settings(MEDIA_ROOT="/media/root"):
            assert_equal(repo.locale_checkout_path(locale), "/media/root/projects/test-project/path/test-locale")
Beispiel #13
0
    def test_connect_normal_auth_account(self):
        self.log_mock.return_value = False

        self.adapter.pre_social_login(MagicMock(), self.get_sociallogin('fxa'))

        assert_true(self.sociallogin.account.pk)
        assert_equal(self.sociallogin.user, self.user)
Beispiel #14
0
    def test_pull_multi_locale(self):
        """
        If the repo is multi-locale, pull all of the repos for the
        active locales.
        """
        locale1 = LocaleFactory.create(code='locale1')
        locale2 = LocaleFactory.create(code='locale2')
        repo = RepositoryFactory.create(
            type=Repository.GIT,
            url='https://example.com/{locale_code}/',
            project__locales=[locale1, locale2]
        )

        repo.locale_url = lambda locale: 'https://example.com/' + locale.code
        repo.locale_checkout_path = lambda locale: '/media/' + locale.code

        with patch('pontoon.base.models.update_from_vcs') as update_from_vcs, \
             patch('pontoon.base.models.get_revision') as mock_get_revision:
            # Return path as the revision so different locales return
            # different values.
            mock_get_revision.side_effect = lambda type, path: path

            assert_equal(repo.pull(), {
                'locale1': '/media/locale1',
                'locale2': '/media/locale2'
            })
            update_from_vcs.assert_has_calls([
                call(Repository.GIT, 'https://example.com/locale1', '/media/locale1'),
                call(Repository.GIT, 'https://example.com/locale2', '/media/locale2')
            ])
Beispiel #15
0
    def test_latest_datetime(self):
        larger = aware_datetime(2015, 1, 1)
        smaller = aware_datetime(2014, 1, 1)

        assert_is_none(latest_datetime([None, None, None]))
        assert_equal(latest_datetime([None, larger]), larger)
        assert_equal(latest_datetime([None, smaller, larger]), larger)
Beispiel #16
0
 def test_homepage(self):
     response = self.client.get('/')
     data = json.loads(response.content)
     nt.assert_in('hello', data)
     nt.assert_equal('world', data['hello'])
     # new shit only in django-nose
     nt.assert_code(response, 200)
Beispiel #17
0
    def test_query_args_filtering(self):
        """
        Tests if query args are honored properly and contributors are filtered.
        """
        locale_first, locale_second = LocaleFactory.create_batch(2)

        first_contributor = self.create_contributor_with_translation_counts(
            approved=12, unapproved=1, needs_work=2, locale=locale_first)
        second_contributor = self.create_contributor_with_translation_counts(
            approved=11, unapproved=1, needs_work=2, locale=locale_second)
        third_contributor = self.create_contributor_with_translation_counts(
            approved=10, unapproved=12, needs_work=2, locale=locale_first)

        # Testing filtering for the first locale
        top_contributors = User.translators.with_translation_counts(aware_datetime(2015, 1, 1), Q(translation__locale=locale_first))
        assert_equal(top_contributors.count(), 2)
        assert_equal(top_contributors[0], third_contributor)
        assert_attributes_equal(top_contributors[0], translations_count=24,
            translations_approved_count=10, translations_unapproved_count=12,
            translations_needs_work_count=2)

        assert_equal(top_contributors[1], first_contributor)
        assert_attributes_equal(top_contributors[1], translations_count=15,
            translations_approved_count=12, translations_unapproved_count=1,
            translations_needs_work_count=2)

        # Testing filtering for the second locale
        top_contributors = User.translators.with_translation_counts(aware_datetime(2015, 1, 1), Q(translation__locale=locale_second))

        assert_equal(top_contributors.count(), 1)
        assert_equal(top_contributors[0], second_contributor)
        assert_attributes_equal(top_contributors[0], translations_count=14,
            translations_approved_count=11, translations_unapproved_count=1,
            translations_needs_work_count=2)
Beispiel #18
0
    def test_plural_translations(self):
        """
        If entity has some plural translations and approved translations their authors
        should be included in commit message.
        """
        first_author, second_author, third_author = UserFactory.create_batch(3)

        TranslationFactory.create(
            locale=self.translated_locale,
            entity=self.main_db_entity,
            user=first_author,
            approved=True
        )
        TranslationFactory.create(
            locale=self.translated_locale,
            entity=self.main_db_entity,
            user=third_author,
            approved=True,
            plural_form=1
        )
        TranslationFactory.create(
            locale=self.translated_locale,
            entity=self.main_db_entity,
            user=second_author,
            approved=False
        )

        self.changeset.update_vcs_entity(self.translated_locale, self.main_db_entity, MagicMock())

        self.changeset.execute_update_vcs()

        assert_equal(
            set(self.changeset.commit_authors_per_locale[self.translated_locale.code]),
            {first_author, third_author}
        )
Beispiel #19
0
    def test_multiple_translations(self):
        """
        If there are multiple translations to the same locale, only authors of
        the final approved version should be returned.
        """
        first_author, second_author = UserFactory.create_batch(2)

        TranslationFactory.create(
            locale=self.translated_locale,
            entity=self.main_db_entity,
            user=first_author,
            approved=True
        )
        TranslationFactory.create(
            locale=self.translated_locale,
            entity=self.main_db_entity,
            user=second_author,
            approved=False
        )

        self.changeset.update_vcs_entity(self.translated_locale, self.main_db_entity, MagicMock())

        self.changeset.execute_update_vcs()

        assert_equal(
            self.changeset.commit_authors_per_locale[self.translated_locale.code],
            [first_author]
        )
Beispiel #20
0
    def test_remove_duplicate_approvals(self):
        """
        Ensure that duplicate approvals are removed.
        """
        # Trigger creation of new approved translation.
        self.main_vcs_translation.strings[None] = 'New Translated String'
        self.main_vcs_translation.fuzzy = False

        # Translation approved after the sync started simulates the race
        # where duplicate translations occur.
        duplicate_translation = TranslationFactory.create(
            entity=self.main_db_entity,
            locale=self.translated_locale,
            string='Other New Translated String',
            approved=True,
            approved_date=aware_datetime(1970, 1, 3)
        )
        ChangedEntityLocale.objects.filter(entity=self.main_db_entity).delete()

        with patch('pontoon.sync.tasks.VCSProject', return_value=self.vcs_project):
            sync_project_repo(self.db_project.pk, self.repository.pk,
                              self.project_sync_log.pk, self.now)

        # Only one translation should be approved: the duplicate_translation.
        assert_equal(self.main_db_entity.translation_set.filter(approved=True).count(), 1)
        new_translation = self.main_db_entity.translation_set.get(
            string='New Translated String'
        )
        assert_false(new_translation.approved)
        assert_true(new_translation.approved_date is None)

        duplicate_translation.refresh_from_db()
        assert_true(duplicate_translation.approved)
        assert_equal(duplicate_translation.approved_date, aware_datetime(1970, 1, 3))
Beispiel #21
0
 def test_entity_filters(self):
     """
     Tests if right filter calls right method in the Entity manager.
     """
     filters = (
         'missing',
         'fuzzy',
         'suggested',
         'translated',
         'untranslated',
         'has-suggestions',
         'unchanged',
     )
     for filter_ in filters:
         filter_name = filter_.replace('-', '_')
         with patch('pontoon.base.models.Entity.objects.{}'.format(filter_name), return_value=Entity.objects.all()) as filter_mock:
             self.client.post('/get-entities/', {
                 'project': self.resource.project.slug,
                 'locale': self.locale.code,
                 'paths[]': [self.resource.path],
                 'filter': filter_,
                 'limit': 1,
             }, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
             assert_true(filter_mock.called)
             assert_equal(filter_mock.call_args, call(self.locale))
Beispiel #22
0
    def test_basic_run(self):
        """
        When the command is run, any scheduled commands that are due to run should be sent to
        shove.
        """
        command1 = self._mock_command(is_due=False, command='foo')
        command2 = self._mock_command(is_due=True, command='bar')
        command3 = self._mock_command(is_due=True, command='baz')

        path = 'captain.projects.management.commands.process_command_schedule.ScheduledCommand'
        with patch(path) as MockScheduledCommand:
            MockScheduledCommand.objects.all.return_value = [command1, command2, command3]
            cmd = process_command_schedule.Command()
            cmd.handle()

        # Commands that aren't due shoudn't have been run.
        assert_false(command1.project.send_command.called)

        # Commands that are due should have been run, and should have been saved with an updated
        # last_run.
        command2.project.send_command.assert_called_with(None, 'bar')
        assert_equal(command2.last_run, self.now)
        command2.save.assert_called_with()

        command3.project.send_command.assert_called_with(None, 'baz')
        assert_equal(command3.last_run, self.now)
        command3.save.assert_called_with()
Beispiel #23
0
    def test_handle_log_found(self, mock_log):
        """If a command log is found, save the given output to the log."""
        command_log = CommandLogFactory.create()
        monitor_shove_logs.handle_log_event(command_log.pk, 6, 'asdf')

        mock_log.assert_called_with('asdf')
        command_log = CommandLog.objects.get(pk=command_log.pk)  # Refresh from DB
        assert_equal(command_log.return_code, 6)
Beispiel #24
0
 def test_format_builtin(self):
     """
     Test that there exist built-in formats. We're not interested in
     testing "all" of them, just that the capability exists.
     """
     with self.settings(TIME_ZONE='UTC'):
         datetime = aware_datetime(2015, 1, 1, 5, 7)
     assert_equal(format_datetime(datetime, 'time'), '05:07 UTC')
Beispiel #25
0
    def test_timeline_join(self):
        """Last page of results should include informations about the when user joined pontoon."""
        self.client.get('/contributors/{}/timeline/?page=3'.format(self.user.username))

        assert_equal(self.mock_render.call_args[0][2]['events'][-1], {
            'date': self.user.date_joined,
            'type': 'join'
        })
Beispiel #26
0
    def test_locale_url(self):
        """Fill in the {locale_code} variable in the URL."""
        repo = RepositoryFactory.create(
            url='https://example.com/path/to/{locale_code}/',
        )
        locale = LocaleFactory.create(code='test-locale')

        assert_equal(repo.locale_url(locale), 'https://example.com/path/to/test-locale/')
Beispiel #27
0
 def test_given_period(self):
     """
     Checks if view sets and returns data for right period.
     """
     with patch('django.utils.timezone.now', wraps=now, return_value=aware_datetime(2015, 7, 5)):
         self.client.get('/contributors/?period=6')
         assert_equal(self.mock_render.call_args[0][0]['period'], 6)
         assert_equal(self.mock_translations_manager.call_args[0][0], aware_datetime(2015, 1, 5))
Beispiel #28
0
 def test_pull(self):
     repo = RepositoryFactory.create(type=Repository.GIT, url="https://example.com")
     with patch("pontoon.base.models.update_from_vcs") as update_from_vcs, patch(
         "pontoon.base.models.get_revision"
     ) as mock_get_revision:
         mock_get_revision.return_value = "asdf"
         assert_equal(repo.pull(), {"single_locale": "asdf"})
         update_from_vcs.assert_called_with(Repository.GIT, "https://example.com", repo.checkout_path)
Beispiel #29
0
 def test_checkout_path_multi_locale(self):
     """
     The checkout_path for multi-locale repos should not include the
     locale_code variable.
     """
     repo = RepositoryFactory.create(url="https://example.com/path/to/{locale_code}/", project__slug="test-project")
     with self.settings(MEDIA_ROOT="/media/root"):
         assert_equal(repo.checkout_path, "/media/root/projects/test-project/path/to")
Beispiel #30
0
    def test_locales_parts_stats_no_page_one_resource(self):
        """
        Return empty list in no subpage and only one resource defined.
        """
        project_details = self._fetch_locales_parts_stats()
        details = project_details.get(self.locale.code)

        assert_equal(details, [])
Beispiel #31
0
    def test_load_utf8_bom(self):
        """
        Ensure that the langfile parser can load UTF-8 files with an encoded
        BOM at the beginning of the file.

        See https://docs.python.org/2/library/codecs.html for details (search
        for the string "utf-8-sig").
        """
        current_dir = os.path.dirname(__file__)
        resource = lang.parse(os.path.join(current_dir, 'bom.lang'))
        assert_equal(len(resource.translations), 1)
        assert_attributes_equal(resource.translations[0],
                                source_string='Source String',
                                strings={None: 'Translated String'})
Beispiel #32
0
    def test_translation_counts(self):
        """
        Checks if translation counts are calculated properly.
        Tests creates 3 contributors with different numbers translations and checks if their counts match.
        """

        first_contributor = self.create_contributor_with_translation_counts(
            approved=7, unapproved=3, needs_work=2)
        second_contributor = self.create_contributor_with_translation_counts(
            approved=5, unapproved=9, needs_work=2)
        third_contributor = self.create_contributor_with_translation_counts(
            approved=1, unapproved=2, needs_work=5)

        top_contributors = User.translators.with_translation_counts()
        assert_equal(top_contributors.count(), 3)

        assert_equal(top_contributors[0], second_contributor)
        assert_equal(top_contributors[1], first_contributor)
        assert_equal(top_contributors[2], third_contributor)

        assert_attributes_equal(top_contributors[0],
                                translations_count=16,
                                translations_approved_count=5,
                                translations_unapproved_count=9,
                                translations_needs_work_count=2)
        assert_attributes_equal(top_contributors[1],
                                translations_count=12,
                                translations_approved_count=7,
                                translations_unapproved_count=3,
                                translations_needs_work_count=2)
        assert_attributes_equal(top_contributors[2],
                                translations_count=8,
                                translations_approved_count=1,
                                translations_unapproved_count=2,
                                translations_needs_work_count=5)
Beispiel #33
0
    def test_update_vcs_last_translation_no_translations(self):
        """
        If there are no translations in the database, do not set the
        last_updated and last_translator fields on the VCS translation.
        """
        self.main_db_translation.delete()

        self.changeset.update_vcs_entity(self.translated_locale,
                                         self.main_db_entity,
                                         self.main_vcs_entity)
        self.changeset.execute()

        assert_equal(self.main_vcs_translation.last_updated, None)
        assert_equal(self.main_vcs_translation.last_translator, None)
Beispiel #34
0
    def test_project_top_contributors(self):
        """
        Tests if view returns top contributors specific for given project.
        """
        first_project = ProjectFactory.create()
        ResourceFactory.create(project=first_project)
        first_project_contributor = TranslationFactory.create(
            entity__resource__project=first_project).user

        second_project = ProjectFactory.create()
        ResourceFactory.create(project=second_project)
        second_project_contributor = TranslationFactory.create(
            entity__resource__project=second_project).user

        with patch.object(views.ProjectContributorsView,
                          'render_to_response',
                          return_value=HttpResponse('')) as mock_render:

            self.client.get('/projects/{}/contributors/'.format(
                first_project.slug))
            assert_equal(mock_render.call_args[0][0]['project'], first_project)
            assert_equal(list(mock_render.call_args[0][0]['contributors']),
                         [first_project_contributor])

            self.client.get('/projects/{}/contributors/'.format(
                second_project.slug))
            assert_equal(mock_render.call_args[0][0]['project'],
                         second_project)
            assert_equal(list(mock_render.call_args[0][0]['contributors']),
                         [second_project_contributor])
Beispiel #35
0
    def test_locale_top_contributors(self):
        """
        Tests if view returns top contributors specific for given locale.
        """
        first_locale = LocaleFactory.create()
        first_locale_contributor = TranslationFactory.create(
            locale=first_locale,
            entity__resource__project__locales=[first_locale]).user

        second_locale = LocaleFactory.create()
        second_locale_contributor = TranslationFactory.create(
            locale=second_locale,
            entity__resource__project__locales=[second_locale]).user

        with patch.object(views.LocaleContributorsView,
                          'render_to_response',
                          return_value=HttpResponse('')) as mock_render:
            self.client.get('/{}/contributors/'.format(first_locale.code))
            assert_equal(mock_render.call_args[0][0]['locale'], first_locale)
            assert_equal(list(mock_render.call_args[0][0]['contributors']),
                         [first_locale_contributor])

            self.client.get('/{}/contributors/'.format(second_locale.code))
            assert_equal(mock_render.call_args[0][0]['locale'], second_locale)
            assert_equal(list(mock_render.call_args[0][0]['contributors']),
                         [second_locale_contributor])
Beispiel #36
0
    def test_minimal_quality(self):
        """
        View shouldn't return any entries if 70% of quality at minimum.
        """
        # Generate some random entries that shouldn't be similar
        TranslationMemoryFactory.create_batch(5)

        response = self.client.get('/translation-memory/', {
            'text': 'no match',
            'pk': 2,
            'locale': 'en-GB'
        })
        assert_code(response, 200)
        assert_equal(response.content, '[]')
Beispiel #37
0
    def test_manage_project_strings_new_all_empty(self):
        """Test that sending empty data doesn't create empty strings in the database.
        """
        project = ProjectFactory.create(data_source='database',
                                        repositories=[])
        url = reverse('pontoon.admin.project.strings', args=(project.slug, ))

        # Test sending a well-formatted batch of strings.
        new_strings = "  \n   \n\n"
        response = self.client.post(url, {'new_strings': new_strings})
        assert_code(response, 200)

        # Verify no strings have been created as entities.
        entities = list(Entity.objects.filter(resource__project=project))
        assert_equal(len(entities), 0)
Beispiel #38
0
 def test_resource_paths_without_pc_exclude_hidden(
         self, source_directory_path_mock):
     """
     We should filter out resources that are contained in the hidden paths.
     """
     source_directory_path_mock.return_value = '/root'
     hidden_paths = (
         ('/root/.hidden_folder/templates', [], ('bar.pot', )),
         ('/root/templates', [], ('foo.pot', )),
     )
     with patch('pontoon.sync.vcs.models.scandir.walk',
                wraps=scandir,
                return_value=hidden_paths):
         assert_equal(list(self.vcs_project.resource_paths_without_pc()),
                      ['/root/templates/foo.pot'])
Beispiel #39
0
    def test_release_lock_after_timeout(self):
        """
        Tests if lock is released after specified timeout.
        """
        with patch('pontoon.sync.core.cache') as mock_cache:

            @serial_task(3)
            def timeout_task(self):
                return 42

            first_call = timeout_task.delay()

            assert_true(first_call.successful())
            assert_equal(first_call.get(), 42)
            mock_cache.add.assert_called_with(ANY, ANY, timeout=3)
Beispiel #40
0
    def test_create_repository_log(self):
        assert_false(RepositorySyncLog.objects.exists())

        repo = RepositoryFactory.create()
        self.db_project.repositories = [repo]
        self.db_project.save()
        self.mock_pull_changes.return_value = [
            True,
            {repo.pk: Locale.objects.filter(pk=self.translated_locale.pk)},
        ]

        sync_translations(self.db_project.pk, self.project_sync_log.pk, self.now)

        log = RepositorySyncLog.objects.get(repository=repo.pk)
        assert_equal(log.repository, repo)
Beispiel #41
0
    def test_unapprove_translation(self):
        """Check if unapprove view works properly."""
        translation = TranslationFactory.create()
        translation.approved = True
        translation.save()

        response = self.client.ajax_post('/unapprove-translation/', {
            'translation': translation.pk,
            'paths': [],
        })
        assert_code(response, 200)

        translation.refresh_from_db()
        assert_equal(translation.approved, False)
        assert_equal(translation.unapproved_user, self.user)
Beispiel #42
0
    def test_relative_resource_paths_pot(self):
        """
        If a resource ends in .pot, replace the extension with .po since
        relative paths are used within non-source locales that do not
        have .pot files.
        """
        with patch.object(VCSProject,
                          'source_directory_path',
                          new_callable=PropertyMock,
                          return_value='/root/'):
            self.vcs_project.resources_for_path = Mock(
                return_value=['/root/foo.pot', '/root/meh/bar.pot'])

            assert_equal(list(self.vcs_project.relative_resource_paths()),
                         ['foo.po', 'meh/bar.po'])
Beispiel #43
0
 def test_given_period(self):
     """
     Checks if view sets and returns data for right period.
     """
     with patch(
         "django.utils.timezone.now",
         wraps=now,
         return_value=aware_datetime(2015, 7, 5),
     ):
         self.client.get("/contributors/?period=6")
         assert_equal(self.mock_render.call_args[0][0]["period"], 6)
         assert_equal(
             self.mock_users_with_translations_counts.call_args[0][0],
             aware_datetime(2015, 1, 5),
         )
Beispiel #44
0
    def test_not_authed_public_project(self):
        """
        If the user is not authenticated and we're translating project
        ID 1, return a 200.
        """
        # Clear out existing project with ID=1 if necessary.
        Project.objects.filter(id=1).delete()
        locale = LocaleFactory.create(code='fakelocale')
        project = ProjectFactory.create(id=1,
                                        slug='valid-project',
                                        locales=[locale])
        ResourceFactory.create(project=project)

        response = self.client.get('/fakelocale/valid-project/')
        assert_equal(response.status_code, 200)
Beispiel #45
0
def assert_attributes_equal(original, **expected_attrs):
    """
    Assert that the given object has attributes matching the given
    values.
    """
    if not expected_attrs:
        raise ValueError('Expected some attributes to check.')
    for key, value in expected_attrs.items():
        original_value = getattr(original, key)
        assert_equal(
            original_value,
            value,
            ('Attribute `{key}` does not match: {original_value} != {value}'.
             format(key=key, original_value=original_value, value=value)),
        )
Beispiel #46
0
    def test_end_time_skipped(self):
        """Include skipped repos in finding the latest end time."""
        sync_log = SyncLogFactory.create()
        RepositorySyncLogFactory.create(project_sync_log__sync_log=sync_log,
                                        end_time=aware_datetime(2015, 1, 1))
        ProjectSyncLogFactory.create(sync_log=sync_log,
                                     skipped=True,
                                     skipped_end_time=aware_datetime(
                                         2015, 1, 2))
        ProjectSyncLogFactory.create(sync_log=sync_log,
                                     skipped=True,
                                     skipped_end_time=aware_datetime(
                                         2015, 1, 4))

        assert_equal(sync_log.end_time, aware_datetime(2015, 1, 4))
Beispiel #47
0
    def test_timeline(self):
        """Backend should return events filtered by page number requested by user."""
        self.client.get('/contributors/{}/timeline/?page=2'.format(
            self.user.username))

        assert_equal(
            self.mock_render.call_args[0][2]['events'],
            [{
                'date': dt,
                'type': 'translation',
                'count': count,
                'project': self.project,
                'translation': translations[0][0],
            } for (dt, count), translations in self.translations.items()[10:20]
             ])
    def test_execute_log_error(self):
        """
        If the return code from execute is non-zero and log_errors is
        True, log an error message.
        """
        repo = VCSRepository('/path')

        with patch('pontoon.sync.vcs.repositories.execute') as mock_execute, \
                patch('pontoon.sync.vcs.repositories.log') as mock_log:
            mock_execute.return_value = 1, 'output', 'stderr'
            assert_equal(
                repo.execute('command', cwd='working_dir', log_errors=True),
                (1, 'output', 'stderr'))
            mock_log.error.assert_called_with(
                CONTAINS('stderr', 'command', 'working_dir'))
Beispiel #49
0
    def test_inplace_mode(self):
        """
        Inplace mode of get_entites, should return all entities in a single batch.
        """
        response = self.client.ajax_post('/get-entities/', {
            'project': self.resource.project.slug,
            'locale': self.locale.code,
            'paths[]': [self.resource.path],
            'inplace_editor': True,
            # Inplace mode shouldn't respect paging or limiting page
            'limit': 1,
        })

        assert_code(response, 200)
        assert_equal(response.json()['has_next'], False)
        assert_equal([e['pk'] for e in response.json()['entities']], self.entities_pks)
Beispiel #50
0
    def test_timeline(self):
        """Backend should return events filtered by page number requested by user."""
        self.client.get("/contributors/{}/timeline/?page=2".format(
            self.user.username))

        assert_equal(
            self.mock_render.call_args[0][2]["events"],
            [{
                "date": dt,
                "type": "translation",
                "count": count,
                "project": self.project,
                "translation": translations[0][0],
            } for (dt, count
                   ), translations in list(self.translations.items())[10:20]],
        )
Beispiel #51
0
def assert_redirects(response,
                     expected_url,
                     status_code=302,
                     host=None,
                     secure=False):
    """
    Assert that the given response redirects to the expected URL.

    The main difference between this and TestCase.assertRedirects is
    that this version doesn't follow the redirect.
    """
    if host is None:
        host = "{}://{}".format("https" if secure else "http", host
                                or "testserver")
    assert_equal(response.status_code, status_code)
    assert_equal(response["Location"], host + expected_url)
    def test_invalid_slugs(self):
        """
        If some of projects have invalid slug, we should warn user about them.
        """
        handle_project = ProjectFactory.create()

        self.execute_command(handle_project.slug, 'aaa', 'bbb')

        self.mock_sync_project.delay.assert_called_with(handle_project.pk,
                                                        ANY,
                                                        no_pull=False,
                                                        no_commit=False,
                                                        force=False)

        assert_equal(self.command.stderr.getvalue(),
                     'Couldn\'t find projects with following slugs: aaa, bbb')
Beispiel #53
0
 def test_mark_newline_escape_placeables(self):
     """Test detecting newline escape sequences"""
     assert_equal(
         mark_placeables(u'A string\\n'),
         u'A string<mark class="placeable" title="Escaped newline">\\n</mark>'
     )
     assert_equal(
         mark_placeables(u'\\nA string'),
         u'<mark class="placeable" title="Escaped newline">\\n</mark>A string'
     )
     assert_equal(
         mark_placeables(u'A\\nstring'),
         u'A<mark class="placeable" title="Escaped newline">\\n</mark>string'
     )
     assert_equal(mark_placeables(u'A string'), u'A string')
     assert_equal(mark_placeables(u'A\nstring'), u'A\nstring')
Beispiel #54
0
    def test_basic(self):
        # Check for self.main_db_resource to be updated and
        # self.other_db_resource to be created.
        self.main_db_resource.total_strings = 5000
        self.main_db_resource.save()
        self.other_db_resource.delete()

        update_resources(self.db_project, self.vcs_project)
        self.main_db_resource.refresh_from_db()
        assert_equal(self.main_db_resource.total_strings,
                     len(self.main_vcs_resource.entities))

        other_db_resource = Resource.objects.get(
            path=self.other_vcs_resource.path)
        assert_equal(other_db_resource.total_strings,
                     len(self.other_vcs_resource.entities))
Beispiel #55
0
    def test_for_project_locale_order(self):
        """
        Return entities in correct order.
        """
        # First entity
        EntityFactory.create(order=1,
                             resource=self.main_resource,
                             string='Second String')
        # Second entity
        EntityFactory.create(order=0,
                             resource=self.main_resource,
                             string='First String')
        entities = Entity.for_project_locale(self.project, self.locale)

        assert_equal(entities[2]['original'], 'First String')
        assert_equal(entities[3]['original'], 'Second String')
Beispiel #56
0
    def test_end_time(self):
        """
        Return the latest end time among repo sync logs for this log.
        """
        project = ProjectFactory.create(repositories=[])
        repo1, repo2 = RepositoryFactory.create_batch(2, project=project)
        project_sync_log = ProjectSyncLogFactory.create(project=project)

        RepositorySyncLogFactory.create(project_sync_log=project_sync_log,
                                        repository=repo1,
                                        end_time=aware_datetime(2015, 1, 1))
        RepositorySyncLogFactory.create(project_sync_log=project_sync_log,
                                        repository=repo2,
                                        end_time=aware_datetime(2015, 1, 2))

        assert_equal(project_sync_log.end_time, aware_datetime(2015, 1, 2))
Beispiel #57
0
    def test_contributors_order(self):
        """
        Checks if users are ordered by count of contributions.
        """
        contributors = [
            self.create_contributor_with_translation_counts(2),
            self.create_contributor_with_translation_counts(4),
            self.create_contributor_with_translation_counts(9),
            self.create_contributor_with_translation_counts(1),
            self.create_contributor_with_translation_counts(6),
        ]

        assert_equal(list(User.translators.with_translation_counts()), [
            contributors[2], contributors[4], contributors[1], contributors[0],
            contributors[3]
        ])
Beispiel #58
0
    def test_moz_langpack_contributors(self):
        """
        If a .inc file has a commented-out entity named
        MOZ_LANGPACK_CONTRIBUTORS, the parser should un-comment it and
        process it as an entity.
        """
        input_string = dedent("""
            #define String Some String

            # #define MOZ_LANGPACK_CONTRIBUTORS Contributor list
        """)

        path, resource = self.parse_string(input_string)
        assert_equal(len(resource.translations), 2)
        assert_attributes_equal(resource.translations[1],
                                key='MOZ_LANGPACK_CONTRIBUTORS',
                                strings={None: 'Contributor list'})
Beispiel #59
0
    def test_url_for_path(self):
        """
        Return the first locale_checkout_path for locales active for the
        repo's project that matches the given path.
        """
        matching_locale = LocaleFactory.create(code='match')
        non_matching_locale = LocaleFactory.create(code='nomatch')
        repo = RepositoryFactory.create(
            project__locales=[matching_locale, non_matching_locale],
            project__slug='test-project',
            url='https://example.com/path/to/{locale_code}/',
        )

        with self.settings(MEDIA_ROOT='/media/root'):
            test_path = '/media/root/projects/test-project/path/to/match/foo/bar.po'
            assert_equal(repo.url_for_path(test_path),
                         'https://example.com/path/to/match/')
Beispiel #60
0
    def test_no_translations(self):
        """
        We don't attribute anyone if there aren't any new translations.
        """
        TranslationFactory.create(
            locale=self.translated_locale,
            entity=self.main_db_entity,
            approved=True,
            date=aware_datetime(2015, 1, 1)
        )

        with patch.object(self.main_db_entity, 'has_changed', return_value=False):
            self.changeset.update_vcs_entity(
                self.translated_locale, self.main_db_entity, MagicMock()
            )
            self.changeset.execute_update_vcs()
            assert_equal(self.changeset.commit_authors_per_locale[self.translated_locale.code], [])