#!/usr/bin/env python2.7
import unittest

import sys
sys.path.append(".")

import settings

import client

authserver = None


class TestClient(unittest.TestCase):
    '''An integration test for the Client class.'''
    def setUp(self):
        pass

    @classmethod
    def setUpClass(cls):
        # Fire up a server to test against.
        import subprocess
        import requests
        cls.servers = []
        from collections import OrderedDict
        servers = OrderedDict()
        servers[settings.AUTHSERVER] = [sys.executable]+['authserver.py', '--dburi=sqlite://']
        servers[settings.CHARSERVER] = [sys.executable]+['charserver.py', '--dburi=sqlite://']
        servers['http://localhost:28017'] = ['mongod', '--rest', '--oplogSize=1', '--directoryperdb', '--smallfiles', '--dbpath=./mongodb-unittest/']
        servers[settings.ZONESERVER] = [sys.executable]+['masterzoneserver.py', '--dburi=sqlite://']
        for uri, args in servers.iteritems():
            if sys.executable in args:
                args.extend(['--log-file-prefix=log/%s.log' % args[1], '--logging=info'])
            print "Starting %s at %s" % (' '.join(args), uri)
            cmd = args
            s = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            cls.servers.append(s)
            status = None
            while status != 200:
                try:
                    r = requests.get(uri)
                    status = r.status_code
                except requests.ConnectionError:
                    continue

    @classmethod
    def tearDownClass(cls):
        from signal import SIGINT
        for server in cls.servers:
            server.send_signal(SIGINT)

    def test___init__(self):
        c = client.Client()
        self.assertTrue(c)

    def test___init___autoauth(self):
        c = client.Client(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD)
        self.assertTrue(c.cookies)
        self.assertTrue(c.cookies['user'])

    def test___init___autoauth_bad(self):
        with self.assertRaises(client.AuthenticationError):
            client.Client(username="BadUser", password="BadPassword")

    def test_authenticate(self):
        c = client.Client()
        result = c.authenticate(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD)
        self.assertTrue(result)

    def test_authenticate_bad(self):
        c = client.Client()
        result = c.authenticate(username='BadUser', password='BadPassword')
        self.assertFalse(result)

    def test_characters(self):
        c = client.Client(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD)
        self.assertTrue(c.characters)
        self.assertIn('Graxnor', c.characters)

    def test_characters_get_obj(self):
        c = client.Client(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD)
        self.assertIn('Graxnor', c.characters)
        self.assertTrue(c.characters['Graxnor'])

    def test_character_obj_zone(self):
        c = client.Client(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD)
        self.assertTrue(c.get_zone('Graxnor'))

    def test_get_zone_url(self):
        c = client.Client(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD)
        zoneid = c.get_zone('Graxnor')
        zoneurl = c.get_zone_url(zoneid)
        self.assertIn('http', zoneurl)

    def test_get_objects(self):
        c = client.Client(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD)
        c.get_objects()
        self.assertTrue(c.objects)


if __name__ == '__main__':
    unittest.main()

@@ -1723,32 +1723,76 @@
 __init__(self):%0A
+        '''Init the Client with no args.'''%0A
         c = clie
@@ -1795,32 +1795,32 @@
 client.Client()%0A
-
         self.ass
@@ -1861,32 +1861,97 @@
 autoauth(self):%0A
+        '''Init the Client with a known good user/pass combo.'''%0A
         c = clie
@@ -2145,32 +2145,119 @@
 auth_bad(self):%0A
+        '''Initting the Client with a bad user/pass combo will raise an exception. '''%0A
         with sel
@@ -2396,32 +2396,96 @@
 enticate(self):%0A
+        '''Authenticating after initting Client should work.'''%0A
         c = clie
@@ -2662,32 +2662,111 @@
 cate_bad(self):%0A
+        '''Authenticating with bad credentials after init does not succeed.'''%0A
         c = clie
@@ -2910,32 +2910,107 @@
 aracters(self):%0A
+        '''After authenticating, Client has a dictionary of characters.'''%0A
         c = clie
@@ -3596,16 +3596,16 @@
 nor'))%0A%0A
-
     def 
@@ -3621,32 +3621,95 @@
 zone_url(self):%0A
+        '''Client can get the url for our character's zone.'''%0A
         c = clie

Add a bunch of test docstrings.