Пример #1
0
        public void CreateArticle_saves_a_article_via_context()
        {
            var mockArticle = new Mock<Article>();

            //Mock the properties of MailClient
            mockArticle.SetupProperty(article => article.Title, "TestTitle")
                .SetupProperty(article => article.ArticleContent, "TestContent")
                .SetupProperty(article => article.ArticleDate, new DateTime(2016, 09, 22))
                .SetupProperty(article => article.ArticleImage, "")
                .SetupProperty(article => article.ArticleTags, "TestTag")
                .SetupProperty(article => article.ArticleCategory, "TestCategory")
                .SetupProperty(article => article.ArticleMap, "")
                .SetupProperty(article => article.ArticleType, "TestType")
                .SetupProperty(article => article.ArticleImageAltText, "");

            var mockSet = new Mock<DbSet<Article>>();

            var mockContext = new Mock<DbConnectionContext>();
            mockContext.Setup(m => m.Articles).Returns(mockSet.Object);

            var service = new EF_ArticleRepository(mockContext.Object);
            service.CreateNewArticle(mockArticle.Object);

            mockSet.Verify(m => m.Add(It.IsAny<Article>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        public ActionResult Login(User model, string returnUrl)
        {
            // Lets first check if the Model is valid or not
            if (ModelState.IsValid)
            {
                using (var entities = new MySiteEntities())
                {
                    IArticleRepository ar = new EF_ArticleRepository(new DbConnectionContext());
                    var username = model.UserId;
                    var password = model.Password;

                    // Now if our password was enctypted or hashed we would have done the
                    // same operation on the user entered password here, But for now
                    // since the password is in plain text lets just authenticate directly

                    var userValid = entities.Users.Any(user => user.UserId == username && user.Password == password);

                    // User found in the database
                    if (userValid)
                    {
                        FormsAuthentication.SetAuthCookie(username, false);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        return RedirectToAction("Index", "Home");
                    }
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }