I usually use pytest and put similar stuff into tests/functional/conftest.py
import pytest
@pytest.fixture(scope='module')
def test_app():
import cookielib
from webtest import TestApp
from zope.component import getSiteManager
from gp.app import main
app = main({}, **{
'mongodb.url': 'mongodb://localhost:27017/unittest',
'auth.secret': 'mysecret',
'password.rounds': 4,
})
def app_registry(*args):
return app.registry
# setup test registry, otherwise we wouldn't find our utilities
getSiteManager.sethook(app_registry)
jar = cookielib.CookieJar()
testapp = TestApp(app, cookiejar=jar)
return testapp
@pytest.fixture(scope='module')
def user_app(request, test_app): #, mongodb_testing):
from gp.model import user_model
user = user_model.User()
user.user_name = 'user_name'
user.password = 'password'
# save user to mongo
user.m.save()
# log in as user
response = test_app.post_json('/auth/login',
dict(user_name=user.user_name, password='password'))
assert response.status_int == 302
def teardown_test_data():
user_model.User.m.collection.drop()
request.addfinalizer(teardown_test_data)
return test_app
with this setup you can use the user_app fixture in every test in
tests/functional/test_*.py
this setup is preparing the fixtures once per test module - so it should be
reasonable fast...
On 19.06.2015 00:06, Jeff Dairiki wrote:
> One way is to step through the whole login procedure. I.e. make sure your
> test app is configured with a particular user
> (and password) in its user db, then so something like::
>
> res = self.app.get('/login')
> login_form = res.forms['login-form']
> login_form['username'] = 'someuser'
> login_form['password'] = 'sekret'
> res = login_form.submit().maybe_follow(status=200)
>
>
> On Wed, Jun 17, 2015 at 10:39 AM, Winston Ferreira <[email protected]
> <mailto:[email protected]>> wrote:
>
> Hello,
>
> How do I set an authentication cookie with WebTest for unittesting?
>
> |
>
> import unittest2 as unittest
> from webtest import TestApp
>
> from sisfest import main
>
>
> class MyTestCase(unittest.TestCase):
>
> def setUp(self):
> self.app = TestApp(main({}))
>
> def test_view_permission(self):
> self.app.get('/admin_only_url', status=200)
> # this will always return status=403
>
> |
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
> [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
> [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.