public HttpContextBase GetHttpContext() { var mock = new Mock <HttpContextBase>(); using (var db = DbTestBase.CreateNewCerebelloEntities()) { Principal principal; if (this.IsAuthenticated) { User user; string securityToken; user = SecurityManager.AuthenticateUser( this.UserNameOrEmail, this.Password, string.Format("{0}", this.RouteData.Values["practice"]), db.Users, out securityToken); user.LastActiveOn = DateTime.UtcNow; var ticket = new FormsAuthenticationTicket( version: 1, name: this.UserNameOrEmail, issueDate: DateTime.UtcNow, expiration: DateTime.UtcNow.AddYears(1), isPersistent: true, userData: securityToken, cookiePath: FormsAuthentication.FormsCookiePath); principal = new AuthenticatedPrincipal(new FormsIdentity(ticket), new UserData { FullName = this.FullName, Email = this.UserNameOrEmail, Id = this.UserDbId, IsUsingDefaultPassword = this.Password == Code.Constants.DEFAULT_PASSWORD, PracticeIdentifier = this.UserPracticeIdentifier, }); } else { principal = new AnonymousPrincipal(new GuestIdentity()); } mock.SetupGet(m => m.Request).Returns(GetRequest()); mock.SetupGet(m => m.Response).Returns(GetResponse()); mock.SetupGet(m => m.User).Returns(principal); mock.SetupGet(m => m.Server).Returns(new HttpServerUtilityBaseStub()); } return(mock.Object); }
public TController CreateController <TController>(bool callOnActionExecuting = true, Action <CerebelloEntities> setupNewDb = null, bool allowEmailSending = false) where TController : Controller { var mockController = new Mock <TController> { CallBase = true }; var controller = mockController.Object; var rootController = controller as RootController; if (rootController != null) { rootController.CerebelloEntitiesCreator = () => { var db = DbTestBase.CreateNewCerebelloEntities(); if (setupNewDb != null) { setupNewDb(db); } return(db); }; if (!allowEmailSending) { rootController.EmailSender = eml => { // Nothing to do, beacause we don't want to send any e-mail. } } ; } var privateObject = new PrivateObject(controller); privateObject.Invoke("Initialize", this.GetRequestContext()); if (callOnActionExecuting) { ((IActionFilter)controller).OnActionExecuting(this.CreateActionExecutingContext()); } return(controller); }
/// <summary> /// Sets up Miguel as the current logged user, in a valid sittuation. /// </summary> public void SetCurrentUser_Miguel_CorrectPassword(int?userId = null, string practice = "consultoriodrhouse") { // Setting user details. this.IsAuthenticated = true; this.FullName = "Júlio Cezar Almeida"; this.UserNameOrEmail = "*****@*****.**"; this.Password = "******"; this.UserPracticeIdentifier = practice; // Setting DB info. if (userId.HasValue) { this.UserDbId = userId.Value; } else { using (var db = DbTestBase.CreateNewCerebelloEntities()) this.UserDbId = db.Users.Single(u => u.UserName == "masbicudo").Id; } }
/// <summary> /// Sets up André as the current logged user, in a valid sittuation. /// </summary> public void SetCurrentUser_Andre_CorrectPassword(int?userId = null, string practice = "consultoriodrhouse") { // Setting user details. this.IsAuthenticated = true; this.FullName = "André Rodrigues Pena"; this.UserNameOrEmail = "*****@*****.**"; this.Password = "******"; this.UserPracticeIdentifier = practice; // Setting DB info. if (userId.HasValue) { this.UserDbId = userId.Value; } else { using (var db = DbTestBase.CreateNewCerebelloEntities()) this.UserDbId = db.Users.Single(u => u.UserName == "andrerpena").Id; } }