Test Failed
Pull Request — master (#277)
by Gleyberson
01:32
created

TestKytosAuth.test_authenticate()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 2
dl 0
loc 12
rs 9.95
c 0
b 0
f 0
1
"""kytos.utils.decorators tests."""
2
import unittest
3
from unittest.mock import MagicMock, patch
4
5
from kytos.utils.config import KytosConfig
6
from kytos.utils.decorators import kytos_auth
7
8
9
class TestKytosAuth(unittest.TestCase):
10
    """Test the decorator kytos_auth."""
11
12
    def setUp(self):
13
        """Execute steps before each tests."""
14
        self.kytos_auth = kytos_auth(MagicMock())
15
        self.kytos_auth.__get__(MagicMock(), MagicMock())
16
17
        config = KytosConfig('/tmp/.kytosrc').config
18
        config.set('auth', 'user', 'username')
19
        config.set('auth', 'token', 'hash')
20
        self.kytos_auth.config = config
21
22
    @staticmethod
23
    def _expected_response(status_code):
24
        """Expected response mock."""
25
        response = MagicMock()
26
        response.json.return_value = {"hash": "hash"}
27
        response.status_code = status_code
28
        return response
29
30
    @patch('requests.get')
31
    @patch('configparser.ConfigParser.set')
32
    @patch('configparser.ConfigParser.get', return_value='value')
33
    @patch('configparser.ConfigParser.has_option', return_value=False)
34
    @patch('kytos.utils.decorators.getpass', return_value='password')
35
    @patch('builtins.input', return_value='username')
36
    def test__call__(self, *args):
37
        """Test __call__ method."""
38
        (_, _, _, _, mock_set, mock_requests_get) = args
39
        mock_requests_get.return_value = self._expected_response(201)
40
41
        self.kytos_auth.__call__()
42
43
        mock_set.assert_any_call('auth', 'user', 'username')
44
        mock_set.assert_any_call('auth', 'token', 'hash')
45
46
    @patch('sys.exit')
47
    @patch('requests.get')
48
    @patch('kytos.utils.config.KytosConfig.save_token')
49
    @patch('kytos.utils.decorators.getpass', return_value='password')
50
    def test_authenticate(self, *args):
51
        """Test authenticate method."""
52
        (_, mock_save_token, mock_requests_get, _) = args
53
        mock_requests_get.return_value = self._expected_response(401)
54
55
        self.kytos_auth.authenticate()
56
57
        mock_save_token.assert_not_called()
58