Skip to content

Commit

Permalink
fixes - Fix SonarCube smells and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zkarpinski committed Jan 3, 2024
1 parent bae6ea9 commit c0f87c6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions codeinsight_sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def __init__(self, response: requests.Response):
super().__init__("Error: %s - %s" % (self.code, self.message))

except KeyError:
raise Exception(f"Error parsing response: {resp}")
raise ValueError(f"Error parsing response: {resp}")
except json.decoder.JSONDecodeError:
raise Exception(f"Error decoding response: {resp}")
raise ValueError(f"Error decoding response: {resp}")
13 changes: 6 additions & 7 deletions codeinsight_sdk/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create(client, cls):
}
handler = handlers.get(k)
if handler is None:
raise Exception(f"Handler not found for class '{k}'")
raise ValueError(f"Handler not found for class '{k}'")
return handler(client, cls)

@abc.abstractmethod
Expand Down Expand Up @@ -56,7 +56,7 @@ def get(self, id:int) -> Project:
project_data = resp.json()['data']
return self.cls.from_dict(project_data)

def get_id(self, projectName:str) -> int:
def get_id(self, project_name:str) -> int:
"""
Retrieves the ID of a project based on its name.
Expand All @@ -66,15 +66,14 @@ def get_id(self, projectName:str) -> int:
Returns:
int: The ID of the project.
"""
path = f"project/id"
params = {"projectName": projectName}
path = "project/id"
params = {"projectName": project_name}
resp = self.client.request("GET", url_part=path, params=params)
try:
projectId = resp.json()['Content: '] # Yes, the key is called 'Content: ' ...
project_id = resp.json()['Content: '] # Yes, the key is called 'Content: ' ...
except KeyError:
raise CodeInsightError(resp)
#raise Exception(f"Content key not found in response: {resp.json()}")
return projectId
return project_id

def get_inventory_summary(self, project_id:int) -> List[ProjectInventoryItem]:
"""
Expand Down
13 changes: 6 additions & 7 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,24 @@ def test_get_all_projects(self, client):
projects = client.projects.all()
assert len(projects) > 0

def test_get_project_id(self,client):
projectName = "Test"
def test_get_project_id(self, client):
project_name = "Test"
with requests_mock.Mocker() as m:
m.get(f"{TEST_URL}/codeinsight/api/project/id", text='{ "Content: ": 1 }') # Yes, the key is called 'Content: ' ...
project_id = client.projects.get_id(projectName)
project_id = client.projects.get_id(project_name)
assert project_id == 1

def test_get_project_id_invalid(self,client):
projectName = "Invalid_Project"
project_name = "Invalid_Project"
fake_response_json = """{ "Arguments: " : ["",""],
"Key: ": " InvalidProjectNameParm",
"Error: ": "The project name entered was not found" }
"""
with requests_mock.Mocker() as m:
# Note, the key names end with a colon and space '...: '
# Note, the key names end with a colon and space '...: '
m.get(f"{TEST_URL}/codeinsight/api/project/id", text=fake_response_json, status_code=400)
with pytest.raises(CodeInsightError):
project_id = client.projects.get_id(projectName)
assert project_id != 1
client.projects.get_id(project_name)

def test_get_project(self,client):
project_id = 1
Expand Down
4 changes: 2 additions & 2 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from codeinsight_sdk import CodeInsightClient
from codeinsight_sdk.handlers import *
from codeinsight_sdk.models import *
from codeinsight_sdk.handlers import Handler, ProjectHandler, ReportHandler
from codeinsight_sdk.models import Project, Report

class TestHandlers(object):
@pytest.fixture
Expand Down
15 changes: 12 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ def project(self):
def test_project(self, project):
assert project.id == 1
assert project.name == "Test"
assert isinstance(project, Project)


@pytest.mark.skip(reason="Not implemented")
class TestReport(object):
@pytest.fixture
def report(self):
return Report(id=1)
return Report(id=1,
name="Test",
path="path/to/report",
default=True,
enabled=True,
enableProjectPicker=True,
order=1,
createdDateTime="Today",
updatedDateTime="Tomorrow")

def test_report(self, report):
assert report.id == 1
assert report.enabled == True
assert isinstance(report, Report)
16 changes: 8 additions & 8 deletions tests/test_not_implemented.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ def client(self):
## Coming soon features ##
def test_inventories(self, client):
with pytest.raises(NotImplementedError):
client.inventories() != None
client.inventories()

def test_vulnerabilities(self, client):
with pytest.raises(NotImplementedError):
client.vulnerabilites() != None
client.vulnerabilites()

def test_users(self, client):
with pytest.raises(NotImplementedError):
client.users() != None
client.users()

def test_licenses(self, client):
with pytest.raises(NotImplementedError):
client.licenses() != None
client.licenses()

def test_tasks(self, client):
with pytest.raises(NotImplementedError):
client.tasks() != None
client.tasks()

def test_rules(self, client):
with pytest.raises(NotImplementedError):
client.rules() != None
client.rules()

def test_files(self, client):
with pytest.raises(NotImplementedError):
client.files() != None
client.files()

def test_folders(self, client):
with pytest.raises(NotImplementedError):
client.folders() != None
client.folders()

0 comments on commit c0f87c6

Please sign in to comment.