def _make_space(environ, space_name): """ The details of creating the bags and recipes that make up a space. """ store = environ['tiddlyweb.store'] member = environ['tiddlyweb.usersign']['name'] # XXX stub out the clumsy way for now # can make this much more declarative space = Space(space_name) for bag_name in space.list_bags(): bag = Bag(bag_name) bag.policy = _make_policy(member) if Space.bag_is_public(bag_name): bag.policy.read = [] store.put(bag) public_recipe = Recipe(space.public_recipe()) public_recipe.set_recipe(space.public_recipe_list()) private_recipe = Recipe(space.private_recipe()) private_recipe.set_recipe(space.private_recipe_list()) private_recipe.policy = _make_policy(member) public_recipe.policy = _make_policy(member) public_recipe.policy.read = [] store.put(public_recipe) store.put(private_recipe)
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)
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
def post_createbag(environ, start_response): user = get_user_object(environ) store = environ['tiddlyweb.store'] bag_name = environ['tiddlyweb.query'].get('bag', [''])[0] publicity = environ['tiddlyweb.query'].get('publicity', [''])[0] description = environ['tiddlyweb.query'].get('description', [''])[0] if not bag_name: raise HTTP400('missing data') bag = Bag(bag_name) try: bag.skinny = True bag = store.get(bag) raise HTTP400('bag exists') except NoBagError: pass if publicity == 'public': bag = ensure_public_bag( store, user['name'], desc=description, name=bag_name) elif publicity == 'protected': bag = ensure_protected_bag( store, user['name'], desc=description, name=bag_name) else: bag = ensure_private_bag( store, user['name'], desc=description, name=bag_name) # the bag has already been stored raise HTTP303('%s/tiddlers' % bag_url(environ, bag))
def test_validate_bag_desc(): bag = Bag('barney') bag.desc = '<script>alert("foo");</script>' validate_bag(bag) assert bag.desc == '<script>alert("foo");</script>'
def test_filter_bag_by_filter(): """ Confirm a bag will properly filter. """ bagfour = Bag('bagfour') store.put(bagfour) bagfour.store = store for tiddler in tiddlers: tiddler.bag = 'bagfour' store.put(tiddler) filtered_tiddlers = list(control._filter_tiddlers_from_bag(bagfour, 'select=title:TiddlerOne', environ=environ)) assert len(filtered_tiddlers) == 1 assert filtered_tiddlers[0].title == 'TiddlerOne' filtered_tiddlers = list(control._filter_tiddlers_from_bag(bagfour, 'select=tag:tagone', environ=environ)) assert len(filtered_tiddlers) == 2 filters, thing = parse_for_filters( 'select=tag:tagone;select=title:TiddlerThree', environ=environ) filtered_tiddlers = list(control._filter_tiddlers_from_bag(bagfour, filters, environ=environ)) assert len(filtered_tiddlers) == 1 assert filtered_tiddlers[0].title == 'TiddlerThree'
def determine_tiddler_bag_from_recipe(recipe, tiddler, environ=None): """ We have a recipe and a tiddler. We need to know the bag in which this tiddler can be found. This is different from determine_bag_for_tiddler(). That one finds the bag the tiddler _could_ be in. This is the bag the tiddler _is_ in. We reverse the recipe_list, and filter each bag according to the rule. Then we look in the list of tiddlers and see if ours is in there. """ store = recipe.store template = _recipe_template(environ) for bag, filter_string in reversed(recipe.get_recipe(template)): if isinstance(bag, basestring): bag = Bag(name=bag) if store: bag = store.get(bag) # If there is a filter_string then we need to load the tiddlers off # the store. If there's not, then we can just use the list that is # already in the bag, saving a bit of time. if filter_string: for candidate_tiddler in filter_tiddlers_from_bag(bag, filter_string): if tiddler.title == candidate_tiddler.title: return bag else: for candidate_tiddler in bag.gen_tiddlers(): if tiddler.title == candidate_tiddler.title: return bag raise NoBagError('no suitable bag for %s' % tiddler.title)
def test_filter_bag_by_filter(): """ Confirm a bag will properly filter. """ bagfour = Bag('bagfour') store.put(bagfour) bagfour.store = store for tiddler in tiddlers: tiddler.bag = 'bagfour' store.put(tiddler) filtered_tiddlers = list( control._filter_tiddlers_from_bag(bagfour, 'select=title:TiddlerOne', environ=environ)) assert len(filtered_tiddlers) == 1 assert filtered_tiddlers[0].title == 'TiddlerOne' filtered_tiddlers = list( control._filter_tiddlers_from_bag(bagfour, 'select=tag:tagone', environ=environ)) assert len(filtered_tiddlers) == 2 filters, thing = parse_for_filters( 'select=tag:tagone;select=title:TiddlerThree', environ=environ) filtered_tiddlers = list( control._filter_tiddlers_from_bag(bagfour, filters, environ=environ)) assert len(filtered_tiddlers) == 1 assert filtered_tiddlers[0].title == 'TiddlerThree'
def _process_choices(environ, start_response, form): store = environ['tiddlyweb.store'] user = environ['tiddlyweb.usersign'] tmp_bag = form['tmp_bag'][0] bag = form['target_bag'][0] if bag: bag = Bag(bag) try: bag.skinny = True bag = store.get(bag) except NoBagError: return _send_wimport(environ, start_response, 'chosen bag does not exist') else: bag = form['new_bag'][0] bag = _make_bag(environ, bag) try: bag.policy.allows(user, 'write') except (ForbiddenError, UserRequiredError): return _send_wimport(environ, start_response, 'you may not write to that bag') tiddler_titles = form['tiddler'] for title in tiddler_titles: tiddler = Tiddler(title.decode('utf-8', 'ignore'), tmp_bag) tiddler = store.get(tiddler) tiddler.bag = bag.name store.put(tiddler) tmp_bag = Bag(tmp_bag) store.delete(tmp_bag) bagurl = bag_url(environ, bag) + '/tiddlers' raise HTTP302(bagurl)
def _create_bag(environ): """Take the form input and turn it into a bag.""" query_data = _flatten_form_data(environ['tiddlyweb.query']) logging.debug(query_data) store = environ['tiddlyweb.store'] try: new_bag_name = query_data['bag_name'] if _bag_exists(store, new_bag_name): raise HTTP409('That bag may not be created.') new_bag = Bag(new_bag_name) username = environ['tiddlyweb.usersign']['name'] new_bag.policy.owner = username new_bag.policy.manage = [username] new_bag.desc = query_data.get('bag_desc', '') for policy_type in ('read', 'write', 'create', 'delete'): texted = query_data.get(policy_type + '_text', None) logging.debug('texted: %s' % texted) if texted: new_bag.policy.__setattr__(policy_type, [x.lstrip().rstrip() for x in texted.split(',')]) else: set = query_data[policy_type] new_bag.policy.__setattr__(policy_type, _policy_form_to_entry(username, set)) store.put(new_bag) except KeyError, exc: raise HTTP400('something went wrong processing for: %s' % exc)
def _ensure_bags(self, environ, username): # for privacy in ["public", "protected", "private"]: for privacy in ["private", "protected"]: only_user = ["admin", username] if privacy=="public": readPerms = [] writePerms = [] elif privacy=="protected": readPerms = [] writePerms = only_user elif privacy=="private": readPerms = only_user writePerms = only_user bag = Bag(privacy+"-"+username) store = environ['tiddlyweb.store'] try: store.get(bag) return except NoBagError: bag.desc = 'tiddlyguv private user bag' bag.policy.owner = username bag.policy.manage = ["admin", username] bag.policy.read = readPerms bag.policy.write = writePerms bag.policy.create = ["admin", username] bag.policy.delete = ["admin", username] store.put(bag)
def test_post_no_title(): """ post a tiddler with no title set and make sure it gets into the store """ store = setup_store() setup_web() http = httplib2.Http() #post some fields to a tiddler response = http.request('http://test_domain:8001/bags/foo/tiddlers', method='POST', headers={'Content-type': 'application/x-www-form-urlencoded'}, body='text=Hi%20There')[0] assert response.status == 204 #now find the tiddler just entered and check it bag = Bag('foo') bag = store.get(bag) tiddlers = bag.list_tiddlers() assert len(tiddlers) == 1 tiddler = store.get(tiddlers[0]) assert tiddler.title != '' assert tiddler.text == 'Hi There'
def remove_user(environ, space_name, user_name): """ remove user_name from space_name while checking the policy allows the logged in user to do it """ store = environ['tiddlyweb.store'] user = User(user_name) user = store.get(user) logged_in_user = environ['tiddlyweb.usersign'] space_definition = environ['tiddlyweb.config']['space'] space = [] for name, values in space_definition['bags'].iteritems(): bag = Bag(name.replace('SPACE_NAME', space_name)) bag = store.get(bag) bag.policy.allows(logged_in_user, 'manage') bag.policy = remove_from_policy(user, bag.policy) space.append(bag) for name, values in space_definition['recipes'].iteritems(): recipe = Recipe(name.replace('SPACE_NAME', space_name)) recipe = store.get(recipe) recipe.policy.allows(logged_in_user, 'manage') recipe.policy = remove_from_policy(user, recipe.policy) space.append(recipe) for thing in space: store.put(thing)
def tiddler_as(self, tiddler): logging.debug("###################################\nDOING TIDDLER_AS\n\n\n###################################") """ Take the single tiddler provided and inject it into a TiddlyWiki. """ environ = self.environ store = self.environ["tiddlyweb.store"] try: recipe_name =tiddler.recipe tiddler_as_name = tiddler.title resource = Recipe(recipe_name) resource= store.get(resource) tiddlers = control.get_tiddlers_from_recipe(resource) bag =Bag("tmp",tmpbag=True) logging.debug("have tiddlers %s"%tiddlers) for a_tiddler in tiddlers: a_tiddler.recipe = recipe_name a_tiddler = store.get(a_tiddler) if a_tiddler.title == "DefaultTiddlers": a_tiddler.text = "[[%s]]"%tiddler_as_name logging.debug("tiddler_as overriding DefaultTiddlers") bag.add_tiddler(a_tiddler) except AttributeError: resource = Bag(tiddler.bag) bag = store.get(resource) self._prepare_twp(bag) return self.build_non_js_version(bag,default=[tiddler])
def test_tiddler_racing(): def race_tiddlers(bag_name, count=2, intitles=['x', 'z'], outtitles=['y']): bag = Bag(bag_name) store.put(bag) tiddlers = Tiddlers(store=store) for title in ['x', 'y', 'z']: tiddler = Tiddler(title, 'foo') store.put(tiddler) tiddlers.add(tiddler) tiddler = Tiddler('y', 'foo') store.delete(tiddler) tids = list(tiddlers) assert len(tids) == count for title in intitles: assert title in [tid.title for tid in tids] for title in outtitles: assert title not in [tid.title for tid in tids] stored_config = config.get('collections.use_memory') config['collections.use_memory'] = False race_tiddlers('foo') config['collections.use_memory'] = True race_tiddlers('bar', count=3, intitles=['x', 'y', 'z'], outtitles=[]) store.delete(Bag('foo')) store.delete(Bag('bar')) config['collections.use_memory'] = stored_config
def create_wiki(environ, name, mode='private', username=None, desc='', validate=True): """ Create a wiki with the name, name. For now a wiki is just a bag a policy. """ store = environ['tiddlyweb.store'] if username is None: username = environ['tiddlyweb.usersign']['name'] bag = Bag(name) # We want this get to fail. try: store.get(bag) return False except NoBagError: pass try: bag.policy = WIKI_MODES[mode](username) except KeyError: bag.policy = WIKI_MODES['private'](username) bag.desc = desc if validate: validate_bag(bag, environ) store.put(bag) return bag
def _bag_list(self, tiddlers): if '/feedbag' in self.environ['selector.matches'][0]: representation_link = '%s/feedbag' % (self._server_prefix()) bag = Bag('feedbag') bag.policy.manage = ["NONE"] bag.policy.delete = ["NONE"] bag.desc = 'Recent Public Stuff' else: name = self.environ['wsgiorg.routing_args'][1]['bag_name'] name = urllib.unquote(name) name = name.decode('utf-8') representation_link = '%s/bags/%s/tiddlers' % ( self._server_prefix(), encode_name(name)) bag = self.environ['tiddlyweb.store'].get(Bag(name)) representations = self._tiddler_list_header(representation_link) user_object = get_user_object(self.environ) publicity = '' try: bag.policy.allows(user_object, 'manage') policy = bag.policy publicity = determine_publicity(user_object, policy) except (UserRequiredError, ForbiddenError): policy = None try: bag.policy.allows(user_object, 'delete') delete = True except (UserRequiredError, ForbiddenError): delete = False data = {'title': 'TiddlyHoster Bag %s' % bag.name, 'policy': policy, 'publicity': publicity, 'delete': delete, 'bag': bag, 'tiddlers': tiddlers, 'representations': representations} del self.environ['tiddlyweb.title'] return send_template(self.environ, 'baglist.html', data)
def make_fake_space(store, name): def set_policy(policy, private=False): for policy_attr in policy.attributes: if policy_attr not in ['read', 'owner']: setattr(policy, policy_attr, [name]) if private: policy.read = [name] public_recipe = Recipe('%s_public' % name) private_recipe = Recipe('%s_private' % name) public_bag = Bag('%s_public' % name) private_bag = Bag('%s_private' % name) archive_bag = Bag('%s_archive' % name) set_policy(public_recipe.policy) set_policy(private_recipe.policy, private=True) set_policy(public_bag.policy) set_policy(private_bag.policy, private=True) set_policy(archive_bag.policy, private=True) public_recipe.set_recipe([('system', ''), ('tiddlyspace', ''), ('%s_public' % name, '')]) private_recipe.set_recipe([('system', ''), ('tiddlyspace', ''), ('%s_public' % name, ''), ('%s_private' % name, '')]) for entity in [ public_recipe, private_recipe, public_bag, private_bag, archive_bag ]: store.put(entity)
def _create_bag(environ): """Take the form input and turn it into a bag.""" query_data = _flatten_form_data(environ['tiddlyweb.query']) logging.debug(query_data) store = environ['tiddlyweb.store'] try: new_bag_name = query_data['bag_name'] if _bag_exists(store, new_bag_name): raise HTTP409('That bag may not be created.') new_bag = Bag(new_bag_name) username = environ['tiddlyweb.usersign']['name'] new_bag.policy.owner = username new_bag.policy.manage = [username] new_bag.desc = query_data.get('bag_desc', '') for policy_type in ('read', 'write', 'create', 'delete'): texted = query_data.get(policy_type + '_text', None) logging.debug('texted: %s' % texted) if texted: new_bag.policy.__setattr__( policy_type, [x.lstrip().rstrip() for x in texted.split(',')]) else: set = query_data[policy_type] new_bag.policy.__setattr__( policy_type, _policy_form_to_entry(username, set)) store.put(new_bag) except KeyError, exc: raise HTTP400('something went wrong processing for: %s' % exc)
def test_post_fields(): """ post some fields to a tiddler """ store = setup_store() setup_web() http = httplib2.Http() #make sure there is nothing in bag 'foo' bag = Bag('foo') bag = store.get(bag) assert len(bag.list_tiddlers()) == 0 response = http.request('http://test_domain:8001/bags/foo/tiddlers', method='POST', headers={'Content-type': 'application/x-www-form-urlencoded'}, body='title=HelloWorld&field1=foo&field2=bar')[0] assert response.status == 204 #now find the tiddler just entered tiddler = Tiddler('HelloWorld', 'foo') try: store.get(tiddler) except NoTiddlerError: raise AssertionError('tiddler was not put into store') #and check the fields assert tiddler.title == 'HelloWorld' assert len(tiddler.fields) == 2 assert tiddler.fields['field1'] == 'foo' assert tiddler.fields['field2'] == 'bar'
def test_bag_policy(): bag = Bag('policy_tester') bag.policy = Policy(read=['chris','jeremy']) assert bag.policy.allows(chris_info, 'read') py.test.raises(UserRequiredError, 'bag.policy.allows(guest_info, "read")')
def test_bag_policy(): bag = Bag('policy_tester') bag.policy = Policy(read=['chris', 'jeremy']) assert bag.policy.allows(chris_info, 'read') py.test.raises(UserRequiredError, 'bag.policy.allows(guest_info, "read")')
def tiddler_as(self, tiddler): """ entry point for a single tiddler. Sets some variables and passes the tiddler into list_tiddlers for turning into HTML """ if 'tiddler' not in self.environ['tiddlyweb.recipe_template']: self.environ['tiddlyweb.recipe_template']['tiddler'] = tiddler.title if 'bag' not in self.environ['tiddlyweb.recipe_template']: self.environ['tiddlyweb.recipe_template']['bag'] = tiddler.bag if tiddler.recipe and 'recipe' not in self.environ['tiddlyweb.recipe_template']: self.environ['tiddlyweb.recipe_template']['recipe'] = tiddler.recipe bag = Bag('tmpbag',tmpbag=True) bag.add_tiddler(tiddler) self.plugin_name = self.set_plugin_name('single_tiddler') if self.plugin_name not in self.environ['tiddlyweb.config']['tw_pages_serializers']: content = self.pass_through_external_serializer(self.plugin_name, tiddler) return content self.page_title = self.environ['tiddlyweb.config']['tw_pages_serializers'][self.plugin_name]['title'] or tiddler.title return self.list_tiddlers(bag)
def make_space(space_name, store, member): """ The details of creating the bags and recipes that make up a space. """ space = Space(space_name) for bag_name in space.list_bags(): bag = Bag(bag_name) bag.policy = _make_policy(member) if Space.bag_is_public(bag_name): bag.policy.read = [] store.put(bag) info_tiddler = Tiddler('SiteInfo', space.public_bag()) info_tiddler.text = 'Space %s' % space_name store.put(info_tiddler) public_recipe = Recipe(space.public_recipe()) public_recipe.set_recipe(space.public_recipe_list()) private_recipe = Recipe(space.private_recipe()) private_recipe.set_recipe(space.private_recipe_list()) private_recipe.policy = _make_policy(member) public_recipe.policy = _make_policy(member) public_recipe.policy.read = [] store.put(public_recipe) store.put(private_recipe)
def test_readable_tiddlers_by_bag(): bagone = Bag('cdentread') bagone.policy.read = ['cdent'] store.put(bagone) bagtwo = Bag('fndread') bagtwo.policy.read = ['fnd'] store.put(bagtwo) bagthree = Bag('allread') store.put(bagthree) count = 0 tiddlers = [] for bag in ['cdent', 'fnd', 'all', 'cdent', 'fnd', 'all', 'notreal']: count += 1 tiddler = Tiddler('tiddler%s' % count, '%sread' % bag) tiddler.text = 'narf' tiddlers.append(tiddler) usersign = {'name': 'cdent', 'roles': []} readable = list(readable_tiddlers_by_bag(store, tiddlers, usersign)) assert (['tiddler1', 'tiddler3', 'tiddler4', 'tiddler6', 'tiddler7'] == [tiddler.title for tiddler in readable]) usersign = {'name': 'fnd', 'roles': []} readable = list(readable_tiddlers_by_bag(store, tiddlers, usersign)) assert (['tiddler2', 'tiddler3', 'tiddler5', 'tiddler6', 'tiddler7'] == [tiddler.title for tiddler in readable]) usersign = {'name': 'GUEST', 'roles': []} readable = list(readable_tiddlers_by_bag(store, tiddlers, usersign)) assert (['tiddler3', 'tiddler6', 'tiddler7'] == [tiddler.title for tiddler in readable])
def tiddler_as(self, tiddler): """ entry point for a single tiddler. Sets some variables and passes the tiddler into list_tiddlers for turning into HTML """ if 'tiddler' not in self.environ['tiddlyweb.recipe_template']: self.environ['tiddlyweb.recipe_template'][ 'tiddler'] = tiddler.title if 'bag' not in self.environ['tiddlyweb.recipe_template']: self.environ['tiddlyweb.recipe_template']['bag'] = tiddler.bag if tiddler.recipe and 'recipe' not in self.environ[ 'tiddlyweb.recipe_template']: self.environ['tiddlyweb.recipe_template'][ 'recipe'] = tiddler.recipe bag = Bag('tmpbag', tmpbag=True) bag.add_tiddler(tiddler) self.plugin_name = self.set_plugin_name('single_tiddler') if self.plugin_name not in self.environ['tiddlyweb.config'][ 'tw_pages_serializers']: content = self.pass_through_external_serializer( self.plugin_name, tiddler) return content self.page_title = self.environ['tiddlyweb.config'][ 'tw_pages_serializers'][self.plugin_name]['title'] or tiddler.title return self.list_tiddlers(bag)
def test_search_follow_syntax(): QUERY = u'ftitle:GettingStarted (bag:cdent_public OR bag:fnd_public)' store.put(Bag(u'fnd_public')) store.put(Bag(u'cdent_public')) tiddler = Tiddler(u'GettingStarted', u'fnd_public') tiddler.text = u'fnd starts' tiddler.fields[u'house'] = u'treehouse' tiddler.fields[u'car'] = u'porsche' store.put(tiddler) tiddler = Tiddler(u'GettingStarted', u'cdent_public') tiddler.text = u'cdent starts' tiddler.fields[u'left-hand'] = u'well dirty' store.put(tiddler) tiddler = Tiddler(u'other', u'cdent_public') tiddler.text = u'cdent starts' store.put(tiddler) tiddlers = list(store.search(u'starts')) assert len(tiddlers) == 3 tiddlers = list(store.search(QUERY)) assert len(tiddlers) == 2 tiddlers = list(store.search(u'cdent starts')) assert len(tiddlers) == 2 tiddlers = list(store.search(u'fnd starts')) assert len(tiddlers) == 1 tiddler = list(store.search(u'left-hand:"well dirty"')) assert len(tiddlers) == 1
def test_get_revision(): """ Test we are able to retrieve a particular revision. """ bagone = Bag('bagone') bagone.add_tiddlers(tiddlers) store.put(bagone) store.put(bagone) tiddler = Tiddler('RevisionTiddler') tiddler.text='how now 1' tiddler.bag = 'bagone' store.put(tiddler) tiddler.text = 'how now 2' store.put(tiddler) tiddler.text = 'how now 3' store.put(tiddler) tiddler = Tiddler(title='RevisionTiddler', bag='bagone') tiddler = store.get(tiddler) assert tiddler.text == 'how now 3' assert tiddler.revision == 3 tiddler = Tiddler(title='RevisionTiddler', bag='bagone') tiddler.revision = 2 tiddler = store.get(tiddler) assert tiddler.text == 'how now 2' assert tiddler.revision == 2 revisions = store.list_tiddler_revisions(tiddler) assert len(revisions) == 3 assert revisions[0] == 3
def test_list_tiddlers(): bag = Bag('bag1') bag = store.get(bag) tiddlers = bag.list_tiddlers() assert len(tiddlers) == 1 assert 'tiddler1' in [tiddler.title for tiddler in tiddlers]
def test_bag_get(): store = Store('tiddlywebplugins.mappingsql', {'db_config': 'sqlite:///test.db'}, {'tiddlyweb.config': config}) bag = Bag('avox') assert len(bag.list_tiddlers()) == 0 bag = store.get(bag) assert len(bag.list_tiddlers()) == 1 assert "NONE" in bag.policy.write
def test_filter_by_text_string_negate(): bag = Bag('bag0') bag = store.get(bag) bags_tiddlers = bag.list_tiddlers() assert len(bags_tiddlers) == 10 found_tiddlers = list(control.filter_tiddlers_from_bag(bag, 'select=text:!tiddler 0')) assert len(found_tiddlers) == 9
def tagged_tiddlers(environ, start_response): store = environ['tiddlyweb.store'] bag_name = unicode(urllib.unquote(environ['wsgiorg.routing_args'][1]['bag_name']), 'utf-8') tag_name = unicode(urllib.unquote(environ['wsgiorg.routing_args'][1]['tag_name']), 'utf-8') bag = store.get(Bag(bag_name)) tmp_bag = Bag('tmpbag', tmpbag=True) tmp_bag.add_tiddlers(control.filter_tiddlers_from_bag(bag, 'select=tag:%s' % tag_name)) return send_tiddlers(environ, start_response, tmp_bag)
def clear_store(): try: store.delete(Bag("notfoo")) except NoBagError: pass try: store.delete(Bag("foo")) except NoBagError: pass
def test_filter_by_text_string_negate(): bag = Bag('bag0') bag = store.get(bag) bags_tiddlers = bag.list_tiddlers() assert len(bags_tiddlers) == 10 found_tiddlers = control.filter_tiddlers_from_bag(bag, '[!text[tiddler 0]]') assert len(found_tiddlers) == 9
def test_store(): """ An exploratory test to learn how this stuff works. """ bag = Bag(u'bar') bag.policy.owner = u'cdent' bag.policy.read = [u'cdent'] store.put(bag) sbag = store.storage.session.query( sql.sBag).filter(sql.sBag.name == u'bar').one() assert sbag.name == bag.name assert sbag.policy.owner == bag.policy.owner assert sbag.policy.read == u'["cdent"]' bag_d = Bag(u'bar') bag_d = store.get(bag_d) assert bag_d.name == bag.name assert bag_d.policy.owner == bag.policy.owner assert bag_d.policy.read == bag.policy.read tiddler = Tiddler(u'tidlname', bag.name) tiddler.text = u'foo' tiddler.modifier = u'nancy' store.put(tiddler) stiddler = store.storage.session.query( sql.sTiddler).filter(sql.sTiddler.title == u'tidlname').filter( sql.sTiddler.bag_name == u'bar').one() assert stiddler.title == 'tidlname' assert stiddler.bag_name == 'bar' assert len(stiddler.revisions) == 1 tiddler = Tiddler(u'tidlname', bag.name) tiddler.text = u'foo1' tiddler.modifier = u'karl' store.put(tiddler) stiddler = store.storage.session.query( sql.sTiddler).filter(sql.sTiddler.title == u'tidlname').filter( sql.sTiddler.bag_name == u'bar').one() assert stiddler.title == u'tidlname' assert stiddler.bag_name == u'bar' assert len(stiddler.revisions) == 2 tiddler_d = Tiddler(u'tidlname', u'bar') tiddler_d = store.get(tiddler_d) assert tiddler_d.text == tiddler.text assert tiddler_d.modifier == u'karl' assert tiddler_d.creator == u'nancy' tiddlers = store.list_tiddler_revisions(tiddler_d) assert len(tiddlers) == 2 store.delete(tiddler_d)
def play(): store = _store() store.list_bags() bag = Bag('ahosey') bag = store.get(bag) tiddlers = bag.list_tiddlers() print len(tiddlers) tiddler = store.get(tiddlers[5]) print tiddler.text
def test_filter_by_text_string(): bag = Bag('bag0') bag = store.get(bag) bags_tiddlers = bag.list_tiddlers() assert len(bags_tiddlers) == 10 found_tiddlers = control.filter_tiddlers_from_bag(bag, '[text[tiddler 0]]') assert len(found_tiddlers) == 1 assert found_tiddlers[0].title == 'tiddler0'
def _read_bag_perms(environ, tiddler): perms = [] if 'tiddlyweb.usersign' in environ: store = tiddler.store if store: bag = Bag(tiddler.bag) bag.skinny = True bag = store.get(bag) perms = bag.policy.user_perms(environ['tiddlyweb.usersign']) return perms
def test_filter_by_text(): bag = Bag('bag0') bag = store.get(bag) bags_tiddlers = bag.list_tiddlers() assert len(bags_tiddlers) == 10 found_tiddlers = list(control.filter_tiddlers_from_bag(bag, 'select=text:tiddler 0')) assert len(found_tiddlers) == 1 assert found_tiddlers[0].title == 'tiddler0'
def test_put_get_bag(): bag = Bag('testone') bag.policy.read = ['cdent'] store.put(bag) read_bag = Bag('testone') read_bag.skinny = True read_bag = store.get(read_bag) assert read_bag.policy.read == ['cdent']
def test_bag_etag(): """ Explicitly test bag_etag method (not used by the core code). """ bag1 = Bag('foo') bag1.desc = 'desc' bag2 = Bag('foo') bag2.desc = 'desc' assert bag_etag(environ, bag1) == bag_etag(environ, bag2)
def test_handle_empty_policy(): bag = Bag(u'empty') store.put(bag) new_bag = store.get(Bag(u'empty')) assert new_bag.policy.read == [] assert new_bag.policy.manage == [] assert new_bag.policy.create == [] assert new_bag.policy.write == [] assert new_bag.policy.accept == [] assert new_bag.policy.owner == None
def setup_module(module): module.store = Store(config['server_store'][0], config['server_store'][1], environ={'tiddlyweb.config': config}) try: bag = Bag('holder') store.delete(bag) except: pass bag = Bag('holder') module.store.put(bag)
def test_store_bag(): bag_in = Bag('bagone') bag_in.desc = 'bag description' bag_in.policy.read = ['reader'] bag_in.policy.write = ['writer'] store.put(bag_in) bag_out = store.get(Bag('bagone')) assert bag_out.name == bag_in.name
def test_delete_member(): cookie = get_auth('fnd', 'bird') http = httplib2.Http() response, content = http.request( 'http://0.0.0.0:8080/spaces/extra/members/psd', method='DELETE', ) assert response['status'] == '403' response, content = http.request( 'http://extra.0.0.0.0:8080/spaces/extra/members/psd', method='DELETE', ) assert response['status'] == '403' response, content = http.request( 'http://0.0.0.0:8080/spaces/extra/members/psd', headers={'Cookie': 'tiddlyweb_user="******"' % cookie}, method='DELETE', ) assert response['status'] == '403' response, content = http.request( 'http://extra.0.0.0.0:8080/spaces/extra/members/psd', headers={'Cookie': 'tiddlyweb_user="******"' % cookie}, method='DELETE', ) assert response['status'] == '204' # delete self response, content = http.request( 'http://extra.0.0.0.0:8080/spaces/extra/members/fnd', headers={'Cookie': 'tiddlyweb_user="******"' % cookie}, method='DELETE', ) assert response['status'] == '204' bag = store.get(Bag('extra_private')) assert bag.policy.owner == 'cdent' assert bag.policy.read == ['cdent'] assert bag.policy.accept == ['NONE'] assert bag.policy.manage == ['cdent'] assert bag.policy.write == ['cdent'] assert bag.policy.create == ['cdent'] assert bag.policy.delete == ['cdent'] bag = store.get(Bag('extra_archive')) assert bag.policy.owner == 'cdent' assert bag.policy.read == ['cdent'] assert bag.policy.accept == ['NONE'] assert bag.policy.manage == ['cdent'] assert bag.policy.write == ['cdent'] assert bag.policy.create == ['cdent'] assert bag.policy.delete == ['cdent']
def check_bag(tiddler, store, bag_names): """ check that the tiddler is not in the listed bags """ for bag_name in bag_names: bag = Bag(bag_name) bag = store.get(bag) tiddlers = bag.gen_tiddlers() if tiddler.title in [reserved.title for reserved in tiddlers]: raise InvalidTiddlerError('Tiddler name is reserved: %s' \ % tiddler.title)
def test_bag_has_description(): """ Confirm a bag can set and use a description. """ bag = Bag('hasbeen', desc='monkey puzzle') assert bag.name == 'hasbeen' assert bag.desc == 'monkey puzzle' bag.desc = 'collapsing sideburns' assert bag.desc == 'collapsing sideburns'
def test_store_bag(): bag = Bag('bag1') bag.desc = 'bag 1 desc' store.put(bag) # these should be _description, not description etc assert os.path.exists('store/bag1/description') assert os.path.exists('store/bag1/policy') loaded_bag = Bag('bag1') loaded_bag = store.get(loaded_bag) assert loaded_bag.desc == bag.desc
def test_where_it_goes(): store = Store(SAMPLE_CONFIG['server_store'][0], SAMPLE_CONFIG['server_store'][1], environ=ENVIRON) bbag = Bag('bbag') cbag = Bag('cbag') store.put(bbag) store.put(cbag) assert os.path.exists('store1/bags/bbag/tiddlers') assert os.path.exists('store2/bags/cbag/tiddlers') assert not os.path.exists('store2/bags/bbag/tiddlers') assert not os.path.exists('store1/bags/cbag/tiddlers')
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 test_delete(): bag = Bag('deleteme') bag.desc = 'delete me please' store.put(bag) stored_bag = Bag('deleteme') stored_bag = store.get(stored_bag) assert stored_bag.desc == 'delete me please' deleted_bag = Bag('deleteme') store.delete(deleted_bag) py.test.raises(NoBagError, 'store.get(deleted_bag)') py.test.raises(NoBagError, 'store.delete(deleted_bag)')
def tagged_tiddlers(environ, start_response): store = environ['tiddlyweb.store'] bag_name = unicode( urllib.unquote(environ['wsgiorg.routing_args'][1]['bag_name']), 'utf-8') tag_name = unicode( urllib.unquote(environ['wsgiorg.routing_args'][1]['tag_name']), 'utf-8') bag = store.get(Bag(bag_name)) tmp_bag = Bag('tmpbag', tmpbag=True) tmp_bag.add_tiddlers( control.filter_tiddlers_from_bag(bag, 'select=tag:%s' % tag_name)) return send_tiddlers(environ, start_response, tmp_bag)
def get_tiddlers(environ, start_response): """ Get the list of tiddlers produced by this recipe. """ usersign = environ['tiddlyweb.usersign'] store = environ['tiddlyweb.store'] filters = environ['tiddlyweb.filters'] recipe = _determine_recipe(environ) title = 'Tiddlers From Recipe %s' % recipe.name title = environ['tiddlyweb.query'].get('title', [title])[0] # check the recipe can be read recipe.policy.allows(usersign, 'read') # check the bags in the recipe can be read try: template = control.recipe_template(environ) for bag_name, _ in recipe.get_recipe(template): bag = Bag(bag_name) bag = store.get(bag) bag.policy.allows(usersign, 'read') except NoBagError, exc: raise HTTP404('recipe %s lists an unknown bag: %s' % (recipe.name, exc))