Mercurial > hg > AuthRPC
annotate wibble/tests.py @ 13:21eb67081c43
Added docstrings
author | Ben Croston <ben@croston.org> |
---|---|
date | Mon, 05 Sep 2011 12:42:05 +0100 |
parents | 4ac14cfadc15 |
children | 45c1d78559e2 |
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 | |
29 | |
30 ##### server vvv ##### | |
31 class api(object): | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
32 def mymethod(self): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
33 return 'wibbler woz ere' |
4 | 34 |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
35 def echo(self, mystring): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
36 return 'ECHO: ' + mystring |
7 | 37 |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
38 def raiseexception(self): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
39 dividebyzeroerror = 1/0 |
5 | 40 |
11 | 41 def returnnothing(self): |
42 pass | |
43 | |
4 | 44 def myauth(username, password, useragent=None): |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
45 return username == 'testuser' and \ |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
46 hashlib.md5('s3cr3t').hexdigest() == password and \ |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
47 useragent == 'wibble_unittest' |
4 | 48 |
49 def make_server(): | |
5 | 50 from server import JsonRpcApp |
4 | 51 class myhandler(simple_server.WSGIRequestHandler): |
52 def log_request(self, *a, **b): | |
53 pass # do not output log messages | |
54 application = JsonRpcApp(api(), auth=myauth) | |
55 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler) | |
56 ##### server ^^^ ##### | |
57 | |
58 ##### client vvv ##### | |
12 | 59 class AuthTest(unittest.TestCase): |
7 | 60 def runTest(self): |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
61 from client import ServerProxy, UnauthorisedException |
7 | 62 self.client = ServerProxy('http://localhost:1337/', |
63 username='testuser', | |
64 password='s3cr3t', | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
65 user_agent='InternetExploiter') |
7 | 66 with self.assertRaises(UnauthorisedException): |
67 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
68 | |
12 | 69 self.client = ServerProxy('http://localhost:1337/', |
70 username='testuser', | |
71 password='wrongpassword', | |
72 user_agent='wibble_unittest') | |
73 with self.assertRaises(UnauthorisedException): | |
74 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
75 | |
76 self.client = ServerProxy('http://localhost:1337/', | |
77 username='wronguser', | |
78 password='s3cr3t', | |
79 user_agent='wibble_unittest') | |
80 with self.assertRaises(UnauthorisedException): | |
81 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
82 | |
4 | 83 class WibbleTests(unittest.TestCase): |
84 def setUp(self): | |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
85 from client import ServerProxy |
4 | 86 self.client = ServerProxy('http://localhost:1337/', |
87 username='testuser', | |
88 password='s3cr3t', | |
89 user_agent='wibble_unittest') | |
90 | |
5 | 91 class IgnoreClassNameTest(WibbleTests): |
4 | 92 def runTest(self): |
93 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
94 | |
7 | 95 class ExceptionTest(WibbleTests): |
96 def runTest(self): | |
97 with self.assertRaises(Exception): | |
98 self.client.raiseexception() | |
4 | 99 |
7 | 100 class BadRequestTest(WibbleTests): |
101 def runTest(self): | |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
102 from client import BadRequestException |
7 | 103 with self.assertRaises(BadRequestException): |
104 self.client.FunctionDoesNotExist() | |
105 | |
106 class EchoTest(WibbleTests): | |
107 def runTest(self): | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
108 if platform.python_version().startswith('3'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
109 POUND = '\u00A3' |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
110 else: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
111 POUND = unicode('\u00A3') |
7 | 112 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND) |
113 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!') | |
11 | 114 |
115 class ReturnNothing(WibbleTests): | |
116 def runTest(self): | |
117 self.assertEqual(self.client.returnnothing(), None) | |
4 | 118 ##### client ^^^ ##### |
119 | |
5 | 120 finished = False |
4 | 121 def suite(): |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
122 if platform.python_version().startswith('2'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
123 # create server |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
124 def test_wrapper(): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
125 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
126 while not finished: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
127 server.handle_request() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
128 thread = Thread(target=test_wrapper) |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
129 thread.start() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
130 time.sleep(0.1) # wait for server thread to start |
7 | 131 |
132 # tests are as client | |
4 | 133 suite = unittest.TestSuite() |
12 | 134 suite.addTest(AuthTest()) |
5 | 135 suite.addTest(IgnoreClassNameTest()) |
7 | 136 suite.addTest(ExceptionTest()) |
137 suite.addTest(BadRequestTest()) | |
138 suite.addTest(EchoTest()) | |
11 | 139 suite.addTest(ReturnNothing()) |
4 | 140 return suite |
141 | |
142 if __name__ == '__main__': | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
143 import sys |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
144 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
|
145 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
|
146 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
147 try: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
148 server.serve_forever() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
149 except KeyboardInterrupt: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
150 sys.exit() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
151 |
5 | 152 unittest.TextTestRunner(verbosity=2).run(suite()) |
153 finished = True | |
4 | 154 |
5 | 155 # make a dummy request to get server thread out of loop |
156 try: | |
157 import urllib | |
158 urllib.urlopen('http://localhost:1337/') | |
159 except: | |
160 pass | |
161 |