Пример #1
0
        public static void MockUser(this TestApplication app)
        {
            var userRepo       = app.GetService <IRepository <User> >();
            var passwordHasher = app.GetService <IPasswordHasher <User> >();

            var user = new User
            {
                CreatedAtUtc   = DateTime.UtcNow.AddDays(-1),
                UserName       = "******",
                DisplayName    = "FancyUser",
                HashedPassword = passwordHasher.HashPassword(null, "111111")
            };

            userRepo.Save(user);

            var lastSigninTime = DateTime.UtcNow.AddMinutes(-30);
            var claims         = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(), ClaimValueTypes.Integer32),
                new Claim(ClaimTypes.Name, user.UserName, ClaimValueTypes.String),
                new Claim("SigninTime", lastSigninTime.Ticks.ToString(), ClaimValueTypes.Integer64)
            };
            var identity = new ClaimsIdentity(claims, IdentityConstants.ApplicationScheme);

            app.User = new ClaimsPrincipal(identity);
        }
Пример #2
0
        public static T CreateController <T>(this TestApplication app) where T : Controller
        {
            var httpContext = app.GetService <IHttpContextFactory>().Create(new DefaultHttpContext().Features);

            httpContext.User = app.User;

            var actionContext = new ActionContext(
                httpContext,
                new RouteData(),
                new ControllerActionDescriptor
            {
                ControllerTypeInfo = typeof(T).GetTypeInfo()
            });

            return(app.GetService <IControllerFactory>()
                   .CreateController(new ControllerContext(actionContext)) as T);
        }
Пример #3
0
        public static ModelStateDictionary ValidateModel(this TestApplication app, object model)
        {
            var validator     = app.GetService <IObjectModelValidator>();
            var actionContext = new ActionContext();

            validator.Validate(actionContext, null, string.Empty, model);

            return(actionContext.ModelState);
        }
        public static User CreateUser(this TestApplication app, string username, string password = null, string displayName = null)
        {
            var actualPassword = string.IsNullOrEmpty(password) ? Guid.NewGuid().ToString("N").Substring(4, 8) : password;
            var userManager    = app.GetService <UserManager <User> >();
            var user           = new User
            {
                UserName     = username,
                DisplayName  = string.IsNullOrEmpty(displayName) ? username : displayName,
                CreatedAtUtc = DateTime.UtcNow
            };

            var task = userManager.CreateAsync(user, actualPassword);

            task.ConfigureAwait(false);
            task.Wait();

            return(user);
        }
Пример #5
0
        public static User GetDiscussionUser(this TestApplication app)
        {
            var userRepo = app.GetService <IRepository <User> >();

            return(app.User.ToDiscussionUser(userRepo));
        }