You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.6 KiB
125 lines
3.6 KiB
''' tests for the login, logout, registration and account settings''' |
|
|
|
import pytest |
|
|
|
from . import testapp |
|
from .. import get_user |
|
|
|
|
|
# helper functions |
|
|
|
def assert_user_is_logged_in(response, username): |
|
''' checks if login was successful ''' |
|
assert '<!-- user is logged in -->' in response |
|
assert 'id="login-form"' not in response |
|
assert 'Logged in as <span>{}</span>'.format(username) in response |
|
|
|
|
|
def assert_user_login_failed(response, username): |
|
''' checks if login was un successful ''' |
|
assert '<!-- No logged in user -->' in response |
|
assert 'id="login-form' in response |
|
assert 'Logged in as <span>{}</span>'.format(username) not in response |
|
assert 'You entered the wrong username or password' in response |
|
|
|
|
|
# tests for login and logout of users |
|
|
|
def test_account_login_for_active_users(testapp): |
|
''' check if user login works ''' |
|
testapp.reset() |
|
|
|
user = get_user('user') |
|
root = testapp.get('/') |
|
login_form = root.forms['login-form'] |
|
login_form.set('username', user.username) |
|
login_form.set('password', user.first_name) |
|
response = login_form.submit() |
|
|
|
# a login leads to a redirect |
|
assert response.status == '302 Found' |
|
assert response.location.endswith('/orders') |
|
|
|
# the layout should reflect the login |
|
response = testapp.get('/faq') |
|
assert_user_is_logged_in(response, user.username) |
|
|
|
|
|
def test_account_login_for_inactive_users(testapp): |
|
''' check if user login works ''' |
|
testapp.reset() |
|
|
|
user = get_user('unvalidated') |
|
root = testapp.get('/') |
|
login_form = root.forms['login-form'] |
|
login_form.set('username', user.username) |
|
login_form.set('password', user.first_name) |
|
response = login_form.submit() |
|
|
|
assert_user_login_failed(response, user.username) |
|
|
|
|
|
def test_account_login_fails(testapp): |
|
''' check if user login works ''' |
|
testapp.reset() |
|
|
|
root = testapp.get('/') |
|
login_form = root.forms['login-form'] |
|
login_form.set('username', 'EricIdle') |
|
login_form.set('password', 'wrong password') |
|
response = login_form.submit() |
|
|
|
assert_user_login_failed(response, 'EricIdle') |
|
assert '/account/forgot-password' in response |
|
|
|
|
|
def test_account_login_works_after_failed_login(testapp): |
|
''' check if user login works after failed attempt ''' |
|
testapp.reset() |
|
|
|
root = testapp.get('/') |
|
login_form = root.forms['login-form'] |
|
login_form.set('username', 'EricIdle') |
|
login_form.set('password', 'wrong password') |
|
response = login_form.submit() |
|
|
|
assert_user_login_failed(response, 'EricIdle') |
|
|
|
login_form = response.forms['login-form'] |
|
login_form.set('username', 'EricIdle') |
|
login_form.set('password', 'Eric') |
|
login_form.submit() |
|
response = testapp.get('/faq') |
|
|
|
assert_user_is_logged_in(response, 'EricIdle') |
|
|
|
|
|
def test_account_login_fails_after_failed_login(testapp): |
|
''' check if user login works after failed attempt ''' |
|
testapp.reset() |
|
|
|
root = testapp.get('/') |
|
login_form = root.forms['login-form'] |
|
login_form.set('username', 'EricIdle') |
|
login_form.set('password', 'wrong password') |
|
response = login_form.submit() |
|
|
|
assert_user_login_failed(response, 'EricIdle') |
|
|
|
login_form = response.forms['login-form'] |
|
login_form.set('username', 'EricIdle') |
|
login_form.set('password', 'wrong password') |
|
response = login_form.submit() |
|
|
|
assert_user_login_failed(response, 'EricIdle') |
|
|
|
|
|
def test_account_logout_works(testapp): |
|
''' check if a user can log out ''' |
|
testapp.reset() |
|
|
|
testapp.login('user') |
|
response = testapp.get('/account/logout').follow() |
|
|
|
assert '<!-- No logged in user -->' in response |
|
assert 'id="login-form' in response
|
|
|