-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Test] Test the get_discoveries cli command #166
Merged
Merged
Changes from 94 commits
Commits
Show all changes
99 commits
Select commit
Hold shift + click to select a range
b28ee54
header comment
alaabenfatma 06e1279
import libs
alaabenfatma 3dc3d19
init + config parser
alaabenfatma c096c49
print discoveries on the console.
alaabenfatma 230ab56
format
alaabenfatma 67a8283
export_csv function
alaabenfatma 1a17c01
format
alaabenfatma fc27308
max_number_of_discoveries to be shown.
alaabenfatma 1298daf
formatting
alaabenfatma 2dcf655
'run' function
alaabenfatma 9e6dabc
assign_categories to rules
alaabenfatma bc513a7
export_discoveries function + importation of libs
alaabenfatma bdc9b3a
Integrate 'get_discoveries' into the cli
alaabenfatma 7f77545
update requirements
alaabenfatma 52ba495
remove 'vscode' auto import
alaabenfatma 63dbc1b
+1 whitespace
alaabenfatma d6698eb
removed 'print' and use 'console.print' instead. + format
alaabenfatma 629d57c
pep8
alaabenfatma 0a0396f
break into two lines
alaabenfatma 7ea7a25
pep8 if condition
alaabenfatma 376d4df
rename variable
alaabenfatma 160226a
comparison with None (pep8)
alaabenfatma 636e282
pep8 client.py
alaabenfatma 4adb3c7
fixed trailing whitespace
alaabenfatma 40d2490
Merge branch 'develop' into feat/cli/get_discoveries
alaabenfatma 5c5d3c2
fixed comment
alaabenfatma 8b83e27
Merge branch 'feat/cli/get_discoveries' of github.com:SAP/credential-…
alaabenfatma f99a6b6
remove unused 'status' + progress label
alaabenfatma 898a637
fixed alphabetical order (+ old wrong order)
alaabenfatma 2026667
3rd-party library import order
alaabenfatma 1d5c3a0
remove dots from the comments
alaabenfatma 378053d
comments explaining why we delete certain columns
alaabenfatma 9f75676
added YES & NO to possible user input.
alaabenfatma 6a5b6fb
pep8 format
alaabenfatma 7b3d425
Add seperator -------
alaabenfatma 9e404f3
remove empty lines
alaabenfatma b79e0c7
add --filename optional argument.
alaabenfatma 07de7f8
enable printing discoveries filtered by filename
alaabenfatma 8644407
pass the discoveries as an argument instead.
alaabenfatma f47a3ff
pass discoveries to instead of repo_url to generate CSV string.
alaabenfatma 1b1001d
blank line
alaabenfatma f96c5b7
filter discoveries + assign categories function
alaabenfatma 24e7d47
convert discoveries to csv
alaabenfatma 904a9ea
adapt code to new refactor
alaabenfatma 6a85511
remove assign_categories & export_csv from client.py
alaabenfatma fcd4639
pep8
alaabenfatma fa1c7ca
define new argument
alaabenfatma 80dc181
filter discoveries based on state --state argument
alaabenfatma 8f81fd4
removed unused libraries
alaabenfatma 7b0ad97
docs header
alaabenfatma e79e144
correct variable name + refactor line of code
alaabenfatma a8c5d3e
Docs + formatting
alaabenfatma 0f7f79b
whitespace for better UX
alaabenfatma 5aa3b69
Exit with a code equal to that of retrieved discoveries.
alaabenfatma cc9f721
load vanilla packages
alaabenfatma 816001f
load pandas for CSV testing
alaabenfatma b0f8f5e
load credential-digger modules + parameterized
alaabenfatma 3c2f66e
define test class body
alaabenfatma 5c7b4e3
Set up sqlite database and CSV export temporary paths
alaabenfatma 694068d
init sqlite database
alaabenfatma 3966c98
typo + pep8
alaabenfatma 4a0668a
insert fake discoveries into the database
alaabenfatma 4dae763
more verbose field values
alaabenfatma 7640881
remove sqlite database & exported csv file after finishing tests
alaabenfatma 4bbeb37
body of the get_discoveries test function
alaabenfatma 87d6699
correct typo + parameterize the test_get_discoveries function to cove…
alaabenfatma c0c4a44
Test if we correctly retrieve the correct number of discoveries per s…
alaabenfatma 724a14d
test if CSV file contains non-NaN values
alaabenfatma c1ccd67
Test if generated CSV file contains NaN fields.
alaabenfatma 9c1087d
removed useless pass
alaabenfatma 5b95e95
rule_id change
alaabenfatma b0f300c
autopep8
alaabenfatma 6d790c2
Test if we retrieve the correct number of discoveries based on a file…
alaabenfatma 50f977c
Test if --filename argument works correctly.
alaabenfatma bca3394
remove unused variable
alaabenfatma 001417e
Merge 'develop' into 'test/get_discoveries_cli'
alaabenfatma b0bb1e8
Merge branch 'develop' into test/get_discoveries_cli
alaabenfatma 73d0439
uselss line removed
alaabenfatma ec157dc
removed unused --state 'all'
alaabenfatma fdaf79e
remove the 'all' state
alaabenfatma 92624b2
removed extra line
alaabenfatma 99e5564
Fixed comments
alaabenfatma f8b9a36
Postgres test
alaabenfatma d063401
Removed unused whitespace
alaabenfatma 6e24309
removed unused whitespace
alaabenfatma 874deea
rename file
alaabenfatma 6cba548
unused import
alaabenfatma f4fd226
used one line instead
alaabenfatma 84731bf
Load environment variables
alaabenfatma cee0f77
pass --dotenv argument
alaabenfatma f0b4e89
fix unbound variable exc
marcorosa 68cf12f
use git secrets env variables
marcorosa d569701
add_rules
alaabenfatma d2f6240
remove blankspaces
alaabenfatma a7827a5
correct indentation.
alaabenfatma 9e78ab9
sqlite: correct indentation
alaabenfatma ed310f1
add docstring
alaabenfatma da321d6
More expressive docstring.
alaabenfatma 6da5450
comment -> docstring
alaabenfatma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
tests/functional_tests/test_get_discoveries_postgres.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import os | ||
import random | ||
import tempfile | ||
import unittest | ||
|
||
import pandas as pd | ||
from credentialdigger.cli import cli | ||
from credentialdigger.client_postgres import PgClient | ||
from parameterized import param, parameterized | ||
|
||
REPO_URL = ''.join(random.choice('0123456789ABCDEF') for i in range(16)) | ||
|
||
|
||
class TestGetDiscoveries(unittest.TestCase): | ||
dotenv = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.env') | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
# Add rules | ||
cli.main(['', 'add_rules', '--dotenv', cls.dotenv, | ||
'tests/functional_tests/test_rules.yml']) | ||
|
||
# Set CSV temporary export path | ||
cls.tmp_path = tempfile.mkdtemp() | ||
cls.csv_path = os.path.join(cls.tmp_path, 'test.csv') | ||
|
||
# Set up Postgres client | ||
client = PgClient(dbname=os.getenv('POSTGRES_DB'), | ||
dbuser=os.getenv('POSTGRES_USER'), | ||
dbpassword=os.getenv('POSTGRES_PASSWORD'), | ||
dbhost=os.getenv('DBHOST'), | ||
dbport=os.getenv('DBPORT')) | ||
|
||
client.add_repo(REPO_URL) | ||
|
||
# Insert fake discoveries | ||
discoveries = [] | ||
discoveries_count = 5 | ||
for state in ['new', 'false_positive', 'addressing', | ||
'not_relevant', 'fixed']: | ||
for i in range(discoveries_count): | ||
discovery = { | ||
'file_name': 'danger' if state == 'new' else 'fake_file', | ||
'commit_id': '0xtmp_commit_id', | ||
'line_number': '1', | ||
'snippet': 'tmp_snippet', | ||
'rule_id': 1, | ||
'state': state, | ||
'timestamp': '2021-08-05T01:13', | ||
} | ||
discoveries.append(discovery) | ||
discoveries_count += 1 | ||
client.add_discoveries(discoveries, REPO_URL) | ||
cls.client = client | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
# Remove the repo and all its discoveries | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. convert this into a docstring |
||
cls.client.delete_repo(REPO_URL) | ||
cls.client.delete_discoveries(REPO_URL) | ||
os.remove(cls.csv_path) | ||
|
||
@parameterized.expand([ | ||
param(state='new', count=5), | ||
param(state='false_positive', count=6), | ||
param(state='addressing', count=7), | ||
param(state='not_relevant', count=8), | ||
param(state='fixed', count=9) | ||
]) | ||
def test_get_discoveries(self, state, count): | ||
""" Test if we retrieve the correct number of discoveries for every | ||
possible state value. | ||
|
||
Parameters | ||
---------- | ||
state: str | ||
The state to filter discoveries on | ||
count: int | ||
The expected number of discoveries to be returned | ||
""" | ||
with self.assertRaises(SystemExit) as cm: | ||
cli.main( | ||
[ | ||
'', | ||
'get_discoveries', | ||
REPO_URL, | ||
'--save', | ||
self.csv_path, | ||
'--state', | ||
state, | ||
'--dotenv', | ||
self.dotenv | ||
] | ||
) | ||
self.assertEqual(cm.exception.code, count) | ||
|
||
@parameterized.expand([ | ||
param(file='danger', count=5), | ||
param(file='fake_file', count=30) | ||
]) | ||
def test_get_discoveries_per_file(self, file, count): | ||
""" Test if we retrieve the correct number of discoveries based on | ||
filename input. | ||
|
||
Parameters | ||
---------- | ||
file: str | ||
The file name to filter discoveries on | ||
count: int | ||
The expected number of discoveries to be returned | ||
""" | ||
with self.assertRaises(SystemExit) as cm: | ||
cli.main( | ||
[ | ||
'', | ||
'get_discoveries', | ||
REPO_URL, | ||
'--save', | ||
self.csv_path, | ||
'--filename', | ||
file, | ||
'--dotenv', | ||
self.dotenv | ||
] | ||
) | ||
self.assertEqual(cm.exception.code, count) | ||
|
||
def test_csv_written(self): | ||
""" Test if the CLI command writes correctly the CSV file. """ | ||
with self.assertRaises(SystemExit): | ||
cli.main( | ||
[ | ||
'', | ||
'get_discoveries', | ||
REPO_URL, | ||
'--save', | ||
self.csv_path, | ||
'--dotenv', | ||
self.dotenv | ||
] | ||
) | ||
data_frame = pd.read_csv(self.csv_path) | ||
try: | ||
assert data_frame.notna().values.all() | ||
except AssertionError: | ||
assert False, 'CSV file contains NaN' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import os | ||
import tempfile | ||
import unittest | ||
|
||
import pandas as pd | ||
from credentialdigger.cli import cli | ||
from credentialdigger.client_sqlite import SqliteClient | ||
from parameterized import param, parameterized | ||
|
||
|
||
class TestGetDiscoveries(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
# Set up sqlite database and CSV export temporary paths | ||
marcorosa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cls.tmp_path = tempfile.mkdtemp() | ||
cls.db_path = os.path.join(cls.tmp_path, 'test_db.sqlite') | ||
cls.csv_path = os.path.join(cls.tmp_path, 'test.csv') | ||
|
||
# Set up sqlite database | ||
client = SqliteClient(cls.db_path) | ||
client.add_rules_from_file('tests/functional_tests/test_rules.yml') | ||
client.add_repo('test_repo') | ||
|
||
# Insert fake discoveries | ||
discoveries = [] | ||
discoveries_count = 5 | ||
for state in ['new', 'false_positive', 'addressing', | ||
'not_relevant', 'fixed']: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as in the other file |
||
for i in range(discoveries_count): | ||
discovery = { | ||
'file_name': 'danger' if state == 'new' else 'fake_file', | ||
'commit_id': '0xtmp_commit_id', | ||
'line_number': '1', | ||
'snippet': 'tmp_snippet', | ||
'rule_id': 1, | ||
'state': state, | ||
'timestamp': '2021-08-05T01:13', | ||
} | ||
discoveries.append(discovery) | ||
discoveries_count += 1 | ||
client.add_discoveries(discoveries, 'test_repo') | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
os.remove(cls.db_path) | ||
os.remove(cls.csv_path) | ||
|
||
@parameterized.expand([ | ||
param(state='new', count=5), | ||
param(state='false_positive', count=6), | ||
param(state='addressing', count=7), | ||
param(state='not_relevant', count=8), | ||
param(state='fixed', count=9) | ||
]) | ||
def test_get_discoveries(self, state, count): | ||
""" Test if we retrieve the correct number of discoveries for every | ||
possible state value. | ||
|
||
Parameters | ||
---------- | ||
state: str | ||
The state to filter discoveries on | ||
count: int | ||
The expected number of discoveries to be returned | ||
""" | ||
with self.assertRaises(SystemExit) as cm: | ||
cli.main( | ||
[ | ||
'', | ||
'get_discoveries', | ||
'test_repo', | ||
'--sqlite', | ||
self.db_path, | ||
'--save', | ||
self.csv_path, | ||
'--state', | ||
state | ||
] | ||
) | ||
self.assertEqual(cm.exception.code, count) | ||
|
||
@parameterized.expand([ | ||
param(file='danger', count=5), | ||
param(file='fake_file', count=30) | ||
]) | ||
def test_get_discoveries_per_file(self, file, count): | ||
""" Test if we retrieve the correct number of discoveries based on | ||
filename input. | ||
|
||
Parameters | ||
---------- | ||
file: str | ||
The file name to filter discoveries on | ||
count: int | ||
The expected number of discoveries to be returned | ||
""" | ||
with self.assertRaises(SystemExit) as cm: | ||
cli.main( | ||
[ | ||
'', | ||
'get_discoveries', | ||
'test_repo', | ||
'--sqlite', | ||
self.db_path, | ||
'--save', | ||
self.csv_path, | ||
'--filename', | ||
file | ||
] | ||
) | ||
self.assertEqual(cm.exception.code, count) | ||
|
||
def test_csv_written(self): | ||
""" Test if the CLI command writes correctly the CSV file. """ | ||
with self.assertRaises(SystemExit): | ||
cli.main( | ||
[ | ||
'', | ||
'get_discoveries', | ||
'test_repo', | ||
'--sqlite', | ||
self.db_path, | ||
'--save', | ||
self.csv_path | ||
] | ||
) | ||
data_frame = pd.read_csv(self.csv_path) | ||
try: | ||
assert data_frame.notna().values.all() | ||
except AssertionError: | ||
assert False, 'CSV file contains NaN' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong indentation (they should start below "new")