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
1 parent
4f72e10
commit ec2a880
Showing
4 changed files
with
108 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"""Test forms.""" | ||
import pytest | ||
|
||
from sampleapp.blueprints.public.forms import LoginForm | ||
from sampleapp.blueprints.public.forms import RegisterForm | ||
|
||
|
||
@pytest.mark.usefixtures("db") | ||
class TestRegisterForm: | ||
"""Register form.""" | ||
|
||
def test_validate_email_already_registered(self, user): | ||
"""Enter email that is already registered.""" | ||
form = RegisterForm( | ||
email=user.email, password="example", confirm="example", agree_terms="1", | ||
) | ||
|
||
assert not form.validate() | ||
assert "Email already registered" in form.email.errors | ||
|
||
@pytest.mark.parametrize( | ||
"invalid_password", ["Example", "eXaMpLe", "EXAMPLE", "e", "example1", "foobar"] | ||
) | ||
def test_validate_password_do_not_match(self, user, invalid_password): | ||
"""Enter email that is already registered.""" | ||
form = RegisterForm( | ||
email=user.email, | ||
password="example", | ||
confirm=invalid_password, | ||
agree_terms="1", | ||
) | ||
|
||
assert not form.validate() | ||
assert "Passwords must match" in form.confirm.errors | ||
|
||
def test_validate_terms_not_agreed(self, user): | ||
"""Enter email that is already registered.""" | ||
form = RegisterForm(email=user.email, password="example", confirm="example") | ||
|
||
assert not form.validate() | ||
assert ( | ||
"You need to agree to Terms of Service to register" | ||
in form.agree_terms.errors | ||
) | ||
|
||
def test_validate_success(self, user): | ||
"""Register with success.""" | ||
form = RegisterForm( | ||
email="new@test.test", | ||
password="example", | ||
confirm="example", | ||
agree_terms="1", | ||
) | ||
assert form.validate() | ||
|
||
|
||
@pytest.mark.usefixtures("db") | ||
class TestLoginForm: | ||
"""Login form.""" | ||
|
||
def test_validate_success(self, user, default_password): | ||
"""Login successful.""" | ||
form = LoginForm(email=user.email, password=default_password) | ||
assert form.validate() | ||
assert form.user == user | ||
|
||
def test_validate_unknown_email(self, user, default_password): | ||
"""Unknown username.""" | ||
form = LoginForm(email="unknown@gmail.com", password=default_password) | ||
assert not form.validate() | ||
assert "Invalid email or password" in form.email.errors | ||
assert form.user is None | ||
|
||
def test_validate_invalid_password(self, user, default_password): | ||
"""Invalid password.""" | ||
form = LoginForm(email=user.email, password=default_password + "1") | ||
assert not form.validate() | ||
assert "Invalid email or password" in form.email.errors | ||
|
||
def test_validate_inactive_user(self, inactive_user, default_password): | ||
"""Inactive user.""" | ||
# Correct email and password, but user is not activated | ||
form = LoginForm(email=inactive_user.email, password=default_password) | ||
assert not form.validate() | ||
assert "User not activated" in form.email.errors |
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 |
---|---|---|
@@ -1,52 +1,54 @@ | ||
from flask import url_for | ||
|
||
|
||
def test_can_log_in_returns_200(testapp, user): | ||
def test_can_log_in_returns_200(testapp, user, default_password): | ||
"""Login successful.""" | ||
res = testapp.get(url_for("public.login")) | ||
form = res.form | ||
form["email"] = user.email | ||
form["password"] = "myprecious" | ||
form["password"] = default_password | ||
res = form.submit().follow() | ||
assert res.status_code == 200 | ||
|
||
|
||
def test_can_log_with_different_email_case_in_returns_200(db, testapp, user_factory): | ||
def test_can_log_with_different_email_case_in_returns_200( | ||
db, testapp, user_factory, default_password | ||
): | ||
user_factory(email="myemail@gmail.com") | ||
res = testapp.get(url_for("public.login")) | ||
form = res.form | ||
form["email"] = "MyEmail@Gmail.com" | ||
form["password"] = "myprecious" | ||
form["password"] = default_password | ||
res = form.submit().follow() | ||
assert res.status_code == 200 | ||
|
||
|
||
def test_sees_alert_on_log_out(testapp, user): | ||
def test_sees_alert_on_log_out(testapp, user, default_password): | ||
"""Show alert on logout.""" | ||
res = testapp.get(url_for("public.login")) | ||
form = res.form | ||
form["email"] = user.email | ||
form["password"] = "myprecious" | ||
form["password"] = default_password | ||
res = form.submit().follow() | ||
res = testapp.get(url_for("public.logout")).follow() | ||
assert "You are logged out." in res | ||
|
||
|
||
def test_sees_error_message_if_password_is_incorrect(testapp, user): | ||
def test_sees_error_message_if_password_is_incorrect(testapp, user, default_password): | ||
"""Show error if password is incorrect.""" | ||
res = testapp.get(url_for("public.login")) | ||
form = res.form | ||
form["email"] = user.email | ||
form["password"] = "wrong" | ||
form["password"] = default_password + "1" | ||
res = form.submit() | ||
assert "Invalid email or password" in res | ||
|
||
|
||
def test_sees_error_message_if_username_doesnt_exist(testapp, user): | ||
def test_sees_error_message_if_username_doesnt_exist(testapp, user, default_password): | ||
"""Show error if username doesn't exist.""" | ||
res = testapp.get(url_for("public.login")) | ||
form = res.form | ||
form["email"] = "unknown@gmail.com" | ||
form["password"] = "myprecious" | ||
form["password"] = default_password | ||
res = form.submit() | ||
assert "Invalid email or password" in res |