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_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)
Example #4
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 #5
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})
    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 #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 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 #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 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 #12
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())
    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 #14
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)
Example #15
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 #16
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 #17
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 #18
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 #19
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 #20
0
    def test_project_slugs(self):
        """
        If project slugs are passed to Command.handle, only sync projects
        matching those slugs.
        """
        ignore_project, handle_project = ProjectFactory.create_batch(2)

        self.execute_command(handle_project.slug)
        self.mock_sync_project.delay.assert_called_with(handle_project.pk, no_pull=False, no_commit=False)
Example #21
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 #22
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)
 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 #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/')
        assert_redirects(response, reverse('pontoon.home'))
        assert_equal(self.client.session['translate_error'], {'none': None})
Example #25
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 #26
0
    def test_handle_project_slugs(self):
        """
        If project slugs are passed to Command.handle, only sync projects
        matching those slugs.
        """
        ignore_project, handle_project = ProjectFactory.create_batch(2)
        self.command.handle_project = Mock()
        self.execute_command(handle_project.slug)

        self.command.handle_project.assert_called_with(handle_project)
        assert_not_in(call(ignore_project), self.command.handle_project.mock_calls)
Example #27
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 #28
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 #29
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 #30
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)