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
14 """Variant of twill.unit.TestInfo that runs a function as a test script,
15 not twill script from a file.
16 """
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
38 time.sleep(self.sleep)
39
40 self.script(self)
41
42
44
45
46 for i in xrange(count):
47 d = os.path.dirname(d)
48 return d
49
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
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
72 self.twillOutput = StringIO()
73 self.twillErr = StringIO()
74 twill.set_output(self.twillOutput)
75 twill.set_errout(self.twillErr)
76
77 self.server_port = 8080
78
79
80
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
88 """Zero-arg run-the-server function to be passed to TestInfo."""
89
90 runExampleServer('127.0.0.1', self.server_port, 'sstore')
91
92
101
102
103
104
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
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
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
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
163
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
180 twill.set_output(None)
181 twill.set_errout(None)
182
183
184 if __name__ == '__main__':
185 unittest.main()
186