Skip to content

Commit 9a7c013

Browse files
sacca97copernico
authored andcommitted
updated test classes
1 parent abd7a88 commit 9a7c013

14 files changed

+150
-275
lines changed

prospector/api/api_test.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
client = TestClient(app)
77

88

9-
# def test_read_main():
10-
# response = client.get("/")
11-
# assert response.status_code == 200
12-
# assert "<title>Prospector</title>" in response.text
13-
14-
159
def test_status():
1610
response = client.get("/status")
1711
assert response.status_code == 200
@@ -42,10 +36,7 @@ def test_get_specific_commit():
4236
assert response.json()[0]["commit_id"] == commit_id
4337

4438

45-
# @pytest.mark.skip(reason="will raise exception")
4639
def test_get_commits_by_repository():
4740
repository = "https://github.com/apache/dubbo"
4841
response = client.get("/commits/" + repository)
4942
assert response.status_code == 200
50-
assert response.json()[0]["commit_id"] == "yyy"
51-
assert response.json()[1]["commit_id"] == "zzz"

prospector/client/cli/prospector_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ def get_candidates(
338338
filter_extension=advisory_record.files_extension,
339339
)
340340

341-
core_statistics.record("candidates", len(candidates), unit="commits")
341+
core_statistics.record(
342+
"candidates", len(candidates), unit="commits", overwrite=True
343+
)
342344
logger.info("Found %d candidates" % len(candidates))
343345
writer.print(f"Found {len(candidates)} candidates")
344346

prospector/client/cli/prospector_client_test.py

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,19 @@
44

55
from commitdb.postgres import PostgresCommitDB
66

7-
# from .prospector_client import prospector
8-
9-
# def test_prospector_client():
10-
# results = prospector(
11-
# "CVE-2014-0050",
12-
# "https://github.com/apache/struts",
13-
# publication_date="2016-12-12T12:18Z",
14-
# debug=True,
15-
# )
16-
# pprint(results)
17-
18-
19-
@pytest.fixture
20-
def setupdb():
21-
db = PostgresCommitDB()
22-
db.connect()
23-
db.reset()
24-
return db
25-
26-
27-
@pytest.mark.skip(reason="not implemented yet")
28-
def test_main_runonce(setupdb: PostgresCommitDB):
29-
args = [
30-
"python",
31-
"main.py",
32-
"CVE-2019-11278",
33-
"--repository",
34-
"https://github.com/cloudfoundry/uaa",
35-
"--tag-interval=v74.0.0:v74.1.0",
36-
"--use-backend=optional",
37-
]
38-
subprocess.run(args)
39-
40-
setupdb.reset()
41-
42-
43-
# def test_main_runtwice(setupdb):
44-
# db = setupdb
45-
# db.connect(DB_CONNECT_STRING)
46-
# args = [
47-
# "PROGRAM_NAME",
48-
# "CVE-2019-11278",
49-
# "--repository",
50-
# "https://github.com/cloudfoundry/uaa",
51-
# "--tag-interval=v74.0.0:v74.1.0",
52-
# ]
53-
# main(args)
54-
# main(args)
55-
# db.reset()
56-
57-
58-
# def test_main_ping_server():
59-
# args = ["PROGRAM_NAME", "--verbose", "--ping"]
60-
# assert main(args)
7+
from .prospector_client import prospector
8+
9+
OPENCAST_CVE = "CVE-2021-21318"
10+
OPENCAST_REPO = "https://github.com/opencast/opencast"
11+
12+
13+
def test_prospector_client():
14+
results, _ = prospector(
15+
vulnerability_id=OPENCAST_CVE,
16+
repository_url=OPENCAST_REPO,
17+
version_interval="9.1:9.2",
18+
fetch_references=False,
19+
git_cache="/tmp/gitcache",
20+
limit_candidates=5000,
21+
)
22+
assert results[0].commit_id == "b18c6a7f81f08ed14884592a6c14c9ab611ad450"

prospector/commitdb/commitdb_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
def setupdb():
99
db = PostgresCommitDB()
1010
db.connect()
11-
db.reset()
11+
# db.reset()
1212
return db
1313

1414

prospector/datamodel/advisory_test.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,12 @@ def test_advisory_basic():
6969
def test_get_advisory():
7070
advisory = AdvisoryRecord("CVE-2021-22696")
7171
advisory.get_advisory()
72-
print(advisory.__dict__)
7372
assert advisory.cve_id == "CVE-2021-22696"
74-
assert ("3.4.0", "3.4.3") in advisory.versions
73+
assert "3.4.0" in advisory.versions["affected"]
74+
assert "3.4.3" in advisory.versions["fixed"]
7575

7676

7777
def test_build_advisory_record():
7878
advisory = build_advisory_record("CVE-2021-29943", fetch_references=True)
79-
print(advisory.__dict__)
8079

81-
assert advisory.cve_id == "CVE-2014-005"
82-
assert advisory.versions == []
83-
84-
85-
def test_private():
86-
for i in cve_list.split()[:10]:
87-
advisory = build_advisory_record(i, fetch_references=False)
88-
print(advisory.cve_id, advisory.affected_products)
89-
time.sleep(6)
90-
raise NotImplementedError
80+
assert advisory.cve_id == "CVE-2021-29943"

prospector/datamodel/commit_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_preprocess_commit(repository: Git):
3030
def test_preprocess_commit_set(repository: Git):
3131

3232
repo = repository
33-
commit_set = repo.get_commits(since="1615441712", until="1617441712")
33+
commit_set = repo.create_commits(since="1615441712", until="1617441712")
3434
preprocessed_commits = []
3535

3636
for commit_id in commit_set:

prospector/datamodel/nlp_test.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,27 @@ def test_extract_similar_words():
2929

3030

3131
def test_extract_affected_filenames():
32-
result1 = extract_affected_filenames(ADVISORY_TEXT_1)
33-
result2 = extract_affected_filenames(ADVISORY_TEXT_2)
34-
result3 = extract_affected_filenames(ADVISORY_TEXT_3)
35-
result4 = extract_affected_filenames(ADVISORY_TEXT_4)
36-
result5 = extract_affected_filenames(ADVISORY_TEXT_5)
37-
assert result1 == set(["JwtRequestCodeFilter", "request_uri"])
38-
assert result2 == set(
39-
[
40-
"OAuthConfirmationController.java",
41-
"@ModelAttribute",
42-
"authorizationRequest",
43-
"OpenID",
44-
]
45-
)
46-
assert result3 == set(["FileNameUtils"])
47-
48-
assert result4 == set(["MultipartStream.java", "FileUpload"]) # Content-Type
49-
assert result5 == set(["JsonMapObjectReaderWriter"])
50-
32+
result1, _ = extract_affected_filenames(ADVISORY_TEXT_1)
33+
result2, ext2 = extract_affected_filenames(ADVISORY_TEXT_2)
5134

52-
def test_adv_record_path_extraction_has_real_paths():
53-
# result = extract_affected_files_paths(ADVISORY_TEXT_2)
54-
print("")
55-
# assert result == ["FileNameUtils", "//../foo", "\\..\\foo"]
56-
57-
58-
def test_adv_record_path_extraction_strict_extensions():
59-
"""
60-
If strict_extensions is True, it will always extract tokens with (back) slashes,
61-
but it will only collect single file names if they have the correct extension.
62-
"""
63-
# result = extract_affected_files_paths(
64-
# ADVISORY_TEXT_2
65-
# + " Developer.gery put something here to check if foo.java and bar.cpp will be found.",
66-
# strict_extensions=True,
67-
# )
68-
print("result")
69-
# assert result == ["FileNameUtils", "//../foo", "\\..\\foo", "foo", "bar"]
35+
assert "JwtRequestCodeFilter" in result1
36+
assert "OAuthConfirmationController" in result2 and "java" in ext2
7037

7138

7239
def test_extract_jira_references():
73-
x = extract_jira_references("apache/ambari", "AMBARI-25329")
74-
print(x)
40+
# x = extract_jira_references("apache/ambari", "AMBARI-25329")
7541
pass
7642

7743

7844
def test_extract_gh_issues():
7945
d = extract_ghissue_references("https://github.com/apache/commons-text", "#341")
80-
print(d)
81-
raise NotImplementedError
46+
assert "341" in d
47+
assert (
48+
"Interpolation Defaults Interpolation Defaults Changes similar to what was recently released in commons-configuration"
49+
in d["341"]
50+
)
8251

8352

8453
def test_extract_filenames_single():
85-
d = extract_affected_filenames(ADVISORY_TEXT_6)
86-
raise Exception(d)
54+
fn, ext = extract_affected_filenames(ADVISORY_TEXT_6)
55+
assert "Content-Length" in fn

prospector/git/fixtures_test.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

prospector/git/git_test.py

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
# from pprint import pprint
21
import os.path
32
import time
43

54
import pytest
65

7-
from .git import Exec, Git
8-
9-
# from .version_to_tag import version_to_wide_interval_tags
10-
from .version_to_tag import get_possible_tags
6+
from git.git import Exec, Git
117

128
NEBULA = "https://github.com/slackhq/nebula"
139
BEAM = "https://github.com/apache/beam"
@@ -29,75 +25,48 @@ def repository() -> Git:
2925

3026

3127
def test_extract_timestamp(repository: Git):
32-
commit = repository.get_commit(COMMIT_ID)
28+
commit = repository.get_commit(OPENCAST_COMMIT)
3329
commit.extract_timestamp(format_date=True)
34-
assert commit.get_timestamp() == "2020-07-01 15:20:52"
30+
assert commit.get_timestamp() == "2020-01-16 22:34:35"
3531
commit.extract_timestamp(format_date=False)
36-
assert commit.get_timestamp() == 1593616852
32+
assert commit.get_timestamp() == 1579214075
3733

3834

3935
def test_show_tags(repository: Git):
4036
tags = repository.execute("git name-rev --tags")
41-
print(tags)
42-
raise Exception()
37+
assert tags is not None
4338

4439

4540
def test_get_tags_for_commit(repository: Git):
4641
commits = repository.create_commits()
4742
commit = commits.get(OPENCAST_COMMIT)
4843
if commit is not None:
49-
tags = commit.find_tag("8.1")
50-
raise Exception(tags)
44+
tags = commit.find_tags()
45+
assert len(tags) == 62
46+
assert "10.2" in tags and "11.3" in tags and "9.4" in tags
5147

5248

5349
def test_create_commits(repository: Git):
5450
commits = repository.create_commits()
55-
commit = commits.get(COMMIT_ID)
56-
assert len(commits) == 357
57-
assert commit.get_id() == COMMIT_ID
51+
commit = commits.get(OPENCAST_COMMIT)
52+
assert len(commits) == 18178
53+
assert commit.get_id() == OPENCAST_COMMIT
5854

5955

6056
def test_get_hunks_count(repository: Git):
6157
commits = repository.create_commits()
6258
commit = commits.get(OPENCAST_COMMIT)
63-
diff, hunks = commit.get_diff()
64-
print(diff)
65-
raise Exception()
66-
assert hunks == 2
59+
_, hunks = commit.get_diff()
60+
assert hunks == 7
6761

6862

6963
def test_get_changed_files(repository: Git):
70-
commit = repository.get_commit(COMMIT_ID)
64+
commit = repository.get_commit(OPENCAST_COMMIT)
7165

7266
changed_files = commit.get_changed_files()
7367
assert len(changed_files) == 0
7468

7569

76-
@pytest.mark.skip(reason="Skipping this test")
77-
def test_extract_timestamp_from_version():
78-
repo = Git(NEBULA)
79-
repo.clone()
80-
assert repo.extract_timestamp_from_version("v1.5.2") == 1639518536
81-
assert repo.extract_timestamp_from_version("INVALID_VERSION_1_0_0") is None
82-
83-
84-
def test_get_tag_for_version():
85-
repo = Git(NEBULA)
86-
repo.clone()
87-
tags = repo.get_tags()
88-
assert get_possible_tags(tags, "1.5.2") == ["v1.5.2"]
89-
90-
91-
def test_get_commit_parent():
92-
repo = Git(NEBULA)
93-
repo.clone()
94-
id = repo.get_commit_id_for_tag("v1.6.1")
95-
commit = repo.get_commit(id)
96-
97-
commit.get_parent_id()
98-
assert True # commit.parent_id == "4c0ae3df5ef79482134b1c08570ff51e52fdfe06"
99-
100-
10170
def test_run_cache():
10271
_exec = Exec(workdir=os.path.abspath("."))
10372
start = time.time_ns()

0 commit comments

Comments
 (0)