def test_translated(self): bylicense30ported = LicenseFactory(license_code="by-nc", version="3.0", jurisdiction_code="ar") bylicense30unported = LicenseFactory(license_code="by-nc", version="3.0", jurisdiction_code="") bylicense40 = LicenseFactory(license_code="by-nc", version="4.0", jurisdiction_code="") cc0v1license = LicenseFactory(license_code="CC0", version="1.0", jurisdiction_code="") should_be_translated = [ LegalCodeFactory(license=bylicense40), LegalCodeFactory(license=cc0v1license), ] should_not_be_translated = [ LegalCodeFactory(license=bylicense30ported), LegalCodeFactory(license=bylicense30unported), ] self.assertCountEqual(should_be_translated, list(LegalCode.objects.translated())) self.assertCountEqual( should_not_be_translated, set(LegalCode.objects.all()) - set(LegalCode.objects.translated()), )
def test_sampling_plus(self): self.assertTrue( LicenseFactory(license_code="nc-sampling+").sampling_plus) self.assertTrue(LicenseFactory(license_code="sampling+").sampling_plus) self.assertFalse(LicenseFactory(license_code="sampling").sampling_plus) self.assertFalse(LicenseFactory(license_code="MIT").sampling_plus) self.assertFalse( LicenseFactory(license_code="by-nc-nd-sa").sampling_plus)
def test_resource_slug(self): license = LicenseFactory(license_code="qwerty", version="2.7", jurisdiction_code="zys") self.assertEqual("qwerty_27_zys", license.resource_slug) license = LicenseFactory(license_code="qwerty", version="2.7", jurisdiction_code="") self.assertEqual("qwerty_27", license.resource_slug)
def test_resource_name(self): license = LicenseFactory(license_code="qwerty", version="2.7", jurisdiction_code="zys") self.assertEqual("QWERTY 2.7 ZYS", license.resource_name) license = LicenseFactory(license_code="qwerty", version="2.7", jurisdiction_code="") self.assertEqual("QWERTY 2.7", license.resource_name)
def test_get_metadata(self): license = LicenseFactory( **{ "license_code": "by-nc", "version": "3.0", "title_english": "The Title", "jurisdiction_code": "xyz", "permits_derivative_works": False, "permits_reproduction": False, "permits_distribution": True, "permits_sharing": True, "requires_share_alike": True, "requires_notice": True, "requires_attribution": True, "requires_source_code": True, "prohibits_commercial_use": True, "prohibits_high_income_nation_use": False, }) LegalCodeFactory(license=license, language_code="pt") LegalCodeFactory(license=license, language_code="en") data = license.get_metadata() expected_data = { "jurisdiction": "xyz", "license_code": "by-nc", "permits_derivative_works": False, "permits_distribution": True, "permits_reproduction": False, "permits_sharing": True, "prohibits_commercial_use": True, "prohibits_high_income_nation_use": False, "requires_attribution": True, "requires_notice": True, "requires_share_alike": True, "requires_source_code": True, "title_english": "The Title", "translations": { "en": { "deed": "/licenses/by-nc/3.0/xyz/", "license": "/licenses/by-nc/3.0/xyz/legalcode", "title": "The Title", }, "pt": { "deed": "/licenses/by-nc/3.0/xyz/deed.pt", "license": "/licenses/by-nc/3.0/xyz/legalcode.pt", "title": "The Title", }, }, "version": "3.0", } self.assertEqual(expected_data, data)
def test_valid(self): bylicense30ported = LicenseFactory(license_code="by-nc", version="3.0", jurisdiction_code="ar") bylicense30unported = LicenseFactory(license_code="by-nc", version="3.0", jurisdiction_code="") nonbylicense30ported = LicenseFactory(license_code="xyz", version="3.0", jurisdiction_code="ar") nonbylicense30unported = LicenseFactory(license_code="xyz", version="3.0", jurisdiction_code="") bylicense40 = LicenseFactory(license_code="by-nc", version="4.0", jurisdiction_code="") nonbylicense40 = LicenseFactory(license_code="xyz", version="4.0", jurisdiction_code="") cc0v1license = LicenseFactory(license_code="CC0", version="1.0", jurisdiction_code="") noncc0v1license = LicenseFactory(license_code="xyz", version="1.0", jurisdiction_code="") # Test valid() should_be_valid = [ LegalCodeFactory(license=bylicense30ported), LegalCodeFactory(license=bylicense30unported), LegalCodeFactory(license=bylicense40), LegalCodeFactory(license=cc0v1license), ] should_not_be_valid = [ LegalCodeFactory(license=nonbylicense30ported), LegalCodeFactory(license=nonbylicense30unported), LegalCodeFactory(license=nonbylicense40), LegalCodeFactory(license=noncc0v1license), ] self.assertCountEqual(should_be_valid, list(LegalCode.objects.valid())) self.assertCountEqual( should_not_be_valid, set(LegalCode.objects.all()) - set(LegalCode.objects.valid()), ) # Test validgroups() self.assertCountEqual( should_be_valid, list(LegalCode.objects.validgroups()["by4.0"]) + list(LegalCode.objects.validgroups()["by3.0"]) + list(LegalCode.objects.validgroups()["zero1.0"]), ) self.assertCountEqual( should_not_be_valid, set(LegalCode.objects.all()) - set( list(LegalCode.objects.validgroups()["by4.0"]) + list(LegalCode.objects.validgroups()["by3.0"]) + list(LegalCode.objects.validgroups()["zero1.0"])), )
def test_has_english(self): license = LicenseFactory() lc_fr = LegalCodeFactory(license=license, language_code="fr") self.assertFalse(lc_fr.has_english()) lc_en = LegalCodeFactory(license=license, language_code="en") self.assertTrue(lc_fr.has_english()) self.assertTrue(lc_en.has_english())
def test_translation_filename(self): data = [ # ("expected", license_code, version, jurisdiction, language), ("/foo/legalcode/de/LC_MESSAGES/by-sa_03.po", "by-sa", "0.3", "", "de"), ( "/foo/legalcode/de/LC_MESSAGES/by-sa_03_xx.po", "by-sa", "0.3", "xx", "de", ), ] for expected, license_code, version, jurisdiction, language in data: with self.subTest(expected): license = LicenseFactory( license_code=license_code, version=version, jurisdiction_code=jurisdiction, ) self.assertEqual( expected, LegalCodeFactory( license=license, language_code=language).translation_filename(), )
def _test_get_deed_or_license_path(self, data): for ( version, license_code, jurisdiction_code, language_code, expected_deed_path, expected_deed_symlinks, expected_license_path, expected_license_symlinks, ) in data: license = LicenseFactory( license_code=license_code, version=version, jurisdiction_code=jurisdiction_code, ) legalcode = LegalCodeFactory(license=license, language_code=language_code) self.assertEqual( [expected_deed_path, expected_deed_symlinks], legalcode.get_file_and_links("deed"), ) self.assertEqual( [expected_license_path, expected_license_symlinks], legalcode.get_file_and_links("legalcode"), )
def test_upload_messages_english_resource_exists(self): # English because it's the source messages and is handled differently license = LicenseFactory(license_code="by-nd", version="4.0") legalcode = LegalCodeFactory( license=license, language_code=DEFAULT_LANGUAGE_CODE, ) test_resources = [ { "slug": license.resource_slug, } ] test_pofile = polib.POFile() with mpo(self.helper, "get_transifex_resources") as mock_gtr: mock_gtr.return_value = test_resources with mp("licenses.transifex.get_pofile_content") as mock_gpc: mock_gpc.return_value = "not really" with mpo(self.helper, "update_source_messages") as mock_usm: self.helper.upload_messages_to_transifex( legalcode, test_pofile ) mock_gtr.assert_called_with() mock_gpc.assert_called_with(test_pofile) mock_usm.assert_called_with( "by-nd_40", "/trans/repo/legalcode/en/LC_MESSAGES/by-nd_40.po", "not really", )
def test_upload_messages_non_english_resource_exists(self): # non-English because it's not the source messages and is handled # differently license = LicenseFactory(license_code="by-nd", version="4.0") legalcode = LegalCodeFactory(license=license, language_code="fr") test_resources = [ { "slug": license.resource_slug, } ] test_pofile = mock.MagicMock() with mpo(self.helper, "get_transifex_resources") as mock_gtr: mock_gtr.return_value = test_resources with mp("licenses.transifex.get_pofile_content") as mock_gpc: mock_gpc.return_value = "not really" with mpo(self.helper, "update_translations") as mock_ut: self.helper.upload_messages_to_transifex( legalcode, test_pofile ) mock_gtr.assert_called_with() mock_gpc.assert_called_with(test_pofile) mock_ut.assert_called_with( "by-nd_40", "fr", "/trans/repo/legalcode/fr/LC_MESSAGES/by-nd_40.po", "not really", )
def test_str(self): license = LicenseFactory(license_code="bx-oh", version="1.3", jurisdiction_code="any") self.assertEqual( str(license), f"License<{license.license_code},{license.version},{license.jurisdiction_code}>", )
def test_metadata_view(self): LicenseFactory() with mock.patch.object(License, "get_metadata") as mock_get_metadata: mock_get_metadata.return_value = {"foo": "bar"} rsp = self.client.get(reverse("metadata")) self.assertEqual(200, rsp.status_code) mock_get_metadata.assert_called_with() self.assertEqual(b"- foo: bar\n", rsp.content)
def test_level_of_freedom(self): data = [ ("by", FREEDOM_LEVEL_MAX), ("devnations", FREEDOM_LEVEL_MIN), ("sampling", FREEDOM_LEVEL_MIN), ("sampling+", FREEDOM_LEVEL_MID), ("by-nc", FREEDOM_LEVEL_MID), ("by-nd", FREEDOM_LEVEL_MID), ("by-sa", FREEDOM_LEVEL_MAX), ] for license_code, expected_freedom in data: with self.subTest(license_code): license = LicenseFactory(license_code=license_code) self.assertEqual(expected_freedom, license.level_of_freedom)
def test_text_in_deeds(self): LicenseFactory() for license in License.objects.filter(version="4.0"): with self.subTest(license.fat_code): # Test in English and for 4.0 since that's how we've set up the strings to test for url = build_deed_url( license.license_code, license.version, license.jurisdiction_code, "en", ) rsp = self.client.get(url) self.assertEqual(rsp.status_code, 200) self.validate_deed_text(rsp, license)
def test_license_deed_view_code_version_jurisdiction_language(self): license = LicenseFactory( license_code="by-nc", jurisdiction_code="es", version="3.0" ) language_code = "fr" lc = LegalCodeFactory(license=license, language_code=language_code) # "<code:license_code>/<version:version>/<jurisdiction:jurisdiction>/deed.<lang:target_lang>" url = lc.deed_url # Mock 'get_translation_object' because we have no 3.0 translations imported yet # and we can't use 4.0 to test jurisdictions. translation_object = DjangoTranslation(language="fr") with mock.patch.object(LegalCode, "get_translation_object") as mock_gto: mock_gto.return_value = translation_object rsp = self.client.get(url) self.assertEqual(200, rsp.status_code)
def test_get_legalcode_for_language_code(self): license = LicenseFactory() lc_pt = LegalCodeFactory(license=license, language_code="pt") lc_en = LegalCodeFactory(license=license, language_code="en") with override(language="pt"): result = license.get_legalcode_for_language_code(None) self.assertEqual(lc_pt.id, result.id) result = license.get_legalcode_for_language_code("pt") self.assertEqual(lc_pt.id, result.id) result = license.get_legalcode_for_language_code("en") self.assertEqual(lc_en.id, result.id) with self.assertRaises(LegalCode.DoesNotExist): license.get_legalcode_for_language_code("en_us") result = license.get_legalcode_for_language_code("en-us") self.assertEqual(lc_en.id, result.id)
def test_upload_messages_to_transifex_no_resource_yet(self): # English so we can create the resource license = LicenseFactory(license_code="by-nd", version="4.0") legalcode = LegalCodeFactory( license=license, language_code=DEFAULT_LANGUAGE_CODE, ) pofile_content = """ msgid "" msgstr "" "Project-Id-Version: by-nd-4.0\n" "Language: en\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" msgid "license_medium" msgstr "Attribution-NoDerivatives 4.0 International" """ english_pofile = polib.pofile(pofile=pofile_content) with mpo(self.helper, "get_transifex_resources") as mock_gtr: mock_gtr.return_value = [] with mpo(self.helper, "create_resource") as mock_create_resource: with mpo(legalcode, "get_pofile") as mock_gpwem: mock_gpwem.return_value = english_pofile with mp( "licenses.transifex.get_pofile_content" ) as mock_gpc: mock_gpc.return_value = "not really" self.helper.upload_messages_to_transifex(legalcode) mock_create_resource.assert_called_with( "by-nd_40", "CC BY-ND 4.0", "/trans/repo/legalcode/en/LC_MESSAGES/by-nd_40.po", "not really", ) mock_gpwem.assert_called_with() mock_gtr.assert_called_with()
def test_sa(self): self.assertFalse(LicenseFactory(license_code="xyz").sa) self.assertTrue(LicenseFactory(license_code="xyz-sa").sa)
def test_superseded(self): lic1 = LicenseFactory() lic2 = LicenseFactory(is_replaced_by=lic1) self.assertTrue(lic2.superseded) self.assertFalse(lic1.superseded)
def test_rdf(self): license = LicenseFactory(license_code="bx-oh", version="1.3", jurisdiction_code="any") self.assertEqual("RDF Generation Not Implemented", license.rdf())
def test_get_deed_or_license_path(self): """ 4.0 formula: /licenses/VERSION/LICENSE_deed_LANGAUGE.html /licenses/VERSION/LICENSE_legalcode_LANGAUGEhtml 4.0 examples: /licenses/4.0/by-nc-nd_deed_en.html /licenses/4.0/by-nc-nd_legalcode_en.html /licenses/4.0/by_deed_en.html /licenses/4.0/by_legalcode_en.html /licenses/4.0/by_deed_zh-Hans.html /licenses/4.0/by_legalcode_zh-Hans.html 3.0 formula: /licenses/VERSION/JURISDICTION/LICENSE_deed_LANGAUGE.html /licenses/VERSION/JURISDICTION/LICENSE_legalcode_LANGAUGE.html 3.0 examples: /licenses/3.0/xu/by_deed_en.html /licenses/3.0/xu/by_legalcode_en.html /licenses/3.0/am/by_deed_hy.html /licenses/3.0/am/by_legalcode_hy.html /licenses/3.0/rs/by_deed_rs-Cyrl.html /licenses/3.0/rs/by_legalcode_rs-Cyrl.html For jurisdiction, I used “xu” to mean “unported”. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#User-assigned_code_elements. cc0 formula: /publicdomain/VERSION/LICENSE_deed_LANGAUGE.html /publicdomain/VERSION/LICENSE_legalcode_LANGAUGE.html cc0 examples: /publicdomain/1.0/zero_deed_en.html /publicdomain/1.0/zero_legalcode_en.html /publicdomain/1.0/zero_deed_ja.html /publicdomain/1.0/zero_legalcode_ja.html """ data = [ ( "4.0", "by-nc-nd", "", "en", "licenses/4.0/by-nc-nd_deed_en.html", "licenses/4.0/by-nc-nd_legalcode_en.html", ), ( "4.0", "by", "", "en", "licenses/4.0/by_deed_en.html", "licenses/4.0/by_legalcode_en.html", ), ( "4.0", "by", "", "zh-Hans", "licenses/4.0/by_deed_zh-Hans.html", "licenses/4.0/by_legalcode_zh-Hans.html", ), ( "3.0", "by", "", "en", "licenses/3.0/xu/by_deed_en.html", "licenses/3.0/xu/by_legalcode_en.html", ), ( "1.0", "CC0", "", "en", "publicdomain/1.0/zero_deed_en.html", "publicdomain/1.0/zero_legalcode_en.html", ), ( "1.0", "CC0", "", "ja", "publicdomain/1.0/zero_deed_ja.html", "publicdomain/1.0/zero_legalcode_ja.html", ), ] for ( version, license_code, jurisdiction_code, language_code, expected_deed_path, expected_license_path, ) in data: license = LicenseFactory( license_code=license_code, version=version, jurisdiction_code=jurisdiction_code, ) legalcode = LegalCodeFactory(license=license, language_code=language_code) self.assertEqual(expected_deed_path, legalcode.get_deed_path()) self.assertEqual(expected_license_path, legalcode.get_license_path())
def test_logos(self): # Every license includes "cc-logo" self.assertIn("cc-logo", LicenseFactory().logos()) self.assertEqual(["cc-logo", "cc-zero"], LicenseFactory(license_code="CC0").logos()) self.assertEqual( ["cc-logo", "cc-by"], LicenseFactory( license_code="by", version="4.0", prohibits_commercial_use=False, requires_share_alike=False, permits_derivative_works=True, ).logos(), ) self.assertEqual( ["cc-logo", "cc-by", "cc-nc"], LicenseFactory( license_code="by-nc", version="3.0", prohibits_commercial_use=True, requires_share_alike=False, permits_derivative_works=True, ).logos(), ) self.assertEqual( ["cc-logo", "cc-by", "cc-nd"], LicenseFactory( license_code="by-nd", version="4.0", prohibits_commercial_use=False, requires_share_alike=False, permits_derivative_works=False, ).logos(), ) self.assertEqual( ["cc-logo", "cc-by", "cc-sa"], LicenseFactory( license_code="by-sa", version="4.0", prohibits_commercial_use=False, requires_share_alike=True, permits_derivative_works=True, ).logos(), ) self.assertEqual( ["cc-logo", "cc-by", "cc-nc", "cc-sa"], LicenseFactory( license_code="by-nc-sa", version="4.0", prohibits_commercial_use=True, requires_share_alike=True, permits_derivative_works=True, ).logos(), ) self.assertEqual( ["cc-logo", "cc-by", "cc-nc", "cc-sa"], LicenseFactory( license_code="by-nc-sa", version="3.0", prohibits_commercial_use=True, requires_share_alike=True, permits_derivative_works=True, ).logos(), )
def test_nd(self): self.assertFalse(LicenseFactory(license_code="xyz").nd) self.assertTrue(LicenseFactory(license_code="by-nd-xyz").nd)
def test_nc(self): self.assertFalse(LicenseFactory(license_code="xyz").nc) self.assertTrue(LicenseFactory(license_code="by-nc-xyz").nc)
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
def setUp(self): self.by = LicenseFactory( license_code="by", version="4.0", permits_derivative_works=True, permits_reproduction=True, permits_distribution=True, permits_sharing=True, requires_share_alike=False, requires_notice=True, requires_attribution=True, requires_source_code=False, prohibits_commercial_use=False, prohibits_high_income_nation_use=False, ) self.by_nc = LicenseFactory( license_code="by-nc", version="4.0", permits_derivative_works=True, permits_reproduction=True, permits_distribution=True, permits_sharing=True, requires_share_alike=False, requires_notice=True, requires_attribution=True, requires_source_code=False, prohibits_commercial_use=True, prohibits_high_income_nation_use=False, ) self.by_nc_nd = LicenseFactory( license_code="by-nc-nd", version="4.0", permits_derivative_works=False, permits_reproduction=True, permits_distribution=True, permits_sharing=True, requires_share_alike=False, requires_notice=True, requires_attribution=True, requires_source_code=False, prohibits_commercial_use=True, prohibits_high_income_nation_use=False, ) self.by_nc_sa = LicenseFactory( license_code="by-nc-sa", version="4.0", permits_derivative_works=True, permits_reproduction=True, permits_distribution=True, permits_sharing=True, requires_share_alike=True, requires_notice=True, requires_attribution=True, requires_source_code=False, prohibits_commercial_use=True, prohibits_high_income_nation_use=False, ) self.by_nd = LicenseFactory( license_code="by-nd", version="4.0", permits_derivative_works=False, permits_reproduction=True, permits_distribution=True, permits_sharing=True, requires_share_alike=False, requires_notice=True, requires_attribution=True, requires_source_code=False, prohibits_commercial_use=False, prohibits_high_income_nation_use=False, ) self.by_sa = LicenseFactory( license_code="by-sa", version="4.0", permits_derivative_works=True, permits_reproduction=True, permits_distribution=True, permits_sharing=True, requires_share_alike=True, requires_notice=True, requires_attribution=True, requires_source_code=False, prohibits_commercial_use=False, prohibits_high_income_nation_use=False, ) for license in License.objects.all(): LegalCodeFactory(license=license, language_code="en") LegalCodeFactory(license=license, language_code="es") LegalCodeFactory(license=license, language_code="fr") self.by_sa_30_es = LicenseFactory( license_code="by-sa", version="3.0", jurisdiction_code="es", permits_derivative_works=True, permits_reproduction=True, permits_distribution=True, permits_sharing=True, requires_share_alike=True, requires_notice=True, requires_attribution=True, requires_source_code=False, prohibits_commercial_use=False, prohibits_high_income_nation_use=False, ) LegalCodeFactory( license=self.by_sa_30_es, language_code="es-es" ) # Default lang super().setUp()