annotate wibble/tests.py @ 7:3a6f3193cc7d

Created new tests
author Ben Croston <ben@croston.org>
date Thu, 01 Sep 2011 19:19:03 +0100
parents f5e3ba8cfcd0
children 685479d1f0a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
1 #!/usr/bin/env python
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
2 import unittest
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
3 import hashlib
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
4 from threading import Thread
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
5 import time
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
6 from wsgiref import simple_server
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
7 import platform
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
8
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
9 ##### server vvv #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
10 class api(object):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
11 def mymethod(self):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
12 return 'wibbler woz ere'
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
13
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
14 def echo(self, mystring):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
15 return 'ECHO: ' + mystring
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
16
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
17 def raiseexception(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
18 dividebyzeroerror = 1/0
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
19
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
20 def myauth(username, password, useragent=None):
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
21 return username == 'testuser' and \
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
22 hashlib.md5('s3cr3t').hexdigest() == password and \
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
23 useragent == 'wibble_unittest'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
24
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
25 def make_server():
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
26 from server import JsonRpcApp
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
27 class myhandler(simple_server.WSGIRequestHandler):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
28 def log_request(self, *a, **b):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
29 pass # do not output log messages
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
30 application = JsonRpcApp(api(), auth=myauth)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
31 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
32 ##### server ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
33
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
34 ##### client vvv #####
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
35 class NotAuthTest(unittest.TestCase):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
36 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
37 from client.ServerProxy import ServerProxy, UnauthorisedException
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
38 self.client = ServerProxy('http://localhost:1337/',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
39 username='testuser',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
40 password='s3cr3t',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
41 user_agent='InternerExploiter')
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
42 with self.assertRaises(UnauthorisedException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
43 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
44
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
45 class WibbleTests(unittest.TestCase):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
46 def setUp(self):
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
47 from client.ServerProxy import ServerProxy
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
48 self.client = ServerProxy('http://localhost:1337/',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
49 username='testuser',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
50 password='s3cr3t',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
51 user_agent='wibble_unittest')
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
52
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
53 class IgnoreClassNameTest(WibbleTests):
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
54 def runTest(self):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
55 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
56
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
57 class ExceptionTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
58 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
59 with self.assertRaises(Exception):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
60 self.client.raiseexception()
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
61
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
62 class BadRequestTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
63 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
64 from client.ServerProxy import BadRequestException
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
65 with self.assertRaises(BadRequestException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
66 self.client.FunctionDoesNotExist()
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
67
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
68 class EchoTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
69 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
70 POUND = '\xc2\xa3'
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
71 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND)
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
72 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!')
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
73 ##### client ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
74
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
75 finished = False
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
76 def suite():
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
77 if platform.python_version().startswith('3'):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
78 # no tests for python 3 because server not ported yet
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
79 return unittest.TestSuite()
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
80
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
81 # create server
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
82 def test_wrapper():
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
83 server = make_server()
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
84 while not finished:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
85 server.handle_request()
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
86 thread = Thread(target=test_wrapper)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
87 thread.start()
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
88 time.sleep(0.1) # wait for server thread to start
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
89
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
90 # tests are as client
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
91 suite = unittest.TestSuite()
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
92 suite.addTest(NotAuthTest())
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
93 suite.addTest(IgnoreClassNameTest())
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
94 suite.addTest(ExceptionTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
95 suite.addTest(BadRequestTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
96 suite.addTest(EchoTest())
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
97 return suite
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
98
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
99 if __name__ == '__main__':
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
100 unittest.TextTestRunner(verbosity=2).run(suite())
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
101 finished = True
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
102
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
103 # make a dummy request to get server thread out of loop
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
104 try:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
105 import urllib
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
106 urllib.urlopen('http://localhost:1337/')
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
107 except:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
108 pass
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
109