# Path: test/fixtures.py
# def make_test_env(module, hsearch=False):
#     """
#     If hsearch is False, don't bother updating the whoosh index
#     for this test instance. We do this by removing the store HOOK
#     used by whoosh.
#     """
#     global SESSION_COUNT
# 
#     # bump up a level if we're already in the test instance
#     if os.getcwd().endswith('test_instance'):
#         os.chdir('..')
# 
#     try:
#         shutil.rmtree('test_instance')
#     except:
#         pass
# 
#     os.system('echo "drop database if exists tiddlyspacetest; create database tiddlyspacetest character set = utf8mb4 collate = utf8mb4_bin;" | mysql')
#     if SESSION_COUNT > 1:
#         del sys.modules['tiddlywebplugins.tiddlyspace.store']
#         del sys.modules['tiddlywebplugins.mysql3']
#         del sys.modules['tiddlywebplugins.sqlalchemy3']
#         import tiddlywebplugins.tiddlyspace.store
#         import tiddlywebplugins.mysql3
#         import tiddlywebplugins.sqlalchemy3
#         tiddlywebplugins.mysql3.Session.remove()
#         clear_hooks(HOOKS)
#     SESSION_COUNT += 1
#     db_config = init_config['server_store'][1]['db_config']
#     db_config = db_config.replace('///tiddlyspace?', '///tiddlyspacetest?')
#     init_config['server_store'][1]['db_config'] = db_config
#     init_config['log_level'] = 'DEBUG'
# 
#     if sys.path[0] != os.getcwd():
#         sys.path.insert(0, os.getcwd())
#     spawn('test_instance', init_config, instance_module)
#     os.chdir('test_instance')
#     os.symlink('../tiddlywebplugins/templates', 'templates')
#     os.symlink('../tiddlywebplugins', 'tiddlywebplugins')
# 
#     from tiddlyweb.web import serve
#     module.store = get_store(init_config)
# 
#     app = serve.load_app()
# 
#     if not hsearch:
#         from tiddlywebplugins.whoosher import _tiddler_change_handler
#         try:
#             HOOKS['tiddler']['put'].remove(_tiddler_change_handler)
#             HOOKS['tiddler']['delete'].remove(_tiddler_change_handler)
#         except ValueError:
#             pass
# 
#     def app_fn():
#         return app
#     module.app_fn = app_fn
# 
# def get_auth(username, password):
#     http = httplib2.Http()
#     response, _ = http.request(
#             'http://0.0.0.0:8080/challenge/tiddlywebplugins.tiddlyspace.cookie_form',
#             body='user=%s&password=%s' % (username, password),
#             method='POST',
#             headers={'Content-Type': 'application/x-www-form-urlencoded'})
#     assert response.previous['status'] == '303'
# 
#     user_cookie = response.previous['set-cookie']
#     cookie = Cookie.SimpleCookie()
#     cookie.load(user_cookie)
#     return cookie['tiddlyweb_user'].value


import simplejson
import httplib2
import wsgi_intercept
from wsgi_intercept import httplib2_intercept
from test.fixtures import make_test_env, get_auth
"""
Test user creation, especially with regard
to validating names. This is important in the
tiddlyspace setting (as opposed to
tiddlywebplugins.socialusers which uses handles
user creation) because usernames are more constrained
there than they are in generic TiddlyWeb. The 
constraints are:

    * the usernames must be valid hostnames
    * the usernames must not conflict with the
      names of existing spaces and users

socialusers checks to see if a user exists after
it has validated the username. TiddlySpace needs
to override socialusers:_validate_user with its
own routines. These routines should check the above
constraints.
"""





def setup_module(module):

# Path: test/fixtures.py
# def make_test_env(module, hsearch=False):
#     """
#     If hsearch is False, don't bother updating the whoosh index
#     for this test instance. We do this by removing the store HOOK
#     used by whoosh.
#     """
#     global SESSION_COUNT
# 
#     # bump up a level if we're already in the test instance
#     if os.getcwd().endswith('test_instance'):
#         os.chdir('..')
# 
#     try:
#         shutil.rmtree('test_instance')
#     except:
#         pass
# 
#     os.system('echo "drop database if exists tiddlyspacetest; create database tiddlyspacetest character set = utf8mb4 collate = utf8mb4_bin;" | mysql')
#     if SESSION_COUNT > 1:
#         del sys.modules['tiddlywebplugins.tiddlyspace.store']
#         del sys.modules['tiddlywebplugins.mysql3']
#         del sys.modules['tiddlywebplugins.sqlalchemy3']
#         import tiddlywebplugins.tiddlyspace.store
#         import tiddlywebplugins.mysql3
#         import tiddlywebplugins.sqlalchemy3
#         tiddlywebplugins.mysql3.Session.remove()
#         clear_hooks(HOOKS)
#     SESSION_COUNT += 1
#     db_config = init_config['server_store'][1]['db_config']
#     db_config = db_config.replace('///tiddlyspace?', '///tiddlyspacetest?')
#     init_config['server_store'][1]['db_config'] = db_config
#     init_config['log_level'] = 'DEBUG'
# 
#     if sys.path[0] != os.getcwd():
#         sys.path.insert(0, os.getcwd())
#     spawn('test_instance', init_config, instance_module)
#     os.chdir('test_instance')
#     os.symlink('../tiddlywebplugins/templates', 'templates')
#     os.symlink('../tiddlywebplugins', 'tiddlywebplugins')
# 
#     from tiddlyweb.web import serve
#     module.store = get_store(init_config)
# 
#     app = serve.load_app()
# 
#     if not hsearch:
#         from tiddlywebplugins.whoosher import _tiddler_change_handler
#         try:
#             HOOKS['tiddler']['put'].remove(_tiddler_change_handler)
#             HOOKS['tiddler']['delete'].remove(_tiddler_change_handler)
#         except ValueError:
#             pass
# 
#     def app_fn():
#         return app
#     module.app_fn = app_fn
# 
# def get_auth(username, password):
#     http = httplib2.Http()
#     response, _ = http.request(
#             'http://0.0.0.0:8080/challenge/tiddlywebplugins.tiddlyspace.cookie_form',
#             body='user=%s&password=%s' % (username, password),
#             method='POST',
#             headers={'Content-Type': 'application/x-www-form-urlencoded'})
#     assert response.previous['status'] == '303'
# 
#     user_cookie = response.previous['set-cookie']
#     cookie = Cookie.SimpleCookie()
#     cookie.load(user_cookie)
#     return cookie['tiddlyweb_user'].value


# Path: test/test_user.py
import simplejson
import httplib2
import wsgi_intercept
from wsgi_intercept import httplib2_intercept
from test.fixtures import make_test_env, get_auth

"""
Test user creation, especially with regard
to validating names. This is important in the
tiddlyspace setting (as opposed to
tiddlywebplugins.socialusers which uses handles
user creation) because usernames are more constrained
there than they are in generic TiddlyWeb. The 
constraints are:

    * the usernames must be valid hostnames
    * the usernames must not conflict with the
      names of existing spaces and users

socialusers checks to see if a user exists after
it has validated the username. TiddlySpace needs
to override socialusers:_validate_user with its
own routines. These routines should check the above
constraints.
"""





def setup_module(module):

    make_test_env(module)