class XBasicsTestCase( TestCase ): def setUp( self ): self.app = CreateApp( "testing" ) self.app_context = self.app.app_context() self.app_context.push() db.create_all() def tearDown( self ): db.session.remove() db.drop_all() self.app_context.pop() def test_app_exists( self ): self.assertFalse( current_app is None ) def test_app_is_testing( self ): self.assertTrue( current_app.config[ "TESTING" ] )
# -*- coding: utf-8 -*- # @Time : 2018/4/24 下午2:21 # @author : Xmchx # @File : celery_worker.py from app import CreateApp from extensions import celery app = CreateApp() app.app_context().push()
from app import db, CreateApp from models import * app = CreateApp() with app.app_context(): db.create_all()
class XUserModelTestCase( TestCase ): def setUp( self ): self.app = CreateApp( "testing" ) self.app_context = self.app.app_context() self.app_context.push() db.create_all() XRole.InsertRoles() def tearDown( self ): db.session.remove() db.drop_all() self.app_context.pop() def test_password_setter( self ): u = XUser( password="******" ) self.assertTrue( u.password_hash is not None ) def test_no_password_getter( self ): u = XUser( password="******" ) with self.assertRaises( AttributeError ): u.password def test_password_verification( self ): u = XUser( password="******" ) self.assertTrue( u.VerifyPassword( "fox" ) ) self.assertFalse( u.VerifyPassword( "cat" ) ) def test_password_salts_are_random( self ): u1 = XUser( password="******" ) u2 = XUser( password="******" ) self.assertTrue( u1.password_hash != u2.password_hash ) def test_valid_confirmation_token( self ): u = XUser( password="******" ) db.session.add(u) db.session.commit() token = u.GenerateConfirmationToken() self.assertTrue( u.Confirm( token ) ) def test_invalid_reset_token( self ): u1 = XUser( password="******" ) u2 = XUser( password="******" ) db.session.add( u1 ) db.session.add( u2 ) token = u1.GenerateResetToken() self.assertFalse( u2.ResetPassword( token, "horse" ) ) self.assertTrue( u2.VerifyPassword( "dog" ) ) def test_valid_email_change_token( self ): u = XUser( email="*****@*****.**", password="******" ) db.session.add( u ) db.session.commit() token = u.GenerateEmailChangeToken( "*****@*****.**" ) self.assertTrue( u.ChangeEmail( token ) ) self.assertTrue( u.email == "*****@*****.**" ) def test_invalid_email_change_token( self ): u1 = XUser( email="*****@*****.**", password="******" ) u2 = XUser( email="*****@*****.**", password="******" ) db.session.add( u1 ) db.session.add( u2 ) db.session.commit() token = u1.GenerateEmailChangeToken( "*****@*****.**" ) self.assertFalse( u2.ChangeEmail( token ) ) self.assertTrue( u2.email == "*****@*****.**" ) def test_duplicate_email_change_token( self ): u1 = XUser( email="*****@*****.**", password="******" ) u2 = XUser( email="*****@*****.**", password="******" ) db.session.add( u1 ) db.session.add( u2 ) db.session.commit() token = u2.GenerateEmailChangeToken( "*****@*****.**" ) self.assertFalse( u2.ChangeEmail( token ) ) self.assertTrue( u2.email == "*****@*****.**" ) def test_roles_and_permissions( self ): u = XUser( email="*****@*****.**", password="******" ) self.assertTrue( u.Can( XPermission.WRITE_ARTICLES ) ) self.assertFalse( u.Can( XPermission.MODERATE_COMMENTS ) ) def test_anonymous_user( self ): u = XAnonymousUser() self.assertFalse( u.Can( XPermission.FOLLOW ) ) def test_timestamps( self ): u = XUser( password="******" ) db.session.add( u ) db.session.commit() self.assertTrue( ( datetime.utcnow() - u.member_since ).total_seconds() < 3 ) self.assertTrue( (datetime.utcnow() - u.last_seen).total_seconds() < 3 ) def test_ping( self ): u = XUser( password="******" ) db.session.add( u ) db.session.commit() last_seen_before = u.last_seen time.sleep( 2 ) u.Ping() self.assertTrue( u.last_seen > last_seen_before )