Package openid :: Package test :: Module test_examples
[frames] | no frames]

Source Code for Module openid.test.test_examples

  1  "Test some examples." 
  2   
  3  import socket 
  4  import os.path, unittest, sys, time 
  5  from cStringIO import StringIO 
  6   
  7  import twill.commands, twill.parse, twill.unit 
  8   
  9  from openid.consumer.discover import \ 
 10       OpenIDServiceEndpoint, OPENID_1_1_TYPE 
 11  from openid.consumer.consumer import AuthRequest 
 12   
13 -class TwillTest(twill.unit.TestInfo):
14 """Variant of twill.unit.TestInfo that runs a function as a test script, 15 not twill script from a file. 16 """ 17 18 # twill.unit is pretty small to start with, we're overriding 19 # run_script and bypassing twill.parse, so it may make sense to 20 # rewrite twill.unit altogether. 21 22 # Desirable features: 23 # * better unittest.TestCase integration. 24 # - handle logs on setup and teardown. 25 # - treat TwillAssertionError as failed test assertion, make twill 26 # assertions more consistant with TestCase.failUnless idioms. 27 # - better error reporting on failed assertions. 28 # - The amount of functions passed back and forth between TestInfo 29 # and TestCase is currently pretty silly. 30 # * access to child process's logs. 31 # TestInfo.start_server redirects stdout/stderr to StringIO 32 # objects which are, afaict, inaccessible to the caller of 33 # test.unit.run_child_process. 34 # * notice when the child process dies, i.e. if you muck up and 35 # your runExampleServer function throws an exception. 36
37 - def run_script(self):
38 time.sleep(self.sleep) 39 # twill.commands.go(self.get_url()) 40 self.script(self)
41 42
43 -def splitDir(d, count):
44 # in python2.4 and above, it's easier to spell this as 45 # d.rsplit(os.sep, count) 46 for i in xrange(count): 47 d = os.path.dirname(d) 48 return d
49
50 -def runExampleServer(host, port, data_path):
51 thisfile = os.path.abspath(sys.modules[__name__].__file__) 52 topDir = splitDir(thisfile, 3) 53 exampleDir = os.path.join(topDir, 'examples') 54 serverExample = os.path.join(exampleDir, 'server.py') 55 serverModule = {} 56 execfile(serverExample, serverModule) 57 serverMain = serverModule['main'] 58 59 serverMain(host, port, data_path)
60 61 62
63 -class TestServer(unittest.TestCase):
64 """Acceptance tests for examples/server.py. 65 66 These are more acceptance tests than unit tests as they actually 67 start the whole server running and test it on its external HTTP 68 interface. 69 """ 70
71 - def setUp(self):
72 self.twillOutput = StringIO() 73 self.twillErr = StringIO() 74 twill.set_output(self.twillOutput) 75 twill.set_errout(self.twillErr) 76 # FIXME: make sure we pick an available port. 77 self.server_port = 8080 78 79 # We need something to feed the server as a realm, but it needn't 80 # be reachable. (Until we test realm verification.) 81 self.realm = 'http://127.0.0.1/%s' % (self.id(),) 82 self.return_to = self.realm + '/return_to' 83 84 twill.commands.reset_browser()
85 86
87 - def runExampleServer(self):
88 """Zero-arg run-the-server function to be passed to TestInfo.""" 89 # FIXME - make sure sstore starts clean. 90 runExampleServer('127.0.0.1', self.server_port, 'sstore')
91 92
93 - def v1endpoint(self, port):
94 """Return an OpenID 1.1 OpenIDServiceEndpoint for the server.""" 95 base = "http://%s:%s" % (socket.getfqdn('127.0.0.1'), port) 96 ep = OpenIDServiceEndpoint() 97 ep.claimed_id = base + "/id/bob" 98 ep.server_url = base + "/openidserver" 99 ep.type_uris = [OPENID_1_1_TYPE] 100 return ep
101 102 103 # TODO: test discovery 104
105 - def test_checkidv1(self):
106 """OpenID 1.1 checkid_setup request.""" 107 ti = TwillTest(self.twill_checkidv1, self.runExampleServer, 108 self.server_port, sleep=0.2) 109 twill.unit.run_test(ti) 110 111 if self.twillErr.getvalue(): 112 self.fail(self.twillErr.getvalue())
113 114
115 - def test_allowed(self):
116 """OpenID 1.1 checkid_setup request.""" 117 ti = TwillTest(self.twill_allowed, self.runExampleServer, 118 self.server_port, sleep=0.2) 119 twill.unit.run_test(ti) 120 121 if self.twillErr.getvalue(): 122 self.fail(self.twillErr.getvalue())
123 124
125 - def twill_checkidv1(self, twillInfo):
126 endpoint = self.v1endpoint(self.server_port) 127 authreq = AuthRequest(endpoint, assoc=None) 128 url = authreq.redirectURL(self.realm, self.return_to) 129 130 c = twill.commands 131 132 try: 133 c.go(url) 134 c.get_browser()._browser.set_handle_redirect(False) 135 c.submit("yes") 136 c.code(302) 137 headers = c.get_browser()._browser.response().info() 138 finalURL = headers['Location'] 139 self.failUnless('openid.mode=id_res' in finalURL, finalURL) 140 self.failUnless('openid.identity=' in finalURL, finalURL) 141 except twill.commands.TwillAssertionError, e: 142 msg = '%s\nFinal page:\n%s' % ( 143 str(e), c.get_browser().get_html()) 144 self.fail(msg)
145 146
147 - def twill_allowed(self, twillInfo):
148 endpoint = self.v1endpoint(self.server_port) 149 authreq = AuthRequest(endpoint, assoc=None) 150 url = authreq.redirectURL(self.realm, self.return_to) 151 152 c = twill.commands 153 154 try: 155 c.go(url) 156 c.code(200) 157 c.get_browser()._browser.set_handle_redirect(False) 158 c.formvalue(1, 'remember', 'true') 159 c.find('name="login_as" value="bob"') 160 c.submit("yes") 161 c.code(302) 162 # Since we set remember=yes, the second time we shouldn't 163 # see that page. 164 c.go(url) 165 c.code(302) 166 headers = c.get_browser()._browser.response().info() 167 finalURL = headers['Location'] 168 self.failUnless(finalURL.startswith(self.return_to)) 169 except twill.commands.TwillAssertionError, e: 170 from traceback import format_exc 171 msg = '%s\nTwill output:%s\nTwill errors:%s\nFinal page:\n%s' % ( 172 format_exc(), 173 self.twillOutput.getvalue(), 174 self.twillErr.getvalue(), 175 c.get_browser().get_html()) 176 self.fail(msg)
177 178
179 - def tearDown(self):
180 twill.set_output(None) 181 twill.set_errout(None)
182 183 184 if __name__ == '__main__': 185 unittest.main() 186