view 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
line wrap: on
line source

#!/usr/bin/env python
import unittest
import hashlib
from threading import Thread
import time
from wsgiref import simple_server
import platform

##### server vvv #####
class api(object):
  def mymethod(self):
      return 'wibbler woz ere'

  def echo(self, mystring):
      return 'ECHO: ' + mystring

  def raiseexception(self):
      dividebyzeroerror = 1/0

def myauth(username, password, useragent=None):
  return username == 'testuser' and \
         hashlib.md5('s3cr3t').hexdigest() == password and \
         useragent == 'wibble_unittest'

def make_server():
    from server import JsonRpcApp
    class myhandler(simple_server.WSGIRequestHandler):
        def log_request(self, *a, **b):
            pass # do not output log messages
    application = JsonRpcApp(api(), auth=myauth)
    return simple_server.make_server('localhost', 1337, application, handler_class=myhandler)
##### server ^^^ #####

##### client vvv #####
class NotAuthTest(unittest.TestCase):
    def runTest(self):
        from client.ServerProxy import ServerProxy, UnauthorisedException
        self.client = ServerProxy('http://localhost:1337/',
                                  username='testuser',
                                  password='s3cr3t',
                                  user_agent='InternerExploiter')
        with self.assertRaises(UnauthorisedException):
            self.assertEqual(self.client.api.mymethod(),self.client.mymethod())

class WibbleTests(unittest.TestCase):
    def setUp(self):
        from client.ServerProxy import ServerProxy
        self.client = ServerProxy('http://localhost:1337/',
                                  username='testuser',
                                  password='s3cr3t',
                                  user_agent='wibble_unittest')

class IgnoreClassNameTest(WibbleTests):
    def runTest(self):
        self.assertEqual(self.client.api.mymethod(),self.client.mymethod())

class ExceptionTest(WibbleTests):
    def runTest(self):
        with self.assertRaises(Exception):
            self.client.raiseexception()

class BadRequestTest(WibbleTests):
    def runTest(self):
        from client.ServerProxy import BadRequestException
        with self.assertRaises(BadRequestException):
            self.client.FunctionDoesNotExist()

class EchoTest(WibbleTests):
    def runTest(self):
        POUND = '\xc2\xa3'
        self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND)
        self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!')
##### client ^^^ #####

finished = False
def suite():
    if platform.python_version().startswith('3'):
        # no tests for python 3 because server not ported yet
        return unittest.TestSuite()

    # create server
    def test_wrapper():
        server = make_server()
        while not finished:
            server.handle_request()
    thread = Thread(target=test_wrapper)
    thread.start()
    time.sleep(0.1) # wait for server thread to start

    # tests are as client
    suite = unittest.TestSuite()
    suite.addTest(NotAuthTest())
    suite.addTest(IgnoreClassNameTest())
    suite.addTest(ExceptionTest())
    suite.addTest(BadRequestTest())
    suite.addTest(EchoTest())
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())
    finished = True

    # make a dummy request to get server thread out of loop
    try:
       import urllib
       urllib.urlopen('http://localhost:1337/')
    except:
       pass