Skip to content

Commit 7f4283c

Browse files
authored
Add tests for datasources (#32)
Fix mindersec/minder-rules-and-profiles#303
1 parent 5b5f219 commit 7f4283c

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

minder-tests/api-datasources.robot

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
*** Settings ***
2+
Documentation Test suite for the Minder data sources REST API
3+
4+
Resource resources/keywords.robot
5+
Library resources.datasources.DataSources
6+
7+
Suite Setup Load Config
8+
9+
Test Setup Default Setup
10+
Test Teardown Default Teardown
11+
12+
*** Keywords ***
13+
Default Setup
14+
Set Project as Environment Variable with Test Name
15+
16+
Default Teardown
17+
Remove Project Environment Variable for Test
18+
19+
*** Test Cases ***
20+
Test the List Data Sources API
21+
[Documentation] Test that we can list data sources
22+
23+
${data_sources}= When Client lists data sources
24+
Then data sources are empty ${data_sources}
25+
26+
[Teardown] Run Keywords Default Teardown
27+
28+
Test the Create Data Source API
29+
[Documentation] Test that we can create a data source
30+
31+
Given Client adds a data source test-data-source
32+
${data_sources}= When Client lists data sources
33+
Then data sources are not empty ${data_sources}
34+
35+
[Teardown] Run Keywords Cleanup Minder Data Sources
36+
... AND Default Teardown
37+
38+
Test the Update Data Source API
39+
[Documentation] Test that we can modify an existing data source
40+
41+
Given Client adds a data source test-data-source
42+
Given Client updates a data source test-data-source
43+
${data_sources}= When Client lists data sources
44+
Then data sources are not empty ${data_sources}
45+
46+
[Teardown] Run Keywords Cleanup Minder Data Sources
47+
... AND Default Teardown

resources/datasources.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import json
2+
import os
3+
4+
from robot.api import logger
5+
from robot.api.deco import keyword
6+
7+
from resources.errors import ConfigurationError
8+
from resources.minder_restapi_lib import MinderRestApiLib
9+
10+
11+
class DataSources:
12+
def __init__(self):
13+
self.rest_api = MinderRestApiLib()
14+
self.datasources = dict()
15+
16+
@keyword
17+
def client_lists_data_sources(self):
18+
"""Lists available data sources."""
19+
project = os.getenv("MINDER_PROJECT")
20+
if not project:
21+
raise ConfigurationError("MINDER_PROJECT environment variable is not set")
22+
23+
params = {
24+
"context.project_id": project,
25+
}
26+
27+
return self.rest_api.get_request("/data_sources", params=params)
28+
29+
@keyword
30+
def client_adds_a_data_source(self, data_source_name):
31+
project = os.getenv("MINDER_PROJECT")
32+
if not project:
33+
raise ConfigurationError("MINDER_PROJECT environment variable is not set")
34+
35+
data_source = {
36+
"version": "v1",
37+
"type": "data-source",
38+
"name": data_source_name,
39+
"context": {
40+
"project_id": project,
41+
},
42+
"rest": {
43+
"def": {
44+
"license": {
45+
"endpoint": "https://raw.githubusercontent.com/spdx/license-list-data/refs/heads/main/json/licenses.json",
46+
"parse": "json",
47+
"input_schema": {},
48+
}
49+
}
50+
}
51+
}
52+
53+
try:
54+
resp = self.rest_api.post_request(
55+
"/data_source", data=json.dumps({"dataSource": data_source})
56+
)
57+
self.datasources[resp["dataSource"]["name"]] = resp["dataSource"]
58+
except Exception as e:
59+
logger.error(f"Failed to create data source: {str(e)}")
60+
raise
61+
62+
@keyword
63+
def client_updates_a_data_source(self, data_source_name):
64+
project = os.getenv("MINDER_PROJECT")
65+
if not project:
66+
raise ConfigurationError("MINDER_PROJECT environment variable is not set")
67+
68+
data_source = self.datasources[data_source_name]
69+
if not data_source:
70+
raise ConfigurationError(f"No data sources created for {data_source_name}")
71+
72+
data_source["rest"]["def"]["license"]["endpoint"] = "https://github.com"
73+
logger.info(f"updating datasource {data_source}")
74+
75+
try:
76+
resp = self.rest_api.put_request(
77+
"/data_source",
78+
data=json.dumps({"dataSource": data_source}),
79+
)
80+
self.datasources[resp["dataSource"]["name"]] = resp["dataSource"]
81+
except Exception as e:
82+
logger.error(f"Failed to update data source: {str(e)}")
83+
raise
84+
85+
@keyword
86+
def delete_data_source(self, data_source_id):
87+
"""Delete a Minder data source by ID."""
88+
project = os.getenv("MINDER_PROJECT")
89+
if not project:
90+
raise ConfigurationError("MINDER_PROJECT environment variable is not set")
91+
92+
params = {
93+
"context.project_id": project,
94+
}
95+
96+
try:
97+
logger.info(
98+
self.rest_api.delete_request(f"/data_source/{data_source_id}", params=params)
99+
)
100+
except Exception as e:
101+
logger.error(f"Failed to delete data source: {str(e)}")
102+
raise
103+
104+
@keyword
105+
def cleanup_minder_data_sources(self):
106+
"""Deletes all created data sources."""
107+
for datasource in self.datasources.values():
108+
logger.info(f"cleaning up datasource {datasource}")
109+
self.delete_data_source(datasource["id"])
110+
111+
@keyword
112+
def data_sources_are_empty(self, data_sources):
113+
if len(data_sources["dataSources"]):
114+
raise ValueError("Results should be empty")
115+
116+
@keyword
117+
def data_sources_are_not_empty(self, data_sources):
118+
if not len(data_sources["dataSources"]):
119+
raise ValueError("Results should not be empty")

resources/minder_restapi_lib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def post_request(self, path, **kwargs):
7373
def patch_request(self, path, **kwargs):
7474
return self._make_request("PATCH", path, **kwargs)
7575

76+
@keyword
77+
def put_request(self, path, **kwargs):
78+
return self._make_request("PUT", path, **kwargs)
79+
7680
@keyword
7781
def delete_request(self, path, **kwargs):
7882
return self._make_request("DELETE", path, **kwargs)

0 commit comments

Comments
 (0)