Mercurial > hg > AuthRPC
annotate wibble/tests.py @ 14:45c1d78559e2
Added more tests
author | Ben Croston <ben@croston.org> |
---|---|
date | Mon, 05 Sep 2011 13:08:07 +0100 |
parents | 4ac14cfadc15 |
children |
rev | line source |
---|---|
4 | 1 #!/usr/bin/env python |
10 | 2 |
3 # Copyright (c) 2011 Ben Croston | |
4 # | |
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of | |
6 # this software and associated documentation files (the "Software"), to deal in | |
7 # the Software without restriction, including without limitation the rights to | |
8 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
9 # of the Software, and to permit persons to whom the Software is furnished to do | |
10 # so, subject to the following conditions: | |
11 # | |
12 # The above copyright notice and this permission notice shall be included in all | |
13 # copies or substantial portions of the Software. | |
14 # | |
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 # SOFTWARE. | |
22 | |
4 | 23 import unittest |
24 import hashlib | |
25 from threading import Thread | |
26 import time | |
27 from wsgiref import simple_server | |
28 import platform | |
14 | 29 import urllib |
30 | |
31 try: | |
32 urllib.urlopen('http://www.wyre-it.co.uk/') | |
33 NO_INTERNET = False | |
34 except IOError: | |
35 NO_INTERNET = True | |
4 | 36 |
37 ##### server vvv ##### | |
38 class api(object): | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
39 def mymethod(self): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
40 return 'wibbler woz ere' |
4 | 41 |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
42 def echo(self, mystring): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
43 return 'ECHO: ' + mystring |
7 | 44 |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
45 def raiseexception(self): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
46 dividebyzeroerror = 1/0 |
5 | 47 |
11 | 48 def returnnothing(self): |
49 pass | |
50 | |
4 | 51 def myauth(username, password, useragent=None): |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
52 return username == 'testuser' and \ |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
53 hashlib.md5('s3cr3t').hexdigest() == password and \ |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
54 useragent == 'wibble_unittest' |
4 | 55 |
56 def make_server(): | |
5 | 57 from server import JsonRpcApp |
4 | 58 class myhandler(simple_server.WSGIRequestHandler): |
59 def log_request(self, *a, **b): | |
60 pass # do not output log messages | |
61 application = JsonRpcApp(api(), auth=myauth) | |
62 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler) | |
63 ##### server ^^^ ##### | |
64 | |
65 ##### client vvv ##### | |
12 | 66 class AuthTest(unittest.TestCase): |
7 | 67 def runTest(self): |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
68 from client import ServerProxy, UnauthorisedException |
7 | 69 self.client = ServerProxy('http://localhost:1337/', |
70 username='testuser', | |
71 password='s3cr3t', | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
72 user_agent='InternetExploiter') |
7 | 73 with self.assertRaises(UnauthorisedException): |
74 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
75 | |
12 | 76 self.client = ServerProxy('http://localhost:1337/', |
77 username='testuser', | |
78 password='wrongpassword', | |
79 user_agent='wibble_unittest') | |
80 with self.assertRaises(UnauthorisedException): | |
81 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
82 | |
83 self.client = ServerProxy('http://localhost:1337/', | |
84 username='wronguser', | |
85 password='s3cr3t', | |
86 user_agent='wibble_unittest') | |
87 with self.assertRaises(UnauthorisedException): | |
88 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
89 | |
14 | 90 |
91 @unittest.skipIf(NO_INTERNET,'www.wyre-it.co.uk:80 not contactable') | |
92 class NotFoundTest(unittest.TestCase): | |
93 def runTest(self): | |
94 from client import ServerProxy, NotFoundException | |
95 self.client = ServerProxy('http://www.wyre-it.co.uk/notfound.txt') | |
96 with self.assertRaises(NotFoundException): | |
97 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
98 | |
99 class NetworkSocketTest(unittest.TestCase): | |
100 def runTest(self): | |
101 from client import ServerProxy, NetworkSocketException | |
102 self.client = ServerProxy('http://localhost:666/') | |
103 with self.assertRaises(NetworkSocketException): | |
104 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
105 | |
4 | 106 class WibbleTests(unittest.TestCase): |
107 def setUp(self): | |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
108 from client import ServerProxy |
4 | 109 self.client = ServerProxy('http://localhost:1337/', |
110 username='testuser', | |
111 password='s3cr3t', | |
112 user_agent='wibble_unittest') | |
113 | |
5 | 114 class IgnoreClassNameTest(WibbleTests): |
4 | 115 def runTest(self): |
116 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
117 | |
7 | 118 class ExceptionTest(WibbleTests): |
119 def runTest(self): | |
120 with self.assertRaises(Exception): | |
121 self.client.raiseexception() | |
4 | 122 |
7 | 123 class BadRequestTest(WibbleTests): |
124 def runTest(self): | |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
125 from client import BadRequestException |
7 | 126 with self.assertRaises(BadRequestException): |
127 self.client.FunctionDoesNotExist() | |
128 | |
129 class EchoTest(WibbleTests): | |
130 def runTest(self): | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
131 if platform.python_version().startswith('3'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
132 POUND = '\u00A3' |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
133 else: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
134 POUND = unicode('\u00A3') |
7 | 135 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND) |
136 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!') | |
11 | 137 |
138 class ReturnNothing(WibbleTests): | |
139 def runTest(self): | |
140 self.assertEqual(self.client.returnnothing(), None) | |
4 | 141 ##### client ^^^ ##### |
142 | |
5 | 143 finished = False |
4 | 144 def suite(): |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
145 if platform.python_version().startswith('2'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
146 # create server |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
147 def test_wrapper(): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
148 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
149 while not finished: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
150 server.handle_request() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
151 thread = Thread(target=test_wrapper) |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
152 thread.start() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
153 time.sleep(0.1) # wait for server thread to start |
7 | 154 |
155 # tests are as client | |
4 | 156 suite = unittest.TestSuite() |
12 | 157 suite.addTest(AuthTest()) |
14 | 158 suite.addTest(NotFoundTest()) |
159 suite.addTest(NetworkSocketTest()) | |
5 | 160 suite.addTest(IgnoreClassNameTest()) |
7 | 161 suite.addTest(ExceptionTest()) |
162 suite.addTest(BadRequestTest()) | |
163 suite.addTest(EchoTest()) | |
11 | 164 suite.addTest(ReturnNothing()) |
4 | 165 return suite |
166 | |
167 if __name__ == '__main__': | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
168 import sys |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
169 if platform.python_version().startswith('2') and 'serve' in sys.argv: |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
170 print 'Listening on port 1337 (Ctrl-C qo quit)...' |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
171 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
172 try: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
173 server.serve_forever() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
174 except KeyboardInterrupt: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
175 sys.exit() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
176 |
5 | 177 unittest.TextTestRunner(verbosity=2).run(suite()) |
178 finished = True | |
4 | 179 |
5 | 180 # make a dummy request to get server thread out of loop |
181 try: | |
182 import urllib | |
183 urllib.urlopen('http://localhost:1337/') | |
184 except: | |
185 pass | |
186 |