# 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 make_fake_space(store, name):
#     """
#     Call the spaces api to create a space.
#     """
#     make_space(name, store, name)
# 
# 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: tiddlywebplugins/tiddlyspace/spaces.py
# def change_space_member(store, space_name, add=None, remove=None,
#         current_user=None):
#     """
#     The guts of adding a member to space.
#     """
#     try:
#         space = Space(space_name)
#     except ValueError, exc:
#         raise HTTP404('Space %s invalid: %s' % (space_name, exc))
#     private_bag = store.get(Bag(space.private_bag()))
# 
#     if current_user:
#         private_bag.policy.allows(current_user, 'manage')
# 
#     if remove and len(private_bag.policy.manage) == 1:
#         raise HTTP403('must not remove the last member from a space')
# 
#     # This will throw NoUserError (to be handled by the caller)
#     # if the user does not exist.
#     if add:
#         store.get(User(add))
# 
#     bags = [store.get(Bag(bag)) for bag in space.list_bags()]
#     recipes = [store.get(Recipe(bag)) for bag in space.list_recipes()]
#     for entity in bags + recipes:
#         new_policy = _update_policy(entity.policy, add=add, subtract=remove)
#         entity.policy = new_policy
#         store.put(entity)


from test.fixtures import make_test_env, make_fake_space, get_auth
from wsgi_intercept import httplib2_intercept
from tiddlyweb.model.tiddler import Tiddler
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.model.user import User
from tiddlywebplugins.tiddlyspace.spaces import change_space_member
import wsgi_intercept
import httplib2
import simplejson






def setup_module(module):
    make_test_env(module)
    httplib2_intercept.install()
    wsgi_intercept.add_wsgi_intercept('0.0.0.0', 8080, app_fn)
    wsgi_intercept.add_wsgi_intercept('thing.0.0.0.0', 8080, app_fn)
    wsgi_intercept.add_wsgi_intercept('foo.0.0.0.0', 8080, app_fn)
    user = User('thingone')
    user.set_password('how')
    store.put(user)
    user = User('thingtwo')
    user.set_password('how')
    store.put(user)
    module.http = httplib2.Http()


def test_create_spaces():

# 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 make_fake_space(store, name):
#     """
#     Call the spaces api to create a space.
#     """
#     make_space(name, store, name)
# 
# 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: tiddlywebplugins/tiddlyspace/spaces.py
# def change_space_member(store, space_name, add=None, remove=None,
#         current_user=None):
#     """
#     The guts of adding a member to space.
#     """
#     try:
#         space = Space(space_name)
#     except ValueError, exc:
#         raise HTTP404('Space %s invalid: %s' % (space_name, exc))
#     private_bag = store.get(Bag(space.private_bag()))
# 
#     if current_user:
#         private_bag.policy.allows(current_user, 'manage')
# 
#     if remove and len(private_bag.policy.manage) == 1:
#         raise HTTP403('must not remove the last member from a space')
# 
#     # This will throw NoUserError (to be handled by the caller)
#     # if the user does not exist.
#     if add:
#         store.get(User(add))
# 
#     bags = [store.get(Bag(bag)) for bag in space.list_bags()]
#     recipes = [store.get(Recipe(bag)) for bag in space.list_recipes()]
#     for entity in bags + recipes:
#         new_policy = _update_policy(entity.policy, add=add, subtract=remove)
#         entity.policy = new_policy
#         store.put(entity)


# Path: test/test_privacy.py
from test.fixtures import make_test_env, make_fake_space, get_auth
from wsgi_intercept import httplib2_intercept
from tiddlyweb.model.tiddler import Tiddler
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.model.user import User
from tiddlywebplugins.tiddlyspace.spaces import change_space_member
import wsgi_intercept
import httplib2
import simplejson







def setup_module(module):
    make_test_env(module)
    httplib2_intercept.install()
    wsgi_intercept.add_wsgi_intercept('0.0.0.0', 8080, app_fn)
    wsgi_intercept.add_wsgi_intercept('thing.0.0.0.0', 8080, app_fn)
    wsgi_intercept.add_wsgi_intercept('foo.0.0.0.0', 8080, app_fn)
    user = User('thingone')
    user.set_password('how')
    store.put(user)
    user = User('thingtwo')
    user.set_password('how')
    store.put(user)
    module.http = httplib2.Http()


def test_create_spaces():

    cookie = get_auth('thingone', 'how')