-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
146 additions
and
5 deletions.
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
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,83 @@ | ||
import random | ||
|
||
import factory | ||
import factory.fuzzy | ||
from pygitguardian.models import Match, PolicyBreak, ScanResult | ||
|
||
from ggshield.core.scan.scannable import StringScannable | ||
from ggshield.utils.git_shell import Filemode | ||
|
||
|
||
def get_line_index(content, index): | ||
"""Return the index of the line containing the caracter at the given index""" | ||
current_line_index = 0 | ||
lines = content.splitlines(keepends=True) | ||
while True: | ||
line = lines.pop(0) | ||
if index <= len(line): | ||
return current_line_index | ||
index -= len(line) | ||
current_line_index += 1 | ||
|
||
|
||
class ScannableFactory(factory.Factory): | ||
class Meta: | ||
model = StringScannable | ||
|
||
url = factory.Faker("hostname") | ||
content = factory.Faker("text") | ||
# Only returning FILE for new, since diff would need a custom content | ||
filemode = Filemode.FILE | ||
|
||
|
||
class MatchFactory(factory.Factory): | ||
class Meta: | ||
model = Match | ||
|
||
content = factory.Faker("text") | ||
match_len = factory.fuzzy.FuzzyInteger(5, 15) | ||
index_start = factory.lazy_attribute( | ||
lambda obj: random.randint(0, len(obj.content) - obj.match_len) | ||
) | ||
index_end = factory.lazy_attribute(lambda obj: obj.index_start + obj.match_len) | ||
match = factory.lazy_attribute( | ||
lambda obj: obj.content[obj.index_start : obj.index_end] | ||
) | ||
line_start = factory.lazy_attribute( | ||
lambda obj: get_line_index(obj.content, obj.index_start) | ||
) | ||
line_end = factory.lazy_attribute( | ||
lambda obj: get_line_index(obj.content, obj.index_end) | ||
) | ||
match_type = factory.fuzzy.FuzzyText(length=10) | ||
|
||
|
||
class PolicyBreakFactory(factory.Factory): | ||
class Meta: | ||
model = PolicyBreak | ||
|
||
break_type = "a" | ||
policy = "Secrets detection" | ||
validity = "valid" | ||
known_secret = False | ||
incident_url = None | ||
is_excluded = False | ||
exclude_reason = None | ||
diff_kind = None | ||
content = factory.Faker("text") | ||
nb_matches = factory.fuzzy.FuzzyInteger(1, 2) | ||
|
||
@factory.lazy_attribute | ||
def matches(self): | ||
# Note: matches may overlap | ||
return [MatchFactory(content=self.content) for _ in range(self.nb_matches)] | ||
|
||
|
||
class ScanResultFactory(factory.Factory): | ||
class Meta: | ||
model = ScanResult | ||
|
||
policy_break_count = factory.lazy_attribute(lambda obj: len(obj.policy_breaks)) | ||
policy_breaks = [] | ||
policies = ["Secrets detection"] | ||
is_diff = False |
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,23 @@ | ||
import pytest | ||
|
||
from tests.factories import get_line_index | ||
|
||
|
||
TEST_CONTENT = """aaa | ||
bb | ||
cccc""" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("index", "expected_line_index"), | ||
( | ||
(1, 0), | ||
(4, 0), | ||
(5, 1), | ||
(7, 1), | ||
(8, 2), | ||
(11, 2), | ||
), | ||
) | ||
def test_get_line_index(index, expected_line_index): | ||
assert get_line_index(TEST_CONTENT, index) == expected_line_index |
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