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.
116 lines
3.7 KiB
116 lines
3.7 KiB
11 years ago
|
# -*- coding: utf-8 -*-
|
||
|
'''Functional tests using WebTest.
|
||
|
|
||
|
See: http://webtest.readthedocs.org/
|
||
|
'''
|
||
11 years ago
|
import pytest
|
||
11 years ago
|
from flask import url_for
|
||
|
|
||
11 years ago
|
|
||
|
from {{cookiecutter.app_name}}.user.models import User
|
||
11 years ago
|
from .factories import UserFactory
|
||
|
|
||
11 years ago
|
@pytest.fixture
|
||
|
def user(db):
|
||
|
return UserFactory(password='myprecious')
|
||
11 years ago
|
|
||
11 years ago
|
class TestLoggingIn:
|
||
11 years ago
|
|
||
11 years ago
|
def test_can_log_in_returns_200(self, user, testapp):
|
||
11 years ago
|
# Goes to homepage
|
||
11 years ago
|
res = testapp.get("/")
|
||
11 years ago
|
# Fills out login form in navbar
|
||
|
form = res.forms['loginForm']
|
||
11 years ago
|
form['username'] = user.username
|
||
11 years ago
|
form['password'] = 'myprecious'
|
||
|
# Submits
|
||
11 years ago
|
res = form.submit().follow()
|
||
11 years ago
|
assert res.status_code == 200
|
||
11 years ago
|
|
||
11 years ago
|
def test_sees_alert_on_log_out(self, user, testapp):
|
||
|
res = testapp.get("/")
|
||
11 years ago
|
# Fills out login form in navbar
|
||
|
form = res.forms['loginForm']
|
||
11 years ago
|
form['username'] = user.username
|
||
|
form['password'] = 'myprecious'
|
||
11 years ago
|
# Submits
|
||
11 years ago
|
res = form.submit().follow()
|
||
11 years ago
|
res = testapp.get(url_for('public.logout')).follow()
|
||
11 years ago
|
# sees alert
|
||
11 years ago
|
assert 'You are logged out.' in res
|
||
11 years ago
|
|
||
11 years ago
|
def test_sees_error_message_if_password_is_incorrect(self, user, testapp):
|
||
11 years ago
|
# Goes to homepage
|
||
11 years ago
|
res = testapp.get("/")
|
||
11 years ago
|
# Fills out login form, password incorrect
|
||
|
form = res.forms['loginForm']
|
||
11 years ago
|
form['username'] = user.username
|
||
11 years ago
|
form['password'] = 'wrong'
|
||
|
# Submits
|
||
|
res = form.submit()
|
||
|
# sees error
|
||
11 years ago
|
assert "Invalid password" in res
|
||
11 years ago
|
|
||
11 years ago
|
def test_sees_error_message_if_username_doesnt_exist(self, user, testapp):
|
||
11 years ago
|
# Goes to homepage
|
||
11 years ago
|
res = testapp.get("/")
|
||
11 years ago
|
# Fills out login form, password incorrect
|
||
|
form = res.forms['loginForm']
|
||
|
form['username'] = 'unknown'
|
||
|
form['password'] = 'myprecious'
|
||
|
# Submits
|
||
|
res = form.submit()
|
||
|
# sees error
|
||
11 years ago
|
assert "Unknown user" in res
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
class TestRegistering:
|
||
11 years ago
|
|
||
11 years ago
|
def test_can_register(self, user, testapp):
|
||
|
old_count = len(User.query.all())
|
||
11 years ago
|
# Goes to homepage
|
||
11 years ago
|
res = testapp.get("/")
|
||
11 years ago
|
# Clicks Create Account button
|
||
|
res = res.click("Create account")
|
||
|
# Fills out the form
|
||
|
form = res.forms["registerForm"]
|
||
|
form['username'] = 'foobar'
|
||
|
form['email'] = 'foo@bar.com'
|
||
|
form['password'] = 'secret'
|
||
|
form['confirm'] = 'secret'
|
||
|
# Submits
|
||
11 years ago
|
res = form.submit().follow()
|
||
11 years ago
|
assert res.status_code == 200
|
||
11 years ago
|
# A new user was created
|
||
11 years ago
|
assert len(User.query.all()) == old_count + 1
|
||
11 years ago
|
|
||
11 years ago
|
def test_sees_error_message_if_passwords_dont_match(self, user, testapp):
|
||
11 years ago
|
# Goes to registration page
|
||
11 years ago
|
res = testapp.get(url_for("public.register"))
|
||
11 years ago
|
# Fills out form, but passwords don't match
|
||
|
form = res.forms["registerForm"]
|
||
|
form['username'] = 'foobar'
|
||
|
form['email'] = 'foo@bar.com'
|
||
|
form['password'] = 'secret'
|
||
|
form['confirm'] = 'secrets'
|
||
|
# Submits
|
||
|
res = form.submit()
|
||
|
# sees error message
|
||
11 years ago
|
assert "Passwords must match" in res
|
||
11 years ago
|
|
||
11 years ago
|
def test_sees_error_message_if_user_already_registered(self, user, testapp):
|
||
11 years ago
|
user = UserFactory(active=True) # A registered user
|
||
|
user.save()
|
||
|
# Goes to registration page
|
||
11 years ago
|
res = testapp.get(url_for("public.register"))
|
||
11 years ago
|
# Fills out form, but username is already registered
|
||
11 years ago
|
form = res.forms["registerForm"]
|
||
|
form['username'] = user.username
|
||
|
form['email'] = 'foo@bar.com'
|
||
|
form['password'] = 'secret'
|
||
|
form['confirm'] = 'secret'
|
||
|
# Submits
|
||
|
res = form.submit()
|
||
|
# sees error
|
||
11 years ago
|
assert "Username already registered" in res
|