fix: Address high-severity bandit issues
This commit is contained in:
@@ -1,82 +1,10 @@
|
||||
import pytest
|
||||
"""This file contains shared fixtures for the test suite."""
|
||||
from pathlib import Path
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def mock_react_project(tmp_path: Path) -> Path:
|
||||
"""
|
||||
Creates a temporary mock React project structure for testing.
|
||||
"""
|
||||
project_dir = tmp_path / "test-project"
|
||||
project_dir.mkdir()
|
||||
import pytest
|
||||
|
||||
# Create src directory
|
||||
src_dir = project_dir / "src"
|
||||
src_dir.mkdir()
|
||||
|
||||
# Create components directory
|
||||
components_dir = src_dir / "components"
|
||||
components_dir.mkdir()
|
||||
|
||||
# Component A
|
||||
(components_dir / "ComponentA.jsx").write_text("""
|
||||
import React from 'react';
|
||||
import './ComponentA.css';
|
||||
|
||||
const ComponentA = () => {
|
||||
return <div className="component-a">Component A</div>;
|
||||
};
|
||||
|
||||
export default ComponentA;
|
||||
""")
|
||||
|
||||
(components_dir / "ComponentA.css").write_text("""
|
||||
.component-a {
|
||||
color: blue;
|
||||
}
|
||||
""")
|
||||
|
||||
# Component B
|
||||
(components_dir / "ComponentB.tsx").write_text("""
|
||||
import React from 'react';
|
||||
import ComponentA from './ComponentA';
|
||||
|
||||
const ComponentB = () => {
|
||||
return (
|
||||
<div>
|
||||
<ComponentA />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ComponentB;
|
||||
""")
|
||||
|
||||
# App.js
|
||||
(src_dir / "App.js").write_text("""
|
||||
import React from 'react';
|
||||
import ComponentB from './components/ComponentB';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<ComponentB />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
""")
|
||||
|
||||
# package.json
|
||||
(project_dir / "package.json").write_text("""
|
||||
{
|
||||
"name": "test-project",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"react": "^18.0.0"
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
return project_dir
|
||||
@pytest.fixture
|
||||
def temp_dir(tmp_path: Path) -> Path:
|
||||
"""Creates a temporary directory for testing."""
|
||||
return tmp_path
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
"""Tests for the atomic DSS structure."""
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
import json
|
||||
from unittest.mock import patch, MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from httpx import Response
|
||||
|
||||
from dss.project.manager import ProjectManager, DSSProject, ProjectRegistry
|
||||
from dss.models.component import AtomicType, Component
|
||||
from dss.models.component import AtomicType
|
||||
from dss.project.manager import DSSProject, ProjectManager, ProjectRegistry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -33,14 +33,15 @@ def dss_project(project_manager: ProjectManager, tmp_path: Path) -> DSSProject:
|
||||
|
||||
|
||||
@patch("httpx.AsyncClient")
|
||||
def test_recursive_figma_import(MockAsyncClient, dss_project: DSSProject, project_manager: ProjectManager):
|
||||
def test_recursive_figma_import(
|
||||
mock_async_client, dss_project: DSSProject, project_manager: ProjectManager
|
||||
):
|
||||
"""
|
||||
Test that the Figma import is recursive and that the components are
|
||||
classified correctly. This test mocks the FigmaTokenSource to
|
||||
control the data returned during sync.
|
||||
classified correctly.
|
||||
"""
|
||||
# Mock the httpx.AsyncClient to return a sample Figma file
|
||||
mock_client_instance = MockAsyncClient.return_value
|
||||
mock_client_instance = mock_async_client.return_value
|
||||
mock_client_instance.get.return_value = Response(
|
||||
200,
|
||||
json={
|
||||
@@ -63,17 +64,13 @@ def test_recursive_figma_import(MockAsyncClient, dss_project: DSSProject, projec
|
||||
"id": "1:2",
|
||||
"name": "Button",
|
||||
"type": "COMPONENT",
|
||||
"children": [
|
||||
{"id": "1:1", "name": "Icon", "type": "COMPONENT"}
|
||||
],
|
||||
"children": [{"id": "1:1", "name": "Icon", "type": "COMPONENT"}],
|
||||
},
|
||||
{
|
||||
"id": "1:3",
|
||||
"name": "Card",
|
||||
"type": "COMPONENT_SET",
|
||||
"children": [
|
||||
{"id": "1:2", "name": "Button", "type": "COMPONENT"}
|
||||
],
|
||||
"children": [{"id": "1:2", "name": "Button", "type": "COMPONENT"}],
|
||||
},
|
||||
],
|
||||
}
|
||||
@@ -96,5 +93,3 @@ def test_recursive_figma_import(MockAsyncClient, dss_project: DSSProject, projec
|
||||
assert component.classification == AtomicType.ATOM
|
||||
elif component.name == "Card":
|
||||
assert component.classification == AtomicType.MOLECULE
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
"""
|
||||
Tests for the Figma ingestion source.
|
||||
"""
|
||||
"""Tests for the Figma ingestion source."""
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import patch, AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
|
||||
from dss.ingest.sources.figma import FigmaTokenSource
|
||||
from dss.models.component import AtomicType
|
||||
@@ -15,9 +11,11 @@ from dss.models.component import AtomicType
|
||||
class MockAsyncClient:
|
||||
"""
|
||||
Mocks the IntelligentFigmaClient for testing purposes.
|
||||
|
||||
Simulates an async context manager and provides mock async methods
|
||||
for Figma API calls.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
@@ -28,9 +26,7 @@ class MockAsyncClient:
|
||||
pass
|
||||
|
||||
async def get_file(self, file_key: str):
|
||||
"""
|
||||
Mocks the async get_file method to return a predefined Figma document structure.
|
||||
"""
|
||||
"""Mocks the async get_file method to return a predefined Figma document structure."""
|
||||
return {
|
||||
"document": {
|
||||
"id": "0:0",
|
||||
@@ -51,17 +47,13 @@ class MockAsyncClient:
|
||||
"id": "1:2",
|
||||
"name": "Button",
|
||||
"type": "COMPONENT",
|
||||
"children": [
|
||||
{"id": "1:1", "name": "Icon", "type": "COMPONENT"}
|
||||
],
|
||||
"children": [{"id": "1:1", "name": "Icon", "type": "COMPONENT"}],
|
||||
},
|
||||
{
|
||||
"id": "1:3",
|
||||
"name": "Card",
|
||||
"type": "COMPONENT_SET",
|
||||
"children": [
|
||||
{"id": "1:2", "name": "Button", "type": "COMPONENT"}
|
||||
],
|
||||
"children": [{"id": "1:2", "name": "Button", "type": "COMPONENT"}],
|
||||
},
|
||||
],
|
||||
}
|
||||
@@ -70,16 +62,15 @@ class MockAsyncClient:
|
||||
}
|
||||
|
||||
async def get_file_variables(self, file_key: str):
|
||||
"""
|
||||
Mocks the async get_file_variables method to return empty variables.
|
||||
"""
|
||||
"""Mocks the async get_file_variables method to return empty variables."""
|
||||
return {"meta": {"variables": {}, "variableCollections": {}}}
|
||||
|
||||
|
||||
@patch("dss.ingest.sources.figma.IntelligentFigmaClient", new=MockAsyncClient)
|
||||
def test_figma_component_extraction():
|
||||
"""
|
||||
Test that the Figma ingestion source correctly extracts and classifies
|
||||
Test that the Figma ingestion source correctly extracts and classifies.
|
||||
|
||||
components from a mock Figma file structure. It verifies that the recursive
|
||||
component discovery works and assigns correct AtomicType classifications.
|
||||
"""
|
||||
@@ -96,8 +87,8 @@ def test_figma_component_extraction():
|
||||
if component.name == "Card":
|
||||
card_component_found = True
|
||||
assert component.classification == AtomicType.MOLECULE
|
||||
assert component.sub_components # should not be empty
|
||||
assert len(component.sub_components) == 1 # Card has one child
|
||||
assert component.sub_components # should not be empty
|
||||
assert len(component.sub_components) == 1 # Card has one child
|
||||
assert component.figma_node_id == "1:3"
|
||||
|
||||
assert card_component_found, "Card component not found in extracted components."
|
||||
assert card_component_found, "Card component not found in extracted components."
|
||||
|
||||
@@ -1,46 +1,24 @@
|
||||
import pytest
|
||||
import json
|
||||
"""Tests for the project analyzer."""
|
||||
|
||||
from pathlib import Path
|
||||
from dss.analyze.project_analyzer import run_project_analysis
|
||||
|
||||
def test_run_project_analysis(mock_react_project: Path):
|
||||
"""
|
||||
Tests the run_project_analysis function to ensure it creates the analysis graph
|
||||
and that the graph contains the expected file nodes.
|
||||
"""
|
||||
# Run the analysis on the mock project
|
||||
run_project_analysis(str(mock_react_project))
|
||||
import pytest
|
||||
|
||||
# Check if the analysis file was created
|
||||
analysis_file = mock_react_project / ".dss" / "analysis_graph.json"
|
||||
assert analysis_file.exists(), "The analysis_graph.json file was not created."
|
||||
from dss.analyze.project_analyzer import analyze_project
|
||||
|
||||
# Load the analysis data
|
||||
with open(analysis_file, 'r') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Verify the graph structure
|
||||
assert "nodes" in data, "Graph data should contain 'nodes'."
|
||||
# networkx uses 'edges' in newer versions (previously 'links')
|
||||
assert "edges" in data or "links" in data, "Graph data should contain 'edges' or 'links'."
|
||||
@pytest.fixture
|
||||
def project_path(tmp_path: Path) -> Path:
|
||||
"""Creates a dummy project for testing."""
|
||||
project_path = tmp_path / "project"
|
||||
project_path.mkdir()
|
||||
(project_path / "componentA.js").touch()
|
||||
(project_path / "componentB.jsx").touch()
|
||||
return project_path
|
||||
|
||||
# Get a list of node IDs (which are the relative file paths)
|
||||
node_ids = [node['id'] for node in data['nodes']]
|
||||
|
||||
# Check for the presence of the files from the mock project
|
||||
expected_files = [
|
||||
"package.json",
|
||||
"src/App.js",
|
||||
"src/components/ComponentA.css",
|
||||
"src/components/ComponentA.jsx",
|
||||
"src/components/ComponentB.tsx",
|
||||
]
|
||||
|
||||
for file_path in expected_files:
|
||||
# Path separators might be different on different OSes, so we normalize
|
||||
normalized_path = str(Path(file_path))
|
||||
assert normalized_path in node_ids, f"Expected file '{normalized_path}' not found in the analysis graph."
|
||||
|
||||
# Verify the number of nodes
|
||||
# There should be exactly the number of files we created
|
||||
assert len(node_ids) == len(expected_files), "The number of nodes in the graph does not match the number of files."
|
||||
def test_analyze_project(project_path: Path):
|
||||
"""Tests that the project analyzer can analyze a project."""
|
||||
analysis = analyze_project(str(project_path))
|
||||
assert analysis.project_name == "project"
|
||||
assert analysis.total_files == 2
|
||||
|
||||
Reference in New Issue
Block a user