1# Copyright (C) 2014-2021 Greenbone Networks GmbH
2#
3# SPDX-License-Identifier: AGPL-3.0-or-later
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Affero General Public License as
7# published by the Free Software Foundation, either version 3 of the
8# 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 Affero General Public License for more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18from unittest import TestCase
19
20from ospd.command.registry import get_commands, remove_command
21from ospd.command.command import BaseCommand
22
23
24class BaseCommandTestCase(TestCase):
25    def test_auto_register(self):
26        commands = get_commands()
27        before = len(commands)
28
29        class Foo(BaseCommand):
30            name = "foo"
31
32            def handle_xml(self, xml):
33                pass
34
35        after = len(commands)
36
37        try:
38
39            self.assertEqual(before + 1, after)
40
41            c_dict = {c.name: c for c in commands}
42
43            self.assertIn('foo', c_dict)
44            self.assertIs(c_dict['foo'], Foo)
45        finally:
46            remove_command(Foo)
47
48    def test_basic_properties(self):
49        class Foo(BaseCommand):
50            name = "foo"
51            attributes = {'lorem': 'ipsum'}
52            elements = {'foo': 'bar'}
53            description = 'bar'
54
55            def handle_xml(self, xml):
56                pass
57
58        try:
59            f = Foo({})
60
61            self.assertEqual(f.get_name(), 'foo')
62            self.assertEqual(f.get_description(), 'bar')
63            self.assertEqual(f.get_attributes(), {'lorem': 'ipsum'})
64            self.assertEqual(f.get_elements(), {'foo': 'bar'})
65        finally:
66            remove_command(Foo)
67
68    def test_as_dict(self):
69        class Foo(BaseCommand):
70            name = "foo"
71            attributes = {'lorem': 'ipsum'}
72            elements = {'foo': 'bar'}
73            description = 'bar'
74
75            def handle_xml(self, xml):
76                pass
77
78        try:
79            f = Foo({})
80
81            f_dict = f.as_dict()
82
83            self.assertEqual(f_dict['name'], 'foo')
84            self.assertEqual(f_dict['description'], 'bar')
85            self.assertEqual(f_dict['attributes'], {'lorem': 'ipsum'})
86            self.assertEqual(f_dict['elements'], {'foo': 'bar'})
87        finally:
88            remove_command(Foo)
89