<commit_before>from mock import patch
import pytest
from Tests.test_utils import get_yaml

RETURN_ERROR_TARGET = 'GetDockerImageLatestTag.return_error'

MOCK_TAG_LIST = [{
    u'last_updated': u'2019-10-23T09:13:30.84299Z',
    u'name': u'1.0.0.2876',
    u'repository': 7863337,
    u'creator': 4824052,
    u'image_id': None,
    u'v2': True,
    u'last_updater_username': u'containersci',
    u'last_updater': 4824052,
    u'images': [{
        u'features': u'',
        u'os_features': u'',
        u'variant': None,
        u'os_version': None,
        u'architecture': u'amd64',
        u'os': u'linux',
        u'digest': u'DIGEST',
        u'size': 79019268
    }],
    u'full_size': 79019268,
    u'id': 73482510
}, {
    u'last_updated': u'2019-10-16T06:47:29.631011Z',
    u'name': u'1.0.0.2689',
    u'repository': 7863337,
    u'creator': 4824052,
    u'image_id': None,
    u'v2': True,
    u'last_updater_username': u'containersci',
    u'last_updater': 4824052,
    u'images': [{
        u'features': u'',
        u'os_features': u'',
        u'variant': None,
        u'os_version': None,
        u'architecture': u'amd64',
        u'os': u'linux',
        u'digest': u'DIGEST',
        u'size': 77021619
    }],
    u'full_size': 77021619,
    u'id': 72714981
}]

# demisto/python-deb doesn't contain a latest tag
@pytest.mark.parametrize('image', ['python', 'python-deb', 'python3', 'python3-deb'])
def test_get_docker_image_latest_tag(image):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag = DockerImageValidator.get_docker_image_latest_tag('demisto/' + image, 'demisto/' + image)
    # current latest tag is 2.7.16.2728 or 3.7.2.2728 disable-secrets-detection
    assert int(tag.split('.')[3]) >= 2728


# disable-secrets-detection-start
def test_get_docker_image_from_yml():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    with patch.object(DockerImageValidator, '__init__', lambda x, y, z, w: None):
        # Test integration case
        docker_validator = DockerImageValidator(None, None, None)
        docker_validator.yml_file = get_yaml("Tests/scripts/hook_validations/tests/tests_data/fake_integration.yml")
        docker_validator.is_integration = True
        docker_image = docker_validator.get_docker_image_from_yml()
        assert docker_image == "demisto/pyjwt:1.0"
        # Test script case
        docker_validator.yml_file = get_yaml("Tests/scripts/hook_validations/tests/tests_data/fake-script.yml")
        docker_validator.is_integration = False
        docker_image = docker_validator.get_docker_image_from_yml()
        assert docker_image == "demisto/stix2:172.16.58.3"
# disable-secrets-detection-end


def test_lexical_find_latest_tag():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag_list = ["2.0.2000", "2.1.2700", "2.1.373", "latest"]
    tag = DockerImageValidator.lexical_find_latest_tag(tag_list)
    assert tag == "2.1.2700"


def test_find_latest_tag_by_date():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag = DockerImageValidator.find_latest_tag_by_date(MOCK_TAG_LIST)
    assert tag == "1.0.0.2876"


@pytest.mark.parametrize('www_auth, expected', [('AAArealm="2",service="3"AAA', ('2', '3')), ('bbb', ())])
def test_parse_www_auth(www_auth, expected):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert expected == DockerImageValidator.parse_www_auth(www_auth)


# disable-secrets-detection-start
@pytest.mark.parametrize('input_tags, output_tags',
                         [(['1.2.3.0', '4.5.6.0', '7.8.9.0'], ['4.5.6.0', '1.2.3.0', '7.8.9.0']),
                          (['1.2.3.0', '4.a.6.0', '7.8.9.0'], ['7.8.9.0', '1.2.3.0']),
                          (['aaa', 'bbb'], []), (['6a.7.6'], []), (['6..4'], [])])
# disable-secrets-detection-end
def test_clear_non_numbered_tags(input_tags, output_tags):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert sorted(output_tags) == sorted(DockerImageValidator.clear_non_numbered_tags(input_tags))


def test_parse_docker_image():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert 'demisto/python', '1.3-alpine' == DockerImageValidator.parse_docker_image('demisto/python:1.3-alpine')
    assert 'demisto/slack', '1.2.3.4' == DockerImageValidator.parse_docker_image('demisto/slack:1.2.3.4')
    assert 'demisto/python', '' == DockerImageValidator.parse_docker_image('demisto/python/1.2.3.4')
    assert ('', '') == DockerImageValidator.parse_docker_image('blah/blah')
    assert ('', '1.2.3.4') == DockerImageValidator.parse_docker_image('blah/blah:1.2.3.4')<commit_msg>secrets fix<commit_after>from mock import patch
import pytest
from Tests.test_utils import get_yaml

RETURN_ERROR_TARGET = 'GetDockerImageLatestTag.return_error'

MOCK_TAG_LIST = [{
    u'last_updated': u'2019-10-23T09:13:30.84299Z',
    u'name': u'1.0.0.2876',
    u'repository': 7863337,
    u'creator': 4824052,
    u'image_id': None,
    u'v2': True,
    u'last_updater_username': u'containersci',
    u'last_updater': 4824052,
    u'images': [{
        u'features': u'',
        u'os_features': u'',
        u'variant': None,
        u'os_version': None,
        u'architecture': u'amd64',
        u'os': u'linux',
        u'digest': u'DIGEST',
        u'size': 79019268
    }],
    u'full_size': 79019268,
    u'id': 73482510
}, {
    u'last_updated': u'2019-10-16T06:47:29.631011Z',
    u'name': u'1.0.0.2689',
    u'repository': 7863337,
    u'creator': 4824052,
    u'image_id': None,
    u'v2': True,
    u'last_updater_username': u'containersci',
    u'last_updater': 4824052,
    u'images': [{
        u'features': u'',
        u'os_features': u'',
        u'variant': None,
        u'os_version': None,
        u'architecture': u'amd64',
        u'os': u'linux',
        u'digest': u'DIGEST',
        u'size': 77021619
    }],
    u'full_size': 77021619,
    u'id': 72714981
}]

# demisto/python-deb doesn't contain a latest tag
@pytest.mark.parametrize('image', ['python', 'python-deb', 'python3', 'python3-deb'])
def test_get_docker_image_latest_tag(image):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag = DockerImageValidator.get_docker_image_latest_tag('demisto/' + image, 'demisto/' + image)
    # current latest tag is 2.7.16.2728 or 3.7.2.2728 disable-secrets-detection
    assert int(tag.split('.')[3]) >= 2728


# disable-secrets-detection-start
def test_get_docker_image_from_yml():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    with patch.object(DockerImageValidator, '__init__', lambda x, y, z, w: None):
        # Test integration case
        docker_validator = DockerImageValidator(None, None, None)
        docker_validator.yml_file = get_yaml("Tests/scripts/hook_validations/tests/tests_data/fake_integration.yml")
        docker_validator.is_integration = True
        docker_image = docker_validator.get_docker_image_from_yml()
        assert docker_image == "demisto/pyjwt:1.0"
        # Test script case
        docker_validator.yml_file = get_yaml("Tests/scripts/hook_validations/tests/tests_data/fake-script.yml")
        docker_validator.is_integration = False
        docker_image = docker_validator.get_docker_image_from_yml()
        assert docker_image == "demisto/stix2:1.0.0.204"
# disable-secrets-detection-end


def test_lexical_find_latest_tag():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag_list = ["2.0.2000", "2.1.2700", "2.1.373", "latest"]
    tag = DockerImageValidator.lexical_find_latest_tag(tag_list)
    assert tag == "2.1.2700"


def test_find_latest_tag_by_date():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag = DockerImageValidator.find_latest_tag_by_date(MOCK_TAG_LIST)
    assert tag == "1.0.0.2876"


@pytest.mark.parametrize('www_auth, expected', [('AAArealm="2",service="3"AAA', ('2', '3')), ('bbb', ())])
def test_parse_www_auth(www_auth, expected):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert expected == DockerImageValidator.parse_www_auth(www_auth)


# disable-secrets-detection-start
@pytest.mark.parametrize('input_tags, output_tags',
                         [(['1.2.3.0', '4.5.6.0', '7.8.9.0'], ['4.5.6.0', '1.2.3.0', '7.8.9.0']),
                          (['1.2.3.0', '4.a.6.0', '7.8.9.0'], ['7.8.9.0', '1.2.3.0']),
                          (['aaa', 'bbb'], []), (['6a.7.6'], []), (['6..4'], [])])
# disable-secrets-detection-end
def test_clear_non_numbered_tags(input_tags, output_tags):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert sorted(output_tags) == sorted(DockerImageValidator.clear_non_numbered_tags(input_tags))


# disable-secrets-detection-start
def test_parse_docker_image():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert 'demisto/python', '1.3-alpine' == DockerImageValidator.parse_docker_image('demisto/python:1.3-alpine')
    assert 'demisto/slack', '1.2.3.4' == DockerImageValidator.parse_docker_image('demisto/slack:1.2.3.4')
    assert 'demisto/python', '' == DockerImageValidator.parse_docker_image('demisto/python/1.2.3.4')
    assert ('', '') == DockerImageValidator.parse_docker_image('blah/blah')
    assert ('', '1.2.3.4') == DockerImageValidator.parse_docker_image('blah/blah:1.2.3.4')
# disable-secrets-detection-end
{'fraction_non_alphanumeric': 0.15349032800672835, 'fraction_numerical': 0.0603448275862069, 'mean_word_length': 3.794858870967742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 42, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6535100', 'n_tokens_mistral': 3796, 'n_tokens_neox': 3418, 'n_words': 520}