websauna.tests.test_utils module

Testing utility functions.

websauna.tests.test_utils.PASSWORD = 'ToholamppiMadCowz585'

Unit testing default password

websauna.tests.test_utils.create_logged_in_user(dbsession, registry, web_server, browser, admin=False, email='example@example.com', password='ToholamppiMadCowz585')[source]

For a web browser test session, creates a new user and log it in inside the test browser.

websauna.tests.test_utils.create_user(dbsession, registry, email='example@example.com', password='ToholamppiMadCowz585', admin=False)[source]

A helper function to create normal and admin users for tests.


import transaction
from websauna.tests.test_utils import create_user

def test_some_stuff(dbsession, registry):

    with transaction.manager:
        u = create_user(registry)
        # Do stuff with new user
  • email (str) – User’s email address. If inot given use unit testing default.

  • password (str) – Password as plain text. If not given use unit testing default.

  • admin (bool) – If True run websauna.system.user.usermixin.SiteCreator login and set the user to admin group.

Return type


websauna.tests.test_utils.get_session_from_webdriver(driver, registry)[source]

Extract session cookie from a Selenium driver and fetch a matching pyramid_redis_sesssion data.


def test_newsletter_referral(dbsession, web_server, browser, init):
    '''Referral is tracker for the newsletter subscription.'''

    b = browser
    b.visit(web_server + "/newsletter")

    with transaction.manager:
        r = ReferralProgram()
        r.name = "Foobar program"
        ref_id, slug = r.id, r.slug

    # Inject referral data to the active session. We do this because it is very hard to spoof external links pointing to localhost test web server.
    session = get_session_from_webdriver(b.driver, init.config.registry)
    session["referral"] = {
        "ref": slug,
        "referrer": "http://example.com"

    b.fill("email", "[email protected]")

    # Displayed as a message after succesful form subscription
    assert b.is_text_present("Thank you!")

    # Check we get an entry
    with transaction.manager:
        assert dbsession.query(NewsletterSubscriber).count() == 1
        subscription = dbsession.query(NewsletterSubscriber).first()
        assert subscription.email == "[email protected]"
        assert subscription.ip == ""
        assert subscription.referral_program_id == ref_id
        assert subscription.referrer == "http://example.com"
  • driver (WebDriver) – The active WebDriver (usually browser.driver)

  • registry (Registry) – The Pyramid registry (usually init.config.registry)

Return type


websauna.tests.test_utils.login(web_server, browser, email='example@example.com', password='ToholamppiMadCowz585')[source]

Login user to the website through the test browser.

websauna.tests.test_utils.logout(web_server, browser)[source]

Log out the current user from the test browser.

websauna.tests.test_utils.make_dummy_request(dbsession, registry)[source]

Creates a non-functional HTTP request with registry and dbsession configured.

Useful for crafting requests with custom settings

See also make_routable_request().

Return type


websauna.tests.test_utils.wait_until(callback, expected, deadline=1.0, poll_period=0.05)[source]

A helper function to wait until a variable value is set (in another thread).

This is useful for communicating between Selenium test driver and test runner main thread.

  • callback (Callable) – t.Callable which we expect to return to expected value.

  • deadline – Seconds how long we are going to wait max

  • poll_period – Sleep period between check attemps


The final value of callback