Skip to content

Commit 5da1c3f

Browse files
committed
Add tests
Tests for: * manage.py import command * manage.py improve command * import_runner Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent c1c460d commit 5da1c3f

4 files changed

Lines changed: 188 additions & 269 deletions

File tree

vulnerabilities/tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def no_rmtree(monkeypatch):
6969
"test_github.py",
7070
"test_suse_backports.py",
7171
"test_suse_scores.py",
72-
"test_import_cmd.py",
7372
"test_ubuntu.py",
74-
"test_import_runner.py",
7573
"test_ubuntu_usn.py",
7674
"test_importer_yielder.py",
7775
"test_upstream.py",
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright (c) nexB Inc. and others. All rights reserved.
2+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
3+
# The VulnerableCode software is licensed under the Apache License version 2.0.
4+
# Data generated with VulnerableCode require an acknowledgment.
5+
#
6+
# You may not use this software except in compliance with the License.
7+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software distributed
9+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
# specific language governing permissions and limitations under the License.
12+
#
13+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
14+
# derivative work, you must accompany this data with the following acknowledgment:
15+
#
16+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
17+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
18+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
19+
# for any legal advice.
20+
# VulnerableCode is a free software tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
22+
23+
from unittest.mock import patch
24+
from io import StringIO
25+
26+
from django.core.management import call_command
27+
from django.core.management.base import CommandError
28+
from django.test import TestCase
29+
import pytest
30+
31+
from vulnerabilities.importer import Importer
32+
from vulnerabilities.importer import AdvisoryData
33+
34+
35+
class DummyImporter(Importer):
36+
spdx_license_expression = "dummy license"
37+
38+
def advisory_data(self):
39+
return []
40+
41+
42+
class UnLicensedImporter(Importer):
43+
def advisory_data(self):
44+
return []
45+
46+
47+
MOCK_IMPORTERS_REGISTRY = [DummyImporter, UnLicensedImporter]
48+
MOCK_IMPORTERS_REGISTRY = {
49+
importer.qualified_name: importer for importer in MOCK_IMPORTERS_REGISTRY
50+
}
51+
52+
53+
@patch("vulnerabilities.importers.IMPORTERS_REGISTRY", MOCK_IMPORTERS_REGISTRY)
54+
class TestImportCommand(TestCase):
55+
def test_list_sources(self):
56+
buf = StringIO()
57+
call_command("import", "--list", stdout=buf)
58+
out = buf.getvalue()
59+
assert DummyImporter.qualified_name in out
60+
61+
def test_missing_sources(self):
62+
with pytest.raises(CommandError) as cm:
63+
call_command("import", stdout=StringIO())
64+
65+
err = str(cm)
66+
assert 'Please provide at least one importer to run or use "--all"' in err
67+
68+
def test_error_message_includes_unknown_sources(self):
69+
with pytest.raises(CommandError) as cm:
70+
call_command(
71+
"import",
72+
DummyImporter.qualified_name,
73+
"foo",
74+
"bar",
75+
stdout=StringIO(),
76+
)
77+
78+
err = str(cm)
79+
assert "bar" in err
80+
assert "foo" in err
81+
assert DummyImporter.qualified_name not in err
82+
83+
def test_import_run(self):
84+
buf = StringIO()
85+
call_command("import", DummyImporter.qualified_name, stdout=buf)
86+
out = buf.getvalue()
87+
assert "Successfully imported data using" in out
88+
89+
def test_bad_importer_fail_error(self):
90+
buf = StringIO()
91+
with pytest.raises(CommandError):
92+
call_command("import", UnLicensedImporter.qualified_name, stdout=buf)
93+
out = buf.getvalue()
94+
assert "Failed to run importer" in out

0 commit comments

Comments
 (0)