Mercurial > hg > AuthRPC
comparison 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 |
comparison
equal
deleted
inserted
replaced
6:bb6b8df4dae8 | 7:3a6f3193cc7d |
---|---|
9 ##### server vvv ##### | 9 ##### server vvv ##### |
10 class api(object): | 10 class api(object): |
11 def mymethod(self): | 11 def mymethod(self): |
12 return 'wibbler woz ere' | 12 return 'wibbler woz ere' |
13 | 13 |
14 def myexception(self): | 14 def echo(self, mystring): |
15 raise Exception('This is a test error') | 15 return 'ECHO: ' + mystring |
16 | |
17 def raiseexception(self): | |
18 dividebyzeroerror = 1/0 | |
16 | 19 |
17 def myauth(username, password, useragent=None): | 20 def myauth(username, password, useragent=None): |
18 #raise Exception("This is a test error in auth") | |
19 return username == 'testuser' and \ | 21 return username == 'testuser' and \ |
20 hashlib.md5('s3cr3t').hexdigest() == password and \ | 22 hashlib.md5('s3cr3t').hexdigest() == password and \ |
21 useragent == 'wibble_unittest' | 23 useragent == 'wibble_unittest' |
22 | 24 |
23 def make_server(): | 25 def make_server(): |
28 application = JsonRpcApp(api(), auth=myauth) | 30 application = JsonRpcApp(api(), auth=myauth) |
29 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler) | 31 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler) |
30 ##### server ^^^ ##### | 32 ##### server ^^^ ##### |
31 | 33 |
32 ##### client vvv ##### | 34 ##### client vvv ##### |
35 class NotAuthTest(unittest.TestCase): | |
36 def runTest(self): | |
37 from client.ServerProxy import ServerProxy, UnauthorisedException | |
38 self.client = ServerProxy('http://localhost:1337/', | |
39 username='testuser', | |
40 password='s3cr3t', | |
41 user_agent='InternerExploiter') | |
42 with self.assertRaises(UnauthorisedException): | |
43 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | |
44 | |
33 class WibbleTests(unittest.TestCase): | 45 class WibbleTests(unittest.TestCase): |
34 def setUp(self): | 46 def setUp(self): |
35 from client.ServerProxy import ServerProxy | 47 from client.ServerProxy import ServerProxy |
36 self.client = ServerProxy('http://localhost:1337/', | 48 self.client = ServerProxy('http://localhost:1337/', |
37 username='testuser', | 49 username='testuser', |
40 | 52 |
41 class IgnoreClassNameTest(WibbleTests): | 53 class IgnoreClassNameTest(WibbleTests): |
42 def runTest(self): | 54 def runTest(self): |
43 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) | 55 self.assertEqual(self.client.api.mymethod(),self.client.mymethod()) |
44 | 56 |
45 #def client_tests(): | 57 class ExceptionTest(WibbleTests): |
46 # try: | 58 def runTest(self): |
47 # print(jsonrpc_client.wibble('this should fail')) | 59 with self.assertRaises(Exception): |
48 # except BadRequestException: | 60 self.client.raiseexception() |
49 # pass # test passed | |
50 # else: | |
51 # raise Exception('Test failed (calling unknown method)') | |
52 # | |
53 # print 'All tests passed' | |
54 | 61 |
62 class BadRequestTest(WibbleTests): | |
63 def runTest(self): | |
64 from client.ServerProxy import BadRequestException | |
65 with self.assertRaises(BadRequestException): | |
66 self.client.FunctionDoesNotExist() | |
67 | |
68 class EchoTest(WibbleTests): | |
69 def runTest(self): | |
70 POUND = '\xc2\xa3' | |
71 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND) | |
72 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!') | |
55 ##### client ^^^ ##### | 73 ##### client ^^^ ##### |
56 | 74 |
57 finished = False | 75 finished = False |
58 def suite(): | 76 def suite(): |
59 if platform.python_version().startswith('3'): | 77 if platform.python_version().startswith('3'): |
60 # no tests for python 3 because server not ported yet | 78 # no tests for python 3 because server not ported yet |
61 return unittest.TestSuite() | 79 return unittest.TestSuite() |
80 | |
81 # create server | |
62 def test_wrapper(): | 82 def test_wrapper(): |
63 server = make_server() | 83 server = make_server() |
64 while not finished: | 84 while not finished: |
65 server.handle_request() | 85 server.handle_request() |
66 thread = Thread(target=test_wrapper) | 86 thread = Thread(target=test_wrapper) |
67 thread.start() | 87 thread.start() |
68 time.sleep(0.1) # wait for server thread to start | 88 time.sleep(0.1) # wait for server thread to start |
89 | |
90 # tests are as client | |
69 suite = unittest.TestSuite() | 91 suite = unittest.TestSuite() |
92 suite.addTest(NotAuthTest()) | |
70 suite.addTest(IgnoreClassNameTest()) | 93 suite.addTest(IgnoreClassNameTest()) |
94 suite.addTest(ExceptionTest()) | |
95 suite.addTest(BadRequestTest()) | |
96 suite.addTest(EchoTest()) | |
71 return suite | 97 return suite |
72 | 98 |
73 if __name__ == '__main__': | 99 if __name__ == '__main__': |
74 finished = False | |
75 unittest.TextTestRunner(verbosity=2).run(suite()) | 100 unittest.TextTestRunner(verbosity=2).run(suite()) |
76 finished = True | 101 finished = True |
77 | 102 |
78 # make a dummy request to get server thread out of loop | 103 # make a dummy request to get server thread out of loop |
79 try: | 104 try: |