using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using BookShopOnline.Model;
using BookShopOnline.WebConfig;
using BookShopOnline.SQLServerDal;
using BookShopOnline.Bll;
using BookShopOnline.Utility;
using NUnit.Framework;

namespace BookShopOnline.TestClass
{
    [TestFixture] 
    public class TestUserBll
    {
        #region TestInsertUser
        [Test]//正常(注册用户）
        public void TestInsertUser()
        {
            User user = new User();
            user.RoleID =(int)Role.RegisterUser;
            user.UserName = "ruimingde";
            user.UserPwd = "2193118";
            user.RealName = "郑瑞明";
            user.PostalCode = "363000";
            user.Phone = "123456789,2323423423423";
            user.Email = "ruimingde@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号";

            int result = new UserBll().InsertUser(user);

            Assert.IsTrue(result == 1);
        }

        [Test]//用户名已存在
        public void TestInsertUser2()
        {
            User user = new User();
            user.RoleID = (int)Role.RegisterUser;
            user.UserName = "ruimingde";
            user.UserPwd = "12345";
            user.RealName = "郑瑞明";
            user.PostalCode = "363000";
            user.Phone = "123456789,2323423423423";
            user.Email = "ruimingde2@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号";

            int result = new UserBll().InsertUser(user);

            Assert.IsTrue(result == -2);
        }

        [Test]//Email已存在
        public void TestInsertUser3()
        {
            User user = new User();
            user.RoleID = (int)Role.RegisterUser;
            user.UserName = "ruimingde3";
            user.UserPwd = "12345";
            user.RealName = "郑瑞明";
            user.PostalCode = "363000";
            user.Phone = "123456789,2323423423423";
            user.Email = "ruimingde@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号";

            int result = new UserBll().InsertUser(user);

            Assert.IsTrue(result == -3);
        }

        [Test]//密码长度过短(注册用户）
        public void TestInsertUser4()
        {
            User user = new User();
            user.RoleID = (int)Role.RegisterUser;
            user.UserName = "ruimingde4";
            user.UserPwd = "12345";
            user.RealName = "郑瑞明";
            user.PostalCode = "363000";
            user.Phone = "123456789,2323423423423";
            user.Email = "ruimingde@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号";

            int result = new UserBll().InsertUser(user);

            Assert.IsTrue(result == -4);
        }

        [Test]//正常（管理员）
        public void TestInsertUser5()
        {
            User user = new User();
            user.RoleID = (int)Role.AdminUser;
            user.UserName = "admin";
            user.UserPwd = "admin";
            user.RealName = "郑瑞明";
            user.PostalCode = "363000";
            user.Phone = "123456789,2323423423423";
            user.Email = "admin@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号";

            int result = new UserBll().InsertUser(user);

            Assert.IsTrue(result == 1);
        }

        [Test]//密码长度过短(管理员用户）
        public void TestInsertUser6()
        {
            User user = new User();
            user.RoleID = (int)Role.AdminUser;
            user.UserName = "ruimingde6";
            user.UserPwd = "1234567";
            user.RealName = "郑瑞明";
            user.PostalCode = "363000";
            user.Phone = "123456789,2323423423423";
            user.Email = "ruimingde6@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号";

            int result = new UserBll().InsertUser(user);

            Assert.IsTrue(result == -4);
        }
        #endregion

        #region TestCheckUserName
        [Test]//存在
        public void TestCheckUserName()
        {
            bool isExist = new UserBll().CheckUserName("ruimingde");

            Assert.IsTrue(isExist);
        }

        [Test]//不存在
        public void TestCheckUserName2()
        {
            bool isExist = new UserBll().CheckUserName("ruimingde2");

            Assert.IsFalse(isExist);
        }

        [Test]//用户名后面含有空格
        public void TestCheckUserName3()
        {
            bool isExist = new UserBll().CheckUserName("ruimingde   ");

            Assert.IsTrue(isExist);
        }

        [Test]//空格
        public void TestCheckUserName4()
        {
            bool isExist = new UserBll().CheckUserName("");

            Assert.IsFalse(isExist);
        }

        [Test]//sql注入
        public void TestCheckUserName5()
        {
            bool isExist = new UserBll().CheckUserName("asjdkfsld'or 1=1 --");

            Assert.IsFalse(isExist);
        }

        [Test]//长度超过50
        public void TestCheckUserName6()
        {
            bool isExist = new UserBll().CheckUserName("啊的风景快速打开放声大哭法律手段进房间里适当放宽技术的卡斯蒂就份开始东方式的 适当放宽是的积分三度空间 sakdfjksdfkjsdkfsdkjfjskdfjksdkjfdsfdsfkjsdfjksdjfsdkfd");

            Assert.IsFalse(isExist);
        }
        #endregion

        #region TestCheckEmail
        [Test]//存在
        public void TestCheckEmail()
        {
            bool isExist = new UserBll().CheckEmail("ruimingde@126.com");

            Assert.IsTrue(isExist);
        }

        [Test]//不存在
        public void TestCheckEmail2()
        {
            bool isExist = new UserBll().CheckEmail("ruimingde2@126.com");

            Assert.IsFalse(isExist);
        }
        #endregion

        #region TestCheckUserLogin
        /*
        [Test]//正常
        public void TestCheckUserLogin()
        {
            bool result = new UserBll().CheckUserLogin("test", "123456",Role.RegisterUser);

            Assert.IsTrue(result);
        }

        [Test]//不区分用户名的大小写
        public void TestCheckUserLogin2()
        {
            bool result = new UserBll().CheckUserLogin("Test", "123456", Role.RegisterUser);

            Assert.IsTrue(result);
        }

        [Test]//区分密码的大小写（成功）
        public void TestCheckUserLogin3()
        {
            bool result = new UserBll().CheckUserLogin("test6", "test118", Role.RegisterUser);

            Assert.IsTrue(result);
        }

        [Test]//区分密码的大小写（失败）
        public void TestCheckUserLogin4()
        {
            bool result = new UserBll().CheckUserLogin("test6", "Test118", Role.RegisterUser);

            Assert.IsFalse(result);
        }

        [Test]//用户名的长度超过50
        public void TestCheckUserLogin5()
        {
            bool result = new UserBll().CheckUserLogin("sjdfksjdfksksjdfksjdfksjdfksjdfksjdfkjsdkfjsdkfjsdkfjskadjfslkdjfksdjfksdfjsdkfjsdjfskdjfskdfjsdkfjsdj", "Test118", Role.RegisterUser);

            Assert.IsFalse(result);
        }

        [Test]//密码长度超过50
        public void TestCheckUserLogin6()
        {
            bool result = new UserBll().CheckUserLogin("test6", "sjdfksjdfksksjdfksjdfksjdfksjdfksjdfkjsdkfjsdkfjsdkfjskadjfslkdjfksdjfksdfjsdkfjsdjfskdjfskdfjsdkfjsdj", Role.RegisterUser);

            Assert.IsFalse(result);
        }
         */
        #endregion

        #region TestUpdateUser
        //未修改Email
        [Test]
        public void TestUpdateUser()
        {
            User user=new User();
            user.UserID = 7;
            user.RealName = "郑瑞明2";
            user.PostalCode = "363002";
            user.Phone = "123456789,2323423423422";
            user.Email = "ruimingde@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号2";

            string oldEmail = "ruimingde@126.com";

            int returnValue = new UserBll().UpdateUser(user,oldEmail);

            Assert.IsTrue(returnValue > 0);
        }

        //修改了Email，且该Email是其他用户的
        [Test]
        public void TestUpdateUser2()
        {
            User user = new User();
            user.UserID = 7;
            user.RealName = "郑瑞明2";
            user.PostalCode = "363002";
            user.Phone = "123456789,2323423423422";
            user.Email = "ruimingde2@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号2";

            string oldEmail = "ruimingde@126.com";

            int returnValue = new UserBll().UpdateUser(user, oldEmail);

            Assert.IsTrue(returnValue==-1);
        }

        //修改了Email，且该Email不存在
        [Test]
        public void TestUpdateUser3()
        {
            User user = new User();
            user.UserID = 7;
            user.RealName = "郑瑞明2";
            user.PostalCode = "363002";
            user.Phone = "123456789,2323423423422";
            user.Email = "ruimingde118@126.com";
            user.Address = "福建省漳州市龙文区郭坑镇洛滨村洛滨205号2";

            string oldEmail = "ruimingde@126.com";

            int returnValue = new UserBll().UpdateUser(user, oldEmail);

            Assert.IsTrue(returnValue > 0);
        }
        #endregion
    }
}
