def test_is_valid_name_prefix(self, current_file, pack_metadata, answer,
                                  mocker):
        """
            Given
            - A set of indicator fields

            When
            - Running is_valid_incident_field_name_prefix on it.

            Then
            - Ensure validate fails when the field name does not start with the pack name prefix.
        """
        from demisto_sdk.commands.common.hook_validations import \
            field_base_validator
        with patch.object(StructureValidator, '__init__', lambda a, b: None):
            structure = StructureValidator("")
            structure.current_file = current_file
            structure.old_file = None
            structure.file_path = "random_path"
            structure.is_valid = True
            structure.prev_ver = 'master'
            structure.branch_name = ''
            validator = FieldBaseValidator(structure, set(), set())
            validator.current_file = current_file
            mocker.patch.object(field_base_validator,
                                'get_pack_metadata',
                                return_value=pack_metadata)
            assert validator.is_valid_field_name_prefix() == answer
def test_is_changed_from_version(current_from_version, old_from_version,
                                 answer):
    structure = StructureValidator("")
    structure.old_file = old_from_version
    structure.current_file = current_from_version
    validator = IncidentTypeValidator(structure)
    assert validator.is_changed_from_version() is answer
Exemplo n.º 3
0
 def test_is_changed_type(self, current_type, old_type, is_valid):
     structure = StructureValidator("")
     structure.current_file = {"type": current_type}
     structure.old_file = {"type": old_type}
     validator = IncidentFieldValidator(structure)
     assert validator.is_changed_type() == is_valid, f'is_changed_type({current_type}, {old_type})' \
                                                     f' returns {not is_valid}.'
    def test_is_valid_name_sanity(self, current_file, answer):
        import os
        import sys
        with patch.object(StructureValidator, '__init__', lambda a, b: None):
            structure = StructureValidator("")
            structure.current_file = current_file
            structure.old_file = None
            structure.file_path = "random_path"
            structure.is_valid = True
            structure.prev_ver = 'master'
            structure.branch_name = ''
            validator = FieldBaseValidator(structure, set(), set())
            validator.current_file = current_file

            with open("file", 'w') as temp_out:
                old_stdout = sys.stdout
                sys.stdout = temp_out
                validator.is_valid_name()
                sys.stdout = old_stdout

            with open('file', 'r') as temp_out:
                output = temp_out.read()
                assert ('IF100' in str(output)) is answer
            # remove the temp file
            os.system('rm -rf file')
Exemplo n.º 5
0
 def test_is_changed_from_version(self, current_from_version, old_from_version, answer):
     structure = StructureValidator("")
     structure.old_file = old_from_version
     structure.current_file = current_from_version
     validator = FieldBaseValidator(structure, set(), set())
     assert validator.is_changed_from_version() is answer
     structure.quiet_bc = True
     assert validator.is_changed_from_version() is False
 def test_is_changed_type(self, current_type, old_type, is_valid):
     structure = StructureValidator("")
     structure.current_file = {"type": current_type}
     structure.old_file = {"type": old_type}
     validator = FieldBaseValidator(structure, set(), set())
     assert validator.is_changed_type() == is_valid, f'is_changed_type({current_type}, {old_type})' \
                                                     f' returns {not is_valid}.'
     structure.quiet_bc = True
     assert validator.is_changed_type() is False
 def test_is_valid_system_flag_sanity(self, current_file, answer):
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         validator = IncidentFieldValidator(structure)
         validator.current_file = current_file
         assert validator.is_valid_system_flag() is answer
Exemplo n.º 8
0
def mock_structure(file_path=None, current_file=None, old_file=None):
    # type: (Optional[str], Optional[dict], Optional[dict]) -> StructureValidator
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator(file_path)
        structure.is_valid = True
        structure.scheme_name = 'integration'
        structure.file_path = file_path
        structure.current_file = current_file
        structure.old_file = old_file
        return structure
 def test_is_valid_cliname_invalid(self, cliname, group):
     current_file = {"cliName": cliname, "group": group}
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         validator = IncidentFieldValidator(structure)
         validator.current_file = current_file
         assert not validator.is_valid_cliname()
Exemplo n.º 10
0
 def test_matching_cliname_regex_invalid(self, cliname):
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         current_file = {"cliName": cliname}
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         validator = IncidentFieldValidator(structure)
         validator.current_file = current_file
         assert not validator.is_matching_cliname_regex()
Exemplo n.º 11
0
def get_validator(current_file=None, old_file=None, file_path=""):
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator("")
        structure.current_file = current_file
        structure.old_file = old_file
        structure.file_path = file_path
        structure.is_valid = True
        validator = ScriptValidator(structure)
        validator.old_script = old_file
        validator.current_script = current_file
    return validator
Exemplo n.º 12
0
def mock_structure(file_path=None, current_file=None, old_file=None):
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator(file_path)
        structure.is_valid = True
        structure.scheme_name = 'classifier'
        structure.file_path = file_path
        structure.current_file = current_file
        structure.old_file = old_file
        structure.prev_ver = 'master'
        structure.branch_name = ''
        return structure
 def test_is_valid_content_flag_sanity(self, current_file, answer):
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         structure.prev_ver = 'master'
         structure.branch_name = ''
         validator = FieldBaseValidator(structure, set(), set())
         validator.current_file = current_file
         assert validator.is_valid_content_flag() is answer
Exemplo n.º 14
0
def mock_structure(file_path=None, current_file=None, old_file=None):
    # type: (Optional[str], Optional[dict], Optional[dict]) -> StructureValidator
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator(file_path)
        structure.is_valid = True
        structure.scheme_name = 'playbook'
        structure.file_path = file_path
        structure.current_file = current_file
        structure.old_file = old_file
        structure.prev_ver = 'master'
        structure.branch_name = ''
        return structure
Exemplo n.º 15
0
 def test_is_cliname_is_builtin_key_invalid(self, cliname, group):
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         current_file = {"cliName": cliname, "group": group}
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         structure.prev_ver = 'master'
         structure.branch_name = ''
         validator = IncidentFieldValidator(structure)
         validator.current_file = current_file
         assert not validator.is_cliname_is_builtin_key()
 def test_is_valid_cli_name_invalid(self, cliname, group):
     current_file = {"cliName": cliname, "group": group}
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         structure.prev_ver = 'master'
         structure.branch_name = ''
         validator = FieldBaseValidator(structure, set(), {'id'})
         validator.current_file = current_file
         assert not validator.is_valid_cli_name()
 def test_matching_cliname_regex_invalid(self, cliname):
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         current_file = {"cliName": cliname}
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         structure.prev_ver = 'master'
         structure.branch_name = ''
         validator = FieldBaseValidator(structure, set(), set())
         validator.current_file = current_file
         assert not validator.is_matching_cli_name_regex()
Exemplo n.º 18
0
def mock_structure(file_path=None, current_file=None, old_file=None, quiet_bc=False):
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator(file_path)
        structure.is_valid = True
        structure.scheme_name = 'widget'
        structure.file_path = file_path
        structure.current_file = current_file
        structure.old_file = old_file
        structure.prev_ver = 'master'
        structure.branch_name = ''
        structure.quiet_bc = quiet_bc
        structure.specific_validations = None
        return structure
Exemplo n.º 19
0
def get_validator(current_file=None, old_file=None, file_path=""):
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator("")
        structure.current_file = current_file
        structure.old_file = old_file
        structure.file_path = file_path
        structure.is_valid = True
        structure.prev_ver = 'master'
        structure.branch_name = ''
        structure.quiet_bc = False
        validator = ScriptValidator(structure)
        validator.old_script = old_file
        validator.current_script = current_file
    return validator
Exemplo n.º 20
0
def get_validator(current_file=None, old_file=None, file_path="Packs/exists"):
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator("")
        structure.current_file = current_file
        structure.old_file = old_file
        structure.file_path = file_path
        structure.is_valid = True
        structure.prev_ver = 'master'
        structure.branch_name = ''
        structure.quiet_bc = False
        structure.specific_validations = None
        validator = WizardValidator(structure)
        validator.current_file = current_file
    return validator
Exemplo n.º 21
0
def mock_structure(file_path=None, current_file=None, old_file=None):
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator(file_path)
        structure.is_valid = True
        structure.scheme_name = 'list'
        structure.file_path = file_path
        file = open(file_path, "r")
        structure.current_file = json.loads(file.read())
        file.close()
        structure.old_file = old_file
        structure.prev_ver = 'master'
        structure.branch_name = ''
        structure.quiet_bc = False
        return structure
Exemplo n.º 22
0
 def test_is_cliname_is_builtin_key(self, cliname, group):
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         current_file = {"cliName": cliname, "group": group}
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         structure.prev_ver = 'master'
         structure.branch_name = ''
         structure.specific_validations = None
         validator = FieldBaseValidator(structure, set(), set())
         validator.current_file = current_file
         assert validator.is_cli_name_is_builtin_key()
Exemplo n.º 23
0
 def test_is_valid_cliname(self, _id, cliname, group):
     current_file = {'id': _id, 'cliName': cliname, 'group': group}
     with patch.object(StructureValidator, '__init__', lambda a, b: None):
         structure = StructureValidator("")
         structure.current_file = current_file
         structure.old_file = None
         structure.file_path = "random_path"
         structure.is_valid = True
         structure.prev_ver = 'master'
         structure.branch_name = ''
         structure.specific_validations = None
         validator = FieldBaseValidator(structure, set(), set())
         validator.current_file = current_file
         assert validator.is_valid_cli_name()
def mock_structure(file_path=None, current_file=None, old_file=None):
    with patch.object(StructureValidator, '__init__', lambda a, b: None):
        structure = StructureValidator(file_path)
        structure.is_valid = True
        structure.scheme_name = 'genericfield'
        structure.file_path = file_path
        structure.current_file = current_file
        structure.old_file = old_file
        structure.prev_ver = 'master'
        structure.branch_name = ''
        structure.quiet_bc = False
        structure.skip_schema_check = False
        structure.pykwalify_logs = False
        structure.scheme_name = namedtuple('scheme_name',
                                           'value')(value='genericfield')
        structure.checked_files = set()
        structure.ignored_errors = dict()
        structure.suppress_print = True
        structure.json_file_path = None
        return structure
Exemplo n.º 25
0
    def test_is_valid_grid_fromversion(self, field_type, from_version,
                                       file_type, is_valid):
        """
            Given
            - an invalid indicator-field - the field is of type grid but fromVersion is < 5.5.0.

            When
            - Running is_valid_indicator_grid_fromversion on it.

            Then
            - Ensure validate fails on versions < 5.5.0.
        """
        structure = StructureValidator("")
        structure.file_type = file_type
        structure.current_file = {
            "fromVersion": from_version,
            "type": field_type
        }
        validator = IncidentFieldValidator(structure)
        assert validator.is_valid_indicator_grid_fromversion() == is_valid, \
            f'is_valid_grid_fromVersion({field_type}, {from_version} returns {not is_valid}'
Exemplo n.º 26
0
def test_is_id_and_details_empty(id_, details, is_valid):
    structure = StructureValidator("")
    structure.current_file = {"id": id_, "details": details}
    validator = ReputationValidator(structure)
    assert validator.is_required_fields_empty() == is_valid
Exemplo n.º 27
0
def test_is_valid_id_field(id_, is_valid):
    structure = StructureValidator("")
    structure.current_file = {"id": id_}
    validator = ReputationValidator(structure)
    assert validator.is_valid_indicator_type_id() == is_valid
Exemplo n.º 28
0
def test_is_id_equals_details(id_, details, is_valid):
    structure = StructureValidator("")
    structure.current_file = {"id": id_, "details": details}
    validator = ReputationValidator(structure)
    assert validator.is_id_equals_details() == is_valid, f'is_id_equals_details({id_}, {details})' \
        f' returns {not is_valid}.'
Exemplo n.º 29
0
def test_is_valid_expiration(expiration, is_valid):
    structure = StructureValidator("")
    structure.current_file = {"fromVersion": "5.5.0", "expiration": expiration}
    validator = ReputationValidator(structure)
    assert validator.is_valid_expiration() == is_valid, f'is_valid_expiration({expiration})' \
                                                        f' returns {not is_valid}.'
Exemplo n.º 30
0
def test_is_valid_version(version, is_valid):
    structure = StructureValidator("")
    structure.current_file = {"version": version}
    validator = ReputationValidator(structure)
    assert validator.is_valid_version(
    ) == is_valid, f'is_valid_version({version}) returns {not is_valid}.'