示例#1
0
        public void CreateUserActivityLog_WithValidData_ShouldAddLogToDatabasewithCorrectValues()
        {
            // Arrange
            const string userEmail      = "*****@*****.**";
            const string controllerName = "SomeController";
            const string actionName     = "SomeAction";
            const string httpMethod     = "GET";

            var model = new UserActivityLogCreateModel
            {
                DateTime       = new DateTime(2000, 12, 12),
                UserEmail      = userEmail,
                ActionName     = actionName,
                ControllerName = controllerName,
                HttpMethod     = httpMethod
            };

            // Act
            this.logService.CreateUserActivityLog(model);
            var dbLog = this.dbContext
                        .UserActivityLogs
                        .FirstOrDefault();

            // Assert
            this.dbContext.UserActivityLogs
            .Should()
            .HaveCount(1);

            dbLog
            .Should()
            .BeAssignableTo <UserActivityLog>();

            dbLog
            .Should()
            .Match(x => x.As <UserActivityLog>()
                   .UserEmail == userEmail);

            dbLog
            .Should()
            .Match(x => x.As <UserActivityLog>()
                   .ControllerName == controllerName);

            dbLog
            .Should()
            .Match(x => x.As <UserActivityLog>()
                   .ActionName == actionName);

            dbLog
            .Should()
            .Match(x => x.As <UserActivityLog>()
                   .HttpMethod == httpMethod);
        }
示例#2
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var httpContext = context.HttpContext;

            var userManager      = httpContext.RequestServices.GetService(typeof(UserManager <User>)) as UserManager <User>;
            var dateTimeProvider = httpContext.RequestServices.GetService(typeof(IDateTimeProvider)) as IDateTimeProvider;
            var logService       = httpContext.RequestServices.GetService(typeof(ILogService)) as ILogService;

            var    userClaims = httpContext.User;
            string userEmail  = string.Empty;

            Task.Run(async() =>
            {
                var user  = await userManager.GetUserAsync(userClaims);
                userEmail = await userManager.GetEmailAsync(user);
            })
            .GetAwaiter()
            .GetResult();

            var url         = httpContext.Request.Path;
            var queryString = httpContext.Request.QueryString.ToString();
            var httpMethod  = httpContext.Request.Method;

            var routeValues = context.ActionDescriptor.RouteValues;

            routeValues.TryGetValue("action", out var actionName);
            routeValues.TryGetValue("controller", out var controllerName);
            routeValues.TryGetValue("area", out var areaName);

            var actionArguments = string
                                  .Join(Environment.NewLine, context
                                        .ActionArguments
                                        .Select(arg => $"{arg.Key}: {JsonConvert.SerializeObject(arg.Value)}"));

            var logModel = new UserActivityLogCreateModel
            {
                DateTime        = dateTimeProvider.GetCurrentDateTime(),
                UserEmail       = userEmail,
                HttpMethod      = httpMethod,
                ControllerName  = controllerName,
                ActionName      = actionName,
                AreaName        = areaName,
                Url             = url,
                QueryString     = queryString,
                ActionArguments = actionArguments
            };

            logService.CreateUserActivityLog(logModel);
        }
        public bool CreateUserActivityLog(UserActivityLogCreateModel model)
        {
            var log = this.mapper.Map <UserActivityLog>(model);

            try
            {
                this.ValidateEntityState(log);
                this.db.Logs.Add(log);
                this.db.SaveChanges();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#4
0
        public void CreateUserActivityLog_WithValidData_ShouldAddLogToDatabasewithCorrectValues()
        {
            // Arrange
            var model = new UserActivityLogCreateModel
            {
                DateTime       = new DateTime(2010, 10, 10),
                UserEmail      = UserEmail,
                ActionName     = ActionName,
                ControllerName = ControllerName,
                HttpMethod     = HttpMethod
            };

            // Act
            this.logService.CreateUserActivityLog(model);

            var logs = this.dbContext
                       .Logs
                       .FirstOrDefault();

            // Assert
            this.dbContext
            .Logs
            .Should()
            .HaveCount(1);

            logs
            .Should()
            .BeAssignableTo <UserActivityLog>();

            logs
            .Should()
            .Match(x => x.As <UserActivityLog>().UserEmail == UserEmail);

            logs
            .Should()
            .Match(x => x.As <UserActivityLog>().ControllerName == ControllerName);

            logs
            .Should()
            .Match(x => x.As <UserActivityLog>().ActionName == ActionName);

            logs
            .Should()
            .Match(x => x.As <UserActivityLog>().HttpMethod == HttpMethod);
        }
示例#5
0
        public void CreateUserActivityLog_WithMissingUserEmail_ShoulNotInsertEntityToDatabase()
        {
            // Arrange
            const string controllerName = "SomeController";
            const string actionName     = "SomeAction";
            const string httpMethod     = "GET";

            var model = new UserActivityLogCreateModel
            {
                DateTime       = new DateTime(2000, 1, 1),
                ActionName     = actionName,
                ControllerName = controllerName,
                HttpMethod     = httpMethod
            };

            // Act
            bool result = logService.CreateUserActivityLog(model);

            // Assert
            result.Should().BeFalse();
            this.dbContext.UserActivityLogs.Should().BeEmpty();
        }
示例#6
0
        public void CreateUserActivityLog_WithMissingHttpMethod_ShoulNotInsertEntityToDatabase()
        {
            // Arrange
            const string userEmail      = "*****@*****.**";
            const string controllerName = "SomeController";
            const string actionName     = "SomeAction";

            var model = new UserActivityLogCreateModel
            {
                DateTime       = new DateTime(2000, 1, 1),
                UserEmail      = userEmail,
                ActionName     = actionName,
                ControllerName = controllerName
            };

            // Act
            var result = this.logService.CreateUserActivityLog(model);

            // Assert
            result.Should().BeFalse();
            this.dbContext.UserActivityLogs.Should().BeEmpty();
        }
示例#7
0
        public void CreateUserActivityLog_WithMissingUserEmail_ShouldNotInsertEntityToDatabase()
        {
            // Arrange
            var model = new UserActivityLogCreateModel
            {
                DateTime       = new DateTime(2000, 1, 1),
                ActionName     = ActionName,
                ControllerName = ControllerName,
                HttpMethod     = HttpMethod
            };

            // Act
            var result = this.logService.CreateUserActivityLog(model);

            // Assert
            result
            .Should()
            .BeFalse();

            this.dbContext
            .Logs
            .Should()
            .BeEmpty();
        }