<s> import datetime <EOL> import unittest <EOL> from flask . ext . login import current_user <EOL> from project import db <EOL> from project . models import User <EOL> from project . util import BaseTestCase <EOL> from project . user . forms import RegisterForm , LoginForm , ChangePasswordForm <EOL> from project . token import generate_confirmation_token , confirm_token <EOL> class TestUserForms ( BaseTestCase ) : <EOL> def test_validate_success_register_form ( self ) : <EOL> form = RegisterForm ( <EOL> email = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , confirm = '<STR_LIT>' ) <EOL> self . assertTrue ( form . validate ( ) ) <EOL> def test_validate_invalid_password_format ( self ) : <EOL> form = RegisterForm ( <EOL> email = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , confirm = '<STR_LIT>' ) <EOL> self . assertFalse ( form . validate ( ) ) <EOL> def test_validate_email_already_registered ( self ) : <EOL> form = RegisterForm ( <EOL> email = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> confirm = '<STR_LIT>' <EOL> ) <EOL> self . assertFalse ( form . validate ( ) ) <EOL> def test_validate_success_login_form ( self ) : <EOL> form = LoginForm ( email = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> self . assertTrue ( form . validate ( ) ) <EOL> def test_validate_invalid_email_format ( self ) : <EOL> form = LoginForm ( email = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> self . assertFalse ( form . validate ( ) ) <EOL> def test_validate_success_change_password_form ( self ) : <EOL> form = ChangePasswordForm ( password = '<STR_LIT>' , confirm = '<STR_LIT>' ) <EOL> self . assertTrue ( form . validate ( ) ) <EOL> def test_validate_invalid_change_password ( self ) : <EOL> form = ChangePasswordForm ( password = '<STR_LIT>' , confirm = '<STR_LIT>' ) <EOL> self . assertFalse ( form . validate ( ) ) <EOL> def test_validate_invalid_change_password_format ( self ) : <EOL> form = ChangePasswordForm ( password = '<STR_LIT>' , confirm = '<STR_LIT>' ) <EOL> self . assertFalse ( form . validate ( ) ) <EOL> class TestUserViews ( BaseTestCase ) : <EOL> def test_correct_login ( self ) : <EOL> with self . client : <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> data = dict ( email = "<STR_LIT>" , password = "<STR_LIT>" ) , <EOL> follow_redirects = True <EOL> ) <EOL> self . assertTrue ( response . status_code == <NUM_LIT:200> ) <EOL> self . assertTrue ( current_user . email == "<STR_LIT>" ) <EOL> self . assertTrue ( current_user . is_active ( ) ) <EOL> self . assertTrue ( current_user . is_authenticated ( ) ) <EOL> self . assertTemplateUsed ( '<STR_LIT>' ) <EOL> def test_incorrect_login ( self ) : <EOL> with self . client : <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> data = dict ( email = "<STR_LIT>" , password = "<STR_LIT>" ) , <EOL> follow_redirects = True <EOL> ) <EOL> self . assertTrue ( response . status_code == <NUM_LIT:200> ) <EOL> self . assertIn ( b'<STR_LIT>' , response . data ) <EOL> self . assertFalse ( current_user . is_active ( ) ) <EOL> self . assertFalse ( current_user . is_authenticated ( ) ) <EOL> self . assertTemplateUsed ( '<STR_LIT>' ) <EOL> def test_profile_route_requires_login ( self ) : <EOL> self . client . get ( '<STR_LIT>' , follow_redirects = True ) <EOL> self . assertTemplateUsed ( '<STR_LIT>' ) <EOL> def test_confirm_token_route_requires_login ( self ) : <EOL> self . client . get ( '<STR_LIT>' , follow_redirects = True ) <EOL> self . assertTemplateUsed ( '<STR_LIT>' ) <EOL> def test_confirm_token_route_valid_token ( self ) : <EOL> with self . client : <EOL> self . client . post ( '<STR_LIT>' , data = dict ( <EOL> email = '<STR_LIT>' , password = '<STR_LIT>' <EOL> ) , follow_redirects = True ) <EOL> token = generate_confirmation_token ( '<STR_LIT>' ) <EOL> response = self . client . get ( '<STR_LIT>' + token , follow_redirects = True ) <EOL> self . assertIn ( b'<STR_LIT>' , response . data ) <EOL> self . assertTemplateUsed ( '<STR_LIT>' ) <EOL> user = User . query . filter_by ( email = '<STR_LIT>' ) . first_or_404 ( ) <EOL> self . assertIsInstance ( user . confirmed_on , datetime . datetime ) <EOL> self . assertTrue ( user . confirmed ) <EOL> def test_confirm_token_route_invalid_token ( self ) : <EOL> token = generate_confirmation_token ( '<STR_LIT>' ) <EOL> with self . client : <EOL> self . client . post ( '<STR_LIT>' , data = dict ( <EOL> email = '<STR_LIT>' , password = '<STR_LIT>' <EOL> ) , follow_redirects = True ) <EOL> response = self . client . get ( '<STR_LIT>' + token , follow_redirects = True ) <EOL> self . assertIn ( <EOL> b'<STR_LIT>' , <EOL> response . data <EOL> ) <EOL> def test_confirm_token_route_expired_token ( self ) : <EOL> user = User ( email = '<STR_LIT>' , password = '<STR_LIT>' , confirmed = False ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> token = generate_confirmation_token ( '<STR_LIT>' ) <EOL> self . assertFalse ( confirm_token ( token , - <NUM_LIT:1> ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
