[Kimchi-devel] [PATCHv1 4/4] Fix test cases for authentication

lvroyce0210 at gmail.com lvroyce0210 at gmail.com
Mon Oct 20 13:52:20 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

Authentication function moved to class,
so build a fake user class to cover test.

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
 tests/utils.py | 43 +++++++++++++++++++++++++++++--------------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/tests/utils.py b/tests/utils.py
index 140bb1d..4215bce 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -34,7 +34,8 @@ from lxml import etree
 
 import kimchi.mockmodel
 import kimchi.server
-from kimchi.config import paths
+from kimchi.config import config, paths
+from kimchi.auth import User, USER_NAME, USER_GROUPS, USER_ROLES, tabs
 from kimchi.exception import OperationFailed
 
 _ports = {}
@@ -167,29 +168,43 @@ def get_remote_iso_path():
     return remote_path
 
 
-def patch_auth(sudo=True):
-    """
-    Override the authenticate function with a simple test against an
-    internal dict of users and passwords.
-    """
+class FakeUser(User):
+    auth_type = "fake"
+    sudo = True
+
+    def __init__(self, username):
+        self.user = {}
+        self.user[USER_NAME] = username
+        self.user[USER_GROUPS] = None
+        self.user[USER_ROLES] = dict.fromkeys(tabs, 'user')
 
-    def _get_groups(self):
+    def get_groups(self):
         return ['groupA', 'groupB', 'wheel']
 
-    def _has_sudo(self, result):
-        result.value = sudo
+    def get_roles(self):
+        if self.sudo:
+            self.user[USER_ROLES] = dict.fromkeys(tabs, 'admin')
+        return self.user[USER_ROLES]
+
+    def get_user(self):
+        return self.user
 
-    def _authenticate(username, password, service="passwd"):
+    @staticmethod
+    def authenticate(username, password, service="passwd"):
         try:
             return kimchi.mockmodel.fake_user[username] == password
         except KeyError, e:
             raise OperationFailed("KCHAUTH0001E", {'username': 'username',
                                                    'code': e.message})
 
-    import kimchi.auth
-    kimchi.auth.authenticate = _authenticate
-    kimchi.auth.User.get_groups = _get_groups
-    kimchi.auth.User._has_sudo = _has_sudo
+
+def patch_auth(sudo=True):
+    """
+    Override the authenticate function with a simple test against an
+    internal dict of users and passwords.
+    """
+    config.set("authentication", "method", "fake")
+    FakeUser.sudo = sudo
 
 
 def normalize_xml(xml_str):
-- 
1.8.3.2




More information about the Kimchi-devel mailing list