# Path: authserver.py
# class UserController(object):
# class RegistrationHandler(BaseHandler):
# class AuthHandler(BaseHandler):
# class LogoutHandler(BaseHandler):
# class CharacterHandler(BaseHandler):
#     def hash_password(cls, password):
#     def check_password(cls, plaintext, hashed):
#     def post(self):
#     def register_user(self, username, password, email=None):
#     def post(self):
#     def authenticate(self, username, password):
#     def set_admin(self, user):
#     def set_current_user(self, user):
#     def get(self):
#     def get(self):
#     def get_characters(self, username):
# 
# Path: elixir_models.py
# class RetryDB(RetryOperationalError, SqliteExtDatabase):
# class ComplexEncoder(json.JSONEncoder):
# class BaseModel(Model):
#     class Meta:
# class User(BaseModel):
# class Character(BaseModel):
# class Zone(BaseModel):
# class Message(BaseModel):
# class Object(BaseModel):
# class Message(BaseModel):
# class ScriptedObject(Object):
#     def default(self, obj):
#     def exists(cls, *args, **kwargs):
#     def json_dumps(self):
#     def __repr__(self):
#     def __repr__(self):
#     def loc(self):
#     def loc(self, val):
#     def set_modified(self, date_time=None):
#     def get_objects(since=None, physical=None, limit=None, player=None, scripted=None, name=None):
# def setup(db_uri='simplemmo.sqlite', echo=False):
# 
# Path: elixir_models.py
# class User(BaseModel):
#     '''User contains details useful for authenticating a user for when they
#     initially log in.'''
# 
#     username = CharField(unique=True)
#     password = CharField()
#     email = CharField(null=True)
#     # Also has 'characters'
# 
#     def __repr__(self):
#         uname = self.username
#         s = "s"*(len(self.characters)-1)
#         chars = ', '.join([c.name for c in self.characters])
#         return '<User "%s" owning character%s: %s.>' % (uname, s, chars)


import unittest
import sys
import settings
from tornado.web import Application, decode_signed_value
from tornado.testing import AsyncHTTPTestCase
from mock import Mock
from authserver import PingHandler, AuthHandler, RegistrationHandler
from elixir import session
from elixir_models import metadata, setup_all, create_all
from elixir_models import User
        goodpass = "goodpass"
        password = "badpass"
        add_user(username, goodpass)

        # Test
        qstring = "username=%s&password=%s" % (username, password)
        response = self.fetch('/', method='POST', body=qstring)
        expected = "401: Unauthorized"
        self.assertIn(expected, response.body)
        self.assertEqual(401, response.code)

    def test_post_admin(self):
        for username in settings.ADMINISTRATORS:
            # Setup
            password = "goodpass"
            add_user(username, password)

            # Result: Sets user cookie, result will be 'Login Successful'
            qstring = "username=%s&password=%s" % (username, password)
            response = self.fetch('/', method='POST', body=qstring)
            expected = 'Login successful.'
            self.assertEqual(expected, response.body)

            # Check for cookies
            result = decode_cookies(response)
            self.assertEqual('true', result['admin'])
            self.assertEqual(username, result['user'])

class TestRegistrationHandler(AsyncHTTPTestCase):
    def get_app(self):

# Path: authserver.py
# class UserController(object):
# class RegistrationHandler(BaseHandler):
# class AuthHandler(BaseHandler):
# class LogoutHandler(BaseHandler):
# class CharacterHandler(BaseHandler):
#     def hash_password(cls, password):
#     def check_password(cls, plaintext, hashed):
#     def post(self):
#     def register_user(self, username, password, email=None):
#     def post(self):
#     def authenticate(self, username, password):
#     def set_admin(self, user):
#     def set_current_user(self, user):
#     def get(self):
#     def get(self):
#     def get_characters(self, username):
# 
# Path: elixir_models.py
# class RetryDB(RetryOperationalError, SqliteExtDatabase):
# class ComplexEncoder(json.JSONEncoder):
# class BaseModel(Model):
#     class Meta:
# class User(BaseModel):
# class Character(BaseModel):
# class Zone(BaseModel):
# class Message(BaseModel):
# class Object(BaseModel):
# class Message(BaseModel):
# class ScriptedObject(Object):
#     def default(self, obj):
#     def exists(cls, *args, **kwargs):
#     def json_dumps(self):
#     def __repr__(self):
#     def __repr__(self):
#     def loc(self):
#     def loc(self, val):
#     def set_modified(self, date_time=None):
#     def get_objects(since=None, physical=None, limit=None, player=None, scripted=None, name=None):
# def setup(db_uri='simplemmo.sqlite', echo=False):
# 
# Path: elixir_models.py
# class User(BaseModel):
#     '''User contains details useful for authenticating a user for when they
#     initially log in.'''
# 
#     username = CharField(unique=True)
#     password = CharField()
#     email = CharField(null=True)
#     # Also has 'characters'
# 
#     def __repr__(self):
#         uname = self.username
#         s = "s"*(len(self.characters)-1)
#         chars = ', '.join([c.name for c in self.characters])
#         return '<User "%s" owning character%s: %s.>' % (uname, s, chars)


# Path: tests/integration/test_authserver.py
import unittest
import sys
import settings
from tornado.web import Application, decode_signed_value
from tornado.testing import AsyncHTTPTestCase
from mock import Mock
from authserver import PingHandler, AuthHandler, RegistrationHandler
from elixir import session
from elixir_models import metadata, setup_all, create_all
from elixir_models import User

        goodpass = "goodpass"
        password = "badpass"
        add_user(username, goodpass)

        # Test
        qstring = "username=%s&password=%s" % (username, password)
        response = self.fetch('/', method='POST', body=qstring)
        expected = "401: Unauthorized"
        self.assertIn(expected, response.body)
        self.assertEqual(401, response.code)

    def test_post_admin(self):
        for username in settings.ADMINISTRATORS:
            # Setup
            password = "goodpass"
            add_user(username, password)

            # Result: Sets user cookie, result will be 'Login Successful'
            qstring = "username=%s&password=%s" % (username, password)
            response = self.fetch('/', method='POST', body=qstring)
            expected = 'Login successful.'
            self.assertEqual(expected, response.body)

            # Check for cookies
            result = decode_cookies(response)
            self.assertEqual('true', result['admin'])
            self.assertEqual(username, result['user'])

class TestRegistrationHandler(AsyncHTTPTestCase):
    def get_app(self):

        return Application([('/', RegistrationHandler)], cookie_secret=settings.COOKIE_SECRET)