Passed
Branch master (45645d)
by Humberto
03:09
created

TestMain.test_verify_api_urls()   B

Complexity

Conditions 1

Size

Total Lines 41
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 41
rs 8.9439
c 0
b 0
f 0
cc 1
nop 1
1
"""Module to test the main napp file."""
2
from unittest import TestCase
3
from unittest.mock import Mock
4
5
from kytos.core import Controller
6
from kytos.core.config import KytosConfig
7
8
from napps.kytos.topology.main import Main
9
10
11
class TestMain(TestCase):
12
    """Test the Main class."""
13
14
    def setUp(self):
15
        """Execute steps before each tests.
16
17
        Set the server_name_url_url from kytos/topology
18
        """
19
        self.server_name_url = 'http://localhost:8181/api/kytos/topology'
20
        self.napp = Main(self.get_controller_mock())
21
22
    def test_get_event_listeners(self):
23
        """Verify all event listeners registered."""
24
        expected_events = ['kytos/core.shutdown',
25
                           'kytos/core.shutdown.kytos/topology',
26
                           '.*.interface.is.nni',
27
                           '.*.connection.lost',
28
                           '.*.switch.interface.created',
29
                           '.*.switch.interface.deleted',
30
                           '.*.switch.interface.link_down',
31
                           '.*.switch.interface.link_up',
32
                           '.*.switch.(new|reconnected)',
33
                           'kytos/topology.*.metadata.*']
34
        actual_events = self.napp.listeners()
35
        self.assertEqual(expected_events, actual_events)
36
37
    def test_verify_api_urls(self):
38
        """Verify all APIs registered."""
39
        expected_urls = [
40
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/interfaces'),
41
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/switches'),
42
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/links'),
43
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/'),
44
         ({'key': '[key]', 'interface_id': '[interface_id]'},
45
          {'OPTIONS', 'DELETE'},
46
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata/<key>'),
47
         ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'},
48
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'),
49
         ({'interface_id': '[interface_id]'}, {'GET', 'OPTIONS', 'HEAD'},
50
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'),
51
         ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'},
52
          '/api/kytos/topology/v3/interfaces/<interface_id>/disable'),
53
         ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'},
54
          '/api/kytos/topology/v3/interfaces/<interface_id>/enable'),
55
         ({'dpid': '[dpid]', 'key': '[key]'}, {'OPTIONS', 'DELETE'},
56
          '/api/kytos/topology/v3/switches/<dpid>/metadata/<key>'),
57
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
58
          '/api/kytos/topology/v3/switches/<dpid>/metadata'),
59
         ({'dpid': '[dpid]'}, {'GET', 'OPTIONS', 'HEAD'},
60
          '/api/kytos/topology/v3/switches/<dpid>/metadata'),
61
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
62
          '/api/kytos/topology/v3/switches/<dpid>/disable'),
63
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
64
          '/api/kytos/topology/v3/switches/<dpid>/enable'),
65
         ({'link_id': '[link_id]', 'key': '[key]'}, {'OPTIONS', 'DELETE'},
66
          '/api/kytos/topology/v3/links/<link_id>/metadata/<key>'),
67
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
68
          '/api/kytos/topology/v3/links/<link_id>/metadata'),
69
         ({'link_id': '[link_id]'}, {'GET', 'OPTIONS', 'HEAD'},
70
          '/api/kytos/topology/v3/links/<link_id>/metadata'),
71
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
72
          '/api/kytos/topology/v3/links/<link_id>/disable'),
73
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
74
          '/api/kytos/topology/v3/links/<link_id>/enable')]
75
76
        urls = self.get_napp_urls(self.napp)
77
        self.assertEqual(expected_urls, urls)
78
79
    @staticmethod
80
    def get_controller_mock():
81
        """Return a controller mock."""
82
        options = KytosConfig().options['daemon']
83
        controller = Controller(options)
84
        controller.log = Mock()
85
        return controller
86
87
    @staticmethod
88
    def get_napp_urls(napp):
89
        """Return the kytos/topology urls.
90
91
        The urls will be like:
92
93
        urls = [
94
            (options, methods, url)
95
        ]
96
97
        """
98
        controller = napp.controller
99
        controller.api_server.register_napp_endpoints(napp)
100
101
        urls = []
102
        for rule in controller.api_server.app.url_map.iter_rules():
103
            options = {}
104
            for arg in rule.arguments:
105
                options[arg] = "[{0}]".format(arg)
106
107
            if f'{napp.username}/{napp.name}' in str(rule):
108
                urls.append((options, rule.methods, f'{str(rule)}'))
109
110
        return urls
111
112
    @staticmethod
113
    def get_app_test_client(napp):
114
        """Return a flask api test client."""
115
        napp.controller.api_server.register_napp_endpoints(napp)
116
        return napp.controller.api_server.app.test_client()
117