Package myproxy :: Package ws :: Package client :: Package test :: Module test_client
[hide private]

Source Code for Module myproxy.ws.client.test.test_client

 1  #!/usr/bin/env python 
 2  """Unit tests for MyProxy Web Service client  
 3  """ 
 4  __author__ = "P J Kershaw" 
 5  __date__ = "28/05/12" 
 6  __copyright__ = "(C) 2012 Science and Technology Facilities Council" 
 7  __license__ = "BSD - see LICENSE file in top-level directory" 
 8  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
 9  __revision__ = '$Id$' 
10  import logging 
11  logging.basicConfig(level=logging.DEBUG) 
12  import unittest 
13  import os 
14  from getpass import getpass 
15  from ConfigParser import SafeConfigParser, NoOptionError 
16   
17  from OpenSSL import crypto, SSL 
18   
19  from ndg.httpsclient.ssl_context_util import make_ssl_context 
20   
21  from myproxy.ws.client import MyProxyWSClient 
22  from myproxy.ws.test import test_ca_dir 
23   
24   
25 -class WSClientTestCase(unittest.TestCase):
26 """Test MyProxy Web Service Client""" 27 here_dir = os.path.dirname(os.path.abspath(__file__)) 28 config_filepath = os.path.join(here_dir, 29 'test_myproxywebservice_client.cfg') 30
31 - def __init__(self, *args, **kwargs):
32 self.cfg = SafeConfigParser({'here': self.__class__.here_dir}) 33 self.cfg.optionxform = str 34 self.cfg.read(self.__class__.config_filepath) 35 36 unittest.TestCase.__init__(self, *args, **kwargs)
37
38 - def test01_logon(self):
39 opt_name = 'WSClientTestCase.test01_logon' 40 username = self.cfg.get(opt_name, 'username') 41 try: 42 password = self.cfg.get(opt_name, 'password') 43 except NoOptionError: 44 password = getpass('WSClientTestCase.test01_logon password: ') 45 46 server_url = self.cfg.get(opt_name, 'uri') 47 48 myproxy_client = MyProxyWSClient() 49 myproxy_client.ca_cert_dir = test_ca_dir 50 51 res = myproxy_client.logon(username, password, server_url) 52 self.assert_(res) 53 54 pem_out = res.read() 55 cert = crypto.load_certificate(crypto.FILETYPE_PEM, pem_out) 56 subj = cert.get_subject() 57 self.assert_(subj) 58 self.assert_(subj.CN) 59 print("Returned certificate subject CN=%r" % subj)
60
62 # Some cases may require client to pass cert in SSL handshake 63 opt_name = 'WSClientTestCase.test02_logon_with_ssl_client_authn' 64 username = self.cfg.get(opt_name, 'username') 65 try: 66 password = self.cfg.get(opt_name, 'password') 67 except NoOptionError: 68 password = '' 69 70 server_url = self.cfg.get(opt_name, 'uri') 71 client_cert_filepath = self.cfg.get(opt_name, 'client_cert_filepath') 72 client_key_filepath = self.cfg.get(opt_name, 'client_key_filepath') 73 74 myproxy_client = MyProxyWSClient() 75 76 ssl_ctx = make_ssl_context(cert_file=client_cert_filepath, 77 key_file=client_key_filepath, 78 ca_dir=test_ca_dir, 79 verify_peer=True, 80 url=server_url, 81 method=SSL.SSLv3_METHOD) 82 83 res = myproxy_client.logon(username, password, server_url, 84 ssl_ctx=ssl_ctx) 85 self.assert_(res) 86 87 pem_out = res.read() 88 cert = crypto.load_certificate(crypto.FILETYPE_PEM, pem_out) 89 subj = cert.get_subject() 90 self.assert_(subj) 91 self.assert_(subj.CN) 92 print("Returned certificate subject CN=%r" % subj)
93 94 if __name__ == "__main__": 95 unittest.main() 96