Exemplo n.º 1
0
 def test_scheme_validation_playbook(self, path, scheme, answer, error,
                                     mocker):
     mocker.patch.object(StructureValidator,
                         'scheme_of_file_by_path',
                         return_value=scheme)
     validator = StructureValidator(file_path=path)
     assert validator.is_valid_scheme() is answer, error
Exemplo n.º 2
0
 def initiate_file_validator(self, validator_type):
     """ Run schema validate and file validate of file
     Returns:
         int 0 in case of success
         int 1 in case of error
         int 2 in case of skip
     """
     if self.no_validate:
         print_color(
             f'Validator Skipped on file: {self.output_file} , no-validate flag was set.',
             LOG_COLORS.YELLOW)
         return SKIP_RETURN_CODE
     else:
         print_color('Starting validating files structure',
                     LOG_COLORS.GREEN)
         if self.relative_content_path:
             structure_validator = StructureValidator(
                 self.relative_content_path)
             validator = validator_type(structure_validator)
             if structure_validator.is_valid_file(
             ) and validator.is_valid_file(validate_rn=False):
                 print_color('The files are valid', LOG_COLORS.GREEN)
                 return SUCCESS_RETURN_CODE
             else:
                 print_color('The files are invalid', LOG_COLORS.RED)
                 return ERROR_RETURN_CODE
         else:
             print_color(
                 f'The file {self.output_file} are not part of content repo, Validator Skipped',
                 LOG_COLORS.YELLOW)
             return SKIP_RETURN_CODE
Exemplo n.º 3
0
    def test_invalid_integration_path(self, integration, mocker):
        """
        Given
            - An integration with invalid file path.
        When
            - running is_valid_integration_file_path.
        Then
            - an integration with an invalid file path is invalid.
        """

        structure_validator = StructureValidator(
            integration.yml.path, predefined_scheme='integration')
        validator = IntegrationValidator(structure_validator)
        validator.file_path = 'Packs/VirusTotal/Integrations/VirusTotal/integration-VirusTotal_5.5.yml'

        mocker.patch.object(validator, "handle_error", return_value=True)

        assert not validator.is_valid_integration_file_path()

        structure_validator = StructureValidator(
            integration.path, predefined_scheme='integration')
        validator = IntegrationValidator(structure_validator)
        validator.file_path = 'Packs/VirusTotal/Integrations/Virus_Total_5.yml'

        mocker.patch.object(validator, "handle_error", return_value=True)

        assert not validator.is_valid_integration_file_path()
Exemplo n.º 4
0
 def test_print_error_line(self, path, scheme, error, correct, mocker):
     mocker.patch.object(StructureValidator,
                         'scheme_of_file_by_path',
                         return_value=scheme)
     structure = StructureValidator(file_path=path)
     err = structure.parse_error_line(error)
     assert correct in err[0]
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.º 6
0
 def test_is_valid_file_path(self, path, answer, mocker):
     mocker.patch.object(StructureValidator,
                         "load_data_from_file",
                         return_value=None)
     structure = StructureValidator(path)
     structure.scheme_name = None
     assert structure.is_valid_file_path() is answer
Exemplo n.º 7
0
    def get_display_name(file_path) -> str:
        """ Gets the file name from the pack yml file.

            :param file_path: The pack yml file path

            :rtype: ``str``
            :return
            The display name
        """
        struct = StructureValidator(file_path=file_path,
                                    is_new_file=True,
                                    predefined_scheme=find_type(file_path))
        file_data = struct.load_data_from_file()
        if 'display' in file_data:
            name = file_data.get('display', None)
        elif 'layout' in file_data and isinstance(file_data['layout'], dict):
            name = file_data['layout'].get('id')
        elif 'name' in file_data:
            name = file_data.get('name', None)
        elif 'TypeName' in file_data:
            name = file_data.get('TypeName', None)
        elif 'brandName' in file_data:
            name = file_data.get('brandName', None)
        elif 'id' in file_data:
            name = file_data.get('id', None)
        else:
            name = os.path.basename(file_path)
        return name
Exemplo n.º 8
0
 def test_validate_field_with_aliases__valid(self, pack: Pack):
     """
     Given
         Incident field with a valid Aliases field.
     When
         Validating the item.
     Then
         Ensures the schema is valid.
     """
     field_content = {
         'cliName':
         'mainfield',
         'name':
         'main field',
         'id':
         'incident',
         'content':
         True,
         'type':
         'longText',
         'Aliases': [{
             "cliName": "aliasfield",
             "type": "shortText",
             "name": "Alias Field",
         }],
     }
     incident_field: JSONBased = pack.create_incident_field(
         'incident-field-test',
         content=field_content,
     )
     structure = StructureValidator(incident_field.path)
     assert structure.is_valid_scheme()
Exemplo n.º 9
0
 def test_validate_field_with_aliases__invalid_type(self, pack: Pack):
     """
     Given
         - Incident field with a Aliases field that has an entry with an invalid type.
     When
         - Validating the item.
     Then
         - Ensures the schema is invalid.
     """
     field_content = {
         'cliName': 'mainfield',
         'name': 'main field',
         'id': 'incident',
         'content': True,
         'type': 'longText',
         'Aliases': [{
             "cliName": "alias field",
             "type": "UNKNOWN"
         }]
     }
     incident_field: JSONBased = pack.create_incident_field(
         'incident-field-test',
         content=field_content
     )
     structure = StructureValidator(incident_field.path)
     assert not structure.is_valid_scheme()
Exemplo n.º 10
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}.'
Exemplo n.º 11
0
 def test_is_file_valid(self, source, target, answer):
     try:
         copyfile(source, target)
         structure = StructureValidator(target)
         assert structure.is_valid_file() is answer
     finally:
         os.remove(target)
Exemplo n.º 12
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
Exemplo n.º 13
0
 def test_is_file_valid(self, source, target, answer, mocker):
     mocker.patch.object(BaseValidator, 'check_file_flags', return_value='')
     try:
         copyfile(source, target)
         structure = StructureValidator(target)
         assert structure.is_valid_file() is answer
     finally:
         os.remove(target)
 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
Exemplo n.º 15
0
 def test_release_notes_config_scheme(self, tmpdir,
                                      release_notes_config: dict,
                                      expected: bool):
     file_path: str = f'{tmpdir}/1_0_1.json'
     with open(file_path, 'w') as f:
         f.write(json.dumps(release_notes_config))
     validator = StructureValidator(file_path=file_path,
                                    predefined_scheme='releasenotesconfig')
     assert validator.is_valid_scheme() is expected
Exemplo n.º 16
0
    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 = ''
            structure.specific_validations = None
            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.º 17
0
    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 = ''
            structure.specific_validations = None
            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
Exemplo n.º 18
0
 def get_display_name(file_path):
     struct = StructureValidator(file_path=file_path)
     file_data = struct.load_data_from_file()
     if 'name' in file_data:
         name = file_data.get('name', None)
     elif 'TypeName' in file_data:
         name = file_data.get('TypeName', None)
     else:
         name = os.path.basename(file_path)
         print_error(f"Could not find name in {file_path}")
         # sys.exit(1)
     return name
Exemplo n.º 19
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 = 'layout'
        structure.file_path = file_path
        structure.current_file = current_file
        structure.old_file = old_file
        structure.prev_ver = 'master'
        structure.branch_name = ''
        structure.specific_validations = None
        return structure
Exemplo n.º 20
0
 def get_display_name(file_path):
     struct = StructureValidator(file_path=file_path, is_new_file=True)
     file_data = struct.load_data_from_file()
     if 'name' in file_data:
         name = file_data.get('name', None)
     elif 'TypeName' in file_data:
         name = file_data.get('TypeName', None)
     elif 'brandName' in file_data:
         name = file_data.get('brandName', None)
     else:
         name = os.path.basename(file_path)
     return name
Exemplo n.º 21
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 = 'dashboard'
        structure.file_path = file_path
        structure.current_file = current_file
        structure.old_file = old_file
        structure.prev_ver = 'master'
        structure.branch_name = ''
        structure.specific_validations = None
        return structure
Exemplo n.º 22
0
 def test_is_indicator_with_open_ended(self, pack: Pack):
     field_content = {
         'cliName': 'sanityname',
         'name': 'sanity name',
         'id': 'incident',
         'content': True,
         'type': 'multiSelect',
         'openEnded': True
     }
     incident_field: JSONBased = pack.create_indicator_field(
         'incident-field-test', content=field_content)
     structure = StructureValidator(incident_field.path)
     assert structure.is_valid_scheme()
Exemplo n.º 23
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.º 24
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.º 25
0
    def initiate_file_validator(self, validator_type, scheme_type):
        print_color('Starting validating files structure', LOG_COLORS.GREEN)

        structure = StructureValidator(file_path=str(self.output_file_name),
                                       predefined_scheme=scheme_type)
        validator = validator_type(structure)

        if structure.is_valid_file() and validator.is_valid_file(
                validate_rn=False):
            print_color('The files are valid', LOG_COLORS.GREEN)
            return 0

        else:
            print_color('The files are invalid', LOG_COLORS.RED)
            return 1
    def test_is_valid_indicator_type_from_version(self, pack, field_type,
                                                  from_version, expected):
        """
        Given
        - An indicator field, with its type

        When
        - Running valid_indicator_type_from_version on it.

        Then
        - Ensure if minimal version is needed, and the fromVersion of the indicator field does not satisfy the
          minimal condition, false is returned. Otherwise ensure true is returned.
        """
        indicator_field = pack.create_indicator_field(
            'incident_1', {
                'type': field_type,
                'cliName': 'testindicator',
                'version': -1,
                'fromVersion': from_version,
                'content': True,
                'group': INDICATOR_GROUP_NUMBER
            })
        structure = StructureValidator(indicator_field.path)
        validator = IndicatorFieldValidator(structure)
        assert validator.is_valid_indicator_type_from_version() == expected
Exemplo n.º 27
0
    def test_validate_readme_exists(self, repo, unified, remove_readme,
                                    validate_all, expected_result):
        """
              Given:
                  - An integration yml that was added or modified to validate

              When:
                    All the tests occur twice for unified integrations = [True - False]
                  - The integration is missing a readme.md file in the same folder
                  - The integration has a readme.md file in the same folder
                  - The integration is missing a readme.md file in the same folder but has not been changed or added
                      (This check is for backward compatibility)

              Then:
                  - Ensure readme exists and validation fails
                  - Ensure readme exists and validation passes
                  - Ensure readme exists and validation passes
        """
        read_me_pack = repo.create_pack('README_test')
        script = read_me_pack.create_script('script1', create_unified=unified)

        structure_validator = StructureValidator(script.yml.path)
        script_validator = ScriptValidator(structure_validator,
                                           validate_all=validate_all)
        if remove_readme:
            os.remove(script.readme.path)
        assert script_validator.validate_readme_exists(
            script_validator.validate_all) is expected_result
Exemplo n.º 28
0
 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
         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_system_flag() is answer
Exemplo n.º 29
0
 def test_matching_cliname_regex(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 = ''
         structure.specific_validations = None
         validator = FieldBaseValidator(structure, set(), set())
         validator.current_file = current_file
         assert validator.is_matching_cli_name_regex()
Exemplo n.º 30
0
 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 = ''
         structure.specific_validations = None
         validator = FieldBaseValidator(structure, set(), {'id'})
         validator.current_file = current_file
         assert not validator.is_valid_cli_name()