Coverage for rfpy/auth/errors.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-31 16:00 +0000

1from enum import Enum 

2import re 

3 

4 

5class ErrorType(Enum): 

6 AUTH = 'Authorization Failed' 

7 PROJECT_STATUS = 'Invalid Project Status' 

8 ISSUE_STATUS = 'Invalid Issue Status' 

9 

10 

11class ValidationErrors(object): 

12 

13 '''Captures accumulated validation logic errors''' 

14 

15 def __init__(self, action): 

16 self.action = action 

17 self.errors = [] 

18 

19 @property 

20 def p_action(self): 

21 ''' 

22 Prettified action string name 

23 

24 Action text converted to space separated title case 

25 e.g. thisAndThat -> This And That 

26 ''' 

27 return re.sub(r'([A-Z])', r' \1', self.action).title() 

28 

29 @property 

30 def has_errors(self): 

31 return len(self.errors) > 0 

32 

33 def add(self, error_type, error_message): 

34 self.errors.append((error_type, error_message)) 

35 

36 def auth_failure(self, error_message): 

37 self.add(ErrorType.AUTH, error_message) 

38 

39 def invalid_issue_status(self, status): 

40 msg = f'Action {self.p_action} not permitted at Issue status {status}' 

41 self.add(ErrorType.ISSUE_STATUS, msg) 

42 

43 def invalid_project_status(self, status): 

44 msg = f'Action {self.p_action} not permitted at Project status {status}' 

45 self.add(ErrorType.PROJECT_STATUS, msg) 

46 

47 def __iter__(self): 

48 return iter(self.errors) 

49 

50 def __len__(self): 

51 return len(self.errors) 

52 

53 def as_dict(self): 

54 return [dict(type=error_type.value, message=msg) for error_type, msg in self.errors] 

55 

56 def __repr__(self): 

57 repr = '' 

58 for etype, emsg in self.errors: 

59 v = getattr(etype, 'value', str(etype)) 

60 repr += f"{v}: {emsg} \n" 

61 return repr