# Path: radarpost/mailbox.py
# def create_mailbox(couchdb, dbname):
#     """
#     create a mailbox on the given server with the given name.
#     
#     raises PreconditionFailed if a database with the given name 
#            already exists.
#     raises ValueError if name is not a valid mailbox name.
#     """
#     db = couchdb.create(dbname)
#     bless_mailbox(db)
#     return db
# 
# Path: radarpost/user.py
# class User(Document):
#     """
#     This is a representation of a 'standard' couchdb user
#     document.  Creating one of these in the couchdb users
#     database (usually _users) adds a database user.  They 
#     can be created elsewhere if database level access is 
#     not desirable.
#     
#     See http://wiki.apache.org/couchdb/Security_Features_Overview
#     
#     """
#     def __init__(self, username=None, password=None):
#         """
#         User may be initialized with the special keyword arguments
#         username and password for creating a new User:
#         
#         u = User(username=joe, password="bl0w")
#         """
#         Document.__init__(self)
# 
#         if 'username' is not None: 
#             self.id = 'org.couchdb.user:%s' % username
#             self['name'] = username
#     
#         if password is not None:
#             self.set_password(password)
#         else: 
#             self.lock_password()
# 
#     @classmethod
#     def get_by_username(cls, db, username):
#         natid = 'org.couchdb.user:%s' % username
#         return cls.load(db, natid)
# 
#     @property
#     def username(self):
#         return self['name']
# 
#     def get_public_id(self):
#         # same for now.
#         return self.username
# 
#     type = TextField(default="user")
#     roles = ListField(TextField)
#     password_sha = TextField()
#     salt = TextField()
# 
#     def set_password(self, plaintext_password):
#         self.salt = _generate_salt()
#         self.password_sha = _password_hash(plaintext_password, self.salt)
# 
#     def lock_password(self):
#         self.password_sha = LOCKED_PASSWORD
#         
#     def check_password(self, plaintext_password):
#         if self.password_sha == LOCKED_PASSWORD:
#             return False
#         hashed_pw = _password_hash(plaintext_password, self.salt)
#         return self.password_sha == hashed_pw
# 
#     def is_anonymous(self):
#         return False
# 
#     def has_role(self, role, obj=None):
#         return role in self.roles
# 
#     def has_perm(self, perm, obj=None):
#         if self.has_role(ROLE_ADMIN):
#             return True
#         if perm == PERM_READ:
#             return True
#         return False
# 
# ROLE_ADMIN = 'radar:role:admin'
# 
# Path: radarpost/tests/helpers.py
# def load_test_config():
#     return load_config(get_config_filename())
# 
# Path: radarpost/web/context.py
# def build_routes(config):
#     router = routes.Mapper() #controller_scan=None)
# 
#     for app_module in app_ids(config):
#         # look for routes, fail quietly if the 
#         # router is missing.
#         try:
#             try:
#                 url_module = app_module + '.urls' 
#                 __import__(url_module)
#                 urls = sys.modules[url_module]
#             except ImportError:
#                 continue
#             try:
#                 urls.add_routes(router)
#             except AttributeError:
#                 continue
#         except:
#             log.error("Error loading app urls for '%s': %s" % 
#                       (app_module, traceback.format_exc()))
#             raise
# 
#         # introduce a route that allows reverse mapping a static file
#         # url based on the configuration.  In debug mode, we'll serve
#         # the files, but it is expected that in production actual file 
#         # hosting is handled by a separate web server or configured 
#         # as middleware.  In this case, if the route is ever reached, 
#         # the result is 404.
#         #
#         # the main purpose of having this route in either case is to be 
#         # able to call context.url_for('static_file', 'some/file/name.js') and 
#         # return the appropriate url based on the configuration.
#         # 
#         static_url = config['web.static_files_url']
#         if config['web.debug'] == False:
#             router.connect('static_file', '%s{path:.*?}' % static_url,
#                            action="always_404", controller='radarpost.web.context')
#         else:
#             router.connect('static_file', '%s{path:.*?}' % static_url,
#                            action="serve_static_file", controller='radarpost.web.context')
#             
#     return router
# 
# Path: radarpost/web/context.py
# def get_couchdb_server(self):
#     """
#     get a connection to the configured couchdb server. 
#     """
#     return get_couchdb_server(self.config)
# 
# def get_database_name(self, mailbox_slug):
#     """
#     get the database name corresponding to the 
#     given mailbox slug.
#     """
#     return get_database_name(self.config, mailbox_slug)
# 
# Path: radarpost/web/context.py
# def get_mailbox(self, mailbox_slug, couchdb=None):
#     """
#     get a connection to the couchdb database with the 
#     given slug. 
#     """
#     return get_mailbox(self.config, mailbox_slug, couchdb=couchdb)


from couchdb import Server, PreconditionFailed
from routes.util import URLGenerator
from unittest import TestCase
from radarpost.mailbox import create_mailbox as _create_mailbox
from radarpost.user import User, ROLE_ADMIN
from radarpost.tests.helpers import load_test_config
from radarpost.web.context import build_routes
from radarpost.web.context import get_couchdb_server, get_database_name
from radarpost.web.context import get_mailbox
        from radarpost.web.app import make_app
        from webtest import TestApp
import json

__all__ = ['RadarTestCase']

class RadarTestCase(TestCase):
    """
    helpful base class for testing web interfaces 
    of radarpost
    """
    
    TEST_MAILBOX_SLUG = '__rp_test_mailbox'

    def setUp(self):
        self.config = load_test_config()

# Path: radarpost/mailbox.py
# def create_mailbox(couchdb, dbname):
#     """
#     create a mailbox on the given server with the given name.
#     
#     raises PreconditionFailed if a database with the given name 
#            already exists.
#     raises ValueError if name is not a valid mailbox name.
#     """
#     db = couchdb.create(dbname)
#     bless_mailbox(db)
#     return db
# 
# Path: radarpost/user.py
# class User(Document):
#     """
#     This is a representation of a 'standard' couchdb user
#     document.  Creating one of these in the couchdb users
#     database (usually _users) adds a database user.  They 
#     can be created elsewhere if database level access is 
#     not desirable.
#     
#     See http://wiki.apache.org/couchdb/Security_Features_Overview
#     
#     """
#     def __init__(self, username=None, password=None):
#         """
#         User may be initialized with the special keyword arguments
#         username and password for creating a new User:
#         
#         u = User(username=joe, password="bl0w")
#         """
#         Document.__init__(self)
# 
#         if 'username' is not None: 
#             self.id = 'org.couchdb.user:%s' % username
#             self['name'] = username
#     
#         if password is not None:
#             self.set_password(password)
#         else: 
#             self.lock_password()
# 
#     @classmethod
#     def get_by_username(cls, db, username):
#         natid = 'org.couchdb.user:%s' % username
#         return cls.load(db, natid)
# 
#     @property
#     def username(self):
#         return self['name']
# 
#     def get_public_id(self):
#         # same for now.
#         return self.username
# 
#     type = TextField(default="user")
#     roles = ListField(TextField)
#     password_sha = TextField()
#     salt = TextField()
# 
#     def set_password(self, plaintext_password):
#         self.salt = _generate_salt()
#         self.password_sha = _password_hash(plaintext_password, self.salt)
# 
#     def lock_password(self):
#         self.password_sha = LOCKED_PASSWORD
#         
#     def check_password(self, plaintext_password):
#         if self.password_sha == LOCKED_PASSWORD:
#             return False
#         hashed_pw = _password_hash(plaintext_password, self.salt)
#         return self.password_sha == hashed_pw
# 
#     def is_anonymous(self):
#         return False
# 
#     def has_role(self, role, obj=None):
#         return role in self.roles
# 
#     def has_perm(self, perm, obj=None):
#         if self.has_role(ROLE_ADMIN):
#             return True
#         if perm == PERM_READ:
#             return True
#         return False
# 
# ROLE_ADMIN = 'radar:role:admin'
# 
# Path: radarpost/tests/helpers.py
# def load_test_config():
#     return load_config(get_config_filename())
# 
# Path: radarpost/web/context.py
# def build_routes(config):
#     router = routes.Mapper() #controller_scan=None)
# 
#     for app_module in app_ids(config):
#         # look for routes, fail quietly if the 
#         # router is missing.
#         try:
#             try:
#                 url_module = app_module + '.urls' 
#                 __import__(url_module)
#                 urls = sys.modules[url_module]
#             except ImportError:
#                 continue
#             try:
#                 urls.add_routes(router)
#             except AttributeError:
#                 continue
#         except:
#             log.error("Error loading app urls for '%s': %s" % 
#                       (app_module, traceback.format_exc()))
#             raise
# 
#         # introduce a route that allows reverse mapping a static file
#         # url based on the configuration.  In debug mode, we'll serve
#         # the files, but it is expected that in production actual file 
#         # hosting is handled by a separate web server or configured 
#         # as middleware.  In this case, if the route is ever reached, 
#         # the result is 404.
#         #
#         # the main purpose of having this route in either case is to be 
#         # able to call context.url_for('static_file', 'some/file/name.js') and 
#         # return the appropriate url based on the configuration.
#         # 
#         static_url = config['web.static_files_url']
#         if config['web.debug'] == False:
#             router.connect('static_file', '%s{path:.*?}' % static_url,
#                            action="always_404", controller='radarpost.web.context')
#         else:
#             router.connect('static_file', '%s{path:.*?}' % static_url,
#                            action="serve_static_file", controller='radarpost.web.context')
#             
#     return router
# 
# Path: radarpost/web/context.py
# def get_couchdb_server(self):
#     """
#     get a connection to the configured couchdb server. 
#     """
#     return get_couchdb_server(self.config)
# 
# def get_database_name(self, mailbox_slug):
#     """
#     get the database name corresponding to the 
#     given mailbox slug.
#     """
#     return get_database_name(self.config, mailbox_slug)
# 
# Path: radarpost/web/context.py
# def get_mailbox(self, mailbox_slug, couchdb=None):
#     """
#     get a connection to the couchdb database with the 
#     given slug. 
#     """
#     return get_mailbox(self.config, mailbox_slug, couchdb=couchdb)


# Path: radarpost/web/tests.py
from couchdb import Server, PreconditionFailed
from routes.util import URLGenerator
from unittest import TestCase
from radarpost.mailbox import create_mailbox as _create_mailbox
from radarpost.user import User, ROLE_ADMIN
from radarpost.tests.helpers import load_test_config
from radarpost.web.context import build_routes
from radarpost.web.context import get_couchdb_server, get_database_name
from radarpost.web.context import get_mailbox
        from radarpost.web.app import make_app
        from webtest import TestApp
import json


__all__ = ['RadarTestCase']

class RadarTestCase(TestCase):
    """
    helpful base class for testing web interfaces 
    of radarpost
    """
    
    TEST_MAILBOX_SLUG = '__rp_test_mailbox'

    def setUp(self):
        self.config = load_test_config()

        self.url_gen = URLGenerator(build_routes(self.config), {})