Skip to content

Commit b27a273

Browse files
committed
Testing bare and json commands.
1 parent b6116af commit b27a273

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

tests/reqs_4.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
django==1.11

tests/test_safety.py

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,42 @@ def test_review_fail(self):
5454
result = runner.invoke(cli.cli, ['review', '--bare', '--file', path_to_report])
5555
assert result.exit_code == -1
5656

57+
@patch("safety.safety.get_licenses")
58+
def test_license_bare(self, get_licenses):
59+
runner = CliRunner()
60+
61+
dirname = os.path.dirname(__file__)
62+
with open(os.path.join(dirname, "test_db", "licenses.json")) as f:
63+
licenses_db = json.loads(f.read())
64+
get_licenses.return_value = licenses_db
65+
reqs_path = os.path.join(dirname, "reqs_4.txt")
66+
67+
result = runner.invoke(cli.cli, ['license', '--file', reqs_path, '--bare', '--db', 'licenses.json'])
68+
self.assertEqual(result.exit_code, 0)
69+
self.assertEqual(result.output, 'BSD-3-Clause\n')
70+
71+
@patch("safety.safety.get_licenses")
72+
def test_license_json(self, get_licenses):
73+
runner = CliRunner()
74+
75+
dirname = os.path.dirname(__file__)
76+
with open(os.path.join(dirname, "test_db", "licenses.json")) as f:
77+
licenses_db = json.loads(f.read())
78+
get_licenses.return_value = licenses_db
79+
reqs_path = os.path.join(dirname, "reqs_4.txt")
80+
81+
result = runner.invoke(cli.cli, ['license', '--file', reqs_path, '--json', '--db', 'licenses.json'])
82+
expected_result = json.dumps(
83+
[{
84+
"license": "BSD-3-Clause",
85+
"package": "django",
86+
"version": "1.11"
87+
}],
88+
indent=4, sort_keys=True
89+
)
90+
self.assertEqual(result.exit_code, 0)
91+
self.assertMultiLineEqual(result.output.rstrip(), expected_result)
92+
5793

5894
class TestFormatter(unittest.TestCase):
5995

@@ -269,7 +305,7 @@ def test_get_packages_licenses_without_api_key(self):
269305
key=None
270306
)
271307
db_generic_exception = error.exception
272-
self.assertEqual(str(db_generic_exception), 'API-KEY not provided.')
308+
self.assertEqual(str(db_generic_exception), 'The API-KEY was not provided.')
273309

274310
@patch("safety.safety.requests")
275311
def test_get_packages_licenses_with_invalid_api_key(self, requests):
@@ -387,6 +423,73 @@ def test_get_cached_packages_licenses(self, requests):
387423
self.assertNotEqual(resp, licenses_db)
388424
self.assertEqual(resp, original_db)
389425

426+
def test_report_licenses_bare(self):
427+
from safety.formatter import license_report
428+
429+
reqs = StringIO("Django==1.8.1\n\rinexistent==1.0.0")
430+
packages = util.read_requirements(reqs)
431+
432+
# Using DB: test.test_db.licenses.json
433+
licenses_db = safety.get_licenses(
434+
db_mirror=os.path.join(
435+
os.path.dirname(os.path.realpath(__file__)),
436+
"test_db"
437+
),
438+
cached=False,
439+
key=None,
440+
proxy={},
441+
)
442+
443+
pkgs_licenses = util.get_packages_licenses(packages, licenses_db)
444+
output_report = license_report(
445+
packages=packages,
446+
licenses=pkgs_licenses,
447+
json_report=False,
448+
bare_report=True
449+
)
450+
self.assertEqual(output_report, "BSD-3-Clause")
451+
452+
def test_report_licenses_json(self):
453+
from safety.formatter import license_report
454+
455+
reqs = StringIO("Django==1.8.1\n\rinexistent==1.0.0")
456+
packages = util.read_requirements(reqs)
457+
458+
# Using DB: test.test_db.licenses.json
459+
licenses_db = safety.get_licenses(
460+
db_mirror=os.path.join(
461+
os.path.dirname(os.path.realpath(__file__)),
462+
"test_db"
463+
),
464+
cached=False,
465+
key=None,
466+
proxy={},
467+
)
468+
469+
pkgs_licenses = util.get_packages_licenses(packages, licenses_db)
470+
output_report = license_report(
471+
packages=packages,
472+
licenses=pkgs_licenses,
473+
json_report=True,
474+
bare_report=False
475+
)
476+
477+
expected_result = json.dumps(
478+
[{
479+
"license": "BSD-3-Clause",
480+
"package": "django",
481+
"version": "1.8.1"
482+
},
483+
{
484+
"license": "N/A",
485+
"package": "inexistent",
486+
"version": "1.0.0"
487+
}],
488+
indent=4, sort_keys=True
489+
)
490+
# Packages without license are reported as "N/A"
491+
self.assertEqual(output_report.rstrip(), expected_result)
492+
390493

391494
class ReadRequirementsTestCase(unittest.TestCase):
392495

0 commit comments

Comments
 (0)