def test_nested_variables():
    url = u"/checkform?foo.name=Kevin&foo.age=some%20Numero".encode("utf-8")
    testutil.stop_server(tg_only = True)
    app = testutil.make_app(NestedController)
    testutil.start_server()
    request = app.get(url)
    assert config.get("decoding_filter.encoding", path='/') == "utf8"
    assert request.raw["name"] == "Kevin"
    assert request.raw["age"] == u"some Numero"
def teardown_module():
    fresh_metadata = get_metadata('fresh')
    fresh_metadata.drop_all()
    fresh_metadata.bind.dispose()
    metadata.drop_all()
    sqlalchemy_cleanup()
    if os.path.exists('freshtest.db'):
        os.unlink('freshtest.db')

    stop_server()
 def test_nested_login(self):
     """Check that we can login using a nested form."""
     config.update({
         'identity.form.user_name': 'credentials.user',
         'identity.form.password': '******',
         'identity.form.submit': 'log.me.in'})
     testutil.stop_server(tg_only=False)
     self.app = testutil.make_app(self.root)
     testutil.start_server()
     response = self.app.get('/logged_in_only?'
         'credentials.user=samIam&credentials.pass=secret&log.me.in=Enter')
     assert 'logged_in_only' in response, response
 def test_allow_json_config_false(self):
     """Make sure JSON can still be restricted with a global config on."""
     config.update({'tg.allow_json': False})
     class JSONRoot(controllers.RootController):
         @expose(template="kid:turbogears.tests.simple")
         def allowjsonconfig(self):
             return dict(title="Foobar", mybool=False, someval="foo",
                  tg_template="kid:turbogears.tests.simple")
     testutil.stop_server()
     app = testutil.make_app(JSONRoot)
     testutil.start_server()
     response = app.get('/allowjsonconfig')
     response = app.get('/allowjsonconfig?tg_format=json', status=404)
     assert response.headers["Content-Type"] == "text/html"
     config.update({'tg.allow_json': True})
def teardown_module():
    testutil.stop_server()
def teardown_module():
    testutil.unmount()
    testutil.stop_server()
def teardown_module():
    stop_server()
def test_repeating_fields():
    """Test widget with repeating fields."""
    repetitions = 5

    class MyFields(widgets.WidgetsList):
        name = widgets.TextField(validator=validators.String(not_empty=True))
        comment = widgets.TextField(validator=validators.String())

    form = widgets.TableForm(name="form", fields=[widgets.RepeatingFieldSet(
        name="repeat", fields=MyFields(), repetitions=repetitions)])

    class MyRoot(turbogears.controllers.RootController):

        @expose(template="kid:turbogears.widgets.tests.form")
        def test(self):
            return dict(form=form)

        @expose(template="kid:turbogears.widgets.tests.form")
        def test_value(self):
            value = dict(repeat=[
                {'name': 'foo', 'comment': 'hello'},
                None, None,
                {'name': 'bar', 'comment': 'byebye'}])
            return dict(form=form, value=value)

        @error_handler()
        @validate(form=form)
        @expose(template="kid:turbogears.widgets.tests.form")
        def test_validation(self):
            validation_errors = cherrypy.request.validation_errors
            return dict(form=form, validation_errors=validation_errors)

    app = testutil.make_app(MyRoot)
    response = app.get("/test")
    output = response.body.lower()
    assert 'content="kid"' in output and 'content="genshi"' not in output
    for i in range(repetitions):
        assert 'id="form_repeat_%i"' % i in output
        assert 'name="repeat-%i.name"' % i in output
        assert 'id="form_repeat_%i_name"' % i in output
        assert 'name="repeat-%i.comment"' % i in output
        assert 'id="form_repeat_%i_comment"' % i in output

    response = app.get("/test_value")
    output = response.body.lower()
    assert 'content="kid"' in output and 'content="genshi"' not in output

    name_p = 'name="repeat-0.name"'
    value_p = 'value="foo"'
    assert (re.compile('.*'.join([value_p, name_p])).search(output)
        or re.compile('.*'.join([name_p, value_p])).search(output))

    name_p = 'name="repeat-0.comment"'
    value_p = 'value="hello"'
    assert (re.compile('.*'.join([value_p, name_p])).search(output)
        or re.compile('.*'.join([name_p, value_p])).search(output))

    name_p = 'name="repeat-3.name"'
    value_p = 'value="bar"'
    assert (re.compile('.*'.join([value_p, name_p])).search(output)
        or re.compile('.*'.join([name_p, value_p])).search(output))

    name_p = 'name="repeat-3.comment"'
    value_p = 'value="byebye"'
    assert (re.compile('.*'.join([value_p, name_p])).search(output)
        or re.compile('.*'.join([name_p, value_p])).search(output))

    name_p = 'name="repeat-1.name"'
    value_p = 'value=""'
    assert (re.compile('.*'.join([value_p, name_p])).search(output)
        or re.compile('.*'.join([name_p, value_p])).search(output))

    name_p = 'name="repeat-1.comment"'
    value_p = 'value=""'
    assert (re.compile('.*'.join([value_p, name_p])).search(output)
        or re.compile('.*'.join([name_p, value_p])).search(output))

    #FIXME: Why is stopping the server necessary here?
    testutil.stop_server(tg_only = True)
    app = testutil.make_app(MyRoot)
    testutil.start_server()

    response = app.get("/test_validation?repeat-0.name=foo&repeat-1.name="
        "&repeat-2.name=bar&repeat-3.name=&repeat-4.name=")
    output = response.body.lower()
    assert 'content="kid"' in output and 'content="genshi"' not in output

    validation_errors = response.raw['validation_errors']
    assert validation_errors.has_key("repeat")
    assert isinstance(validation_errors["repeat"], list)
    assert validation_errors["repeat"][0] is None
    assert validation_errors["repeat"][1].has_key("name")
    assert validation_errors["repeat"][2] is None
    assert validation_errors["repeat"][3].has_key("name")
    assert validation_errors["repeat"][4].has_key("name")
 def tearDown(self):
     testutil.stop_server()
     super(TestTGUser, self).tearDown()
     config.update({'identity.on': self._identity_on})