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

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Fri Nov 14 10:37:17 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 9133904..f846d48 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -35,7 +35,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
 from kimchi.utils import kimchi_log
 
@@ -169,29 +170,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