def test_refresh_urls():
    """
    register a url after the server has been started, and then refresh them
    """
    store = setup_store()
    urls_init(config)
    setup_web()
    http = httplib2.Http()

    tiddler = Tiddler('bar', 'foo')
    tiddler.text = 'foo bar'
    store.put(tiddler)

    #check that no url exists yet
    response = http.request('http://test_domain:8001/foo')[0]
    assert response.status == 404

    url(['/foo', '/bags/foo/tiddlers/bar'])

    #resfresh the currently loaded set of urls
    response = http.request('http://test_domain:8001/urls/refresh')[0]
    assert response.status == 200

    #now check it was loaded successfully
    response = http.request('http://test_domain:8001/foo')[0]
    assert response.status == 200
def test_unicode_in_recipes():
    """
    visit a recipe passing unicode in as one of the variables
    """
    store = setup_store()
    url(['/foo/{bar:segment}', '/recipes/custom/tiddlers'])
    urls_init(config)
    setup_web()
    http = httplib2.Http()

    bag = Bag(u'unicodeø•º∆∆˙ª')
    store.put(bag)
    tiddler = Tiddler(u'bar œ∑´®†¥', u'unicodeø•º∆∆˙ª')
    tiddler.text = 'foo bar'
    store.put(tiddler)

    recipe = Recipe('custom')
    recipe.set_recipe([('{{ bar:foo }}', '')])
    store.put(recipe)

    response, content = http.request('http://test_domain:8001/foo/unicodeø•º∆∆˙ª')

    assert response.status == 200

    direct_url = http.request(u'http://test_domain:8001/recipes/custom/tiddlers')[1]
    assert content != direct_url #direct_url should load the foo bag instead

    recipe.set_recipe([(u'unicodeø•º∆∆˙ª', '')])
    store.put(recipe)
    direct_url = http.request(u'http://test_domain:8001/recipes/custom/tiddlers')[1]
    assert content == direct_url
def test_recipe_variables():
    """
    access a recipe with variables in it
    """
    store = setup_store()
    url(['/foo/{name:segment}', '/recipes/custom/tiddlers'])
    urls_init(config)
    setup_web()
    http = httplib2.Http()

    tiddler = Tiddler('bar', 'foo')
    tiddler.text = 'foo bar'
    store.put(tiddler)

    recipe = Recipe('custom')
    recipe.set_recipe([('{{ name:bar }}', '')])
    store.put(recipe)

    response, content = http.request('http://test_domain:8001/foo/foo')

    assert response.status == 200

    direct_url = http.request('http://test_domain:8001/recipes/custom/tiddlers')[1]
    assert content != direct_url #accessing directly, the default bag should be used instead

    #now check that the correct bag was actually loaded)
    recipe.set_recipe([('foo', '')])
    store.put(recipe)
    direct_url = http.request('http://test_domain:8001/recipes/custom/tiddlers')[1]
    assert content == direct_url
def test_add_invalid_selector():
    """
    add an invalid selector path
    """
    store = setup_store()
    urls_init(config)

    try:
        url(['/invalid/{variable:unrecognisedtype}', 'www.google.com'])
        raise AssertionError('Invalid URL put into store successfully')
    except InvalidSelectorURL:
        pass #success
def test_redirect_with_tag():
    """
    add a redirect defined by tagging 'redirect'
    """
    store = setup_store()
    url(['--redirect', '/foo', '/bags'])
    urls_init(config)
    setup_web()
    http =  httplib2.Http()

    http.follow_redirects = False
    response = http.request('http://test_domain:8001/foo')[0]
    assert response.status == 301
    assert response['location'] == '/bags'
def test_redirect_with_www():
    """
    add a redirect without a protocol
    """
    store = setup_store()
    url(['/foo', 'www.google.com'])
    urls_init(config)
    setup_web()
    http =  httplib2.Http()

    http.follow_redirects = False
    response = http.request('http://test_domain:8001/foo')[0]
    assert response.status == 301
    assert response['location'] == 'http://www.google.com'
def test_add_variable_destination_url():
    """
    add a url with a variable destination
    """
    store = setup_store()
    urls_init(config)

    url(['/foobar/{baz:segment}[/]', '/recipes/foobar/tiddlers/{{ baz }}'])

    tiddler = Tiddler('/foobar/{baz:segment}[/]', 'urls')
    try:
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        raise AssertionError('URL not put into store')

    assert tiddler.title == '/foobar/{baz:segment}[/]'
    assert tiddler.text == '/recipes/foobar/tiddlers/{{ baz }}'
def test_add_patterned_url():
    """
    add a URL with variables inside it
    """
    store = setup_store()
    urls_init(config)

    url(['/foobar/{baz:segment}[/]', '/recipes/foobar/tiddlers'])

    tiddler = Tiddler('/foobar/{baz:segment}[/]', 'urls')
    try:
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        raise AssertionError('URL not put into store')

    assert tiddler.title == '/foobar/{baz:segment}[/]'
    assert tiddler.text == '/recipes/foobar/tiddlers'
def test_add_interal_redirect():
    """
    add an internal url, but make it a redirect
    """
    store = setup_store()
    urls_init(config)
    
    url(['--redirect', '/foobar', '/recipes/foobar/tiddlers'])
    
    tiddler = Tiddler('/foobar', 'urls')
    try:
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        raise AssertionError('URL not put into store')
    except NoBagError:
        raise AssertionError('urls bag not created')
    
    assert tiddler.title == '/foobar'
    assert tiddler.text == '/recipes/foobar/tiddlers'
    assert tiddler.tags == ['redirect']
def test_add_external_url():
    """
    add an external (ie - always redirect) url
    """
    store = setup_store()
    urls_init(config)
    
    url(['/google', 'http://www.google.com'])
    
    tiddler = Tiddler('/google', 'urls')
    try:
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        raise AssertionError('URL not put into store')
    except NoBagError:
        raise AssertionError('urls bag not created')
    
    assert tiddler.title == '/google'
    assert tiddler.text == 'http://www.google.com'
    assert tiddler.tags == []
def test_add_unicode():
    """
    add some unicode
    """
    store = setup_store()
    urls_init(config)

    title = urllib.unquote(u'%2Funicode%2F%7E%7E%E2%88%82%C3%A5%C2%A8%5E%C3%A5%7E%C3%9F%E2%88%82%5E%C3%B8')
    text = urllib.unquote(u'/bags/%7E%7E%E2%88%82%C3%A5%C2%A8%5E%C3%A5%7E%C3%9F%E2%88%82%5E%C3%B8/tiddlers/%7E%7E%E2%88%82%C3%A5%C2%A8%5E%C3%A5%7E%C3%9F%E2%88%82%5E%C3%B8')
    print title, text
    url([title, text])

    tiddler = Tiddler(title, 'urls')
    try:
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        raise AssertionError(u'Unicode tiddler not in store')

    assert tiddler.title == title
    assert tiddler.text == text
def test_spaces_in_variables():
    """
    visit a url by passing a variable with spaces into the destination
    """
    store = setup_store()
    url(['/foo/{bar:segment}', '/bags/foo/tiddlers/{{ bar }}.json'])
    urls_init(config)
    setup_web()
    http = httplib2.Http()

    tiddler = Tiddler('bar baz', 'foo')
    tiddler.text = 'foo bar'
    store.put(tiddler)

    response, content = http.request('http://test_domain:8001/foo/bar%20baz')

    assert response.status == 200

    direct_url = http.request('http://test_domain:8001/bags/foo/tiddlers/bar%20baz.json')[1]
    assert content == direct_url
def test_add_invalid_external_url():
    """
    add a URL that does not match any patterns
    ie - it is not internal, but not recognised as external
    """
    store = setup_store()
    urls_init(config)

    try:
        url(['/foo/bar/baz/', 'foo.bar.baz'])
        raise AssertionError('Invalid URL put into store successfully')
    except InvalidDestinationURL:
        pass #success

    tiddler = Tiddler('/foo/bar/baz/', 'urls')
    try:
        store.get(tiddler)
        raise AssertionError('Invalid URL found in store')
    except NoTiddlerError:
        pass #success
def test_replace_variables():
    """
    access a url with variables in the destination
    """
    store = setup_store()
    url(['/foo/{tiddler_name:segment}', '/bags/foo/tiddlers/{{ tiddler_name }}.json'])
    urls_init(config)
    setup_web()
    http = httplib2.Http()

    tiddler = Tiddler('bar', 'foo')
    tiddler.text = 'foo bar'
    store.put(tiddler)

    response, content = http.request('http://test_domain:8001/foo/bar')

    assert response.status == 200

    direct_url = http.request('http://test_domain:8001/bags/foo/tiddlers/bar.json')[1]
    assert content == direct_url
def test_unicode():
    """
    visit a url destination with unicode
    """
    store = setup_store()
    url(['/foo', u'/bags/foo/tiddlers/bar œ∑´®†¥.json'])
    urls_init(config)
    setup_web()
    http = httplib2.Http()

    tiddler = Tiddler(u'bar œ∑´®†¥', 'foo')
    tiddler.text = 'foo bar'
    store.put(tiddler)

    response, content = http.request('http://test_domain:8001/foo')

    assert response.status == 200

    direct_url = http.request(u'http://test_domain:8001/bags/foo/tiddlers/bar%20œ∑´®†¥.json')[1]
    assert content == direct_url
def test_load_url_collection():
    """
    visit a collection of tiddlers at a custom url
    """
    store = setup_store()
    url(['/foo', '/bags/foo/tiddlers.json'])
    urls_init(config)
    setup_web()
    http = httplib2.Http()

    tiddler = Tiddler('bar', 'foo')
    tiddler.text = 'foo bar'
    store.put(tiddler)

    response, content = http.request('http://test_domain:8001/foo')

    assert response.status == 200

    direct_url = http.request('http://test_domain:8001/bags/foo/tiddlers.json')[1]
    assert content == direct_url
def test_add_internal_url():
    """
    add an internal (ie - not mapped, redirected) url
    """
    store = setup_store()
    urls_init(config)
    
    url(['/foo', '/bags/foo/tiddlers'])
    
    tiddler = Tiddler('/foo', 'urls')
    try:
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        raise AssertionError('URL not put into store')
    except NoBagError:
        raise AssertionError('urls bag not created')
    
    assert tiddler.title == '/foo'
    assert tiddler.text == '/bags/foo/tiddlers'
    assert tiddler.tags == []
def test_add_invalid_url():
    """
    add a url with a variable destination but no variable in source to make
    sure that it errors
    """
    store = setup_store()
    urls_init(config)

    #try and add a variable destination with no variable in the source
    try:
        url(['/foobar/baz/tada', '/recipes/{{ foobar }}/tiddlers/{{ baz }}'])
        raise AssertionError('Invalid URL put into store successfully')
    except InvalidDestinationURL:
        pass #success

    tiddler = Tiddler('/foobar/baz/tada', 'urls')
    try:
        store.get(tiddler)
        raise AssertionError('Invalid URL found in store')
    except NoTiddlerError:
        pass #success
def test_add_invalid_internal_url():
    """
    add an internal URL for mapping to (ie - not redirecting) that is
    not supported by tiddlywebplugins.urls (which only supports tiddlers
    and collections of tiddlers
    """
    store = setup_store()
    urls_init(config)

    try:
        url(['/foo/bar/baz/', '/bags/foo'])
        raise AssertionError('Invalid URL put into store successfully')
    except InvalidDestinationURL:
        pass #success

    tiddler = Tiddler('/foo/bar/baz/', 'urls')
    try:
        store.get(tiddler)
        raise AssertionError('Invalid URL found in store')
    except NoTiddlerError:
        pass #success