tests.unit.test_decorators   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 71
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A TestKytosAuth._expected_response() 0 7 1
A TestKytosAuth.test_authenticate_success_and_fail() 0 24 1
A TestKytosAuth.test__call__() 0 15 1
A TestKytosAuth.setUp() 0 9 1
1
"""kytos.utils.decorators tests."""
2 1
import unittest
3 1
from unittest.mock import MagicMock, patch
4
5 1
from kytos.utils.config import KytosConfig
6 1
from kytos.utils.decorators import kytos_auth
7
8
9
# pylint: disable=unnecessary-dunder-call
10 1
class TestKytosAuth(unittest.TestCase):
11
    """Test the decorator kytos_auth."""
12
13 1
    def setUp(self):
14
        """Execute steps before each tests."""
15 1
        self.kytos_auth = kytos_auth(MagicMock())
16 1
        self.kytos_auth.__get__(MagicMock(), MagicMock())
17
18 1
        config = KytosConfig('/tmp/.kytosrc').config
19 1
        config.set('auth', 'user', 'username')
20 1
        config.set('auth', 'token', 'hash')
21 1
        self.kytos_auth.config = config
22
23 1
    @staticmethod
24 1
    def _expected_response(status_code):
25
        """Expected response mock."""
26 1
        response = MagicMock()
27 1
        response.json.return_value = {"hash": "hash"}
28 1
        response.status_code = status_code
29 1
        return response
30
31 1
    @patch('requests.get')
32 1
    @patch('configparser.ConfigParser.set')
33 1
    @patch('configparser.ConfigParser.get', return_value='value')
34 1
    @patch('configparser.ConfigParser.has_option', return_value=False)
35 1
    @patch('kytos.utils.decorators.getpass', return_value='password')
36 1
    @patch('builtins.input', return_value='username')
37 1
    def test__call__(self, *args):
38
        """Test __call__ method."""
39 1
        (_, _, _, _, mock_set, mock_requests_get) = args
40 1
        mock_requests_get.return_value = self._expected_response(201)
41
42 1
        self.kytos_auth.__call__()
43
44 1
        mock_set.assert_any_call('auth', 'user', 'username')
45 1
        mock_set.assert_any_call('auth', 'token', 'hash')
46
47 1
    @patch('requests.get')
48 1
    @patch('kytos.utils.config.KytosConfig.save_token')
49 1
    @patch('builtins.input', return_value='username')
50 1
    @patch('kytos.utils.decorators.getpass', return_value='password')
51 1
    def test_authenticate_success_and_fail(self, *args):
52
        """Test authenticate method.
53
54
        This test check the fail and success cases. At the first, the 401
55
        status code will cause a fail and after that the 201 status code will
56
        test the success case. The authenticate has to be called twice, once
57
        the test `test_authenticate_success_and_fail` calls `authenticate`
58
        the first time, and the fail case will cause the second call."""
59 1
        (_, _, _, mock_requests_get) = args
60 1
        mock_requests_get.side_effect = [self._expected_response(401),
61
                                         self._expected_response(201)]
62
63 1
        authenticate = self.kytos_auth.authenticate
64 1
        self.kytos_auth.authenticate = MagicMock(side_effect=authenticate)
65
66 1
        self.kytos_auth.authenticate()
67
68 1
        call_count = self.kytos_auth.authenticate.call_count
69
70
        self.assertEqual(call_count, 2)
71