Пример #1
0
        public void Add_OneItemAdded_ToDbSet__Success()
        {
            User aUser = new User()
            {
                Name  = "The User",
                Email = "*****@*****.**"
            };
            List <User> users = Builder <User> .CreateListOfSize(5).Build().ToList();

            var mockSet = new Mock <DbSet <User> >();
            var data    = users.AsQueryable();

            mockSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            mockSet.Setup(x => x.Add(It.IsAny <User>())).Callback((User userToAdd) => users.Add(userToAdd)); //added a mock callback for add

            var dbContextMock = new Mock <IUmsDbContext>();

            dbContextMock.Setup(x => x.Users).Returns(mockSet.Object);

            var logic = new UserLogic(dbContextMock.Object);

            logic.Add(aUser); //should add another to the dbset
            Assert.AreEqual(6, dbContextMock.Object.Users.Count());
        }
Пример #2
0
        public ActionResult Register(UserCreateModel parameters)
        {
            var addREsult = _userLogic.Add(parameters);

            if (addREsult.IsSuccess)
            {
                var opResult = _userLogic.ValidateUser(parameters.UserName, parameters.Password);
                if (!opResult.UserClaims.Any(s => s.ClaimValue.ToLower() == "user"))
                {
                    return(View());
                }

                var claims = new[]
                {
                    new Claim(ClaimTypes.Name, opResult.UserName),
                    new Claim("userId", opResult.Id.ToString()),
                    new Claim(ClaimTypes.Role, opResult.UserClaims.FirstOrDefault().ClaimValue)
                };
                var identity = new ClaimsIdentity(claims, "ApplicationCookie");

                var authManager = Request.GetOwinContext().Authentication;
                authManager.SignOut();

                authManager.SignIn(new AuthenticationProperties
                {
                    IsPersistent = false
                }, identity);

                return(RedirectToAction("Index", "home"));
            }
            return(View());
        }
Пример #3
0
        public void Add()
        {
            User aUser = new User()
            {
                Name  = "The User",
                Email = "*****@*****.**"
            };
            IList <User> users = Builder <User> .CreateListOfSize(5).Build().ToList();

            var data    = users.AsQueryable();
            var mockSet = new Mock <DbSet <User> >();

            mockSet.Setup(x => x.Add(It.IsAny <User>())).Callback((User userToAdd) => users.Add(userToAdd)); //changed the position
            mockSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            var dbContextMock = new Mock <IUmsDbContext>();

            dbContextMock.Setup(x => x.Users).Returns(mockSet.Object);

            var logic = new UserLogic(dbContextMock.Object);

            logic.Add(aUser);

            var addedUser = dbContextMock.Object.Users.Last();

            Assert.AreEqual(aUser.Name, addedUser.Name);
            Assert.AreEqual(aUser.Email, addedUser.Email);
        }
        public void Add_Try_With_Extention()
        {
            var aUser = new User
            {
                Id    = 100,
                Name  = "The User",
                Email = "Email"
            };
            List <User> users = Builder <User> .CreateListOfSize(5).Build().ToList();

            var dbContextMock = new MockDbContext <IUmsDbContext>();

            dbContextMock.SetupDbSet(x => x.Users)
            .SetUp(x => x.Add(It.IsAny <User>()))
            .Callback((User lol) => users.Add(lol))
            .Returns((User lol) => lol)
            .Mock()
            .WithData(users);

            var logic = new UserLogic(dbContextMock.Object);

            logic.Add(aUser);

            Assert.AreEqual(6, dbContextMock.Object.Users.Select(x => x).ToList().Count);
        }
        public void Add_Fields()
        {
            var aUser = new User
            {
                Id    = 100,
                Name  = "The User",
                Email = "Email"
            };
            List <User> users = Builder <User> .CreateListOfSize(5).Build().ToList();

            var dbContextMock = new MockDbContext <IUmsDbContext>();

            dbContextMock.SetupDbSet(x => x.Users)
            .SetUp(x => x.Add(It.IsAny <User>()))
            .Callback((User lol) => users.Add(lol))
            .Returns((User lol) => lol)
            .Mock()
            .WithData(users);

            var logic = new UserLogic(dbContextMock.Object);

            logic.Add(aUser);

            User addedUser = dbContextMock.Object.Users.Last();

            Assert.AreEqual(aUser.Name, addedUser.Name);
            Assert.AreEqual(aUser.Email, addedUser.Email);
        }
Пример #6
0
        public void Add_AddedItem_HasAll_RequeredFields_Failed()
        {
            User aUser = new User()
            {
                Name  = "The User",
                Email = "*****@*****.**"
            };
            List <User> users = Builder <User> .CreateListOfSize(5).Build().ToList();

            var mockSet = new Mock <DbSet <User> >();
            var data    = users.AsQueryable();

            mockSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            var dbContextMock = new Mock <IUmsDbContext>();

            dbContextMock.Setup(x => x.Users).Returns(mockSet.Object);

            var logic = new UserLogic(dbContextMock.Object);

            logic.Add(aUser);

            var addedUser = dbContextMock.Object.Users.Last();

            Assert.AreEqual(aUser.Name, addedUser.Name);
            Assert.AreEqual(aUser.Email, addedUser.Email);
        }
Пример #7
0
        public void Consume()
        {
            _channel.ExchangeDeclare(RabbitMqExchange.AuthenticationExchange, ExchangeType.Direct);
            _channel.QueueDeclare(RabbitMqQueues.AddUserQueue, true, false, false, null);
            _channel.QueueBind(RabbitMqQueues.AddUserQueue, RabbitMqExchange.AuthenticationExchange, RabbitMqRouting.AddUser);
            _channel.BasicQos(0, 10, false);

            var consumer = new EventingBasicConsumer(_channel);

            consumer.Received += async(sender, e) =>
            {
                try
                {
                    byte[] body = e.Body.ToArray();
                    string json = Encoding.UTF8.GetString(body);
                    var    user = Newtonsoft.Json.JsonConvert.DeserializeObject <UserRabbitMqSensitiveInformation>(json);

                    await _userLogic.Add(user);
                }
                catch (Exception exception)
                {
                    _logLogic.Log(exception);
                }
            };

            _channel.BasicConsume(RabbitMqQueues.AddUserQueue, true, consumer);
        }
Пример #8
0
        public ActionResult Regist(User user)
        {
            IMessageEntity msg    = new MessageEntity();
            string         result = "";

            string code = Request["code"];

            #region 对user赋值
            user.Upwd  = Md5Helper.GetMd5(user.Upwd, 16);
            user.Id    = CommonUtil.NewShortGuid();
            user.Cdate = DateTime.Now;
            #endregion

            if (Session["code"].ToString() == code)
            {
                if (!string.IsNullOrEmpty(user.Uname) && !string.IsNullOrEmpty(user.Upwd))
                {
                    msg = userLogic.Add <User>("user", user);
                    if (msg.Msgflag)
                    {
                        result = "注册成功";
                    }
                    else
                    {
                        result = "注册失败";
                    }
                }
                else
                {
                    result = "请输入用户名和密码";
                }
            }
            else
            {
                result = "验证码输入错误";
            }


            if (msg.Msgflag)
            {
                return(RedirectToAction("Login", "User"));
            }
            else
            {
                SetRegistSkin();
                ViewData["result"] = result;
                return(View());
            }


            //return Content(result);//往前台输出字符串
        }
Пример #9
0
        public void Add_Failed()
        {
            User aUser = new User()
            {
                Id    = 100,
                Name  = "The User",
                Email = "*****@*****.**"
            };
            List <User> users = Builder <User> .CreateListOfSize(5).Build().ToList();

            users.Add(aUser);

            var dbContextMock = new Mock <IUmsDbContext>();
            //dbContextMock.Setup(x => x.Users).Returns(users);   //how to convert users to IDbSet ??
            var logic = new UserLogic(dbContextMock.Object);

            logic.Add(aUser);
        }
Пример #10
0
        public static void Add()
        {
            Console.WriteLine("Enter name");
            var name = Console.ReadLine();

            Console.WriteLine("Enter date of birth (DD.MM.YYYY)");
            var dateOfBirth = DateTime.Parse(Console.ReadLine());

            var age = (new DateTime(1, 1, 1) + DateTime.Today.Subtract(dateOfBirth)).Year - 1;

            var currentStudent = new User()
            {
                Name        = name,
                DateOfBirth = dateOfBirth,
                Age         = age
            };

            userLogic.Add(currentStudent);
        }
Пример #11
0
        public void Add_WithHelper()
        {
            User aUser = new User()
            {
                Name  = "The User",
                Email = "*****@*****.**"
            };
            IList <User> users = Builder <User> .CreateListOfSize(5).Build().ToList();

            var dbContextMock = MockedDbContext(users);
            var logic         = new UserLogic(dbContextMock.Object);

            logic.Add(aUser);

            var addedUser = dbContextMock.Object.Users.Last();

            Assert.AreEqual(aUser.Name, addedUser.Name);
            Assert.AreEqual(aUser.Email, addedUser.Email);
        }
Пример #12
0
        public ResponseDTO <int> AddUser(RequestDTO <UserRequestDTO> request)
        {
            var response = new ResponseDTO <int>();

            try
            {
                response = userLogic.Add(request.Signature);
            }
            catch (System.Data.SqlClient.SqlException exception)
            {
                response.Success = false;
                response.Message = exception.Message;
                ExceptionHandler.Instance.WriteExceptionLog(exception);
            }
            catch (System.Exception exception)
            {
                response.Success = false;
                response.Message = exception.Message;
                ExceptionHandler.Instance.WriteExceptionLog(exception);
            }
            return(response);
        }
Пример #13
0
        public ActionResult <User> Post([FromBody] User newUser)
        {
            var user = _userLogic.Add(newUser.Code, newUser.FirstName, newUser.LastName);

            return(CreatedAtAction("Get", new { code = newUser.Id }, user));
        }
Пример #14
0
 public void AddUserOk()
 {
     userLogic.Add(user);
 }
Пример #15
0
 public void AddUnprocessableExceptionTest()
 {
     Assert.ThrowsAsync <UnprocessableException>(() => _userLogic.Add(new UserRabbitMqSensitiveInformation()));
 }
Пример #16
0
 private void button1_Click(object sender, EventArgs e)
 {
     userLogic.Add(new User <int>(textBox1.Text, dateTimePicker1.Value, int.Parse(textBox2.Text)));
     Close();
 }
Пример #17
0
 public ActionResult PostUser(
     [FromBody] UserPoco[] appBookPocos)
 {
     _logic.Add(appBookPocos);
     return(Ok());
 }