Exemplo n.º 1
0
def test_i18n():
    render = Render(i18n=views_dir)

    def ok(request):
        return render.from_string('{{ i18n.HELLO }}')

    def fail(request):
        return render.from_string('{{ i18n.FOOBAR }}')

    urls = [
        Rule('/ok/', ok),
        Rule('/fail/', fail),
    ]
    app = Shake(urls)
    c = app.test_client()

    resp = c.get('/ok/?lang=en-US')
    assert resp.status_code == HTTP_OK
    assert resp.data == 'Hello World'

    resp = c.get('/ok/?lang=en_US')
    assert resp.status_code == HTTP_OK
    assert resp.data == 'Hello World'

    resp = c.get('/ok/?lang=es-AR')
    assert resp.data == 'Hola mundo'

    resp = c.get('/fail/?lang=en-US')
    assert resp.data == ''
Exemplo n.º 2
0
def test_url_for():
    def index(request):
        expected = '/hello/world/'
        url = url_for(endpoint, name='world')
        assert url == expected

        expected = 'http://localhost/hello/world/'
        url = url_for(endpoint, name='world', external=True)
        assert url == expected

        expected = '/hello/world/#awesome'
        url = url_for(endpoint, name='world', anchor='awesome')
        assert url == expected

        expected = 'http://localhost/hello/world/#awesome'
        url = url_for(endpoint, name='world', anchor='awesome', external=True)
        assert url == expected

    urls = [
        Rule('/', index),
        Rule('/hello/<name>/', endpoint),
    ]
    app = Shake(urls)
    c = app.test_client()
    resp = c.get('/')
Exemplo n.º 3
0
def test_flash_messagesech():
    def t1(request):
        msgs = get_messages()
        assert msgs == []

    def t2(request):
        flash(request, 'foo')
        flash(request, 'bar', 'error', extra='blub')
        msgs = get_messages()
        assert len(msgs) == 2
        assert (msgs[0]['msg'], msgs[0]['cat'], msgs[0]['extra']) == \
            ('foo', 'info', None)
        assert (msgs[1]['msg'], msgs[1]['cat'], msgs[1]['extra']) == \
            ('bar', 'error', 'blub')
        msgs2 = get_messages()
        assert msgs2 == msgs

    urls = [
        Rule('/t1', t1),
        Rule('/t2', t2),
    ]
    settings = {'SECRET_KEY': 'abc' * 20}
    app = Shake(urls, settings)
    c = app.test_client()
    c.get('/t1')
    c.get('/t2')
Exemplo n.º 4
0
def test_repeated_processors():
    r = []

    def index(request):
        r.append('controller')

    def rq1(request):
        r.append('rq1')

    def rs1(response):
        r.append('rs1')
        return response

    def on_error(e):
        r.append('error')

    urls = [
        Rule('/', index),
    ]
    app = Shake(urls)

    app.before_request(rq1)
    app.before_request(rq1)
    app.before_response(rs1)
    app.before_response(rs1)
    app.on_exception(on_error)
    app.before_request(rq1)
    app.before_response(rs1)
    app.before_request(rq1)
    app.on_exception(on_error)
    c = app.test_client()

    c.get('/')
    assert r == ['rq1', 'controller', 'rs1']
Exemplo n.º 5
0
def test_named_url_for():
    def test(request):
        result = [url_for('a'), url_for('b'), url_for('c')]
        return ' '.join(result)

    urls = [
        Rule('/', test),
        Rule('/home1/', endpoint, name='a'),
        Rule('/home2/', endpoint, name='b'),
        Rule('/home3/', endpoint, name='c'),
    ]
    app = Shake(urls)
    c = app.test_client()

    resp = c.get('/')
    assert resp.status_code == 200
    assert resp.data == '/home1/ /home2/ /home3/'
Exemplo n.º 6
0
def test_session():
    def p1(request):
        request.session['foo'] = 'bar'

    def p2(request):
        assert 'bar' == request.session['foo']

    urls = [
        Rule('/p1/', p1),
        Rule('/p2/', p2),
    ]
    settings = {'SECRET_KEY': 'q' * 22}
    app = Shake(urls, settings)
    c = app.test_client()

    resp = c.get('/p1/')
    resp = c.get('/p2/')
    assert resp.status_code == HTTP_OK
Exemplo n.º 7
0
def test_default_error():
    urls = [
        Rule('/', fail),
    ]
    app = Shake(urls)

    c = app.test_client()
    with pytest.raises(AssertionError):
        c.get('/')
Exemplo n.º 8
0
def test_default_not_allowed():
    urls = [
        Rule('/', no_pass),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_FORBIDDEN
    assert '<title>Access Denied</title>' in resp.data
Exemplo n.º 9
0
def test_default_not_found():
    urls = [
        Rule('/', index),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/bla')
    assert resp.status_code == HTTP_NOT_FOUND
    assert '<title>Page not found</title>' in resp.data
Exemplo n.º 10
0
def test_add_urls():
    def number(request, num):
        return str(num)

    urls = [
        Rule('/', index),
        Rule('/<int:num>/', number),
    ]
    app = Shake()
    app.add_urls(urls)
    c = app.test_client()

    resp = c.get('/')
    assert resp.status_code == HTTP_OK
    assert resp.data == 'hello'

    resp = c.get('/3/')
    assert resp.status_code == HTTP_OK
    assert resp.data == '3'
Exemplo n.º 11
0
def test_string_endpoint():
    urls = [
        Rule('/', 'tests.test_app.index'),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_OK
    assert resp.data == 'hello'
Exemplo n.º 12
0
def test_callable_endpoint():
    urls = [
        Rule('/', index),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_OK
    assert resp.data == 'hello'
Exemplo n.º 13
0
def test_custom_not_found():
    urls = [
        Rule('/', index),
    ]
    settings = {'PAGE_NOT_FOUND': not_found}
    app = Shake(urls, settings)

    c = app.test_client()
    resp = c.get('/bla')
    assert resp.status_code == HTTP_NOT_FOUND
    assert resp.data == 'not found'
Exemplo n.º 14
0
def test_custom_error():
    urls = [
        Rule('/', fail),
    ]
    settings = {'PAGE_ERROR': error, 'DEBUG': False}
    app = Shake(urls, settings)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_ERROR
    assert resp.data == 'error'
Exemplo n.º 15
0
def test_custom_not_allowed():
    urls = [
        Rule('/', no_pass),
    ]
    settings = {'PAGE_NOT_ALLOWED': not_allowed}
    app = Shake(urls, settings)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_FORBIDDEN
    assert resp.data == 'access denied'
Exemplo n.º 16
0
def test_default_not_allowed():
    urls = [
        Rule('/', no_pass),
    ]
    settings = {
        'PAGE_NOT_ALLOWED': not_allowed_page,
    }
    app = Shake(urls, settings)
    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_FORBIDDEN
    assert '<title>Access Denied</title>' in resp.data
Exemplo n.º 17
0
def test_session_nosecret():
    def p1(request):
        request.session['foo'] = 'bar'

    urls = [
        Rule('/', p1),
    ]
    app = Shake(urls)
    c = app.test_client()

    with pytest.raises(RuntimeError):
        c.get('/')
Exemplo n.º 18
0
def test_redirect():
    def redir(request):
        return redirect('/bla')

    urls = [
        Rule('/', redir),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_FOUND
Exemplo n.º 19
0
def test_default_globals_url_for():
    render = Render()

    def foo(request):
        tmpl = "{{ url_for('foo') }}"
        assert render.from_string(tmpl) == '/'

    urls = [
        Rule('/', foo, name='foo'),
    ]
    app = Shake(urls)
    c = app.test_client()
    c.get('/')
Exemplo n.º 20
0
def test_url_for_method():
    def index(request):
        expected = '/get/resource/'
        url = url_for(endpoint, name='resource')
        assert url == expected

        expected = '/get/resource/'
        url = url_for(endpoint, name='resource', method='GET')
        assert url == expected

        expected = '/update/resource/'
        url = url_for(endpoint, name='resource', method='POST')
        assert url == expected

    urls = [
        Rule('/', index),
        Rule('/get/<name>/', endpoint, methods=['GET']),
        Rule('/update/<name>/', endpoint, methods=['POST']),
    ]
    app = Shake(urls)
    c = app.test_client()
    resp = c.get('/')
Exemplo n.º 21
0
def test_default_response():
    def index(request):
        pass

    urls = [
        Rule('/', index),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_OK
    assert resp.data == ''
Exemplo n.º 22
0
def test_default_globals():
    render = Render()

    def foo(request):
        tmpl = '{{ media }}{{ request }}{{ settings }}'
        render.from_string(tmpl)

    urls = [
        Rule('/', foo),
    ]
    app = Shake(urls)
    c = app.test_client()
    c.get('/')
Exemplo n.º 23
0
def test_before_response_return():
    def index(request):
        return 'ok'

    def brs(response):
        pass

    urls = [Rule('/', index)]
    app = Shake(urls)
    app.before_response(brs)

    c = app.test_client()
    with pytest.raises(Exception):
        c.get('/')
Exemplo n.º 24
0
def test_response_none():
    def index(request):
        return None

    urls = [
        Rule('/', index),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_OK
    assert resp.mimetype == 'text/plain'
    assert resp.data == ''
Exemplo n.º 25
0
def test_response_mimetype():
    def index(request):
        return Response('hello world', mimetype='foo/bar')

    urls = [
        Rule('/', index),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.status_code == HTTP_OK
    assert resp.mimetype == 'foo/bar'
    assert resp.data == 'hello world'
Exemplo n.º 26
0
def test_mimetype():
    render1 = Render(views_dir)
    render2 = Render(views_dir, default_mimetype='foo/bar')

    def t1(request):
        resp = render1('view.html')
        assert isinstance(resp, local.app.response_class)
        assert resp.mimetype == 'text/html'

    def t2(request):
        resp = render2('view.html')
        assert isinstance(resp, local.app.response_class)
        assert resp.mimetype == 'foo/bar'

    urls = [
        Rule('/t1', t1),
        Rule('/t2', t2),
    ]
    app = Shake(urls)
    local.app = app
    c = app.test_client()
    c.get('/t1')
    c.get('/t2')
Exemplo n.º 27
0
def test_default_error():
    urls = [
        Rule('/', fail),
    ]
    settings = {
        'DEBUG': False,
        'PAGE_ERROR': error_page,
    }
    app = Shake(urls, settings)
    c = app.test_client()

    resp = c.get('/')
    assert resp.status_code == HTTP_ERROR
    assert '<title>Error</title>' in resp.data
Exemplo n.º 28
0
def test_render_view_controller():
    urls = [
        Rule('/',
             render_view,
             defaults={
                 'render': render,
                 'view': 'view.html'
             }),
    ]
    app = Shake(urls)

    c = app.test_client()
    resp = c.get('/')
    assert resp.data == '<h1>Hello World</h1>'
    assert resp.mimetype == 'text/html'
Exemplo n.º 29
0
def test_default_globals_flash_messages():
    render = Render()

    def foo(request):
        flash(request, 'foo')
        tmpl = '{% for fm in get_messages() %}{{ fm.msg }}{% endfor %}'
        assert render.from_string(tmpl) == 'foo'

    urls = [
        Rule('/', foo),
    ]
    settings = {'SECRET_KEY': 'abc' * 20}
    app = Shake(urls, settings)
    c = app.test_client()
    c.get('/')
Exemplo n.º 30
0
def test_csrf_token_global():
    render = Render()

    def t(request):
        csrf = get_csrf_secret(request)
        tmpl = '{{ csrf_secret.name }} {{ csrf_secret.value }}'
        assert render.from_string(tmpl) == '%s %s' % (csrf.name, csrf.value)

    urls = [
        Rule('/', t),
    ]
    settings = {'SECRET_KEY': 'abc' * 20}
    app = Shake(urls, settings)
    c = app.test_client()
    c.get('/')