Пример #1
0
        public ActionResult Innlogging(AdminInnlogging innlogging)
        {
            if (!ModelState.IsValid) {
                return View();
            }

            var epost = innlogging.Epost;
            var passord = innlogging.Passord;
            var hash = Crypto.LagHash(passord);
            var innloggetAdmin = _adminLogikk.Autentiser(epost, hash);

            if (innloggetAdmin == null) {
                ModelState.AddModelError("", "Feil epost/passord.");
                return View();
            }

            Session["AdminInnlogget"] = true;
            Session["AdminBruker"] = innloggetAdmin;

            /*var cookie = new HttpCookie("AdminBrukerID") {
                Expires = DateTime.Now.AddYears(1),
                Value = innloggetAdmin.ID.ToString()
            };

            Response.Cookies.Add(cookie);*/

            return RedirectToAction("Kontrollpanel");
        }
Пример #2
0
        public void Innlogging_POST_feil()
        {
            // Arrange
            var controller = new AdminController(new AdminLogikk(new AdminRepositoryStub()));
            var innloggingsmodell = new AdminInnlogging();
            controller.ModelState.AddModelError("Epost", "Epost mangler.");
            controller.ModelState.AddModelError("Passord", "Passord mangler.");

            // Act

            var result = (ViewResult)controller.Innlogging(innloggingsmodell);

            // Assert

            Assert.IsTrue(result.ViewData.ModelState.Count == 2);
            Assert.AreEqual(result.ViewName, "");
        }
Пример #3
0
        public void Innlogging_POST_ok()
        {
            // Arrange

            var controller = new AdminController(new AdminLogikk(new AdminRepositoryStub()));
            var builder = new TestControllerBuilder();
            builder.InitializeController(controller);
            var innloggingsmodell = new AdminInnlogging() {
                Epost = "*****@*****.**",
                Passord = "admin"
            };
            var hashPassord = Crypto.LagHash(innloggingsmodell.Passord);

            // Act

            var result = (RedirectToRouteResult)controller.Innlogging(innloggingsmodell);
            var innloggetAdminBruker = (AdminBruker)controller.Session["AdminBruker"];

            // Assert

            Assert.AreEqual(controller.Session["AdminInnlogget"], true);
            Assert.AreEqual(innloggetAdminBruker.Epost, innloggingsmodell.Epost);

            for (var i = 0; i < innloggetAdminBruker.Passord.Length; i++) {
                Assert.AreEqual(innloggetAdminBruker.Passord[i], hashPassord[i]);
            }

            Assert.AreEqual(result.RouteName, "");
            Assert.AreEqual(result.RouteValues.Values.First(), "Kontrollpanel");
        }