Skip to content

Commit 8748df9

Browse files
pUrGe12dependabot[bot]arkid15r
authored
Add API core tests (#1080)
* added tests for api/core * ruff * ruff fixes * migrate to pytest * Bump requests from 2.32.3 to 2.32.4 (#1082) Bumps [requests](https://github.com/psf/requests) from 2.32.3 to 2.32.4. - [Release notes](https://github.com/psf/requests/releases) - [Changelog](https://github.com/psf/requests/blob/main/HISTORY.md) - [Commits](psf/requests@v2.32.3...v2.32.4) --- updated-dependencies: - dependency-name: requests dependency-version: 2.32.4 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update code --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Arkadii Yakovets <arkadii.yakovets@owasp.org> Co-authored-by: Arkadii Yakovets <2201626+arkid15r@users.noreply.github.com>
1 parent 6244176 commit 8748df9

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

tests/api/test_core.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from pathlib import Path
2+
from unittest.mock import patch, MagicMock, mock_open
3+
4+
import pytest
5+
from flask import Flask, Request
6+
from werkzeug.exceptions import NotFound
7+
8+
from nettacker.api.core import (
9+
api_key_is_valid,
10+
get_file,
11+
get_value,
12+
graphs,
13+
languages_to_country,
14+
mime_types,
15+
profiles,
16+
scan_methods,
17+
)
18+
from nettacker.config import Config
19+
20+
21+
@pytest.fixture
22+
def app():
23+
app = Flask(__name__)
24+
app.config["OWASP_NETTACKER_CONFIG"] = {"api_access_key": "test_key"}
25+
return app
26+
27+
28+
@pytest.fixture
29+
def request_():
30+
req = MagicMock(spec=Request)
31+
req.args = {"key": "test_key"}
32+
req.form = {}
33+
req.cookies = {}
34+
return req
35+
36+
37+
def test_get_value(request_):
38+
assert get_value(request_, "key") == "test_key"
39+
assert get_value(request_, "nonexistent") == ""
40+
41+
42+
def test_mime_types():
43+
mtypes = mime_types()
44+
assert ".html" in mtypes
45+
assert mtypes[".html"] == "text/html"
46+
47+
48+
@patch("builtins.open", new_callable=mock_open, read_data="test_data")
49+
def test_get_file_valid(mock_open):
50+
Config.path.web_static_dir = Path.cwd()
51+
filename = Config.path.web_static_dir / "test.txt"
52+
assert get_file(filename) == "test_data"
53+
54+
55+
@patch("builtins.open", side_effect=IOError)
56+
def test_get_file_ioerror(mock_open):
57+
Config.path.web_static_dir = Path.cwd()
58+
filename = Config.path.web_static_dir / "test.txt"
59+
with pytest.raises(NotFound):
60+
get_file(filename)
61+
62+
63+
@patch("builtins.open", side_effect=ValueError)
64+
def test_get_file_valueerror(mock_open):
65+
Config.path.web_static_dir = Path.cwd()
66+
filename = Config.path.web_static_dir / "test.txt"
67+
with pytest.raises(NotFound):
68+
get_file(filename)
69+
70+
71+
def test_get_file_outside_web_static_dir():
72+
Config.path.web_static_dir = Path("/safe/dir").resolve()
73+
filename = Path("/unauthorized/access.txt").resolve()
74+
with pytest.raises(NotFound):
75+
get_file(filename)
76+
77+
78+
def test_api_key_is_valid(app, request_):
79+
with app.test_request_context():
80+
api_key_is_valid(app, request_) # Should not raise
81+
82+
83+
def test_api_key_invalid(app, request_):
84+
request_.args = {"key": "wrong_key"}
85+
with pytest.raises(Exception):
86+
api_key_is_valid(app, request_)
87+
88+
89+
@patch("nettacker.core.app.Nettacker.load_graphs", return_value=["graph1", "graph2"])
90+
def test_graphs(mock_graphs):
91+
result = graphs()
92+
assert '<input id="graph1"' in result
93+
assert '<a class="label label-default">graph2</a>' in result
94+
assert 'value="graph1"' in result
95+
assert 'name="graph_name"' in result
96+
97+
98+
@patch("nettacker.core.app.Nettacker.load_graphs", return_value=[])
99+
def test_graphs_empty(mock_graphs):
100+
result = graphs()
101+
assert "None</a>" in result
102+
103+
104+
@patch(
105+
"nettacker.core.app.Nettacker.load_profiles",
106+
return_value={"scan": {}, "brute": {}, "custom": {}},
107+
)
108+
def test_profiles(mock_profiles):
109+
result = profiles()
110+
assert "checkbox-scan" in result
111+
assert 'label-success">scan</a>' in result
112+
assert 'label-warning">brute</a>' in result
113+
assert 'label-default">custom</a>' in result
114+
115+
116+
@patch(
117+
"nettacker.core.app.Nettacker.load_modules",
118+
return_value={"ssh_brute": {}, "http_vuln": {}, "tcp_scan": {}, "all": {}},
119+
)
120+
def test_scan_methods(mock_methods):
121+
result = scan_methods()
122+
assert "checkbox-scan-module" in result
123+
assert 'label-success">tcp_scan</a>' in result
124+
assert "checkbox-brute-module" in result
125+
assert 'label-warning">ssh_brute</a>' in result
126+
assert "checkbox-vuln-module" in result
127+
assert 'label-danger">http_vuln</a>' in result
128+
assert "all" not in result
129+
130+
131+
@patch("nettacker.core.messages.get_languages", return_value=["en", "fr", "es", "de"])
132+
def test_languages_to_country(mock_langs):
133+
result = languages_to_country()
134+
assert "flag-icon-us" in result
135+
assert "flag-icon-fr" in result
136+
assert '<option selected id="en"' in result
137+
assert "flag-icon-es" in result

0 commit comments

Comments
 (0)