Skip to content
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 99 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 94 commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
b28ee54
header comment
alaabenfatma Aug 2, 2021
06e1279
import libs
alaabenfatma Aug 2, 2021
3dc3d19
init + config parser
alaabenfatma Aug 2, 2021
c096c49
print discoveries on the console.
alaabenfatma Aug 2, 2021
230ab56
format
alaabenfatma Aug 2, 2021
67a8283
export_csv function
alaabenfatma Aug 2, 2021
1a17c01
format
alaabenfatma Aug 2, 2021
fc27308
max_number_of_discoveries to be shown.
alaabenfatma Aug 2, 2021
1298daf
formatting
alaabenfatma Aug 2, 2021
2dcf655
'run' function
alaabenfatma Aug 2, 2021
9e6dabc
assign_categories to rules
alaabenfatma Aug 2, 2021
bc513a7
export_discoveries function + importation of libs
alaabenfatma Aug 2, 2021
bdc9b3a
Integrate 'get_discoveries' into the cli
alaabenfatma Aug 2, 2021
7f77545
update requirements
alaabenfatma Aug 2, 2021
52ba495
remove 'vscode' auto import
alaabenfatma Aug 2, 2021
63dbc1b
+1 whitespace
alaabenfatma Aug 2, 2021
d6698eb
removed 'print' and use 'console.print' instead. + format
alaabenfatma Aug 2, 2021
629d57c
pep8
alaabenfatma Aug 2, 2021
0a0396f
break into two lines
alaabenfatma Aug 2, 2021
7ea7a25
pep8 if condition
alaabenfatma Aug 2, 2021
376d4df
rename variable
alaabenfatma Aug 2, 2021
160226a
comparison with None (pep8)
alaabenfatma Aug 2, 2021
636e282
pep8 client.py
alaabenfatma Aug 2, 2021
4adb3c7
fixed trailing whitespace
alaabenfatma Aug 2, 2021
40d2490
Merge branch 'develop' into feat/cli/get_discoveries
alaabenfatma Aug 2, 2021
5c5d3c2
fixed comment
alaabenfatma Aug 2, 2021
8b83e27
Merge branch 'feat/cli/get_discoveries' of github.com:SAP/credential-…
alaabenfatma Aug 2, 2021
f99a6b6
remove unused 'status' + progress label
alaabenfatma Aug 2, 2021
898a637
fixed alphabetical order (+ old wrong order)
alaabenfatma Aug 3, 2021
2026667
3rd-party library import order
alaabenfatma Aug 3, 2021
1d5c3a0
remove dots from the comments
alaabenfatma Aug 3, 2021
378053d
comments explaining why we delete certain columns
alaabenfatma Aug 3, 2021
9f75676
added YES & NO to possible user input.
alaabenfatma Aug 3, 2021
6a5b6fb
pep8 format
alaabenfatma Aug 3, 2021
7b3d425
Add seperator -------
alaabenfatma Aug 3, 2021
9e404f3
remove empty lines
alaabenfatma Aug 3, 2021
b79e0c7
add --filename optional argument.
alaabenfatma Aug 3, 2021
07de7f8
enable printing discoveries filtered by filename
alaabenfatma Aug 3, 2021
8644407
pass the discoveries as an argument instead.
alaabenfatma Aug 3, 2021
f47a3ff
pass discoveries to instead of repo_url to generate CSV string.
alaabenfatma Aug 3, 2021
1b1001d
blank line
alaabenfatma Aug 3, 2021
f96c5b7
filter discoveries + assign categories function
alaabenfatma Aug 3, 2021
24e7d47
convert discoveries to csv
alaabenfatma Aug 3, 2021
904a9ea
adapt code to new refactor
alaabenfatma Aug 3, 2021
6a85511
remove assign_categories & export_csv from client.py
alaabenfatma Aug 3, 2021
fcd4639
pep8
alaabenfatma Aug 3, 2021
fa1c7ca
define new argument
alaabenfatma Aug 3, 2021
80dc181
filter discoveries based on state --state argument
alaabenfatma Aug 3, 2021
8f81fd4
removed unused libraries
alaabenfatma Aug 3, 2021
7b0ad97
docs header
alaabenfatma Aug 3, 2021
e79e144
correct variable name + refactor line of code
alaabenfatma Aug 3, 2021
a8c5d3e
Docs + formatting
alaabenfatma Aug 3, 2021
0f7f79b
whitespace for better UX
alaabenfatma Aug 3, 2021
5aa3b69
Exit with a code equal to that of retrieved discoveries.
alaabenfatma Aug 5, 2021
cc9f721
load vanilla packages
alaabenfatma Aug 5, 2021
816001f
load pandas for CSV testing
alaabenfatma Aug 5, 2021
b0f8f5e
load credential-digger modules + parameterized
alaabenfatma Aug 5, 2021
3c2f66e
define test class body
alaabenfatma Aug 5, 2021
5c7b4e3
Set up sqlite database and CSV export temporary paths
alaabenfatma Aug 5, 2021
694068d
init sqlite database
alaabenfatma Aug 5, 2021
3966c98
typo + pep8
alaabenfatma Aug 5, 2021
4a0668a
insert fake discoveries into the database
alaabenfatma Aug 5, 2021
4dae763
more verbose field values
alaabenfatma Aug 5, 2021
7640881
remove sqlite database & exported csv file after finishing tests
alaabenfatma Aug 5, 2021
4bbeb37
body of the get_discoveries test function
alaabenfatma Aug 5, 2021
87d6699
correct typo + parameterize the test_get_discoveries function to cove…
alaabenfatma Aug 5, 2021
c0c4a44
Test if we correctly retrieve the correct number of discoveries per s…
alaabenfatma Aug 5, 2021
724a14d
test if CSV file contains non-NaN values
alaabenfatma Aug 5, 2021
c1ccd67
Test if generated CSV file contains NaN fields.
alaabenfatma Aug 5, 2021
9c1087d
removed useless pass
alaabenfatma Aug 5, 2021
5b95e95
rule_id change
alaabenfatma Aug 5, 2021
b0f300c
autopep8
alaabenfatma Aug 5, 2021
6d790c2
Test if we retrieve the correct number of discoveries based on a file…
alaabenfatma Aug 5, 2021
50f977c
Test if --filename argument works correctly.
alaabenfatma Aug 5, 2021
bca3394
remove unused variable
alaabenfatma Aug 5, 2021
001417e
Merge 'develop' into 'test/get_discoveries_cli'
alaabenfatma Aug 10, 2021
b0bb1e8
Merge branch 'develop' into test/get_discoveries_cli
alaabenfatma Aug 10, 2021
73d0439
uselss line removed
alaabenfatma Aug 10, 2021
ec157dc
removed unused --state 'all'
alaabenfatma Aug 13, 2021
fdaf79e
remove the 'all' state
alaabenfatma Aug 13, 2021
92624b2
removed extra line
alaabenfatma Aug 16, 2021
99e5564
Fixed comments
alaabenfatma Aug 16, 2021
f8b9a36
Postgres test
alaabenfatma Aug 16, 2021
d063401
Removed unused whitespace
alaabenfatma Aug 16, 2021
6e24309
removed unused whitespace
alaabenfatma Aug 16, 2021
874deea
rename file
alaabenfatma Aug 16, 2021
6cba548
unused import
alaabenfatma Aug 16, 2021
f4fd226
used one line instead
alaabenfatma Aug 16, 2021
84731bf
Load environment variables
alaabenfatma Aug 16, 2021
cee0f77
pass --dotenv argument
alaabenfatma Aug 17, 2021
f0b4e89
fix unbound variable exc
marcorosa Aug 17, 2021
68cf12f
use git secrets env variables
marcorosa Aug 17, 2021
d569701
add_rules
alaabenfatma Aug 17, 2021
d2f6240
remove blankspaces
alaabenfatma Aug 17, 2021
a7827a5
correct indentation.
alaabenfatma Aug 19, 2021
9e78ab9
sqlite: correct indentation
alaabenfatma Aug 19, 2021
ed310f1
add docstring
alaabenfatma Aug 19, 2021
da321d6
More expressive docstring.
alaabenfatma Aug 19, 2021
6da5450
comment -> docstring
alaabenfatma Aug 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions credentialdigger/cli/get_discoveries.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ def discoveries_to_csv(discoveries):
csv_writer.writeheader()
csv_writer.writerows(discoveries)
csv_data = stringIO.getvalue()
return csv_data
except IndexError as error:
logger.error(error)
except Exception as exception:
logger.exception(exception)

return csv_data


def export_csv(discoveries, client, save=False):
""" Export discoveries as a CSV file.
Expand Down
146 changes: 146 additions & 0 deletions tests/functional_tests/test_get_discoveries_postgres.py
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']:
Copy link
Member

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")

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
Copy link
Member

Choose a reason for hiding this comment

The 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'
132 changes: 132 additions & 0 deletions tests/functional_tests/test_get_discoveries_sqlite.py
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']:
Copy link
Member

Choose a reason for hiding this comment

The 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'