Example #1
0
    def test_disabled_projects(self):
        """Only sync projects that aren't disabled."""
        ProjectFactory.create(disabled=True)
        active_project = ProjectFactory.create(disabled=False)

        self.execute_command()
        self.mock_sync_project.delay.assert_called_with(active_project.pk, no_pull=False, no_commit=False)
Example #2
0
    def test_handle_disabled_projects(self):
        """Only sync projects that aren't disabled."""
        disabled_project = ProjectFactory.create(disabled=True)
        active_project = ProjectFactory.create(disabled=False)
        self.command.handle_project = Mock()
        self.execute_command()

        self.command.handle_project.assert_any_call(active_project)
        assert_not_in(call(disabled_project), self.command.handle_project.mock_calls)
Example #3
0
    def test_locale_not_available(self):
        """
        If the requested locale is not available for this project,
        redirect home.
        """
        LocaleFactory.create(code='fakelocale')
        ProjectFactory.create(slug='valid-project')

        response = self.client.get('/fakelocale/valid-project/path/')
        assert_equal(response.status_code, 404)
Example #4
0
    def test_needs_sync(self):
        """
        Project.needs_sync should be True if ChangedEntityLocale objects
        exist for its entities or if Project.has_changed is True.
        """
        assert_true(ProjectFactory.create(has_changed=True).needs_sync)

        project = ProjectFactory.create(has_changed=False)
        ChangedEntityLocaleFactory.create(entity__resource__project=project)
        assert_true(project.needs_sync)
    def test_sync_log(self):
        """Create a new sync log when command is run."""
        assert_false(SyncLog.objects.exists())

        ProjectFactory.create()
        with patch.object(sync_projects, 'timezone') as mock_timezone:
            mock_timezone.now.return_value = aware_datetime(2015, 1, 1)
            self.execute_command()

        sync_log = SyncLog.objects.all()[0]
        assert_equal(sync_log.start_time, aware_datetime(2015, 1, 1))
Example #6
0
    def test_locale_not_available(self):
        """
        If the requested locale is not available for this project,
        redirect home.
        """
        LocaleFactory.create(code='fakelocale')
        ProjectFactory.create(slug='valid-project')

        response = self.client.get('/fakelocale/valid-project/')
        assert_redirects(response, reverse('pontoon.home'))
        assert_equal(self.client.session['translate_error'], {'none': None})
Example #7
0
    def test_locale_not_available(self):
        """
        If the requested locale is not available for this project,
        redirect home.
        """
        LocaleFactory.create(code="fakelocale")
        ProjectFactory.create(slug="valid-project")

        response = self.client.get("/fakelocale/valid-project/")
        assert_redirects(response, reverse("pontoon.home"))
        assert_equal(self.client.session["translate_error"], {"none": None})
Example #8
0
    def test_non_repository_projects(self):
        """Only sync projects with data_source=repository."""
        ProjectFactory.create(data_source='database')
        repo_project = ProjectFactory.create(data_source='repository')

        self.execute_command()
        self.mock_sync_project.delay.assert_called_with(
            repo_project.pk,
            ANY,
            locale=None,
            no_pull=False,
            no_commit=False,
            force=False
        )
Example #9
0
 def setUp(self):
     self.locale = LocaleFactory.create(cldr_plurals="0,1")
     self.project = ProjectFactory.create(locales=[self.locale])
     self.main_resource = ResourceFactory.create(project=self.project, path="main.lang")
     self.other_resource = ResourceFactory.create(project=self.project, path="other.lang")
     self.main_entity = EntityFactory.create(
         resource=self.main_resource,
         string="Source String",
         string_plural="Plural Source String",
         key="Source String",
     )
     self.other_entity = EntityFactory.create(
         resource=self.other_resource,
         string="Other Source String",
         key="Key" + KEY_SEPARATOR + "Other Source String",
     )
     self.main_translation = TranslationFactory.create(
         entity=self.main_entity, locale=self.locale, plural_form=0, string="Translated String"
     )
     self.main_translation_plural = TranslationFactory.create(
         entity=self.main_entity, locale=self.locale, plural_form=1, string="Translated Plural String"
     )
     self.other_translation = TranslationFactory.create(
         entity=self.other_entity, locale=self.locale, string="Other Translated String"
     )
     self.subpage = SubpageFactory.create(project=self.project, name="Subpage", resources=[self.main_resource])
Example #10
0
    def setUp(self):
        """
        We setup a sample contributor with random set of translations.
        """
        super(ContributorTimelineViewTests, self).setUp()
        self.project = ProjectFactory.create()
        self.translations = OrderedDict()

        for i in range(26):
            date = make_aware(datetime(2016, 12, 1) - timedelta(days=i))
            translations_count = randint(1, 3)
            self.translations.setdefault((date, translations_count), []).append(
                sorted(
                    TranslationFactory.create_batch(
                        translations_count,
                        date=date,
                        user=self.user,
                        entity__resource__project=self.project,
                    ),
                    key=lambda t: t.pk,
                    reverse=True,
                )
            )

        mock_render = patch('pontoon.contributors.views.render', return_value=HttpResponse(''))
        self.mock_render = mock_render.start()
        self.addCleanup(mock_render.stop)
Example #11
0
 def test_get_latest_activity_without_latest(self):
     """
     If the project doesn't have a latest_translation and no locale
     is given, return None.
     """
     project = ProjectFactory.create(latest_translation=None)
     assert_is_none(project.get_latest_activity())
Example #12
0
 def test_repository_for_path_none(self):
     """
     If the project has no matching repositories, raise a ValueError.
     """
     project = ProjectFactory.create(repositories=[])
     with assert_raises(ValueError):
         project.repository_for_path("doesnt/exist")
Example #13
0
    def test_save_latest_translation_update(self):
        """
        When a translation is saved, update the latest_translation
        attribute on the related project, locale, stats, and
        project_locale objects.
        """
        locale = LocaleFactory.create(latest_translation=None)
        project = ProjectFactory.create(locales=[locale], latest_translation=None)
        resource = ResourceFactory.create(project=project)
        stats = StatsFactory.create(locale=locale, resource=resource, latest_translation=None)
        project_locale = ProjectLocale.objects.get(locale=locale, project=project)

        assert_is_none(locale.latest_translation)
        assert_is_none(project.latest_translation)
        assert_is_none(stats.latest_translation)
        assert_is_none(project_locale.latest_translation)

        translation = TranslationFactory.create(
            locale=locale,
            entity__resource=resource,
            date=aware_datetime(1970, 1, 1)
        )
        self.assert_latest_translation(locale, translation)
        self.assert_latest_translation(project, translation)
        self.assert_latest_translation(stats, translation)
        self.assert_latest_translation(project_locale, translation)

        # Ensure translation is replaced for newer translations
        newer_translation = TranslationFactory.create(
            locale=locale,
            entity__resource=resource,
            date=aware_datetime(1970, 2, 1)
        )
        self.assert_latest_translation(locale, newer_translation)
        self.assert_latest_translation(project, newer_translation)
        self.assert_latest_translation(stats, newer_translation)
        self.assert_latest_translation(project_locale, newer_translation)

        # Ensure translation isn't replaced for older translations.
        TranslationFactory.create(
            locale=locale,
            entity__resource=resource,
            date=aware_datetime(1970, 1, 5)
        )
        self.assert_latest_translation(locale, newer_translation)
        self.assert_latest_translation(project, newer_translation)
        self.assert_latest_translation(stats, newer_translation)
        self.assert_latest_translation(project_locale, newer_translation)

        # Ensure approved_date is taken into consideration as well.
        newer_approved_translation = TranslationFactory.create(
            locale=locale,
            entity__resource=resource,
            approved_date=aware_datetime(1970, 3, 1)
        )
        self.assert_latest_translation(locale, newer_approved_translation)
        self.assert_latest_translation(project, newer_approved_translation)
        self.assert_latest_translation(stats, newer_approved_translation)
        self.assert_latest_translation(project_locale, newer_approved_translation)
    def setUp(self):
        timezone_patch = patch.object(sync_projects, 'timezone')
        self.mock_timezone = timezone_patch.start()
        self.addCleanup(timezone_patch.stop)
        self.mock_timezone.now.return_value = aware_datetime(1970, 1, 1)

        self.translated_locale = LocaleFactory.create(code='translated-locale')
        self.inactive_locale = LocaleFactory.create(code='inactive-locale')
        self.repository = RepositoryFactory()

        self.db_project = ProjectFactory.create(
            name='db-project',
            locales=[self.translated_locale],
            repositories=[self.repository]
        )
        self.main_db_resource = ResourceFactory.create(
            project=self.db_project,
            path='main.lang',
            format='lang'
        )
        self.other_db_resource = ResourceFactory.create(
            project=self.db_project,
            path='other.lang',
            format='lang'
        )
        self.missing_db_resource = ResourceFactory.create(
            project=self.db_project,
            path='missing.lang',
            format='lang'
        )

        # Load paths from the fake locale directory.
        checkout_path_patch = patch.object(
            Project,
            'checkout_path',
            new_callable=PropertyMock,
            return_value=FAKE_CHECKOUT_PATH
        )
        checkout_path_patch.start()
        self.addCleanup(checkout_path_patch.stop)

        self.vcs_project = VCSProject(self.db_project)
        self.main_vcs_resource = self.vcs_project.resources[self.main_db_resource.path]
        self.other_vcs_resource = self.vcs_project.resources[self.other_db_resource.path]
        self.missing_vcs_resource = self.vcs_project.resources[self.missing_db_resource.path]
        self.main_vcs_entity = self.main_vcs_resource.entities['Source String']
        self.main_vcs_translation = self.main_vcs_entity.translations['translated-locale']

        # Mock VCSResource.save() for each resource to avoid altering
        # the filesystem.
        resource_save_patch = patch.object(VCSResource, 'save')
        resource_save_patch.start()
        self.addCleanup(resource_save_patch.stop)

        self.changeset = sync_projects.ChangeSet(
            self.db_project,
            self.vcs_project,
            aware_datetime(1970, 1, 1)
        )
Example #15
0
 def test_can_commit_false(self):
     """
     can_commit should be False if there are no repo that can be
     committed to.
     """
     repo = RepositoryFactory.build(type=Repository.FILE)
     project = ProjectFactory.create(repositories=[repo])
     assert_false(project.can_commit)
Example #16
0
 def test_can_commit_true(self):
     """
     can_commit should be True if there is a repo that can be
     committed to.
     """
     repo = RepositoryFactory.build(type=Repository.GIT)
     project = ProjectFactory.create(repositories=[repo])
     assert_true(project.can_commit)
Example #17
0
 def setUp(self):
     self.locale, self.locale_other = LocaleFactory.create_batch(2)
     self.project = ProjectFactory.create(
         locales=[self.locale, self.locale_other])
     self.resource = ResourceFactory.create(project=self.project,
                                            path='/main/path.po')
     EntityFactory.create(resource=self.resource)
     StatsFactory.create(resource=self.resource, locale=self.locale)
Example #18
0
 def test_options(self):
     project = ProjectFactory.create()
     self.execute_command(no_pull=True, no_commit=True)
     self.mock_sync_project.delay.assert_called_with(project.pk,
                                                     ANY,
                                                     no_pull=True,
                                                     no_commit=True,
                                                     force=False)
Example #19
0
 def test_can_commit_true(self):
     """
     can_commit should be True if there is a repo that can be
     committed to.
     """
     repo = RepositoryFactory.build(type=Repository.GIT)
     project = ProjectFactory.create(repositories=[repo])
     assert_true(project.can_commit)
Example #20
0
 def test_can_commit_false(self):
     """
     can_commit should be False if there are no repo that can be
     committed to.
     """
     repo = RepositoryFactory.build(type=Repository.FILE)
     project = ProjectFactory.create(repositories=[repo])
     assert_false(project.can_commit)
Example #21
0
 def test_repository_type_first(self):
     """
     If a project has repos, return the type of the repo created
     first.
     """
     project = ProjectFactory.create(repositories=[])
     RepositoryFactory.create(project=project, type=Repository.GIT)
     RepositoryFactory.create(project=project, type=Repository.HG)
     assert_equal(project.repository_type, Repository.GIT)
Example #22
0
 def test_repository_for_path(self):
     """
     Return the first repo found with a checkout path that contains
     the given path.
     """
     repo1, repo2, repo3 = RepositoryFactory.build_batch(3)
     project = ProjectFactory.create(repositories=[repo1, repo2, repo3])
     path = os.path.join(repo2.checkout_path, 'foo', 'bar')
     assert_equal(project.repository_for_path(path), repo2)
Example #23
0
    def test_cant_commit(self):
        """If project.can_commit is False, do not sync it."""
        project = ProjectFactory.create()

        with patch.object(Project, 'can_commit', new_callable=PropertyMock) as can_commit:
            can_commit.return_value = False

            self.execute_command(projects=project.slug)
            assert_false(self.mock_sync_project.delay.called)
Example #24
0
    def test_invalid_locale_valid_project(self):
        """
        If the project is valid but the locale isn't, redirect home.
        """
        project = ProjectFactory.create(slug='valid-project')
        ResourceFactory.create(project=project)

        response = self.client.get('/invalid-locale/valid-project/path/')
        assert_equal(response.status_code, 404)
Example #25
0
    def test_cant_commit(self):
        """If project.can_commit is False, do not sync it."""
        project = ProjectFactory.create()

        with patch.object(Project, "can_commit", new_callable=PropertyMock) as can_commit:
            can_commit.return_value = False

            self.execute_command(project.slug)
            assert_false(self.mock_sync_project.delay.called)
Example #26
0
 def test_repository_for_path(self):
     """
     Return the first repo found with a checkout path that contains
     the given path.
     """
     repo1, repo2, repo3 = RepositoryFactory.build_batch(3)
     project = ProjectFactory.create(repositories=[repo1, repo2, repo3])
     path = os.path.join(repo2.checkout_path, "foo", "bar")
     assert_equal(project.repository_for_path(path), repo2)
Example #27
0
    def test_invalid_locale_valid_project(self):
        """
        If the project is valid but the locale isn't, redirect home.
        """
        project = ProjectFactory.create(slug='valid-project')
        ResourceFactory.create(project=project)

        response = self.client.get('/invalid-locale/valid-project/path/')
        assert_equal(response.status_code, 404)
Example #28
0
 def test_repository_type_first(self):
     """
     If a project has repos, return the type of the repo created
     first.
     """
     project = ProjectFactory.create(repositories=[])
     RepositoryFactory.create(project=project, type=Repository.GIT)
     RepositoryFactory.create(project=project, type=Repository.HG)
     assert_equal(project.repository_type, Repository.GIT)
    def setUp(self):
        timezone_patch = patch.object(sync_projects, 'timezone')
        self.mock_timezone = timezone_patch.start()
        self.addCleanup(timezone_patch.stop)
        self.mock_timezone.now.return_value = aware_datetime(1970, 1, 1)

        self.translated_locale = LocaleFactory.create(code='translated-locale')
        self.inactive_locale = LocaleFactory.create(code='inactive-locale')

        self.db_project = ProjectFactory.create(
            name='db-project',
            locales=[self.translated_locale],
            repository_type='git',
            repository_url='https://example.com/git'
        )
        self.main_db_resource = ResourceFactory.create(
            project=self.db_project,
            path='main.lang',
            format='lang'
        )
        self.other_db_resource = ResourceFactory.create(
            project=self.db_project,
            path='other.lang',
            format='lang'
        )
        self.missing_db_resource = ResourceFactory.create(
            project=self.db_project,
            path='missing.lang',
            format='lang'
        )

        # Load paths from the fake locale directory.
        checkout_path_patch = patch.object(
            Project,
            'checkout_path',
            new_callable=PropertyMock,
            return_value=FAKE_CHECKOUT_PATH
        )
        checkout_path_patch.start()
        self.addCleanup(checkout_path_patch.stop)

        self.vcs_project = VCSProject(self.db_project)
        self.main_vcs_resource = self.vcs_project.resources[self.main_db_resource.path]
        self.other_vcs_resource = self.vcs_project.resources[self.other_db_resource.path]
        self.missing_vcs_resource = self.vcs_project.resources[self.missing_db_resource.path]
        self.main_vcs_entity = self.main_vcs_resource.entities['Source String']
        self.main_vcs_translation = self.main_vcs_entity.translations['translated-locale']

        # Mock VCSResource.save() for each resource to avoid altering
        # the filesystem.
        for resource in self.vcs_project.resources.values():
            save_patch = patch.object(resource, 'save')
            save_patch.start()
            self.addCleanup(save_patch.stop)

        self.changeset = sync_projects.ChangeSet(self.db_project, self.vcs_project)
Example #30
0
    def test_project_view(self):
        """
        Checks if project page is returned properly.
        """
        project = ProjectFactory.create()
        ResourceFactory.create(project=project)

        with patch('pontoon.base.views.render', wraps=render) as mock_render:
            self.client.get('/projects/{}/'.format(project.slug))
            assert_equal(mock_render.call_args[0][2]['project'], project)
Example #31
0
    def test_invalid_locale_valid_project(self):
        """
        If the project is valid but the locale isn't, redirect home.
        """
        project = ProjectFactory.create(slug='valid-project')
        ResourceFactory.create(project=project)

        response = self.client.get('/invalid-locale/valid-project/')
        assert_redirects(response, reverse('pontoon.home'))
        assert_equal(self.client.session['translate_error'], {'none': None})
 def test_options(self):
     project = ProjectFactory.create()
     self.execute_command(no_pull=True, no_commit=True)
     self.mock_sync_project.delay.assert_called_with(
         project.pk,
         ANY,
         no_pull=True,
         no_commit=True,
         force=False
     )
Example #33
0
    def test_project_view(self):
        """
        Checks if project page is returned properly.
        """
        project = ProjectFactory.create()
        ResourceFactory.create(project=project)

        with patch('pontoon.base.views.render', wraps=render) as mock_render:
            self.client.get('/projects/{}/'.format(project.slug))
            assert_equal(mock_render.call_args[0][2]['project'], project)
Example #34
0
def test_project_view(client):
    """
    Checks if project page is returned properly.
    """
    project = ProjectFactory.create(visibility="public")
    ResourceFactory.create(project=project)

    with patch("pontoon.projects.views.render", wraps=render) as mock_render:
        client.get("/projects/{}/".format(project.slug))
        assert mock_render.call_args[0][2]["project"] == project
Example #35
0
    def test_invalid_locale_valid_project(self):
        """
        If the project is valid but the locale isn't, redirect home.
        """
        project = ProjectFactory.create(slug="valid-project")
        ResourceFactory.create(project=project)

        response = self.client.get("/invalid-locale/valid-project/")
        assert_redirects(response, reverse("pontoon.home"))
        assert_equal(self.client.session["translate_error"], {"none": None})
    def test_syncable_projects_only(self):
        """
        Only sync projects that aren't disabled
        and for which sync isn't disabled.
        """
        ProjectFactory.create(disabled=True)
        ProjectFactory.create(sync_disabled=True)
        active_project = ProjectFactory.create(
            disabled=False,
            sync_disabled=False,
        )

        self.execute_command()
        self.mock_sync_project.delay.assert_called_with(active_project.pk,
                                                        ANY,
                                                        locale=None,
                                                        no_pull=False,
                                                        no_commit=False,
                                                        force=False)
Example #37
0
    def test_invalid_locale_valid_project(self):
        """
        If the project is valid but the locale isn't, redirect home.
        """
        project = ProjectFactory.create(slug='valid-project')
        ResourceFactory.create(project=project)

        response = self.client.get('/invalid-locale/valid-project/')
        assert_redirects(response, reverse('pontoon.home'))
        assert_equal(self.client.session['translate_error'], {'none': None})
Example #38
0
 def setUp(self):
     self.locale, self.locale_other = LocaleFactory.create_batch(2)
     self.project = ProjectFactory.create(
         locales=[self.locale, self.locale_other]
     )
     self.resource = ResourceFactory.create(
         project=self.project,
         path='/main/path.po'
     )
     EntityFactory.create(resource=self.resource)
     StatsFactory.create(resource=self.resource, locale=self.locale)
Example #39
0
    def test_get_latest_activity_with_latest(self):
        """
        If the project has a latest_translation and no locale is given,
        return it.
        """
        project = ProjectFactory.create()
        translation = TranslationFactory.create(entity__resource__project=project)
        project.latest_translation = translation
        project.save()

        assert_equal(project.get_latest_activity(), translation.latest_activity)
Example #40
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])
Example #41
0
    def test_project_add_locale(self):
        locale_kl = LocaleFactory.create(code='kl', name='Klingon')
        locale_gs = LocaleFactory.create(code='gs', name='Geonosian')
        project = ProjectFactory.create(
            data_source='database',
            locales=[locale_kl],
            repositories=[],
        )
        _create_or_update_translated_resources(project, [locale_kl])

        url = reverse('pontoon.admin.project', args=(project.slug, ))

        # Boring data creation for FormSets. Django is painful with that,
        # or I don't know how to handle that more gracefully.
        form = ProjectForm(instance=project)
        form_data = dict(form.initial)
        del form_data['width']
        del form_data['deadline']
        del form_data['contact']
        form_data.update({
            'subpage_set-INITIAL_FORMS': '0',
            'subpage_set-TOTAL_FORMS': '1',
            'subpage_set-MIN_NUM_FORMS': '0',
            'subpage_set-MAX_NUM_FORMS': '1000',
            'externalresource_set-TOTAL_FORMS': '1',
            'externalresource_set-MAX_NUM_FORMS': '1000',
            'externalresource_set-MIN_NUM_FORMS': '0',
            'externalresource_set-INITIAL_FORMS': '0',
            'tag_set-TOTAL_FORMS': '1',
            'tag_set-INITIAL_FORMS': '0',
            'tag_set-MAX_NUM_FORMS': '1000',
            'tag_set-MIN_NUM_FORMS': '0',
            'repositories-INITIAL_FORMS': '0',
            'repositories-MIN_NUM_FORMS': '0',
            'repositories-MAX_NUM_FORMS': '1000',
            'repositories-TOTAL_FORMS': '0',
            # These are the values that actually matter.
            'pk': project.pk,
            'locales': [locale_kl.id, locale_gs.id],
        })

        response = self.client.post(url, form_data)
        assert_code(response, 200)
        assert_not_contains(response, '. Error.')

        # Verify we have the right ProjectLocale objects.
        pl = ProjectLocale.objects.filter(project=project)
        assert_equal(len(pl), 2)

        # Verify that TranslatedResource objects have been created.
        resource = Resource.objects.get(project=project, path='database')
        tr = TranslatedResource.objects.filter(resource=resource)
        assert_equal(len(tr), 2)
Example #42
0
    def setUp(self):
        super(SyncProjectTests, self).setUp()
        self.db_project = ProjectFactory.create()
        self.sync_log = SyncLogFactory.create()

        self.mock_pull_changes = self.patch(
            'pontoon.sync.tasks.pull_changes', return_value=True)
        self.mock_project_needs_sync = self.patch_object(
            Project, 'needs_sync', new_callable=PropertyMock, return_value=True)
        self.mock_sync_project_repo = self.patch('pontoon.sync.tasks.sync_project_repo')

        self.mock_perform_sync_project = self.patch('pontoon.sync.tasks.perform_sync_project', return_value=[[], []])
Example #43
0
    def test_get_latest_activity_with_locale(self):
        """
        If a locale is given, defer to
        ProjectLocale.get_latest_activity.
        """
        locale = LocaleFactory.create()
        project = ProjectFactory.create(locales=[locale])

        with patch.object(ProjectLocale, "get_latest_activity") as mock_get_latest_activity:
            mock_get_latest_activity.return_value = "latest"
            assert_equal(locale.get_latest_activity(project=project), "latest")
            mock_get_latest_activity.assert_called_with(project, locale)
Example #44
0
    def setUp(self):
        super(TranslationActionsTests, self).setUp()
        project = ProjectFactory.create()
        locale = LocaleFactory.create()

        ProjectLocale.objects.create(project=project, locale=locale)

        translation = TranslationFactory.create(locale=locale, entity__resource__project=project)
        translation.approved = True
        translation.save()

        self.translation = translation
Example #45
0
    def setUp(self):
        super(SyncProjectTests, self).setUp()
        self.db_project = ProjectFactory.create()
        self.sync_log = SyncLogFactory.create()

        self.mock_pull_changes = self.patch(
            'pontoon.sync.tasks.pull_changes', return_value=True)
        self.mock_project_needs_sync = self.patch_object(
            Project, 'needs_sync', new_callable=PropertyMock, return_value=True)
        self.mock_sync_project_repo = self.patch('pontoon.sync.tasks.sync_project_repo')

        self.mock_perform_sync_project = self.patch('pontoon.sync.tasks.perform_sync_project')
Example #46
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])
Example #47
0
    def test_project_locale_added(self):
        """
        When a locale is added to a project, has_changed should be set
        to True.
        """
        project = ProjectFactory.create(locales=[], has_changed=False)
        assert_false(project.has_changed)

        locale = LocaleFactory.create()
        ProjectLocaleFactory.create(project=project, locale=locale)
        project.refresh_from_db()
        assert_true(project.has_changed)
Example #48
0
    def test_end_time(self):
        """
        Return the latest end time among repo sync logs for this log.
        """
        project = ProjectFactory.create(repositories=[])
        source_repo, repo1, repo2 = RepositoryFactory.create_batch(3, 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))

        assert_equal(project_sync_log.end_time, aware_datetime(2015, 1, 1))
Example #49
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/{}/ajax/contributors/".format(first_project.slug),
                HTTP_X_REQUESTED_WITH="XMLHttpRequest",
            )
            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/{}/ajax/contributors/".format(second_project.slug),
                HTTP_X_REQUESTED_WITH="XMLHttpRequest",
            )
            assert_equal(mock_render.call_args[0][0]["project"],
                         second_project)
            assert_equal(
                list(mock_render.call_args[0][0]["contributors"]),
                [second_project_contributor],
            )
Example #50
0
    def test_get_latest_activity_with_latest(self):
        """
        If the project has a latest_translation and no locale is given,
        return it.
        """
        project = ProjectFactory.create()
        translation = TranslationFactory.create(
            entity__resource__project=project)
        project.latest_translation = translation
        project.save()

        assert_equal(project.get_latest_activity(),
                     translation.latest_activity)
Example #51
0
    def test_get_latest_activity_with_locale(self):
        """
        If a locale is given, defer to
        ProjectLocale.get_latest_activity.
        """
        locale = LocaleFactory.create()
        project = ProjectFactory.create(locales=[locale])

        with patch.object(ProjectLocale,
                          'get_latest_activity') as mock_get_latest_activity:
            mock_get_latest_activity.return_value = 'latest'
            assert_equal(locale.get_latest_activity(project=project), 'latest')
            mock_get_latest_activity.assert_called_with(project, locale)
Example #52
0
    def test_manage_project_strings_translated_resource(self):
        """Test that adding new strings to a project enables translation of that
        project on all enabled locales.
        """
        locales = [
            LocaleFactory.create(code='kl', name='Klingon'),
            LocaleFactory.create(code='gs', name='Geonosian'),
        ]
        project = ProjectFactory.create(data_source='database',
                                        locales=locales,
                                        repositories=[])
        locales_count = len(locales)
        _create_or_update_translated_resources(project, locales)

        url = reverse('pontoon.admin.project.strings', args=(project.slug, ))

        new_strings = """
            Morty, do you know what "Wubba lubba dub dub" means?
            Oh that's just Rick's stupid non-sense catch phrase.
            It's not.
            In my people's tongue, it means "I am in great pain, please help me".
        """
        strings_count = 4
        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), strings_count)

        # Verify the resource has the right stats.
        resources = Resource.objects.filter(project=project)
        assert_equal(len(resources), 1)
        resource = resources[0]
        assert_equal(resource.total_strings, strings_count)

        # Verify the correct TranslatedResource objects have been created.
        translated_resources = TranslatedResource.objects.filter(
            resource__project=project)
        assert_equal(len(translated_resources), locales_count)

        # Verify stats have been correctly updated on locale, project and resource.
        for tr in translated_resources:
            assert_equal(tr.total_strings, strings_count)

        project = Project.objects.get(id=project.id)
        assert_equal(project.total_strings, strings_count * locales_count)

        for l in locales:
            locale = Locale.objects.get(id=l.id)
            assert_equal(locale.total_strings, strings_count)
Example #53
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])
        resource = ResourceFactory.create(project=project, path='foo.lang', total_strings=1)
        TranslatedResourceFactory.create(resource=resource, locale=locale)

        response = self.client.get('/fakelocale/valid-project/foo.lang/')
        assert_equal(response.status_code, 200)
Example #54
0
    def setUp(self):
        super(TranslationUpdateTestCase, self).setUp()

        locale = LocaleFactory.create()
        project = ProjectFactory.create()
        ProjectLocale.objects.create(
            project=project,
            locale=locale,
        )
        resource = ResourceFactory.create(project=project)
        entity = EntityFactory.create(resource=resource)

        self.translation = TranslationFactory.create(entity=entity, locale=locale)
        self.translation.locale.translators_group.user_set.add(self.user)
Example #55
0
    def setUp(self):
        # Force the checkout path to point to a test directory to make
        # resource file loading pass during tests.
        checkout_path_patch = patch.object(Project,
                                           'checkout_path',
                                           new_callable=PropertyMock,
                                           return_value=os.path.join(
                                               TEST_CHECKOUT_PATH,
                                               'no_resources_test'))
        self.mock_checkout_path = checkout_path_patch.start()
        self.addCleanup(checkout_path_patch.stop)

        self.project = ProjectFactory.create()
        self.vcs_project = VCSProject(self.project)
Example #56
0
def test_project_top_contributors(client):
    """
    Tests if view returns top contributors specific for given project.
    """
    first_project = ProjectFactory.create(visibility=Project.Visibility.PUBLIC)
    ResourceFactory.create(project=first_project)
    first_project_contributor = TranslationFactory.create(
        entity__resource__project=first_project
    ).user

    second_project = ProjectFactory.create(visibility=Project.Visibility.PUBLIC)
    ResourceFactory.create(project=second_project)
    second_project_contributor = TranslationFactory.create(
        entity__resource__project=second_project
    ).user

    with patch(
        "pontoon.projects.views.ProjectContributorsView.render_to_response",
        return_value=HttpResponse(""),
    ) as mock_render:
        client.get(
            "/projects/{}/ajax/contributors/".format(first_project.slug),
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        assert mock_render.call_args[0][0]["project"] == first_project
        assert list(mock_render.call_args[0][0]["contributors"]) == [
            first_project_contributor
        ]

        client.get(
            "/projects/{}/ajax/contributors/".format(second_project.slug),
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        assert mock_render.call_args[0][0]["project"] == second_project
        assert list(mock_render.call_args[0][0]["contributors"]) == [
            second_project_contributor
        ]
Example #57
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)
Example #58
0
    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')
Example #59
0
 def test_for_project_locale_filter(self):
     """
     Evaluate entities filtering by locale, project, obsolete.
     """
     other_locale = LocaleFactory.create()
     other_project = ProjectFactory.create(
         locales=[self.locale, other_locale])
     # Obsolete_entity
     EntityFactory.create(obsolete=True,
                          resource=self.main_resource,
                          string='Obsolete String')
     entities = Entity.for_project_locale(self.project, other_locale)
     assert_equal(len(entities), 0)
     entities = Entity.for_project_locale(other_project, self.locale)
     assert_equal(len(entities), 0)
     entities = Entity.for_project_locale(self.project, self.locale)
     assert_equal(len(entities), 2)