def setup_module(module):
    if os.path.exists('store'):
        shutil.rmtree('store')
    init(config)
    module.savedin = sys.stdin
    module.store = get_store(config)
    module.store.put(Bag('bag1'))
示例#2
0
 def twimport(args):
     """Import tiddlers, recipes, wikis, binary content: <bag> <URI>"""
     bag = args[0]
     urls = args[1:]
     if not bag or not urls:
         raise IndexError('missing args')
     import_list(bag, urls, get_store(config))
示例#3
0
def setup_module(module):
    try:
        shutil.rmtree('indexdir')
        shutil.rmtree('store')
    except:
        pass
    app = load_app()

    def app_fn(): return app

    requests_intercept.install()
    wsgi_intercept.add_wsgi_intercept('tankt.peermore.com', 8080, app_fn)

    store = get_store(config)
    test_bag1 = Bag('newtank')

    try:
        store.delete(test_bag1)
    except StoreError:
        pass

    test_bag1.policy.accept = ['NONE']
    store.put(test_bag1)
    module.environ = {'tiddlyweb.store': store, 'tiddlyweb.config': config}
    module.store = store
    module.cookie, module.csrf = establish_user_auth(config, store,
            'tankt.peermore.com:8080', 'tester')
def test_in_a_recipe():
    from tiddlyweb.config import config
    store = get_store(config)
    bag = Bag('hi')
    store.put(bag)
    tiddler = Tiddler('thing1', 'hi')
    tiddler.tags = ['research']
    store.put(tiddler)
    tiddler = Tiddler('thing2', 'hi')
    store.put(tiddler)

    recipe1 = Recipe('oi')
    recipe1.set_recipe([('hi', 'mselect=tag:research')])
    recipe1.store = store
    recipe2 = Recipe('coi')
    recipe2.set_recipe([('hi', 'select=tag:research')])
    recipe2.store = store
    recipe3 = Recipe('boi')
    recipe3.set_recipe([('hi', '')])
    recipe3.store = store
    environ = {'tiddlyweb.store': store}
    tiddlers = list(control.get_tiddlers_from_recipe(recipe1, environ))
    assert len(tiddlers) == 1
    tiddlers = list(control.get_tiddlers_from_recipe(recipe2, environ))
    assert len(tiddlers) == 1
    tiddlers = list(control.get_tiddlers_from_recipe(recipe3, environ))
    assert len(tiddlers) == 2
def setup_module(module):

    module.store = get_store(config)

    # cascade to deal with differently named files depending on 
    # anydbm impelementation
    try:
        os.unlink('links.db')
    except OSError:
        pass  # not there
    module.links_manager = LinksManager()

    try:
        shutil.rmtree('store')
    except:
        pass
    
    def app():
        return serve.load_app()
    httplib2_intercept.install()
    wsgi_intercept.add_wsgi_intercept('0.0.0.0', 8080, app)

    # for @someone syntax to test correctly we need a corresponding
    # recipe
    module.store.put(Bag('cdent_public'))
    recipe = Recipe('cdent_public')
    recipe.set_recipe([('cdent_public', '')])
    module.store.put(recipe)
示例#6
0
def make_test_env(module):
    global SESSION_COUNT
    try:
        shutil.rmtree('test_instance')
    except:
        pass

    os.system('mysqladmin -f drop tiddlyspacetest create tiddlyspacetest')
    if SESSION_COUNT > 1:
        del sys.modules['tiddlywebplugins.tiddlyspace.store']
        del sys.modules['tiddlywebplugins.mysql2']
        del sys.modules['tiddlywebplugins.sqlalchemy2']
        import tiddlywebplugins.tiddlyspace.store
        import tiddlywebplugins.mysql2
        import tiddlywebplugins.sqlalchemy2
        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.symlink('../tiddlywebplugins/templates', 'templates')

    from tiddlyweb.web import serve
    module.store = get_store(config)

    app = serve.load_app()

    def app_fn():
        return app
    module.app_fn = app_fn
示例#7
0
def setup_module(module):
    try:
        shutil.rmtree('indexdir')
        shutil.rmtree('store')
    except:
        pass

    app = load_app()

    def app_fn(): return app

    httplib2_intercept.install()
    wsgi_intercept.add_wsgi_intercept('tankt.peermore.com', 8080, app_fn)

    store = get_store(config)
    test_bag = Bag('editable')

    try:
        store.delete(test_bag)
    except StoreError:
        pass

    store.put(test_bag)
    module.environ = {'tiddlyweb.store': store, 'tiddlyweb.config': config}
    module.store = store
    module.http = httplib2.Http()
    module.csrf = None
示例#8
0
def test_cookie_set():
    """
    test that we get a cookie relating to the space we are in
    """
    store = get_store(config)
    space = 'foo'
    make_fake_space(store, space)
    user = User('foo')
    user.set_password('foobar')
    store.put(user)

    user_cookie = get_auth('foo', 'foobar')

    response, _ = http.request(
        'http://foo.0.0.0.0:8080/status',
        method='GET',
        headers={'Cookie': 'tiddlyweb_user="******"' % user_cookie})

    assert response['status'] == '200'

    time = datetime.now().strftime('%Y%m%d%H')
    cookie = 'csrf_token=%s:%s:%s' % (
        time, user.usersign,
        sha('%s:%s:%s:%s' %
            (user.usersign, time, space, config['secret'])).hexdigest())
    assert response['set-cookie'] == cookie
def setup_module(module):
    try:
        shutil.rmtree('store')
    except:
        pass
    config['markdown.wiki_link_base'] = ''
    store = get_store(config)

    environ['tiddlyweb.store'] = store

    store.put(Bag('bag'))
    module.store = store

    recipe = Recipe('recipe_public')
    recipe.set_recipe([('bag', '')])
    store.put(recipe)

    tiddlerA = Tiddler('tiddler a', 'bag')
    tiddlerA.text = 'I am _tiddler_'
    store.put(tiddlerA)

    tiddlerB = Tiddler('tiddler b')
    tiddlerB.text = '''
You wish

{{tiddler a}}

And I wish too.
'''
    module.tiddlerB = tiddlerB
 def twimport(args):
     """Import tiddlers, recipes, wikis, binary content: <bag> <URI>"""
     bag = args[0]
     urls = args[1:]
     if not bag or not urls:
         raise IndexError('missing args')
     import_list(bag, urls, get_store(config))
def setup_module(module):
    module.store = get_store(config)
    module.environ = {"tiddlyweb.config": config, "tiddlyweb.store": module.store}
    session = module.store.storage.session
    # delete everything
    Base.metadata.drop_all()
    Base.metadata.create_all()
示例#12
0
def init(config):
    """
    init function for tiddlywebpages.
    Set URLs
    define serializers
    """
    merge_config(config, twp_config)

    #provide a way to allow people to refresh their URLs
    config['selector'].add('/tiddlywebpages/refresh', GET=refresh)

    #get the store
    store = get_store(config)

    #set the default config info
    BAG_OF_TEMPLATES = config['tw_pages']['template_bag']

    if 'config' in config['tw_pages']:
        register_config(config, store)

    for new_filter in config['tw_pages']['filters']:
        _temp = __import__(new_filter, {}, {}, [new_filter])
        TW_PAGES_FILTERS.append((new_filter, getattr(_temp, new_filter)))

    if 'config' in config['tw_pages']:
        register_config(config, store)
    register_templates(config, store)
def url(args):
    """Add a URL via tiddlywebplugins.URLs. Redirect is optional. [--redirect] <selector_path> <destination_url>"""
    if 2 != len(args) != 3:
        print >> sys.stderr, ('you must include both the path you want to use (selector path) and the destination url')
        
    store = get_store(config)
    
    if args[0] == '--redirect':
        redirect = args.pop(0).lstrip('-')
    else:
        redirect = None
    
    selector_path = args[0]
    destination_url = args[1]
    
    tiddler = Tiddler(selector_path)
    tiddler.bag = config['url_bag']
    
    tiddler.text = destination_url
    if redirect:
        tiddler.tags = [redirect]
    
    if validate_url(tiddler):
        store.put(tiddler)
    
    return True
示例#14
0
def test_no_cookie_sent():
    """
    Test no cookie is sent if one is already present
    """
    store = get_store(config)
    space = 'foo'
    make_fake_space(store, space)
    user = User('foo')
    user.set_password('foobar')
    store.put(user)

    user_cookie = get_auth('foo', 'foobar')
    time = datetime.now().strftime('%Y%m%d%H')
    cookie = 'csrf_token=%s:%s:%s' % (
        time, user.usersign,
        sha('%s:%s:%s:%s' %
            (user.usersign, time, space, config['secret'])).hexdigest())

    response, _ = http.request(
        'http://foo.0.0.0.0:8080/status',
        method='GET',
        headers={'Cookie': 'tiddlyweb_user="******"; %s' % (user_cookie, cookie)})

    cookie = response.get('set-cookie')
    if cookie:
        assert 'csrf_token' not in cookie
示例#15
0
def test_validator_nonce_fail():
    """
    test the validator directly
    ensure that it fails when the nonce doesn't match
    """
    store = get_store(config)
    nonce = 'dwaoiju277218ywdhdnakas72'
    username = '******'
    spacename = 'foo'
    secret = '12345'
    timestamp = datetime.now().strftime('%Y%m%d%H')
    environ = {
        'tiddlyweb.usersign': {
            'name': username
        },
        'tiddlyweb.config': {
            'secret': secret,
            'server_host': {
                'host': '0.0.0.0',
                'port': '8080'
            }
        },
        'HTTP_HOST': 'foo.0.0.0.0:8080'
    }
    make_fake_space(store, spacename)

    try:
        csrf = CSRFProtector({})
        result = csrf.check_csrf(environ, nonce)
        raise AssertionError('check_csrf succeeded when nonce didn\'t match')
    except InvalidNonceError, exc:
        assert exc.message == BAD_MATCH_MESSAGE
示例#16
0
def setup_module(module):
    module.TMPDIR = tempfile.mkdtemp()

    _initialize_app(TMPDIR)
    module.ADMIN_COOKIE = make_cookie('tiddlyweb_user', 'admin',
            mac_key=CONFIG['secret'])

    module.STORE = get_store(CONFIG)

    # register admin user
    data = {
        'username': '******',
        'password': '******',
        'password_confirmation': 'secret'
    }
    response, content = _req('POST', '/register', urlencode(data),
            headers={ 'Content-Type': 'application/x-www-form-urlencoded' })

    bag = Bag('alpha')
    bag.policy = Policy(read=['admin'], write=['admin'], create=['admin'],
            delete=['admin'], manage=['admin'])
    STORE.put(bag)

    bag = Bag('bravo')
    STORE.put(bag)

    bag = Bag('charlie')
    bag.policy = Policy(read=['nobody'], write=['nobody'], create=['nobody'],
            delete=['nobody'], manage=['nobody'])
    STORE.put(bag)

    tiddler = Tiddler('index', 'bravo')
    tiddler.text = 'lorem ipsum\ndolor *sit* amet'
    tiddler.type = 'text/x-markdown'
    STORE.put(tiddler)
示例#17
0
def setup_module(module):
    try:
        shutil.rmtree('indexdir')
        shutil.rmtree('store')
    except:
        pass
    app = load_app()

    def app_fn():
        return app

    requests_intercept.install()
    wsgi_intercept.add_wsgi_intercept('tankt.peermore.com', 8080, app_fn)

    store = get_store(config)
    test_bag1 = Bag('newtank')

    try:
        store.delete(test_bag1)
    except StoreError:
        pass

    test_bag1.policy.accept = ['NONE']
    store.put(test_bag1)
    module.environ = {'tiddlyweb.store': store, 'tiddlyweb.config': config}
    module.store = store
    module.cookie, module.csrf = establish_user_auth(
        config, store, 'tankt.peermore.com:8080', 'tester')
示例#18
0
def test_validator_nonce_success():
    """
    test the validator directly
    ensure that it succeeds when the nonce passed in is correct
    """
    store = get_store(config)
    username = '******'
    spacename = 'foo'
    secret = '12345'
    timestamp = datetime.now().strftime('%Y%m%d%H')
    nonce = '%s:%s:%s' % (
        timestamp, username,
        sha('%s:%s:%s:%s' %
            (username, timestamp, spacename, secret)).hexdigest())
    environ = {
        'tiddlyweb.usersign': {
            'name': username
        },
        'tiddlyweb.config': {
            'secret': secret,
            'server_host': {
                'host': '0.0.0.0',
                'port': '8080'
            }
        },
        'HTTP_HOST': 'foo.0.0.0.0:8080'
    }
    make_fake_space(store, spacename)

    csrf = CSRFProtector({})
    result = csrf.check_csrf(environ, nonce)

    assert result == True
def test_cookie_set():
    """
    test that we get a cookie relating to the space we are in
    """
    store = get_store(config)
    space = 'foo'
    make_fake_space(store, space)
    user = User('foo')
    user.set_password('foobar')
    store.put(user)

    user_cookie = get_auth('foo', 'foobar')

    response, content = http.request('http://foo.0.0.0.0:8080/status',
        method='GET',
        headers={
            'Cookie': 'tiddlyweb_user="******"' % user_cookie
        })

    assert response['status'] == '200', content

    time = datetime.now().strftime('%Y%m%d%H')
    cookie = 'csrf_token=%s:%s:%s' % (time, user.usersign,
        sha('%s:%s:%s:%s' % (user.usersign,
        time, space, config['secret'])).hexdigest())
    assert response['set-cookie'] == cookie
示例#20
0
    def _init_store(self, struct):
        """
        creates basic store structure with bags, recipes and users

        (no support for user passwords for security reasons)
        """
        store = get_store(self.init_config)

        bags = struct.get("bags", {})
        for name, data in bags.items():
            desc = data.get("desc")
            bag = Bag(name, desc=desc)
            constraints = data.get("policy", {})
            _set_policy(bag, constraints)
            store.put(bag)

        recipes = struct.get("recipes", {})
        for name, data in recipes.items():  # TODO: DRY
            desc = data.get("desc")
            recipe = Recipe(name, desc=desc)
            recipe.set_recipe(data["recipe"])
            constraints = data.get("policy", {})
            _set_policy(recipe, constraints)
            store.put(recipe)

        users = struct.get("users", {})
        for name, data in users.items():
            note = data.get("note")
            user = User(name, note=note)
            password = data.get("_password")
            if password:
                user.set_password(password)
            for role in data.get("roles", []):
                user.add_role(role)
            store.put(user)
示例#21
0
def setup_module(module):
    reset_textstore()
    module.store = get_store(config)
    module.environ = {
        'tiddlyweb.config': config,
        'tiddlyweb.store': module.store
    }
示例#22
0
def tiddler(context, *args):
    base = context.var_get_text("$BASE_URL")
    path =""
    if not context.environ:
      return ""
    
    store = get_store(config)
    logging.debug("in tiddler macro")
    try:
        environ = context.environ
    except NoAttributeError:
        return _throw_error()
    try:
        tid = context.tiddler
    except NoAttributeError:
        return ""
    if tid.recipe:
        tids = control.get_tiddlers_from_recipe(store.get(Recipe(tid.recipe)))
    elif tid.bag:
        bag = store.get(Bag(tid.bag))
        tids = bag.list_tiddlers()
    else:
        return ""
    try:
        tiddler_requested = args[0].text
        for tiddler in tids:
            if tiddler.title == tiddler_requested:
                tiddler = store.get(tiddler)
    except Exception:
        tiddler = context.tiddler
    if tiddler:
        text = wikitext_to_wikklyhtml(base,path, tiddler.text, environ,tiddler=context.tiddler)
        return "%s"%text
    else:
        return ""#no tiddler with that name
 def wreindex(args):
     """Rebuild the entire whoosh index."""
     try:
         prefix = args[0]
     except IndexError:
         prefix = None
     store = get_store(config)
     schema = config.get('wsearch.schema',
             SEARCH_DEFAULTS['wsearch.schema'])
     if __name__ in config.get('beanstalk.listeners', []):
         _reindex_async(config)
     else:
         for bag in store.list_bags():
             bag = store.get(bag)
             writer = get_writer(config)
             if writer:
                 try:
                     try:
                         tiddlers = bag.get_tiddlers()
                     except AttributeError:
                         tiddlers = store.list_bag_tiddlers(bag)
                     for tiddler in tiddlers:
                         if prefix and not tiddler.title.startswith(prefix):
                             continue
                         tiddler = store.get(tiddler)
                         index_tiddler(tiddler, schema, writer)
                     writer.commit()
                 except:
                     LOGGER.debug('whoosher: exception while indexing: %s',
                             format_exc())
                     writer.cancel()
             else:
                 LOGGER.debug('whoosher: unable to get writer '
                         '(locked) for %s', bag.name)
def delete_subscription(email):
    """
    remove somebody from a particular subscription
    """
    store = get_store(config)
    recipe = determine_bag(email['to'])
    fromAddress = email['from']  
    subscription_bag = get_subscriptions_bag(store)

    try:
        subscribers_tiddler = store.get(Tiddler('bags/%s/tiddlers' % recipe, subscription_bag))
        subscriber_emails = subscribers_tiddler.text.splitlines()
        try:
            subscriber_emails.remove(fromAddress)
        except ValueError:
            pass

        subscribers_tiddler.text = '\n'.join(subscriber_emails)
        store.put(subscribers_tiddler)
    except NoTiddlerError:
        pass
  
    return {'from': email['to'],
        'to': email['from'],
        'subject': 'You have been unsubscribed from %s' % recipe,
        'body': 'Harry the dog is currently whining in sadness.'
    }
def make_subscription(email):
    """
    add somebody to a subscription
    """
    store = get_store(config)
    recipe = determine_bag(email['to'])
    fromAddress = email['from']
    subscription_bag = get_subscriptions_bag(store)
    subscribers_tiddler = Tiddler('bags/%s/tiddlers' % recipe, subscription_bag)
    try:
        subscribers_tiddler = store.get(subscribers_tiddler)
        subscriber_emails = subscribers_tiddler.text.splitlines()
        if fromAddress not in subscriber_emails:
            subscriber_emails.append(fromAddress)
        subscribers_tiddler.text = '\n'.join(subscriber_emails)
        store.put(subscribers_tiddler)
    except NoTiddlerError:
        subscribers_tiddler.text = fromAddress 
        store.put(subscribers_tiddler)

    return {'from': email['to'],
        'to': email['from'],
        'subject': 'You have subscribed to %s' % recipe,
        'body': 'You will now receive daily digests. To unsubscribe please email unsubscribe@%s'
    }
def test_validator_nonce_fail():
    """
    test the validator directly
    ensure that it fails when the nonce doesn't match
    """
    store = get_store(config)
    nonce = 'dwaoiju277218ywdhdnakas72'
    username = '******'
    spacename = 'foo'
    secret = '12345'
    timestamp = datetime.now().strftime('%Y%m%d%H')
    environ = {
       'tiddlyweb.usersign': {'name': username},
       'tiddlyweb.config': {
           'secret': secret,
           'server_host': {
               'host': '0.0.0.0',
               'port': '8080'
           }
        },
        'HTTP_HOST': 'foo.0.0.0.0:8080'
    }
    make_fake_space(store, spacename)

    try:
        csrf = CSRFProtector({})
        result = csrf.check_csrf(environ, nonce)
        raise AssertionError('check_csrf succeeded when nonce didn\'t match')
    except InvalidNonceError, exc:
        assert exc.message == BAD_MATCH_MESSAGE
def test_validator_nonce_success():
    """
    test the validator directly
    ensure that it succeeds when the nonce passed in is correct
    """
    store = get_store(config)
    username = '******'
    spacename = 'foo'
    secret = '12345'
    timestamp = datetime.now().strftime('%Y%m%d%H')
    nonce = '%s:%s:%s' % (timestamp, username,
        sha('%s:%s:%s:%s' % (username, timestamp, spacename, secret)).
        hexdigest())
    environ = {
       'tiddlyweb.usersign': {'name': username},
       'tiddlyweb.config': {
           'secret': secret,
           'server_host': {
               'host': '0.0.0.0',
               'port': '8080'
           }
        },
        'HTTP_HOST': 'foo.0.0.0.0:8080'
    }
    make_fake_space(store, spacename)

    csrf = CSRFProtector({})
    result = csrf.check_csrf(environ, nonce)

    assert result == True
def test_no_cookie_sent():
    """
    Test no cookie is sent if one is already present
    """
    store = get_store(config)
    space = 'foo'
    make_fake_space(store, space)
    user = User('foo')
    user.set_password('foobar')
    store.put(user)

    user_cookie = get_auth('foo', 'foobar')
    time = datetime.now().strftime('%Y%m%d%H')
    cookie = 'csrf_token=%s:%s:%s' % (time, user.usersign,
        sha('%s:%s:%s:%s' % (user.usersign,
        time, space, config['secret'])).hexdigest())

    response, _ = http.request('http://foo.0.0.0.0:8080/status',
        method='GET',
        headers={
            'Cookie': 'tiddlyweb_user="******"; %s' % (user_cookie, cookie)
        })

    cookie = response.get('set-cookie')
    if cookie:
        assert 'csrf_token' not in cookie
示例#29
0
def retrieve_from_store(email):
    """
    get the tiddler requested by the email from the store 
    and return it as an email
    """
    store = get_store(config)
    tiddler_title = clean_subject(email["subject"])
    tiddler = Tiddler(tiddler_title)
    bag = determine_bag(email["to"])
    tiddler.bag = bag

    try:
        tiddler = store.get(tiddler)
        response_text = tiddler.text
    except NoTiddlerError:
        # Tiddler not found. Return a list of all tiddlers
        bag = Bag(bag)
        bag = store.get(bag)
        response_text = "The following tiddlers are in %s:\n" % email["to"].split("@")[1]
        tiddlers = bag.gen_tiddlers()
        tiddlers = [tiddler for tiddler in tiddlers]
        response_text += "\n".join([tiddler.title for tiddler in tiddlers])

    response_email = {"from": email["to"], "to": email["from"], "subject": tiddler.title, "body": response_text}

    return response_email
示例#30
0
def determine_entries(environ):
    """
    returns descendant resources based on the WSGI environment
    """
    candidates = { # XXX: hard-coded; ideally descendants should be determined via HATEOAS-y clues
        "[/]": lambda *args: [Collection("/bags"), Collection("/recipes")],
        "/bags[.{format}]": _bags,
        "/recipes[.{format}]": _recipes,
        "/bags/{bag_name:segment}/tiddlers[.{format}]": _tiddlers
    }

    current_uri = environ["SCRIPT_NAME"]
    config = environ["tiddlyweb.config"]
    store = get_store(config)
    router = Router(mapfile=config["urls_map"], prefix=config["server_prefix"]) # XXX: does not support extensions

    for regex, supported_methods in router.mappings:
        if regex.search(current_uri): # matching route
            pattern = router.routes[regex]
            descendants = candidates[pattern]
            routing_args = environ["wsgiorg.routing_args"]
            descendants = descendants(store, *routing_args[0], **routing_args[1])
            break

    return chain([Collection(current_uri)], descendants)
def setup_module(module):
    """
    clean up the store, establish a registered client
    """
    clean_store()
    module.store = get_store(config)
    environ = {'tiddlyweb.config': config, 'tiddlyweb.store': module.store}
    ensure_bags(config)

    # make an application and store that info
    app = create(name='testapp', owner='appowner1',
            app_url='http://our_test_domain:8001',
            callback_url='http://our_test_domain:8001/_oauth/callback')

    client_id = app.title
    client_secret = app.fields['client_secret']
    store_app(environ, app)

    config['oauth.servers']['testserver']['client_id'] = client_id
    config['oauth.servers']['testserver']['client_secret'] = client_secret

    module.client_id = client_id

    initialize_app(config)

    module.http = Http()

    # we need a user who is going to use the client app
    user = User('cdent')
    user.set_password('cowpig')
    module.store.put(user)
def test_cookie_set():
    """
    test that we get a cookie relating to the space we are in
    """
    store = get_store(config)
    hostname = "foo.0.0.0.0:8080"
    user = User(u"f\u00F6o")
    user.set_password("foobar")
    store.put(user)

    user_cookie = get_auth(u"f\u00F6o", "foobar")

    response, content = http.request(
        "http://foo.0.0.0.0:8080/", method="GET", headers={"Cookie": 'tiddlyweb_user="******"' % user_cookie}
    )

    assert response["status"] == "200", content

    time = datetime.utcnow().strftime("%Y%m%d%H")
    cookie = "csrf_token=%s:%s:%s" % (
        time,
        user.usersign,
        sha("%s:%s:%s:%s" % (user.usersign, time, hostname, config["secret"])).hexdigest(),
    )
    assert response["set-cookie"] == quote(cookie.encode("utf-8"), safe=".!~*'():=")
def _reindex_async(config):
    from tiddlywebplugins.dispatcher.listener import (DEFAULT_BEANSTALK_HOST,
            DEFAULT_BEANSTALK_PORT, BODY_SEPARATOR)
    import beanstalkc
    beanstalk_host = config.get('beanstalk.host', DEFAULT_BEANSTALK_HOST)
    beanstalk_port = config.get('beanstalk.port', DEFAULT_BEANSTALK_PORT)
    beanstalk = beanstalkc.Connection(host=beanstalk_host,
            port=beanstalk_port)
    username = '******'
    beanstalk.use('index')
    store = get_store(config)

    for bag in store.list_bags():
        bag = store.get(bag)
        try:
            tiddlers = bag.get_tiddlers()
        except AttributeError:
            tiddlers = store.list_bag_tiddlers(bag)
        for tiddler in tiddlers:
            tiddler = store.get(tiddler)
            data = BODY_SEPARATOR.join([username, tiddler.bag, tiddler.title,
                str(tiddler.revision)])
            try:
                beanstalk.put(data.encode('UTF-8'))
            except beanstalkc.SocketError as exc:
                LOGGER.error('unable to write to beanstalkd for %s:%s: %s',
                        tiddler.bag, tiddler.title, exc)
def setup_store():
    """
    initialise a blank store, and fill it with some data
    """
    store = get_store(config)
    for bag in BAGS:
        bag = Bag(bag)
        try:
            store.delete(bag)
        except NoBagError:
            pass
        
        store.put(bag)
    
    for recipe, contents in RECIPES.iteritems():
        recipe = Recipe(recipe)
        try:
            store.delete(recipe)
        except NoRecipeError:
            pass
        
        recipe.set_recipe(contents)
        store.put(recipe)
        
    return store
示例#35
0
def setup_module(module):
    try:
        shutil.rmtree('indexdir')
        shutil.rmtree('store')
    except:
        pass

    app = load_app()

    def app_fn():
        return app

    httplib2_intercept.install()
    wsgi_intercept.add_wsgi_intercept('tankt.peermore.com', 8080, app_fn)

    store = get_store(config)
    test_bag = Bag('editable')

    try:
        store.delete(test_bag)
    except StoreError:
        pass

    store.put(test_bag)
    module.environ = {'tiddlyweb.store': store, 'tiddlyweb.config': config}
    module.store = store
    module.http = httplib2.Http()
    module.csrf = None
示例#36
0
def init(config):
    """
    init function for tiddlywebpages.
    Set URLs
    define serializers
    """
    merge_config(config, twp_config)

    # provide a way to allow people to refresh their URLs
    config["selector"].add("/tiddlywebpages/refresh", GET=refresh)

    # get the store
    store = get_store(config)

    # set the default config info
    BAG_OF_TEMPLATES = config["tw_pages"]["template_bag"]

    if "config" in config["tw_pages"]:
        register_config(config, store)

    for new_filter in config["tw_pages"]["filters"]:
        _temp = __import__(new_filter, {}, {}, [new_filter])
        TW_PAGES_FILTERS.append((new_filter, getattr(_temp, new_filter)))

    if "config" in config["tw_pages"]:
        register_config(config, store)
    register_templates(config, store)
示例#37
0
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
示例#38
0
文件: doer.py 项目: cdent/tiddlytoys
def todos(args):
    """List the todos."""
    store = get_store(global_config)
    bag = Bag('do')
    bag = store.get(bag)
    titles = [(tiddler.modified, tiddler.title) for tiddler in
            control.filter_tiddlers_from_bag(bag, 'sort=modified')]
    print '\n'.join(['%s: %s' % title for title in titles])
示例#39
0
def fixbags():
    store = get_store(config)
    bags = store.list_bags()

    for bag in bags:
        bag = store.get(bag)
        bag.policy.read = []
        store.put(bag)
示例#40
0
def setup_store(config):
    global store
    store = get_store(config)
    bag = Bag("tiddlyvoting")
    try:
      store.get(bag)
    except NoBagError:
      store.put(bag)
def setup_module(module):
    try:
        shutil.rmtree('store')
        shutil.rmtree('binarystore')
    except (OSError, IOError):
        pass

    module.store = get_store(config)
示例#42
0
def _write_new_tiddler(source_tiddler, target_tiddler=None):
    # reset the source tiddler as the target tiddler (so its contents
    # are saved
    source_tiddler.revision = 0
    if target_tiddler:
        source_tiddler.title = target_tiddler.title
        source_tiddler.bag = target_tiddler.bag
    store = get_store(config)
    store.put(source_tiddler)
示例#43
0
 def get_atom_config(self, resource_type, resource_name):
     store = get_store(config)
     atomconfig = False
     if resource_type == 'bag':
         try:
             atomconfig = store.get(
                 Tiddler(self.ATOM_CONFIG_TITLE, resource_name))
         except NoTiddlerError, NoBagError:
             pass
示例#44
0
def setup_module(module):
    module.store = get_store(config)
    module.environ = {
        'tiddlyweb.config': config,
        'tiddlyweb.store': module.store
    }
    session = module.store.storage.session
    # delete everything
    Base.metadata.drop_all()
    Base.metadata.create_all()
示例#45
0
def test_post_data_multipart_form():
    """
    test that a form POST requires a nonce
    test using multipart/form-data
    """
    store = get_store(config)
    space = 'foo'
    make_fake_space(store, space)
    user = User('foo')
    user.set_password('foobar')
    store.put(user)
    timestamp = datetime.now().strftime('%Y%m%d%H')
    secret = config['secret']
    nonce = '%s:%s:%s' % (
        timestamp, user.usersign,
        sha('%s:%s:%s:%s' %
            (user.usersign, timestamp, space, secret)).hexdigest())

    user_cookie = get_auth('foo', 'foobar')
    csrf_token = 'csrf_token=%s' % nonce
    data = '''---------------------------168072824752491622650073
Content-Disposition: form-data; name="title"

foobar
---------------------------168072824752491622650073
Content-Disposition: form-data; name="text"

Hello World
---------------------------168072824752491622650073--'''

    #test success
    uri = 'http://foo.0.0.0.0:8080/bags/foo_public/tiddlers?%s' % csrf_token
    response, content = http.request(uri,
        method='POST',
        headers={
            'Content-Type': 'multipart/form-data; ' \
            'boundary=---------------------------168072824752491622650073',
            'Cookie': 'tiddlyweb_user="******"' % user_cookie,
            'Content-Length': '390'
        },
        body=data)
    print content
    assert response['status'] == '204'

    #test failure
    response, _ = http.request('http://foo.0.0.0.0:8080/bags/foo_public/tiddlers',
        method='POST',
        headers={
            'Content-Type': 'multipart/form-data; ' \
            'boundary=---------------------------168072824752491622650073',
            'Cookie': 'tiddlyweb_user="******"' % user_cookie,
            'Content-Length': '267'
        },
        body=data)
    assert response['status'] == '400'
def setup_module(module):
    try:
        shutil.rmtree('store')
    except:
        pass
    module.store = get_store(config)
    init(config)
    base_recipe = Recipe('hi')
    base_recipe.set_recipe([('system', ''), ('stuff', 'select=title:monkey'),
                            ('things', '')])
    module.store.put(base_recipe)
    sys.exit = boring_exit
示例#47
0
def adduserpolicy(username):
    store = get_store(config)
    bags = store.list_bags()

    for bag in bags:
        bag = store.get(bag)
        read_policy = bag.policy.read

        if username not in read_policy:
            print 'adding %s to %s' % (username, bag.name)
            read_policy.append(username)
        store.put(bag)
def addproject(args):
    """make a project space. <project_name>"""
    if len(args) != 1:
        print >> sys.stderr, ('usage: twanager addproject <project_name>')

    #replace PROJECT_NAME with the actual name of the project
    this_project = PROJECT.replace('PROJECT_NAME', args[0])
    this_project = json.loads(this_project)

    #create the space
    project_space = Space({'tiddlyweb.store': get_store(config)})
    project_space.create_space(this_project)
示例#49
0
def _tiddler_written_handler(storage, tiddler):
    schema = storage.environ['tiddlyweb.config'].get(
        'wsearch.schema', SEARCH_DEFAULTS['wsearch.schema'])
    writer = get_writer(storage.environ['tiddlyweb.config'])
    try:
        store = storage.environ.get(
            'tiddlyweb.store', get_store(storage.environ['tiddlyweb.config']))
        temp_tiddler = store.get(Tiddler(tiddler.title, tiddler.bag))
        index_tiddler(tiddler, schema, writer)
    except NoTiddlerError:
        delete_tiddler(tiddler, writer)
    writer.commit()
示例#50
0
def test_validator_no_nonce():
    """
    test the validator directly
    ensure that it fails when the nonce is not present
    """
    store = get_store(config)
    try:
        csrf = CSRFProtector({})
        result = csrf.check_csrf({}, None)
        raise AssertionError(
            'check_csrf succeeded when no csrf_token supplied')
    except InvalidNonceError, exc:
        assert exc.message == 'No csrf_token supplied'
示例#51
0
        def _act(self, job):
            if not self.STORE:
                self.STORE = get_store(self.config)
            info = self._unpack(job)

            tiddler = Tiddler(info['tiddler'], info['bag'])
            try:
                self.STORE.get(tiddler)
            except StoreError:
                return None  #  Tiddler's not there, no need to notify
            user = tiddler.modifier
            if self._user_has_profile(user):
                self._send_ping(user)
def setup_module(module):
    clean_store()
    module.store = get_store(config)
    module.environ = {
        'tiddlyweb.config': config,
        'tiddlyweb.store': module.store
    }
    ensure_bags(config)
    initialize_app(config)
    module.http = Http()

    user = User('cdent')
    user.set_password('cowpig')
    module.store.put(user)
示例#53
0
def setup_module(module):
    for dir in ('store', 'indexdir'):
        try:
            shutil.rmtree(dir)
        except:  # heavy!
            pass

    init(config)
    store = get_store(config)

    store.put(Bag('bagone'))
    store.put(Bag('bagtwo'))

    module.store = store
def populate_store():
    store = get_store(config)

    for bag_name in ["alpha", "bravo"]:
        bag = Bag(bag_name)
        store.put(bag)

        for title in ["foo", "bar"]:
            tiddler = Tiddler(title)
            tiddler.bag = bag.name
            store.put(tiddler)

    for recipe_name in ["omega"]:
        recipe = Recipe(recipe_name)
        store.put(recipe)
示例#55
0
def setup_module(module):
    try:
        shutil.rmtree('indexdir')
        shutil.rmtree('store')
    except:
        pass
    init(config)
    module.store = get_store(config)
    module.environ = {
        'tiddlyweb.config': config,
        'tiddlyweb.store': module.store,
        'tiddlyweb.usersign': {
            'name': 'cdent',
            'roles': ['MEMBER']
        }
    }
示例#56
0
def wreindex(args):
    """Rebuild the entire whoosh index."""
    try:
        prefix = args[0]
    except IndexError:
        prefix = None
    store = get_store(config)
    writer = get_writer(config)
    schema = config.get('wsearch.schema', SEARCH_DEFAULTS['wsearch.schema'])
    for bag in store.list_bags():
        bag = store.get(bag)
        for tiddler in bag.list_tiddlers():
            if prefix and not tiddler.title.startswith(prefix):
                continue
            tiddler = store.get(tiddler)
            index_tiddler(tiddler, schema, writer)
    writer.commit()
示例#57
0
        def _act(self, job):
            """
            Do the action of sending the ping when a job (a tiddler)
            is received from the queue.
            """
            if not self.STORE:
                self.STORE = get_store(self.config)
            info = self._unpack(job)

            tiddler = Tiddler(info['tiddler'], info['bag'])
            try:
                self.STORE.get(tiddler)
            except StoreError:
                return None  # Tiddler's not there, no need to notify
            user = tiddler.modifier
            if self._user_has_profile(user):
                self._send_ping(user)