Skip to content

Commit

Permalink
Merge pull request #16 from zkarpinski/deepsource-transform-94bd8161
Browse files Browse the repository at this point in the history
style: format code with Black
  • Loading branch information
zkarpinski authored Jan 16, 2024
2 parents ab5c48f + ecdf270 commit 5993f4a
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 67 deletions.
17 changes: 11 additions & 6 deletions codeinsight_sdk/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@

import requests
import json

class GenericError(Exception): #pragma: no cover

class GenericError(Exception): # pragma: no cover
"""Generic error class, catch-all for most code insight API errors."""

pass

class NotYetImplementedError(Exception): #pragma: no cover

class NotYetImplementedError(Exception): # pragma: no cover
"""Error class for API features that have not yet been implemented."""

pass


class CodeInsightError(GenericError):
"""Error class for code insight API errors."""

def __init__(self, response: requests.Response):
try:
resp = response.json()
self.code = response.status_code
self.message = resp['Error: ']
self.arguments = resp['Arguments: ']
self.error = resp['Key: ']
self.message = resp["Error: "]
self.arguments = resp["Arguments: "]
self.error = resp["Key: "]
self.add_note(f"Arguments: {self.arguments}")
super().__init__("Error: %s - %s" % (self.code, self.message))

Expand Down
1 change: 1 addition & 0 deletions codeinsight_sdk/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List



class Handler(abc.ABC):
def __init__(self, client):
self.client = client
Expand Down
2 changes: 1 addition & 1 deletion codeinsight_sdk/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .inventory import InventoryHandler
from .project import ProjectHandler
from .report import ReportHandler
from .report import ReportHandler
6 changes: 3 additions & 3 deletions codeinsight_sdk/handlers/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, client):
super().__init__(client)
self.cls = Report

def get(self, id:int):
def get(self, id: int):
"""
Retrieves a report by its ID.
Expand All @@ -35,7 +35,7 @@ def get(self, id:int):
"""
path = f"reports/{id}"
resp = self.client.request("GET", url_part=path)
report_data = resp.json()['data']
report_data = resp.json()["data"]
report = self.cls.from_dict(report_data)
return report

Expand All @@ -50,6 +50,6 @@ def all(self):
path = "reports"
resp = self.client.request("GET", url_part=path)
reports = []
for report_data in resp.json()['data']:
for report_data in resp.json()["data"]:
reports.append(self.cls.from_dict(report_data))
return reports
10 changes: 7 additions & 3 deletions codeinsight_sdk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dataclasses_json import DataClassJsonMixin, dataclass_json
from typing import Any, Optional, List, Dict


@dataclass
class Project(DataClassJsonMixin):
id: int
Expand All @@ -26,6 +27,7 @@ class Vulnerability(DataClassJsonMixin):
vulnerabilityCvssV3Score: int
vulnerabilityCvssV3Severity: str


@dataclass
class ProjectInventoryItem(DataClassJsonMixin):
itemNumber: int
Expand All @@ -45,12 +47,14 @@ class ProjectInventoryItem(DataClassJsonMixin):
vulnerabilitySummary: Optional[List[Dict[str, Dict]]] = None
filePaths: Optional[List[str]] = None

@dataclass_json #Trying this style instead of DataClassJsonMixin

@dataclass_json # Trying this style instead of DataClassJsonMixin
@dataclass
class ProjectInventory():
class ProjectInventory:
projectId: int
inventoryItems: List[ProjectInventoryItem]


@dataclass
class Report(DataClassJsonMixin):
id: int
Expand All @@ -61,4 +65,4 @@ class Report(DataClassJsonMixin):
enableProjectPicker: bool
order: int
createdDateTime: str
updatedDateTime: str
updatedDateTime: str
1 change: 0 additions & 1 deletion examples/example-1-list-projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@


print(client.projects.all())

2 changes: 1 addition & 1 deletion examples/example-9-dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@


inventory = client.project_inventory.get(1)
df = pd.DataFrame(inventory.__dict__['inventoryItems'])
df = pd.DataFrame(inventory.__dict__["inventoryItems"])
5 changes: 2 additions & 3 deletions examples/shared.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

BASE_URL = 'https://api.revenera.com'
AUTH_TOKEN = 'test'
BASE_URL = "https://api.revenera.com"
AUTH_TOKEN = "test"
91 changes: 60 additions & 31 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
TEST_URL = "https://api.revenera.com"
TEST_API_TOKEN = "your_api_token"


class TestCodeInsightClient:
@pytest.fixture
def client(self):
return CodeInsightClient(TEST_URL, TEST_API_TOKEN)

def test_client(self, client):
assert client.base_url == TEST_URL

def test_client_expertimental_disabled(self, client):
assert client.experimental_enabled == False

Expand All @@ -30,44 +31,54 @@ def test_endpoint_not_found(self, client):
with pytest.raises(Exception):
client.projects.all()


class TestProjectEndpoints:
@pytest.fixture
def client(self):
return CodeInsightClient(TEST_URL, TEST_API_TOKEN)

def test_create_project(self, client):
project_name = "Test"
with requests_mock.Mocker() as m:
m.post(f"{TEST_URL}/codeinsight/api/projects", text='{"data": {"id":1}}')
project_id = client.projects.create(project_name)
assert project_id == 1

def test_get_all_projects(self, client):
with requests_mock.Mocker() as m:
m.get(f"{TEST_URL}/codeinsight/api/projects", text='{"data": [{"id":1, "name":"Test"}, {"id":2, "name":"Test 2"}]}')
m.get(
f"{TEST_URL}/codeinsight/api/projects",
text='{"data": [{"id":1, "name":"Test"}, {"id":2, "name":"Test 2"}]}',
)
projects = client.projects.all()
assert len(projects) > 0

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: ' ...
m.get(
f"{TEST_URL}/codeinsight/api/project/id", text='{ "Content: ": 1 }'
) # Yes, the key is called 'Content: ' ...
project_id = client.projects.get_id(project_name)
assert project_id == 1

def test_get_project_id_invalid(self,client):
def test_get_project_id_invalid(self, client):
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 '...: '
m.get(f"{TEST_URL}/codeinsight/api/project/id", text=fake_response_json, status_code=400)
# 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):
client.projects.get_id(project_name)
def test_get_project(self,client):

def test_get_project(self, client):
project_id = 1
fake_response_json = """ { "data": {
"id": 1,
Expand Down Expand Up @@ -96,7 +107,10 @@ def test_get_project(self,client):
}}
"""
with requests_mock.Mocker() as m:
m.get(f"{TEST_URL}/codeinsight/api/projects/{project_id}", text=fake_response_json)
m.get(
f"{TEST_URL}/codeinsight/api/projects/{project_id}",
text=fake_response_json,
)
project = client.projects.get(project_id)
assert project.id == 1
assert project.name == "Test"
Expand All @@ -105,7 +119,7 @@ def test_get_project(self,client):
assert project.vulnerabilities["CvssV2"]["High"] == 2
assert project.vulnerabilities["CvssV2"]["Unknown"] == 4

def test_get_project_inventory_multipage(self,client):
def test_get_project_inventory_multipage(self, client):
project_id = 1
total_pages = 4
total_records = total_pages * 2
Expand All @@ -121,23 +135,32 @@ def test_get_project_inventory_multipage(self,client):
]}
"""
with requests_mock.Mocker() as m:
m.get(f"{TEST_URL}/codeinsight/api/project/inventory/{project_id}",
text=fake_response_json, headers=response_header)
m.get(
f"{TEST_URL}/codeinsight/api/project/inventory/{project_id}",
text=fake_response_json,
headers=response_header,
)
project_inventory = client.projects.get_inventory(project_id)
assert project_inventory.projectId == project_id
assert len(project_inventory.inventoryItems) == total_records
assert project_inventory.inventoryItems[0].vulnerabilities[0].vulnerabilityName == "CVE-2020-1234"

def test_upload_codebase(self,client):
assert (
project_inventory.inventoryItems[0].vulnerabilities[0].vulnerabilityName
== "CVE-2020-1234"
)

def test_upload_codebase(self, client):
project_id = 1
codebase_path = "tests/resources/test_codebase.zip"
with requests_mock.Mocker() as m:
m.post(f"{TEST_URL}/codeinsight/api/project/uploadProjectCodebase", text='{"data": {"id":1}}')
m.post(
f"{TEST_URL}/codeinsight/api/project/uploadProjectCodebase",
text='{"data": {"id":1}}',
)
resp = client.projects.upload_codebase(project_id, codebase_path)
assert resp == 200

#### FIX THIS! ####
def test_get_project_inventory_summary(self,client):
def test_get_project_inventory_summary(self, client):
project_id = 1
total_pages = 4
total_records = total_pages * 2
Expand Down Expand Up @@ -174,9 +197,14 @@ def test_get_project_inventory_summary(self,client):
}
"""
with requests_mock.Mocker() as m:
m.get(f"{TEST_URL}/codeinsight/api/projects/{project_id}/inventorySummary",
text=fake_response_json, headers=response_header)
project_inventory_summary = client.projects.get_inventory_summary(project_id)
m.get(
f"{TEST_URL}/codeinsight/api/projects/{project_id}/inventorySummary",
text=fake_response_json,
headers=response_header,
)
project_inventory_summary = client.projects.get_inventory_summary(
project_id
)

assert len(project_inventory_summary) == 8
assert project_inventory_summary[1].id == 12346
Expand All @@ -186,8 +214,8 @@ class TestReportsEndpoints:
@pytest.fixture
def client(self):
return CodeInsightClient(TEST_URL, TEST_API_TOKEN)
def test_get_reports_all(self,client):

def test_get_reports_all(self, client):
fake_response_json = """ { "data": [
{
"id": 1,
Expand Down Expand Up @@ -216,14 +244,13 @@ def test_get_reports_all(self,client):
}
"""
with requests_mock.Mocker() as m:
m.get(f"{TEST_URL}/codeinsight/api/reports",
text=fake_response_json)
m.get(f"{TEST_URL}/codeinsight/api/reports", text=fake_response_json)
reports = client.reports.all()
assert len(reports) == 2
assert reports[1].id == 2
assert reports[1].enabled == False

def test_get_report(self,client):
def test_get_report(self, client):
report_id = 1
fake_response_json = """ { "data":
{
Expand All @@ -240,8 +267,10 @@ def test_get_report(self,client):
}
"""
with requests_mock.Mocker() as m:
m.get(f"{TEST_URL}/codeinsight/api/reports/{report_id}",
text=fake_response_json)
m.get(
f"{TEST_URL}/codeinsight/api/reports/{report_id}",
text=fake_response_json,
)
report = client.reports.get(1)
assert report.id == 1
assert report.enabled == True
assert report.enabled == True
28 changes: 16 additions & 12 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@

from codeinsight_sdk.models import Project, Report


class TestProject(object):
@pytest.fixture
def project(self):
return Project(id=1, name="Test")

def test_project(self, project):
assert project.id == 1
assert project.name == "Test"
assert isinstance(project, Project)



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

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
Expand Down
Loading

0 comments on commit 5993f4a

Please sign in to comment.