1
|
|
|
# Copyright (C) 2020 Greenbone Networks GmbH |
2
|
|
|
# |
3
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later |
4
|
|
|
# |
5
|
|
|
# This program is free software; you can redistribute it and/or |
6
|
|
|
# modify it under the terms of the GNU General Public License |
7
|
|
|
# as published by the Free Software Foundation; either version 2 |
8
|
|
|
# of the License, or (at your option) any later version. |
9
|
|
|
# |
10
|
|
|
# This program is distributed in the hope that it will be useful, |
11
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
# GNU General Public License for more details. |
14
|
|
|
# |
15
|
|
|
# You should have received a copy of the GNU General Public License |
16
|
|
|
# along with this program; if not, write to the Free Software |
17
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
18
|
|
|
|
19
|
|
|
from unittest import TestCase |
20
|
|
|
|
21
|
|
|
from ospd.command.registry import get_commands, remove_command |
22
|
|
|
from ospd.command.command import BaseCommand |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class BaseCommandTestCase(TestCase): |
26
|
|
|
def test_auto_register(self): |
27
|
|
|
commands = get_commands() |
28
|
|
|
before = len(commands) |
29
|
|
|
|
30
|
|
|
class Foo(BaseCommand): |
31
|
|
|
name = "foo" |
32
|
|
|
|
33
|
|
|
def handle_xml(self, xml): |
34
|
|
|
pass |
35
|
|
|
|
36
|
|
|
after = len(commands) |
37
|
|
|
|
38
|
|
|
try: |
39
|
|
|
|
40
|
|
|
self.assertEqual(before + 1, after) |
41
|
|
|
|
42
|
|
|
c_dict = {c.name: c for c in commands} |
43
|
|
|
|
44
|
|
|
self.assertIn('foo', c_dict) |
45
|
|
|
self.assertIs(c_dict['foo'], Foo) |
46
|
|
|
finally: |
47
|
|
|
remove_command(Foo) |
48
|
|
|
|
49
|
|
|
def test_basic_properties(self): |
50
|
|
|
class Foo(BaseCommand): |
51
|
|
|
name = "foo" |
52
|
|
|
attributes = {'lorem': 'ipsum'} |
53
|
|
|
elements = {'foo': 'bar'} |
54
|
|
|
description = 'bar' |
55
|
|
|
|
56
|
|
|
def handle_xml(self, xml): |
57
|
|
|
pass |
58
|
|
|
|
59
|
|
|
try: |
60
|
|
|
f = Foo({}) |
61
|
|
|
|
62
|
|
|
self.assertEqual(f.get_name(), 'foo') |
63
|
|
|
self.assertEqual(f.get_description(), 'bar') |
64
|
|
|
self.assertEqual(f.get_attributes(), {'lorem': 'ipsum'}) |
65
|
|
|
self.assertEqual(f.get_elements(), {'foo': 'bar'}) |
66
|
|
|
finally: |
67
|
|
|
remove_command(Foo) |
68
|
|
|
|
69
|
|
|
def test_as_dict(self): |
70
|
|
|
class Foo(BaseCommand): |
71
|
|
|
name = "foo" |
72
|
|
|
attributes = {'lorem': 'ipsum'} |
73
|
|
|
elements = {'foo': 'bar'} |
74
|
|
|
description = 'bar' |
75
|
|
|
|
76
|
|
|
def handle_xml(self, xml): |
77
|
|
|
pass |
78
|
|
|
|
79
|
|
|
try: |
80
|
|
|
f = Foo({}) |
81
|
|
|
|
82
|
|
|
f_dict = f.as_dict() |
83
|
|
|
|
84
|
|
|
self.assertEqual(f_dict['name'], 'foo') |
85
|
|
|
self.assertEqual(f_dict['description'], 'bar') |
86
|
|
|
self.assertEqual(f_dict['attributes'], {'lorem': 'ipsum'}) |
87
|
|
|
self.assertEqual(f_dict['elements'], {'foo': 'bar'}) |
88
|
|
|
finally: |
89
|
|
|
remove_command(Foo) |
90
|
|
|
|