Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/bones/email.py: 16%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-16 22:16 +0000

1import string 

2from encodings import idna 

3 

4from viur.core.bones.string import StringBone 

5 

6 

7class EmailBone(StringBone): 

8 """ 

9 The EmailBone class is a designed to store syntactically validated email addresses. 

10 

11 This class provides an email validation method, ensuring that the given email address conforms to the 

12 required format and structure. 

13 """ 

14 type = "str.email" 

15 """ 

16 A string representing the type of the bone, in this case "str.email". 

17 """ 

18 

19 def isInvalid(self, value): 

20 """ 

21 Checks if the provided email address is valid or not. 

22 

23 :param str value: The email address to be validated. 

24 :returns: An error message if the email address is invalid or None if it is valid. 

25 :rtype: str, None 

26 

27 The method checks if the provided email address is valid according to the following criteria: 

28 

29 1. The email address must not be empty. 

30 2. The email address must be shorter than 256 characters. 

31 3. The local part (account) must be shorter than or equal to 64 characters. 

32 4. The email address must contain an "@" symbol, separating the local part (account) and the domain part. 

33 5. The domain part must be a valid IDNA-encoded string and should not contain any spaces. 

34 6. The local part (account) should only contain valid characters. 

35 7. The local part (account) can also contain Unicode characters within the range of U+0080 to U+10FFFF. 

36 """ 

37 if not value: 

38 return "No value entered" 

39 try: 

40 assert len(value) < 256 

41 account, domain = value.split(u"@") 

42 subDomain, tld = domain.rsplit(".", 1) 

43 assert account and subDomain and tld 

44 assert subDomain[0] != "." 

45 assert len(account) <= 64 

46 except: 

47 return "Invalid email entered" 

48 isValid = True 

49 validChars = string.ascii_letters + string.digits + "!#$%&'*+-/=?^_`{|}~." 

50 unicodeLowerBound = u"\u0080" 

51 unicodeUpperBound = u"\U0010FFFF" 

52 for char in account: 

53 if not (char in validChars or (char >= unicodeLowerBound and char <= unicodeUpperBound)): 

54 isValid = False 

55 try: 

56 idna.ToASCII(subDomain) 

57 idna.ToASCII(tld) 

58 except: 

59 isValid = False 

60 if " " in subDomain or " " in tld: 

61 isValid = False 

62 if isValid: 

63 return None 

64 else: 

65 return "Invalid email entered"