File: test_errors.py

package info (click to toggle)
ospd-openvas 22.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,648 kB
  • sloc: python: 14,197; xml: 1,913; makefile: 45; sh: 29
file content (60 lines) | stat: -rw-r--r-- 1,779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2014-2023 Greenbone AG
#
# SPDX-License-Identifier: AGPL-3.0-or-later

"""Test module for OspdCommandError class"""

import unittest

from ospd.errors import OspdError, OspdCommandError, RequiredArgument


class OspdCommandErrorTestCase(unittest.TestCase):
    def test_is_ospd_error(self):
        e = OspdCommandError('message')
        self.assertIsInstance(e, OspdError)

    def test_default_params(self):
        e = OspdCommandError('message')

        self.assertEqual('message', e.message)
        self.assertEqual(400, e.status)
        self.assertEqual('osp', e.command)

    def test_constructor(self):
        e = OspdCommandError('message', 'command', 304)

        self.assertEqual('message', e.message)
        self.assertEqual('command', e.command)
        self.assertEqual(304, e.status)

    def test_string_conversion(self):
        e = OspdCommandError('message foo bar', 'command', 304)

        self.assertEqual('message foo bar', str(e))

    def test_as_xml(self):
        e = OspdCommandError('message')

        self.assertEqual(
            b'<osp_response status="400" status_text="message" />', e.as_xml()
        )


class RequiredArgumentTestCase(unittest.TestCase):
    def test_raise_exception(self):
        with self.assertRaises(RequiredArgument) as cm:
            raise RequiredArgument('foo', 'bar')

        ex = cm.exception
        self.assertEqual(ex.function, 'foo')
        self.assertEqual(ex.argument, 'bar')

    def test_string_conversion(self):
        ex = RequiredArgument('foo', 'bar')
        self.assertEqual(str(ex), 'foo: Argument bar is required')

    def test_is_ospd_error(self):
        e = RequiredArgument('foo', 'bar')
        self.assertIsInstance(e, OspdError)