Esempio n. 1
0
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)
Esempio n. 2
0
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 test_store_recipe():
    recipe_in = Recipe('recipeone')
    recipe_in.desc = 'recipe description'

    store.put(recipe_in)

    recipe_out = store.get(Recipe('recipeone'))

    assert recipe_out.name == recipe_in.name
Esempio n. 4
0
def test_put_get_recipe():
    recipe = Recipe('testone')
    recipe.policy.read = ['cdent']
    store.put(recipe)

    read_recipe = Recipe('testone')
    read_recipe.skinny = True
    read_recipe = store.get(read_recipe)

    assert read_recipe.policy.read == ['cdent']
Esempio n. 5
0
def test_recipe_etag():
    """
    Explicitly test recipe_etag method (not used by the core code).
    """
    recipe1 = Recipe('foo')
    recipe1.desc = 'desc'
    recipe2 = Recipe('foo')
    recipe2.desc = 'desc'

    assert recipe_etag(environ, recipe1) == recipe_etag(environ, recipe2)
Esempio n. 6
0
def test_recipe_no_recipe():
    """
    make a sure a recipe that is stored without a recipe is retrievable
    """
    recipe = Recipe('testrecipe2')
    recipe.desc = 'I enjoy being stored'
    store.put(recipe)

    stored_recipe = Recipe('testrecipe2')
    stored_recipe = store.get(stored_recipe)

    assert stored_recipe.desc == recipe.desc
Esempio n. 7
0
def test_store_recipe():
    recipe = Recipe('recipe1')
    recipe.desc = 'recipe 1 desc'
    recipe.set_recipe([['bag1', '']])

    store.put(recipe)

    assert os.path.exists('store/recipe1.recipe')

    loaded_recipe = Recipe('recipe1')
    loaded_recipe = store.get(loaded_recipe)
    assert loaded_recipe.desc == recipe.desc
Esempio n. 8
0
def test_recipe_weird_bag():
    recipe = Recipe('weirdbags')
    recipe.set_recipe([
        ('foo/bar', ''),
        ('zam/boom', ''),
    ])
    store.put(recipe)

    new_recipe = Recipe('weirdbags')
    new_recipe = store.get(new_recipe)
    bags = [bag for bag, filter in new_recipe.get_recipe()]
    assert bags == ['foo/bar', 'zam/boom']
Esempio n. 9
0
def test_recipe_delete():
    recipe = Recipe('deleteme')
    recipe.desc = 'delete me please'
    store.put(recipe)

    stored_recipe = Recipe('deleteme')
    stored_recipe = store.get(stored_recipe)
    assert stored_recipe.desc == 'delete me please'

    deleted_recipe = Recipe('deleteme')
    store.delete(deleted_recipe)

    py.test.raises(NoRecipeError, 'store.get(deleted_recipe)')
    py.test.raises(NoRecipeError, 'store.delete(deleted_recipe)')
Esempio n. 10
0
def confirm_space(environ, start_response):
    """
    Confirm a spaces exists. If it does, raise 204. If
    not, raise 404.
    """
    store = environ['tiddlyweb.store']
    space_name = get_route_value(environ, 'space_name')
    try:
        space = Space(space_name)
        store.get(Recipe(space.public_recipe()))
        store.get(Recipe(space.private_recipe()))
    except (NoRecipeError, ValueError):
        raise HTTP404('%s does not exist' % space_name)
    start_response('204 No Content', [])
    return ['']
Esempio n. 11
0
def confirm_space(environ, start_response):
    """
    Confirm a spaces exists. If it does, raise 204. If
    not, raise 404.
    """
    store = environ['tiddlyweb.store']
    space_name = environ['wsgiorg.routing_args'][1]['space_name']
    try:
        space = Space(space_name)
        store.get(Recipe(space.public_recipe()))
        store.get(Recipe(space.private_recipe()))
    except NoRecipeError:
        raise HTTP404('%s does not exist' % space_name)
    start_response('204 No Content', [])
    return ['']
Esempio n. 12
0
def test_index_query_in_recipe():
    config['indexer'] = 'test.indexernot'

    bag = Bag('noop')
    store.put(bag)
    tiddler = Tiddler('dwell', 'noop')
    store.put(tiddler)

    recipe = Recipe('coolio')
    recipe.set_recipe([('noop', ''), ('fwoop', '')])
    recipe.store = store

    tiddler = Tiddler('swell')
    py.test.raises(ImportError,
                   'determine_bag_from_recipe(recipe, tiddler, environ)')

    config['indexer'] = 'test.indexer'
    bag = determine_bag_from_recipe(recipe, tiddler, environ)
    assert bag.name == 'fwoop'

    tiddler = Tiddler('dwell')
    bag = determine_bag_from_recipe(recipe, tiddler, environ)
    assert bag.name == 'noop'

    tiddler = Tiddler('carnaby')  # nowhere
    py.test.raises(NoBagError,
                   'determine_bag_from_recipe(recipe, tiddler, environ)')
Esempio n. 13
0
def test_get_recipe_list_templated_filter2():
    recipe = Recipe('tr')
    recipe.set_recipe([
        ('system', 'modifier={{ user }};creator={{ user }}')
        ])
    list = recipe.get_recipe({'user': '******'})
    assert list[0][1] == 'modifier=testuser;creator=testuser'
Esempio n. 14
0
def test_get_recipe():
    """
    Make sure we are in a different scope.
    """
    recipe_out = store.get(Recipe('recipeone'))
    assert recipe_out.name == 'recipeone'
    assert recipe_out.desc == 'recipe description'
Esempio n. 15
0
def _recipe_exists(store, recipe_name):
    recipe = Recipe(recipe_name)
    try:
        store.get(recipe)
    except NoRecipeError:
        return False
    return True
Esempio n. 16
0
def userpage(environ, start_response):
    username = environ['tiddlyweb.usersign']['name']
    user = environ['wsgiorg.routing_args'][1]['user']
    if username != user:
        #raise ForbiddenError
        raise UserRequiredError

    store = environ['tiddlyweb.store']
    user_data = User(user)
    try:
        user_data = store.get(user_data)
        wikinames = user_data.note.split('\n')

        wikis = []
        if wikinames:
            for name in wikinames:
                if not len(name):
                    continue
                recipe = Recipe(name)
                recipe = store.get(recipe)
                url = recipe_url(environ, recipe)
                wikis.append(dict(name=name, url=url, description=recipe.desc))
    except NoUserError:
        wikis = []

    start_response('200 OK', [('Content-Type', 'text/html')])
    environ['tiddlyweb.title'] = 'Hello %s' % user
    template = template_env.get_template('user.html')
    return template.generate(wikis=wikis)
Esempio n. 17
0
 def _recipe_list(self, tiddlers, recipe_name):
     representation_link = '%s/recipes/%s/tiddlers' % (
         self._server_prefix(), encode_name(recipe_name))
     representations = self._tiddler_list_header(representation_link)
     user_object = get_user_object(self.environ)
     recipe = self.environ['tiddlyweb.store'].get(Recipe(recipe_name))
     publicity = ''
     try:
         recipe.policy.allows(user_object, 'manage')
         policy = recipe.policy
         if policy.read == [user_object['name']]:
             publicity = 'private'
         elif policy.read == []:
             publicity = 'public'
         else:
             publicity = 'custom'
         delete = True
     except (UserRequiredError, ForbiddenError):
         policy = None
         delete = False
     data = {
         'title': 'TiddlyHoster Recipe %s' % recipe.name,
         'policy': policy,
         'publicity': publicity,
         'delete': delete,
         'recipe': recipe,
         'tiddlers': tiddlers,
         'representations': representations
     }
     del self.environ['tiddlyweb.title']
     return send_template(self.environ, 'recipelist.html', data)
Esempio n. 18
0
def get(environ, start_response):
    """
    Using query parameters, determine the current tiddler
    and produce an editor for it.
    """
    usersign = environ['tiddlyweb.usersign']
    try:
        tiddler_name = environ['tiddlyweb.query'].get('tiddler', [''])[0]
        recipe_name = environ['tiddlyweb.query'].get('recipe', [''])[0]
        bag_name = environ['tiddlyweb.query'].get('bag', [''])[0]
    except (KeyError, IndexError):
        raise HTTP400('tiddler, recipe and bag query strings required')

    store = environ['tiddlyweb.store']

    tiddler = Tiddler(tiddler_name)
    if bag_name:
        tiddler.bag = bag_name
    else:
        recipe = Recipe(recipe_name)
        try:
            recipe = store.get(recipe)
            tiddler.bag = control.determine_tiddler_bag_from_recipe(
                recipe, tiddler).name
            tiddler.recipe = recipe.name
        except NoRecipeError, exc:
            raise HTTP404('unable to edit %s, recipe %s not found: %s' %
                          (tiddler.title, recipe_name, exc))
        except NoBagError, exc:
            raise HTTP404('unable to edit %s: %s' % (tiddler.title, exc))
Esempio n. 19
0
    def _valid_bag(self, environ, space, container_name):
        """
        Return True if the requested entity is part of the current
        space's recipe or an ADMIN BAG. Otherwise return False
        indicating that privileges will be dropped.
        """
        store = environ['tiddlyweb.store']
        recipe_name = determine_space_recipe(environ, space.name)
        space_recipe = store.get(Recipe(recipe_name))
        template = recipe_template(environ)
        recipe_bags = [bag for bag, _ in space_recipe.get_recipe(template)]
        recipe_bags.extend(space.extra_bags())
        if environ['REQUEST_METHOD'] == 'GET':
            if container_name in recipe_bags:
                return True
            if container_name in ADMIN_BAGS:
                return True
        else:
            base_bags = space.list_bags()
            # add bags in the recipe which may have been added
            # by the recipe mgt. That is: bags which are not
            # included and not core.
            acceptable_bags = [
                bag for bag in recipe_bags
                if not (Space.bag_is_public(bag) or Space.bag_is_private(bag)
                        or Space.bag_is_associate(bag))
            ]
            acceptable_bags.extend(base_bags)
            acceptable_bags.extend(ADMIN_BAGS)
            if container_name in acceptable_bags:
                return True

        return False
Esempio n. 20
0
def test_put_recipe():
    recipe = Recipe('cookies')
    recipe.set_recipe(recipe_list)

    store.put(recipe)

    assert os.path.exists('store/recipes/cookies')
Esempio n. 21
0
def test_get_recipe_list_templated_bag():
    recipe = Recipe('tr')
    recipe.set_recipe([
        ('{{ user }}', '')
        ])
    list = recipe.get_recipe({'user': '******'})
    assert list[0][0] == 'testuser'
Esempio n. 22
0
def _create_recipe(environ):
    """Take the form input and turn it into a recipe."""
    # get bag_names before we flatten because it will be a list
    bag_names = environ['tiddlyweb.query'].get('bags', [])
    query_data = _flatten_form_data(environ['tiddlyweb.query'])
    store = environ['tiddlyweb.store']
    try:
        new_recipe_name = query_data['recipe_name']

        if _recipe_exists(store, new_recipe_name):
            raise HTTP409('That recipe may not be created.')

        new_recipe = Recipe(new_recipe_name)

        username = environ['tiddlyweb.usersign']['name']
        new_recipe.policy.owner = username
        new_recipe.policy.manage = [username]
        new_recipe.desc = query_data.get('recipe_desc', '')

        privacy = query_data['privacy']
        new_recipe.policy.read = _policy_form_to_entry(username, privacy)

        bag_list = []

        if query_data.get('autowiki', 0):
            bag_list.extend(AUTOWIKI_BAGS)

        # don't worry about default content bag yet
        bag_list.extend(bag_names)
        recipe_list = [[bag_name, ''] for bag_name in bag_list]
        new_recipe.set_recipe(recipe_list)

        store.put(new_recipe)
    except KeyError, exc:
        raise HTTP400('something went wrong processing for: %s' % exc)
Esempio n. 23
0
def test_html_list():
    serializer = Serializer('html')
    recipes = [Recipe('recipe' + str(name)) for name in xrange(2)]
    string = ''.join(serializer.list_recipes(recipes))

    assert 'href="recipes/recipe0' in string
    assert 'href="recipes/recipe1' in string
Esempio n. 24
0
def test_list_recipes():
    for i in xrange(50):
        store.put(Recipe(str(i)))

    recipes = store.list_recipes()

    assert len(recipes) == 51  # recipeone is still in there
    assert recipes[-2].name == '9'  # lexical sort
Esempio n. 25
0
def test_get_recipe_list_templated_bag_filter():
    recipe = Recipe('tr')
    recipe.set_recipe([
        ('{{ bagname }}', 'modifier={{ user }}')
        ])
    list = recipe.get_recipe({'user': '******', 'bagname': 'foobar'})
    assert list[0][1] == 'modifier=testuser'
    assert list[0][0] == 'foobar'
Esempio n. 26
0
def test_get_recipe_list_templated_bag_filter_defaulted_bag():
    recipe = Recipe('tr')
    recipe.set_recipe([
        ('{{ bagname:common }}', 'modifier={{ user }}')
        ])
    list = recipe.get_recipe({'user': '******'})
    assert list[0][1] == 'modifier=testuser'
    assert list[0][0] == 'common'
Esempio n. 27
0
 def list_recipes(self):
     url = self.recipes_url
     response, content = self._request('GET', url)
     if self._is_success(response):
         recipes = simplejson.loads(content)
         return [Recipe(recipe) for recipe in recipes]
     else:
         return []
Esempio n. 28
0
def test_put_recipe():
    global OUTPUT
    assert OUTPUT == ''
    recipe = Recipe('keen')
    store.put(recipe)
    store.get(recipe)
    assert 'recipe put hook ahoy' in OUTPUT
    assert 'recipe get hook ahoy' in OUTPUT
Esempio n. 29
0
    def list_recipes(self):
        q = GDRecipe.all()

        try:
            for gdrecipe in q:
                yield Recipe(gdrecipe.name)
        except TypeError:
            pass
Esempio n. 30
0
def test_recipe_get():
    """
    get a recipe from disk and confirm it has proper form.
    """

    stored_recipe = Recipe('testrecipe')
    stored_recipe = store.get(stored_recipe)

    assert stored_recipe.get_recipe() == recipe_list_string