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_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.º 3
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_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
 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.º 8
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()
Exemplo n.º 9
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.º 10
0
    def test_is_valid_from_version_field(self, pack, min_version: LooseVersion, from_version: str, expected: bool):
        """
        Given
        - A field.

        When
        - Validating the expected version is meeting the expected minimal version.

        Then
        - Ensure the expected bool is returned according to whether the condition above is satisfied.
        """
        indicator_field = pack.create_indicator_field('incident_1', {'type': 'html', 'fromVersion': from_version})
        structure = StructureValidator(indicator_field.path)
        validator = FieldBaseValidator(structure, set(), set())
        assert validator.is_valid_from_version_field(min_version, reason_for_min_version='') == expected
Exemplo n.º 11
0
    def test_validate_no_empty_selected_values_value_indicator(self, pack):
        """
        Given
        - An indicator field.

        When
        - Validating its selectValues do no contain empty values.

        Then
        - Ensure false is returned.
        """
        indicator_field = pack.create_indicator_field('ind_1', {'type': 'some-type', 'cliName': 'testindicator',
                                                                'version': -1, 'fromVersion': '5.0.0',
                                                                'content': True, 'group': INDICATOR_GROUP_NUMBER,
                                                                'selectValues': [""]})
        structure = StructureValidator(indicator_field.path)
        validator = FieldBaseValidator(structure, {'some-type'}, set())
        assert not validator.does_not_have_empty_select_values()
Exemplo n.º 12
0
    def test_is_valid_marketplaces_in_aliased_field(self, pack, marketplaces: List[str], expected: bool):
        """
        Given
        - A field with aliases values.

        When
        - Validating the aliased fields are valid.

        Then
        - Ensure the expected bool is returned according to whether the marketplaces of the aliased fileds are valid.
        """

        tested_field = pack.create_incident_field('tested_field', {'Aliases': [{'cliName': 'aliased_field'}]})
        incident_aliased_field = {'name': 'incident_aliased_field', 'cliName': 'aliasedfield'}
        if marketplaces:
            incident_aliased_field['marketplaces'] = marketplaces

        mocked_id_set = {
            'IncidentFields': [{'incident_aliased_field': incident_aliased_field}]
        }
        structure = StructureValidator(tested_field.path)
        validator = FieldBaseValidator(structure, set(), set(), id_set_file=mocked_id_set)
        assert validator.is_aliased_fields_are_valid() == expected
 def test_is_valid_required(self, required, is_valid):
     structure = StructureValidator("")
     structure.current_file = {"required": required}
     validator = FieldBaseValidator(structure, set(), set())
     assert validator.is_valid_required() == is_valid, f'is_valid_required({required})' \
                                                       f' returns {not is_valid}.'
 def test_is_valid_version(self, version, is_valid):
     structure = StructureValidator("")
     structure.current_file = {"version": version}
     validator = FieldBaseValidator(structure, set(), set())
     assert validator.is_valid_version(
     ) == is_valid, f'is_valid_version({version}) returns {not is_valid}.'