Repository: joke2k/faker
Pull request #1237 (2020-08-10 09:34:08)

Don't return too long company ids. Fix for #1233
### What does this changes

This is a bugfix for #1233 . 

### What was wrong

Finnish company id calculation did not handle invalid remainder 1 in checksum calculation, resulting two digit checksums.

### How this fixes it

Company id is generated until a valid one is found




Patch:
diff --git a/faker/providers/bank/fr_FR/__init__.py b/faker/providers/bank/fr_FR/__init__.py
index 0047f58d..f2ff8a98 100644
--- a/faker/providers/bank/fr_FR/__init__.py
+++ b/faker/providers/bank/fr_FR/__init__.py
@@ -2,5 +2,5 @@ from .. import Provider as BankProvider
 
 
 class Provider(BankProvider):
-    bban_format = '########################'
+    bban_format = '#######################'
     country_code = 'FR'
diff --git a/faker/providers/company/fi_FI/__init__.py b/faker/providers/company/fi_FI/__init__.py
index 8718a43e..f7b010f2 100644
--- a/faker/providers/company/fi_FI/__init__.py
+++ b/faker/providers/company/fi_FI/__init__.py
@@ -27,15 +27,21 @@ class Provider(CompanyProvider):
             sum_ = 0
             for x, y in zip(number, factors):
                 sum_ = sum_ + int(x) * y
+            if sum_ % 11 == 1:
+                raise ValueError('Checksum 1 is invalid')
             if sum_ % 11 == 0:
                 return '0'
             else:
                 return str(11 - sum_ % 11)
 
-        first_digit = str(self.random_digit_not_null())
-        body = first_digit + self.bothify('######')
-        cs = calculate_checksum(body)
-        return body + '-' + str(cs)
+        while True:
+            first_digit = str(self.random_digit_not_null())
+            body = first_digit + self.bothify('######')
+            try:
+                cs = calculate_checksum(body)
+            except ValueError:
+                continue
+            return body + '-' + str(cs)
 
     def company_vat(self):
         """


Test patch:
diff --git a/tests/providers/test_bank.py b/tests/providers/test_bank.py
index c1f4cd19..d06eb7fe 100644
--- a/tests/providers/test_bank.py
+++ b/tests/providers/test_bank.py
@@ -131,6 +131,22 @@ class TestEsES(unittest.TestCase):
         assert re.match(r"ES\d{22}", iban)
 
 
+class TestFrFR(unittest.TestCase):
+    """Tests the bank provider for fr_FR locale"""
+
+    def setUp(self):
+        self.fake = Faker('fr_FR')
+        Faker.seed(0)
+
+    def test_bban(self):
+        bban = self.fake.bban()
+        assert re.match(r"\d{23}", bban)
+
+    def test_iban(self):
+        iban = self.fake.iban()
+        assert re.match(r"FR\d{25}", iban)
+
+
 class TestEnPh:
 
     def test_swift(self, faker, num_samples):
diff --git a/tests/providers/test_company.py b/tests/providers/test_company.py
index 5bc28613..4d248d7e 100644
--- a/tests/providers/test_company.py
+++ b/tests/providers/test_company.py
@@ -23,9 +23,10 @@ class TestFiFI(unittest.TestCase):
         self.fake.random.seed(6)
         company_id = self.fake.company_business_id()
         assert company_id.endswith('0')
-        for seed in range(0, 11):
-            self.fake.random.seed(seed)
-            self.fake.company_business_id()
+
+        for seed in range(0, 1000):
+            Faker.seed(seed)
+            assert (len(self.fake.company_business_id()) == 9)
 
 
 class TestHyAm(unittest.TestCase):