Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/bones/credential.py: 26%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-03 13:41 +0000

1from viur.core.bones.base import ReadFromClientError, ReadFromClientErrorSeverity 

2from viur.core.bones.string import StringBone 

3 

4 

5class CredentialBone(StringBone): 

6 """ 

7 A bone for storing credentials. This bone is designed to store sensitive information like 

8 passwords, API keys, or other secret strings. 

9 This is always empty if read from the database. 

10 If it's saved, its ignored if its values is still empty. 

11 If its value is not empty, it will update the value in the database 

12 

13 :ivar str type: The type identifier of the bone, set to "str.credential". 

14 """ 

15 type = "str.credential" 

16 

17 def __init__( 

18 self, 

19 *, 

20 max_length: int = None, # Unlimited length 

21 **kwargs 

22 ): 

23 

24 super().__init__(max_length=max_length, **kwargs) 

25 if self.multiple or self.languages: 

26 raise ValueError("CredentialBone cannot be multiple or translated") 

27 

28 def isInvalid(self, value): 

29 """ 

30 Returns None if the value would be valid for 

31 this bone, an error-message otherwise. 

32 """ 

33 if value is None: 

34 return False 

35 if self.max_length is not None and len(value) > self.max_length: 

36 return "Maximum length exceeded" 

37 

38 def serialize(self, skel: 'SkeletonInstance', name: str, parentIndexed: bool) -> bool: 

39 """ 

40 Serializes the bone's value for the database. 

41 

42 Updates the value in the entity only if a new value is supplied. Ensures the value is 

43 never indexed. 

44 

45 :param skel: The skeleton instance that the bone is part of. 

46 :type skel: SkeletonInstance 

47 :param str name: The name of the bone attribute. 

48 :param bool parentIndexed: Indicates whether the parent entity is indexed. 

49 :return: True if the value was updated in the database, False otherwise. 

50 :rtype: bool 

51 """ 

52 skel.dbEntity.exclude_from_indexes.add(name) # Ensure we are never indexed 

53 if name in skel.accessedValues and skel.accessedValues[name]: 

54 skel.dbEntity[name] = skel.accessedValues[name] 

55 return True 

56 return False 

57 

58 def unserialize(self, valuesCache, name): 

59 """ 

60 Unserializes the bone's value from the database. 

61 

62 This method always returns an empty dictionary as the CredentialBone's value is always empty when read from 

63 the database. 

64 

65 :param dict valuesCache: A dictionary containing the serialized values from the datastore. 

66 :param str name: The name of the bone attribute. 

67 :return: An empty dictionary, as the CredentialBone's value is always empty when read from the database. 

68 :rtype: dict 

69 """ 

70 return {} 

71 

72 def singleValueFromClient(self, value, skel, bone_name, client_data): 

73 if not (err := self.isInvalid(value)): 

74 return value, None 

75 

76 return self.getEmptyValue(), [ReadFromClientError(ReadFromClientErrorSeverity.Invalid, err)]