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