[Kimchi-devel] [PATCH 1/4] Override only the updated "User" methods in "patch_auth"

Crístian Viana vianac at linux.vnet.ibm.com
Wed Feb 26 18:09:50 UTC 2014


The function "patch_auth" creates a fake class to override the existing
kimchi.auth.User with a few updated methods. That attribution overrides
the entire User class along with all its methods.

In order to avoid side effects, override only the methods we need to
change in the "User" class. Other methods not related to "patch_auth"
will not be affected now.
---
 tests/utils.py | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)

diff --git a/tests/utils.py b/tests/utils.py
index 18b707c..faf6730 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -152,25 +152,12 @@ def patch_auth(sudo=True):
     Override the authenticate function with a simple test against an
     internal dict of users and passwords.
     """
-    USER_ID = 'userid'
-    USER_GROUPS = 'groups'
-    USER_SUDO = 'sudo'
 
-    class _User(object):
-        def __init__(self, userid):
-            self.user = {}
-            self.user[USER_ID] = userid
-            self.user[USER_GROUPS] = None
-            self.user[USER_SUDO] = sudo
+    def _get_groups(self):
+        return None
 
-        def get_groups(self):
-            return self.user[USER_GROUPS]
-
-        def has_sudo(self):
-            return self.user[USER_SUDO]
-
-        def get_user(self):
-            return self.user
+    def _has_sudo(self):
+        return sudo
 
     def _authenticate(username, password, service="passwd"):
         try:
@@ -181,7 +168,8 @@ def patch_auth(sudo=True):
 
     import kimchi.auth
     kimchi.auth.authenticate = _authenticate
-    kimchi.auth.User = _User
+    kimchi.auth.User.get_groups = _get_groups
+    kimchi.auth.User.has_sudo = _has_sudo
 
 
 def normalize_xml(xml_str):
-- 
1.8.5.3




More information about the Kimchi-devel mailing list