Coverage for turvallisuusneuvonta/csaf/csaf.py: 84.85%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-05 19:27:17 +00:00

1"""CSAF Document model.""" 

2 

3from __future__ import annotations 

4 

5from typing import Annotated, List, Optional, no_type_check 

6 

7from pydantic import BaseModel, Field, field_validator 

8 

9from turvallisuusneuvonta.csaf.document import Document 

10from turvallisuusneuvonta.csaf.product import ProductTree 

11from turvallisuusneuvonta.csaf.vulnerability import Vulnerability 

12 

13 

14class CSAF(BaseModel): 

15 """ 

16 Representation of security advisory information as a JSON document. 

17 """ 

18 

19 document: Annotated[ 

20 Document, 

21 Field( 

22 description='Captures the meta-data about this document describing a particular set of' 

23 ' security advisories.', 

24 title='Document level meta-data', 

25 ), 

26 ] 

27 product_tree: Annotated[ 

28 Optional[ProductTree], 

29 Field( 

30 description='Is a container for all fully qualified product names that can be referenced elsewhere' 

31 ' in the document.', 

32 title='Product tree', 

33 ), 

34 ] = None 

35 vulnerabilities: Annotated[ 

36 Optional[List[Vulnerability]], 

37 Field( 

38 description='Represents a list of all relevant vulnerability information items.', 

39 min_length=1, 

40 title='Vulnerabilities', 

41 ), 

42 ] = None 

43 

44 @no_type_check 

45 def model_dump_json(self, *args, **kwargs): 

46 kwargs.setdefault('by_alias', True) 

47 return super().model_dump_json(*args, **kwargs) 

48 

49 @classmethod 

50 @no_type_check 

51 @field_validator('vulnerabilities') 

52 def check_len(cls, v): 

53 if not v: 

54 raise ValueError('vulnerabilities present but empty') 

55 return v