Mercurial > hg > AuthRPC
annotate wibble/tests.py @ 11:dbd55e6af4f6
Added test
author | Ben Croston <ben@croston.org> |
---|---|
date | Mon, 05 Sep 2011 12:13:45 +0100 |
parents | 58e764e39492 |
children | 4ac14cfadc15 |
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 ##### | |
7 | 59 class NotAuthTest(unittest.TestCase): |
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 | |
4 | 69 class WibbleTests(unittest.TestCase): |
70 def setUp(self): | |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
71 from client import ServerProxy |
4 | 72 self.client = ServerProxy('http://localhost:1337/', |
73 username='testuser', | |
74 password='s3cr3t', | |
75 user_agent='wibble_unittest') | |
76 | |
5 | 77 class IgnoreClassNameTest(WibbleTests): |
4 | 78 def runTest(self): |
79 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
80 | |
7 | 81 class ExceptionTest(WibbleTests): |
82 def runTest(self): | |
83 with self.assertRaises(Exception): | |
84 self.client.raiseexception() | |
4 | 85 |
7 | 86 class BadRequestTest(WibbleTests): |
87 def runTest(self): | |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
88 from client import BadRequestException |
7 | 89 with self.assertRaises(BadRequestException): |
90 self.client.FunctionDoesNotExist() | |
91 | |
92 class EchoTest(WibbleTests): | |
93 def runTest(self): | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
94 if platform.python_version().startswith('3'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
95 POUND = '\u00A3' |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
96 else: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
97 POUND = unicode('\u00A3') |
7 | 98 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND) |
99 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!') | |
11 | 100 |
101 class ReturnNothing(WibbleTests): | |
102 def runTest(self): | |
103 self.assertEqual(self.client.returnnothing(), None) | |
4 | 104 ##### client ^^^ ##### |
105 | |
5 | 106 finished = False |
4 | 107 def suite(): |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
108 if platform.python_version().startswith('2'): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
109 # create server |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
110 def test_wrapper(): |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
111 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
112 while not finished: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
113 server.handle_request() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
114 thread = Thread(target=test_wrapper) |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
115 thread.start() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
116 time.sleep(0.1) # wait for server thread to start |
7 | 117 |
118 # tests are as client | |
4 | 119 suite = unittest.TestSuite() |
7 | 120 suite.addTest(NotAuthTest()) |
5 | 121 suite.addTest(IgnoreClassNameTest()) |
7 | 122 suite.addTest(ExceptionTest()) |
123 suite.addTest(BadRequestTest()) | |
124 suite.addTest(EchoTest()) | |
11 | 125 suite.addTest(ReturnNothing()) |
4 | 126 return suite |
127 | |
128 if __name__ == '__main__': | |
8
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
129 import sys |
9
23743b0f67ab
Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents:
8
diff
changeset
|
130 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
|
131 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
|
132 server = make_server() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
133 try: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
134 server.serve_forever() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
135 except KeyboardInterrupt: |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
136 sys.exit() |
685479d1f0a7
Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents:
7
diff
changeset
|
137 |
5 | 138 unittest.TextTestRunner(verbosity=2).run(suite()) |
139 finished = True | |
4 | 140 |
5 | 141 # make a dummy request to get server thread out of loop |
142 try: | |
143 import urllib | |
144 urllib.urlopen('http://localhost:1337/') | |
145 except: | |
146 pass | |
147 |