tmf-checker 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. tmf_checker/__init__.py +3 -0
  2. tmf_checker/__main__.py +5 -0
  3. tmf_checker/checks/__init__.py +146 -0
  4. tmf_checker/checks/create.py +562 -0
  5. tmf_checker/checks/errors.py +55 -0
  6. tmf_checker/checks/fields.py +70 -0
  7. tmf_checker/checks/lists.py +115 -0
  8. tmf_checker/checks/missing.py +111 -0
  9. tmf_checker/checks/patch.py +177 -0
  10. tmf_checker/cli.py +252 -0
  11. tmf_checker/client.py +133 -0
  12. tmf_checker/findings.py +119 -0
  13. tmf_checker/report.py +97 -0
  14. tmf_checker/runner.py +76 -0
  15. tmf_checker/spec.py +238 -0
  16. tmf_checker/specs/README.md +48 -0
  17. tmf_checker/specs/SHA256SUMS +10 -0
  18. tmf_checker/specs/TMF620-ProductCatalog-v4.1.0.swagger.json +9210 -0
  19. tmf_checker/specs/TMF620-Product_Catalog_Management-v5.0.0.oas.yaml +14355 -0
  20. tmf_checker/specs/TMF622-ProductOrder-v4.0.0.swagger.json +4331 -0
  21. tmf_checker/specs/TMF622-ProductOrdering-v5.0.0.oas.yaml +13029 -0
  22. tmf_checker/specs/TMF637-ProductInventory-v4.0.0.swagger.json +2751 -0
  23. tmf_checker/specs/TMF637-ProductInventory-v5.0.0.oas.yaml +8363 -0
  24. tmf_checker/specs/TMF666-Account-v4.0.0.swagger.json +6711 -0
  25. tmf_checker/specs/TMF666-Account_Management-v5.0.0.oas.yaml +9818 -0
  26. tmf_checker/specs/TMF678-CustomerBill-v4.0.0.swagger.json +1988 -0
  27. tmf_checker/specs/TMF678-CustomerBill-v5.0.0.oas.yaml +3984 -0
  28. tmf_checker/validate.py +190 -0
  29. tmf_checker/values.py +312 -0
  30. tmf_checker-0.1.0.dist-info/METADATA +180 -0
  31. tmf_checker-0.1.0.dist-info/RECORD +35 -0
  32. tmf_checker-0.1.0.dist-info/WHEEL +4 -0
  33. tmf_checker-0.1.0.dist-info/entry_points.txt +2 -0
  34. tmf_checker-0.1.0.dist-info/licenses/LICENSE +202 -0
  35. tmf_checker-0.1.0.dist-info/licenses/NOTICE +12 -0
@@ -0,0 +1,3 @@
1
+ """tmf-checker: check TM Forum Open API implementations against the published specs."""
2
+
3
+ __version__ = "0.1.0"
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from tmf_checker.cli import main
4
+
5
+ sys.exit(main())
@@ -0,0 +1,146 @@
1
+ """Check families. Each module has `run(ctx, resource) -> Iterable[Finding]`.
2
+
3
+ `Context` holds what checks share: the spec, the client, the run id, a cache of list
4
+ answers, and the resources this run created (deleted at the end by `cleanup`).
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from collections.abc import Iterator
10
+ from dataclasses import dataclass, field
11
+ from typing import Any
12
+ from urllib.parse import quote
13
+
14
+ from tmf_checker.client import Client, Response
15
+ from tmf_checker.findings import Finding
16
+ from tmf_checker.spec import Resource, Spec
17
+ from tmf_checker.validate import issues
18
+
19
+ # Statuses that mean "this operation is not there", not "it answered wrongly".
20
+ NOT_IMPLEMENTED = (404, 405, 501)
21
+ # Attributes a fields= projection may return besides the selection (TMF630).
22
+ ALWAYS_ALLOWED = {"id", "href"}
23
+
24
+
25
+ class Unauthorized(Exception): # noqa: N818
26
+ """The API answered 401: the run cannot go on without credentials."""
27
+
28
+
29
+ class Forbidden(Exception): # noqa: N818
30
+ """The API answered 403: this check cannot judge this resource. The runner turns it
31
+ into a `skipped <check> forbidden` line, never a gap."""
32
+
33
+ def __init__(self, response: Response) -> None:
34
+ super().__init__(f"{response.method} {response.url} answered 403")
35
+ self.response = response
36
+
37
+
38
+ @dataclass
39
+ class Listing:
40
+ response: Response
41
+ items: list[dict[str, Any]] | None # None when the list could not be read
42
+ findings: list[Finding]
43
+
44
+
45
+ @dataclass
46
+ class Context:
47
+ api: str
48
+ spec: Spec
49
+ client: Client
50
+ run_id: str
51
+ allow_writes: bool = False
52
+ allow_orders: bool = False
53
+ listings: dict[str, Listing] = field(default_factory=dict)
54
+ created: list[tuple[Resource, str]] = field(default_factory=list)
55
+ # (collection, id) -> top-level attributes this run sent it, by POST or PATCH
56
+ sent: dict[tuple[str, str], set[str]] = field(default_factory=dict)
57
+ fixtures: dict[str, dict[str, Any]] = field(default_factory=dict) # class -> resource
58
+ preferred: dict[str, Any] = field(default_factory=dict) # property name -> value
59
+ creations: dict[str, Any] = field(default_factory=dict) # collection -> create.Creation
60
+ building: set[str] = field(default_factory=set) # fixtures being created (cycle guard)
61
+ # (collection, attribute path) a write check already reported: missing skips them
62
+ explained: set[tuple[str, str]] = field(default_factory=set)
63
+
64
+ def record_sent(self, collection: str, resource_id: str, body: Any) -> None:
65
+ if isinstance(body, dict):
66
+ names = set(body)
67
+ elif isinstance(body, list): # JSON Patch: /attr/...
68
+ names = {str(op.get("path", "")).split("/")[1] for op in body if isinstance(op, dict)}
69
+ names.discard("")
70
+ else:
71
+ return
72
+ self.sent.setdefault((collection, resource_id), set()).update(names)
73
+
74
+ def finding(
75
+ self,
76
+ method: str,
77
+ path: str,
78
+ kind: str,
79
+ detail: str,
80
+ response: Response | None = None,
81
+ **extra: Any,
82
+ ) -> Finding:
83
+ evidence = response.evidence() if response is not None else {}
84
+ evidence.update(extra)
85
+ if kind in ("unprobed", "refused", "dropped", "server-error", "read-only"):
86
+ # keyed by collection: a PATCH finding is on the item path
87
+ self.explained.add((path.split("/{", 1)[0], detail.split()[0]))
88
+ return Finding(self.api, method, path, kind, detail, evidence or None)
89
+
90
+ def schema_findings(
91
+ self, method: str, path: str, schema: str, body: Any, response: Response, **kw: Any
92
+ ) -> Iterator[Finding]:
93
+ """One `invalid` (or `null-value`) finding per (attribute path, keyword)."""
94
+ for issue in issues(self.spec, schema, body, **kw):
95
+ if issue.keyword == "null":
96
+ yield self.finding(method, path, "null-value", issue.path, response)
97
+ else:
98
+ yield self.finding(
99
+ method,
100
+ path,
101
+ "invalid",
102
+ f"{issue.path} {issue.keyword}",
103
+ response,
104
+ message=issue.message,
105
+ )
106
+
107
+ def listing(self, resource: Resource) -> Listing:
108
+ """The collection GET, once per run; `findings` says why `items` may be None."""
109
+ if resource.collection in self.listings:
110
+ return self.listings[resource.collection]
111
+ response = self.client.get(resource.collection)
112
+ check_auth(response)
113
+ body = response.json()
114
+ findings: list[Finding] = []
115
+ items = None
116
+ path = resource.collection
117
+ if response.status in NOT_IMPLEMENTED:
118
+ findings.append(
119
+ self.finding("GET", path, "unimplemented", str(response.status), response)
120
+ )
121
+ elif not response.ok:
122
+ findings.append(self.finding("GET", path, "error-status", "list", response))
123
+ elif not isinstance(body, list):
124
+ findings.append(self.finding("GET", path, "list-body", "not-an-array", response))
125
+ else:
126
+ items = [item for item in body if isinstance(item, dict)]
127
+ listing = Listing(response, items, findings)
128
+ self.listings[resource.collection] = listing
129
+ return listing
130
+
131
+
132
+ def check_auth(response: Response) -> None:
133
+ """401 stops the run; 403 stops this check on this resource (see `Forbidden`)."""
134
+ if response.status == 403:
135
+ raise Forbidden(response)
136
+ if response.status == 401:
137
+ raise Unauthorized(
138
+ f"{response.method} {response.url} answered 401: pass credentials with --header"
139
+ )
140
+
141
+
142
+ def item_path(resource: Resource, resource_id: str) -> str:
143
+ """The item URL path for an id (the spec's `{id}` placeholder filled in)."""
144
+ assert resource.item is not None
145
+ prefix = resource.item.split("{", 1)[0]
146
+ return prefix + quote(str(resource_id), safe="")