|
1 | 1 | # Standard Library
|
2 | 2 | import sys
|
| 3 | +from datetime import datetime |
3 | 4 | from unittest.mock import patch
|
4 | 5 |
|
5 | 6 | # Third Party
|
6 | 7 | from _pytest.monkeypatch import MonkeyPatch
|
7 | 8 | from resc_backend.resc_web_service.schema.branch import Branch
|
8 | 9 | from resc_backend.resc_web_service.schema.repository import Repository
|
| 10 | +from resc_backend.resc_web_service.schema.scan import ScanRead |
9 | 11 | from resc_backend.resc_web_service.schema.scan_type import ScanType
|
10 | 12 |
|
11 | 13 | # First Party
|
@@ -127,3 +129,80 @@ def test_scan_directory(start_scan):
|
127 | 129 | result = secret_scanner.scan_directory(directory_path=repo_clone_path)
|
128 | 130 | assert result is None
|
129 | 131 | start_scan.assert_called_once()
|
| 132 | + |
| 133 | + |
| 134 | +# not a test class |
| 135 | +def initialize_and_get_repo_scanner_and_branch(): |
| 136 | + repository = Repository(project_key="local", |
| 137 | + repository_id=1, |
| 138 | + repository_name="local", |
| 139 | + repository_url="https://repository.url", |
| 140 | + vcs_instance=1, |
| 141 | + branches=[]) |
| 142 | + |
| 143 | + secret_scanner = SecretScanner( |
| 144 | + gitleaks_binary_path="/tmp/gitleaks", |
| 145 | + gitleaks_rules_path="/rules.toml", |
| 146 | + rule_pack_version="2.0.1", |
| 147 | + output_plugin=RESTAPIWriter(rws_url="https://fakeurl.com:8000"), |
| 148 | + repository=repository, |
| 149 | + username="", |
| 150 | + personal_access_token="") |
| 151 | + |
| 152 | + branch = Branch(branch_id=1, |
| 153 | + branch_name="branch_name1", |
| 154 | + latest_commit="latest_commit_2") |
| 155 | + |
| 156 | + return repository, branch, secret_scanner |
| 157 | + |
| 158 | + |
| 159 | +def test_scan_type_is_base_when_a_latest_scan_is_not_present(): |
| 160 | + repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch() |
| 161 | + |
| 162 | + scan_type = secret_scanner.determine_scan_type(branch, None) |
| 163 | + assert scan_type == ScanType.BASE |
| 164 | + |
| 165 | + |
| 166 | +def test_scan_type_is_base_when_a_latest_scan_is_present_and_rule_pack_is_latest(): |
| 167 | + repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch() |
| 168 | + |
| 169 | + scan_read = ScanRead(id_=1, |
| 170 | + branch_id=1, |
| 171 | + scan_type=ScanType.BASE, |
| 172 | + last_scanned_commit="latest_commit_1", |
| 173 | + timestamp=datetime.utcnow(), |
| 174 | + increment_number=0, |
| 175 | + rule_pack="2.0.2") |
| 176 | + |
| 177 | + scan_type = secret_scanner.determine_scan_type(branch, scan_read) |
| 178 | + assert scan_type == ScanType.BASE |
| 179 | + |
| 180 | + |
| 181 | +def test_scan_type_is_incremental_when_a_latest_scan_is_present_and_rule_pack_is_same(): |
| 182 | + repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch() |
| 183 | + |
| 184 | + scan_read = ScanRead(id_=1, |
| 185 | + branch_id=1, |
| 186 | + scan_type=ScanType.BASE, |
| 187 | + last_scanned_commit="latest_commit_1", |
| 188 | + timestamp=datetime.utcnow(), |
| 189 | + increment_number=0, |
| 190 | + rule_pack="2.0.1") |
| 191 | + |
| 192 | + scan_type = secret_scanner.determine_scan_type(branch, scan_read) |
| 193 | + assert scan_type == ScanType.INCREMENTAL |
| 194 | + |
| 195 | + |
| 196 | +def test_scan_type_is_incremental_when_a_latest_scan_is_present_and_rule_pack_is_same_and_last_commit_is_newer(): |
| 197 | + repository, branch, secret_scanner = initialize_and_get_repo_scanner_and_branch() |
| 198 | + |
| 199 | + scan_read = ScanRead(id_=1, |
| 200 | + branch_id=1, |
| 201 | + scan_type=ScanType.BASE, |
| 202 | + last_scanned_commit="latest_commit_1", |
| 203 | + timestamp=datetime.utcnow(), |
| 204 | + increment_number=0, |
| 205 | + rule_pack="2.0.1") |
| 206 | + |
| 207 | + scan_type = secret_scanner.determine_scan_type(branch, scan_read) |
| 208 | + assert scan_type == ScanType.INCREMENTAL |
0 commit comments