]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/tests/test_auth.py
import ceph 16.2.7
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_auth.py
index 6f1d2a084ec461741c4bb270e5c70f8069d12b7a..d9755de98e45ad1f8af3528fcc0cc9d2c9bbb1ce 100644 (file)
@@ -1,7 +1,15 @@
 import unittest
+from unittest.mock import Mock, patch
 
 from .. import mgr
+from ..controllers.auth import Auth
 from ..services.auth import JwtManager
+from ..tests import ControllerTestCase
+
+mgr.get_module_option.return_value = JwtManager.JWT_TOKEN_TTL
+mgr.get_store.return_value = 'jwt_secret'
+mgr.ACCESS_CTRL_DB = Mock()
+mgr.ACCESS_CTRL_DB.get_attempt.return_value = 1
 
 
 class JwtManagerTest(unittest.TestCase):
@@ -18,3 +26,41 @@ class JwtManagerTest(unittest.TestCase):
         self.assertIsInstance(decoded_token, dict)
         self.assertEqual(decoded_token['iss'], 'ceph-dashboard')
         self.assertEqual(decoded_token['username'], 'my-username')
+
+
+class AuthTest(ControllerTestCase):
+
+    @classmethod
+    def setup_server(cls):
+        cls.setup_controllers([Auth])
+
+    def test_request_not_authorized(self):
+        self.setup_controllers([Auth], cp_config={'tools.authenticate.on': True})
+        self._post('/api/auth/logout')
+        self.assertStatus(401)
+
+    @patch('dashboard.controllers.auth.JwtManager.gen_token', Mock(return_value='my-token'))
+    @patch('dashboard.controllers.auth.AuthManager.authenticate', Mock(return_value={
+        'permissions': {'rgw': ['read']},
+        'pwdExpirationDate': 1000000,
+        'pwdUpdateRequired': False
+    }))
+    def test_login(self):
+        self._post('/api/auth', {'username': 'my-user', 'password': 'my-pass'})
+        self.assertStatus(201)
+        self.assertJsonBody({
+            'token': 'my-token',
+            'username': 'my-user',
+            'permissions': {'rgw': ['read']},
+            'pwdExpirationDate': 1000000,
+            'sso': False,
+            'pwdUpdateRequired': False
+        })
+
+    @patch('dashboard.controllers.auth.JwtManager', Mock())
+    def test_logout(self):
+        self._post('/api/auth/logout')
+        self.assertStatus(200)
+        self.assertJsonBody({
+            'redirect_url': '#/login'
+        })