1# -*- coding: utf-8 -*- 2# Copyright (C) 2018-2021 Greenbone Networks GmbH 3# 4# SPDX-License-Identifier: GPL-3.0-or-later 5# 6# This program is free software: you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation, either version 3 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18 19from gvm.errors import InvalidArgumentType, RequiredArgument 20from gvm.protocols.gmpv208 import FilterType 21 22 23class GmpCreateFilterTestMixin: 24 def test_all_available_filters_types_correct(self): 25 for filter_type in list(FilterType): 26 self.gmp.create_filter( 27 name='f1', 28 term='sort-reverse=threat first=1 rows=1000', 29 filter_type=filter_type, 30 ) 31 32 self.connection.send.has_been_called_with( 33 '<create_filter>' 34 '<name>f1</name>' 35 '<term>sort-reverse=threat first=1 rows=1000</term>' 36 f'<type>{filter_type.value}</type>' 37 '</create_filter>' 38 ) 39 40 def test_create_filter_invalid_filter_type(self): 41 with self.assertRaises(InvalidArgumentType): 42 self.gmp.create_filter( 43 name='f1', 44 term='sort-reverse=threat result_hosts_only=1 ' 45 'notes=1 overrides=1 levels=hml first=1 rows=1000', 46 filter_type='foo', 47 ) 48 49 def test_create_filter_no_filter_type(self): 50 self.gmp.create_filter( 51 name='f1', 52 term='sort-reverse=threat result_hosts_only=1 ' 53 'notes=1 overrides=1 levels=hml first=1 rows=1000', 54 comment='foo', 55 ) 56 57 self.connection.send.has_been_called_with( 58 '<create_filter>' 59 '<name>f1</name>' 60 '<comment>foo</comment>' 61 '<term>sort-reverse=threat result_hosts_only=1 notes=1 ' 62 'overrides=1 levels=hml first=1 rows=1000</term>' 63 '</create_filter>' 64 ) 65 66 def test_create_filter(self): 67 self.gmp.create_filter( 68 name='f1', 69 term='sort-reverse=threat result_hosts_only=1 ' 70 'notes=1 overrides=1 levels=hml first=1 rows=1000', 71 filter_type=FilterType.TASK, 72 comment='foo', 73 ) 74 75 self.connection.send.has_been_called_with( 76 '<create_filter>' 77 '<name>f1</name>' 78 '<comment>foo</comment>' 79 '<term>sort-reverse=threat result_hosts_only=1 notes=1 ' 80 'overrides=1 levels=hml first=1 rows=1000</term>' 81 '<type>task</type>' 82 '</create_filter>' 83 ) 84 85 def test_create_filter_without_term(self): 86 self.gmp.create_filter( 87 name='f1', 88 filter_type=FilterType.TASK, 89 comment='foo', 90 ) 91 92 self.connection.send.has_been_called_with( 93 '<create_filter>' 94 '<name>f1</name>' 95 '<comment>foo</comment>' 96 '<type>task</type>' 97 '</create_filter>' 98 ) 99 100 def test_create_filter_make_unique(self): 101 with self.assertRaises(TypeError): 102 self.gmp.create_filter( 103 name='f1', 104 term='sort-reverse=threat result_hosts_only=1 ' 105 'notes=1 overrides=1 levels=hml first=1 rows=1000', 106 filter_type=FilterType.TASK, 107 make_unique=True, 108 comment='foo', 109 ) 110 111 def test_create_filter_missing_name(self): 112 with self.assertRaises(RequiredArgument): 113 self.gmp.create_filter('') 114 115 with self.assertRaises(RequiredArgument): 116 self.gmp.create_filter(None) 117