def test_update_branch_for_legalcode(self):
     helper = TransifexHelper()
     dummy_repo = DummyRepo("/trans/repo")
     legalcode = LegalCodeFactory(
         license__version="4.0",
         license__license_code="by-nc",
         language_code="fr",
     )
     helper._stats = {
         legalcode.license.resource_slug: {
             legalcode.language_code: {
                 "translated": {
                     "last_activity": now().isoformat(),
                 }
             }
         }
     }
     trb = TranslationBranch.objects.create(
         branch_name=legalcode.branch_name(),
         version=legalcode.license.version,
         language_code=legalcode.language_code,
         complete=False,
     )
     content = b"wxyz"
     # transifex_get_pofile_content
     # save_content_as_pofile_and_mofile
     with mpo(
         helper, "transifex_get_pofile_content"
     ) as mock_get_content, mp(
         "licenses.transifex.save_content_as_pofile_and_mofile"
     ) as mock_save:
         mock_get_content.return_value = content
         mock_save.return_value = [legalcode.translation_filename()]
         result = helper.update_branch_for_legalcode(
             dummy_repo, legalcode, trb
         )
     self.assertIsNone(result)
     mock_get_content.assert_called_with(
         legalcode.license.resource_slug, legalcode.language_code
     )
     mock_save.assert_called_with(legalcode.translation_filename(), content)
     self.assertEqual({legalcode}, set(trb.legalcodes.all()))
     relpath = os.path.relpath(
         legalcode.translation_filename(),
         settings.TRANSLATION_REPOSITORY_DIRECTORY,
     )
     dummy_repo.index.add.assert_called_with([relpath])
    def help_test_check_for_translation_updates(
        self, first_time, changed, resource_exists=True, language_exists=True
    ):
        """
        Helper to test several conditions, since all the setup is so
        convoluted.
        """
        language_code = "zh-Hans"
        license = LicenseFactory(version="4.0", license_code="by-nd")

        first_translation_update_datetime = datetime.datetime(
            2007, 1, 25, 12, 0, 0, tzinfo=utc
        )
        changed_translation_update_datetime = datetime.datetime(
            2020, 9, 30, 13, 11, 52, tzinfo=utc
        )

        if first_time:
            # We don't yet know when the last update was.
            legalcode_last_update = None
        else:
            # The last update we know of was at this time.
            legalcode_last_update = first_translation_update_datetime

        legalcode = LegalCodeFactory(
            license=license,
            language_code=language_code,
            translation_last_update=legalcode_last_update,
        )
        resource_slug = license.resource_slug

        # Will need an English legalcode if we need to create the resource
        if not resource_exists and language_code != DEFAULT_LANGUAGE_CODE:
            LegalCodeFactory(
                license=license,
                language_code=DEFAULT_LANGUAGE_CODE,
            )

        # 'timestamp' returns on translation stats from transifex
        if changed:
            # now it's the newer time
            timestamp = changed_translation_update_datetime.isoformat()
        else:
            # it's still the first time
            timestamp = first_translation_update_datetime.isoformat()

        mock_repo = MagicMock()
        mock_repo.is_dirty.return_value = False

        legalcodes = [legalcode]
        dummy_repo = DummyRepo("/trans/repo")

        # A couple of places use git.Repo(path) to get a git repo object. Have
        # them all get back our same dummy repo.
        def dummy_repo_factory(path):
            return dummy_repo

        helper = TransifexHelper()

        with mpo(
            helper, "handle_legalcodes_with_updated_translations"
        ) as mock_handle_legalcodes, mpo(
            helper, "get_transifex_resource_stats"
        ) as mock_get_transifex_resource_stats, mpo(
            helper, "create_resource"
        ) as mock_create_resource, mpo(
            LegalCode, "get_pofile"
        ) as mock_get_pofile, mpo(
            helper, "upload_messages_to_transifex"
        ) as mock_upload:
            if resource_exists:
                if language_exists:
                    mock_get_transifex_resource_stats.return_value = {
                        resource_slug: {
                            language_code: {
                                "translated": {
                                    "last_activity": timestamp,
                                }
                            }
                        }
                    }
                else:
                    # language does not exist first time, does the second time
                    mock_get_transifex_resource_stats.side_effect = [
                        {resource_slug: {}},
                        {
                            resource_slug: {
                                language_code: {
                                    "translated": {
                                        "last_activity": timestamp,
                                    }
                                }
                            }
                        },
                    ]
            else:
                # First time does not exist, second time does
                mock_get_transifex_resource_stats.side_effect = [
                    {},
                    {
                        resource_slug: {
                            language_code: {
                                "translated": {
                                    "last_activity": timestamp,
                                }
                            }
                        }
                    },
                ]
                # Will need pofile
                mock_get_pofile.return_value = polib.POFile()
            helper.check_for_translation_updates_with_repo_and_legalcodes(
                dummy_repo, legalcodes
            )

        if not resource_exists:
            # Should have tried to create resource
            mock_create_resource.assert_called_with(
                resource_slug=resource_slug,
                resource_name=legalcode.license.fat_code(),
                pofilename=os.path.basename(legalcode.translation_filename()),
                pofile_content=get_pofile_content(
                    mock_get_pofile.return_value
                ),
            )
        else:
            # Not
            mock_create_resource.assert_not_called()

        if language_exists:
            mock_upload.assert_not_called()
        else:
            mock_upload.assert_called()

        mock_get_transifex_resource_stats.assert_called_with()
        legalcode.refresh_from_db()
        if changed:
            # we mocked the actual processing, so...
            self.assertEqual(
                first_translation_update_datetime,
                legalcode.translation_last_update,
            )
            mock_handle_legalcodes.assert_called_with(dummy_repo, [legalcode])
        else:
            self.assertEqual(
                first_translation_update_datetime,
                legalcode.translation_last_update,
            )
            mock_handle_legalcodes.assert_called_with(dummy_repo, [])
        return