Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
260 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
from .fixtures import app1 as app | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def test_client(): | ||
client = app.test_client() | ||
ctx = app.app_context() | ||
ctx.push() | ||
yield client | ||
ctx.pop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from werkzeug.datastructures import MultiDict | ||
|
||
import pytest | ||
|
||
import baseframe.forms as forms | ||
|
||
# Fake password hasher, only suitable for re-use within a single process | ||
password_hash = hash | ||
|
||
|
||
class SimpleUser: | ||
# Fields: | ||
# fullname: Name of the user | ||
# company: Name of user's company | ||
# pw_hash: User's hashed password | ||
|
||
def _set_password(self, value): | ||
self.pw_hash = password_hash(value) | ||
|
||
password = property(fset=_set_password) | ||
|
||
def __init__(self, fullname, company, password): | ||
self.fullname = fullname | ||
self.company = company | ||
self.password = password | ||
|
||
def password_is(self, candidate): | ||
return self.pw_hash == password_hash(candidate) | ||
|
||
|
||
class GetSetForm(forms.Form): | ||
firstname = forms.StringField("First name") | ||
lastname = forms.StringField("Last name") | ||
company = forms.StringField("Company") # Test for NOT having get_/set_company | ||
password = forms.PasswordField("Password") | ||
confirm_password = forms.PasswordField("Confirm password") | ||
|
||
def get_firstname(self, obj): | ||
return obj.fullname.split(' ', 1)[0] | ||
|
||
def get_lastname(self, obj): | ||
parts = obj.fullname.split(' ', 1) | ||
if len(parts) > 1: | ||
return parts[-1] | ||
return '' | ||
|
||
def get_password(self, obj): | ||
return '' | ||
|
||
def get_confirm_password(self, obj): | ||
return '' | ||
|
||
def set_firstname(self, obj): | ||
pass | ||
|
||
def set_lastname(self, obj): | ||
obj.fullname = self.firstname.data + " " + self.lastname.data | ||
|
||
def set_password(self, obj): | ||
obj.password = self.password.data | ||
|
||
def set_confirm_password(self, obj): | ||
pass | ||
|
||
|
||
@pytest.fixture | ||
def user(): | ||
return SimpleUser(fullname="Test user", company="Test company", password="test") | ||
|
||
|
||
def test_no_obj(test_client): | ||
"""Test that the form can be initialized without an object.""" | ||
form = GetSetForm(meta={'csrf': False}) | ||
|
||
# Confirm form is bkank | ||
assert form.firstname.data is None | ||
assert form.lastname.data is None | ||
assert form.company.data is None | ||
assert form.password.data is None | ||
assert form.confirm_password.data is None | ||
|
||
|
||
def test_get(test_client, user): | ||
"""Test that the form loads values from the provided object.""" | ||
form = GetSetForm(obj=user, meta={'csrf': False}) | ||
|
||
# Confirm form loaded from user object | ||
assert form.firstname.data == "Test" | ||
assert form.lastname.data == "user" | ||
assert form.company.data == "Test company" | ||
assert form.password.data == '' | ||
assert form.confirm_password.data == '' | ||
|
||
|
||
def test_get_formdata(test_client, user): | ||
"""Test that the form preferentially loads from form data.""" | ||
form = GetSetForm( | ||
formdata=MultiDict( | ||
{ | ||
'firstname': "Ffirst", | ||
'lastname': "Flast", | ||
'company': "Form company", | ||
'password': "Test123", | ||
'confirm_password': "Mismatched", | ||
} | ||
), | ||
obj=user, | ||
meta={'csrf': False}, | ||
) | ||
|
||
# Confirm form loaded from formdata instead of user object | ||
assert form.firstname.data == "Ffirst" | ||
assert form.lastname.data == "Flast" | ||
assert form.company.data == "Form company" | ||
assert form.password.data == "Test123" | ||
assert form.confirm_password.data == "Mismatched" | ||
|
||
|
||
def test_set(test_client, user): | ||
"""Test that the form populates an object with or without set methods.""" | ||
form = GetSetForm( | ||
formdata=MultiDict( | ||
{ | ||
'firstname': "Ffirst", | ||
'lastname': "Flast", | ||
'company': "Form company", | ||
'password': "Test123", | ||
'confirm_password': "Mismatched", | ||
} | ||
), | ||
obj=user, | ||
meta={'csrf': False}, | ||
) | ||
|
||
# Check user object before and after populating | ||
assert user.fullname == "Test user" | ||
assert user.company == "Test company" | ||
assert user.password_is("test") | ||
|
||
form.populate_obj(user) | ||
|
||
assert user.fullname == "Ffirst Flast" | ||
assert user.company == "Form company" | ||
assert not user.password_is("test") | ||
assert user.password_is("Test123") | ||
assert not hasattr(user, 'confirm_password') |