Compact code and unittests for astakos client
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Thu, 7 Feb 2013 14:06:55 +0000 (16:06 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Thu, 7 Feb 2013 14:06:55 +0000 (16:06 +0200)
kamaki/cli/commands/test_cli.py
kamaki/clients/astakos.py
kamaki/clients/tests/__init__.py
kamaki/clients/tests/astakos.py

index 8b1fe0c..04dcba1 100644 (file)
@@ -44,12 +44,15 @@ _commands = [test_cmds]
 
 class _test_init(_command_init):
 
-    def main(self, client, method=None):
+    def _run(self, client, method=None):
         if method:
             tests._main([client, method], config=self.config)
         else:
             tests._main([client], config=self.config)
 
+    def main(self, client, method=None):
+        return self._run(client, method)
+
 
 @command(test_cmds)
 class test_error(_test_init):
@@ -87,7 +90,7 @@ class test_all(_test_init):
     @errors.generic.all
     def _run(self):
         for client in ('pithos', 'cyclades', 'image', 'astakos'):
-            super(self.__class__, self).main(client)
+            super(self.__class__, self)._run(client)
 
     def main(self):
         self._run()
@@ -99,7 +102,7 @@ class test_pithos(_test_init):
 
     @errors.generic.all
     def _run(self, method=None):
-        super(self.__class__, self).main('pithos', method)
+        super(self.__class__, self)._run('pithos', method)
 
     def main(self, method=None):
         self._run(method)
@@ -111,7 +114,7 @@ class test_cyclades(_test_init):
 
     @errors.generic.all
     def _run(self, method=None):
-        super(self.__class__, self).main('cyclades', method)
+        super(self.__class__, self)._run('cyclades', method)
 
     def main(self, method=None):
         self._run(method)
@@ -123,7 +126,7 @@ class test_image(_test_init):
 
     @errors.generic.all
     def _run(self, method=None):
-        super(self.__class__, self).main('image', method)
+        super(self.__class__, self)._run('image', method)
 
     def main(self, method=None):
         self._run(method)
@@ -135,7 +138,7 @@ class test_astakos(_test_init):
 
     @errors.generic.all
     def _run(self, method=None):
-        super(self.__class__, self).main('astakos', method)
+        super(self.__class__, self)._run('astakos', method)
 
     def main(self, method=None):
         self._run(method)
index 874c85c..49da331 100644 (file)
@@ -49,8 +49,11 @@ class AstakosClient(Client):
         :returns: (dict) authentication information
         """
         self.token = token or self.token
-        self._cache[token] = self.get('/im/authenticate')
-        return self._cache[token].json
+        self._cache[token] = self.get('/im/authenticate').json
+        return self._cache[token]
+
+    def list(self):
+        return self._cache.values()
 
     def _user_info(self, token=None):
         token = token or self.token
@@ -59,26 +62,6 @@ class AstakosClient(Client):
         except KeyError:
             return self.authenticate(token)
 
-    def uuid(self, token=None):
-        return self._user_info(token)['uuid'].strip()
-
-    def name(self, token=None):
-        return self._user_info(token)['name'].strip()
-
-    def username(self, token=None):
-        return self._user_info(token)['username'].strip()
-
-    def token_created(self, token=None):
-        return self._user_info(token)['auth_token_created'].strip()
-
-    def token_expires(self, token=None):
-        return self._user_info(token)['auth_token_expires'].strip()
-
-    def email(self, token=None):
-        return self._user_info(token)['email'].strip()
-
-    def id(self, token=None):
-        """Internal reference for Astakos objects (not a unique user id)
-        For a unique identifier use uuid
-        """
-        return self._user_info(token)['id'].strip()
+    def term(self, key, token=None):
+        """Get (cached) term, from user credentials"""
+        return self._user_info(token)[key]
index 151486e..c342f0e 100644 (file)
@@ -31,7 +31,8 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from unittest import TestCase, TestSuite, makeSuite, TextTestRunner
+import inspect
+from unittest import TestCase, TestSuite, TextTestRunner
 from argparse import ArgumentParser
 from sys import stdout
 from progress.bar import ShadyBar
@@ -45,6 +46,7 @@ def _add_value(foo, value):
         return foo(self, value)
     return wrap
 
+
 class Generic(TestCase):
 
     _waits = []
@@ -128,7 +130,7 @@ class Generic(TestCase):
                 self.assertEqual(unicode(v), unicode(d2[k]))
 
     def test_000(self):
-        import inspect
+        print('')
         methods = [method for method in inspect.getmembers(
             self,
             predicate=inspect.ismethod)\
@@ -177,10 +179,8 @@ def _main(argv, config=None):
         suiteFew.addTest(Image(test_method, config))
     if len(argv) == 0 or argv[0] == 'astakos':
         from kamaki.clients.tests.astakos import Astakos
-        if len(argv) == 1:
-            suiteFew.addTest(makeSuite(Astakos, config))
-        else:
-            suiteFew.addTest(Astakos('test_' + argv[1], config))
+        test_method = 'test_%s' % (argv[1] if len(argv) > 1 else '000')
+        suiteFew.addTest(Astakos(test_method, config))
 
     TextTestRunner(verbosity=2).run(suiteFew)
 
@@ -188,5 +188,5 @@ if __name__ == '__main__':
     parser = init_parser()
     args, argv = parser.parse_known_args()
     if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1:
-        raise Exception('\tusage: tests.py <group> [command]')
+        raise Exception('\tusage: tests <group> [command]')
     main(argv)
index d58d6ac..f42cd72 100644 (file)
@@ -42,14 +42,27 @@ class Astakos(tests.Generic):
             self['astakos', 'token'])
 
     def test_authenticate(self):
+        self._test_authenticate()
+
+    def _test_authenticate(self):
         r = self.client.authenticate()
         for term in (
+            'name',
             'username',
             'auth_token_expires',
-            'auth_token',
             'auth_token_created',
-            'groups',
-            'uniq',
-            'has_credits',
-            'has_signed_terms'):
+            'uuid',
+            'id',
+            'email'):
             self.assertTrue(term in r)
+
+    def test_get(self):
+        self._test_get()
+
+    def _test_get(self):
+        for term in (
+            'uuid',
+            'name',
+            'username'):
+            self.assertEqual(self.client.term(term), self['astakos', term])
+        self.assertTrue(self['astakos', 'email'] in self.client.term('email'))