public void Upsert()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(Upsert))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var roleService = new UserRoleService(context, config);
                var toAdd       = new UserRolePostDto
                {
                    Name = "GOD"
                };

                var toUpdate = new UserRolePostDto
                {
                    Name = "Sclave Master"
                };

                UserRole role = roleService.Create(toAdd);
                context.Entry(role).State = EntityState.Detached;
                UserRole updated = roleService.Upsert(role.Id, toUpdate);

                Assert.AreNotEqual(role.Name, updated.Name);
            }
        }
        public void UpsertShouldModifyTheGivenUserRole()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyTheGivenUserRole))
                          .EnableSensitiveDataLogging()
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var userRoleService = new UserRoleService(context);
                var toAdd           = new UserRolePostDTO
                {
                    Name        = "Newcomer",
                    Description = "A new guy..."
                };

                var added = userRoleService.Create(toAdd);

                //context.Entry(added).State = EntityState.Detached;

                var update = new UserRolePostDTO
                {
                    Name = "Rouge"
                };

                //var updateResult = userRoleService.Upsert(added.Id, update);

                Assert.NotNull(added);
                Assert.AreEqual(toAdd.Name, toAdd.Name);
                Assert.AreNotEqual(update.Name, toAdd.Name);
            }
        }
        public void GetAll()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetAll))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var roleService = new UserRoleService(context, config);
                var first       = new UserRolePostDto
                {
                    Name = "God"
                };
                var second = new UserRolePostDto
                {
                    Name = "Slave Master"
                };
                var third = new UserRolePostDto
                {
                    Name = "Matroana"
                };
                var fourth = new UserRolePostDto
                {
                    Name = "Shobolan"
                };
                var fifth = new UserRolePostDto
                {
                    Name = "Tod Howard"
                };
                roleService.Create(first);
                roleService.Create(second);
                roleService.Create(third);
                roleService.Create(fourth);
                roleService.Create(fifth);
                context.SaveChanges();

                PaginatedList <UserRoleGetDto> populated = roleService.GetAll(1);
                PaginatedList <UserRoleGetDto> empty     = roleService.GetAll(2);

                Assert.AreEqual(5, populated.Entries.Count);
                Assert.Zero(empty.Entries.Count);
            }
        }
Exemple #4
0
        public override void Create(FormCollection formInfo)
        {
            //TODO: 提交前判断用户名是否重复
            T_User user = userService.GetUserInfo(formInfo);

            userService.Create(user);
            T_UserRole userRole = userService.GetNewUserRoleInfo(formInfo);

            urService.Create(userRole);
            T_UserCache userCache = userService.GetNewUserNodeInfo(formInfo);

            unService.Create(userCache);
        }
Exemple #5
0
        public void UserRoleService_CheckInsert_ThrowNameException()
        {
            // Arrange
            var mock = new Mock <IUserRoleRepository>();

            mock.Setup(repo => repo.Create(new UserRoleEntity()))
            .Returns(() => Task.CompletedTask);

            var service = new UserRoleService(mock.Object);

            // Act
            var ex = Assert.ThrowsAnyAsync <NameException>(() => service.Create(new UserRole()));

            // Assert
            Assert.Equal("The UserRole have not empty or null name.", ex.Result.Message);
        }
Exemple #6
0
        public void UserRoleService_CheckInsert_Created()
        {
            // Arrange
            var mock = new Mock <IUserRoleRepository>();

            mock.Setup(repo => repo.Create(StubsObjects.UserRole.ToEntity()))
            .Returns(() => Task.CompletedTask);

            var service = new UserRoleService(mock.Object);

            // Act
            var result = service.Create(StubsObjects.UserRole).Result;

            // Assert
            Assert.Equal(StatusCode.Created, result);
        }
        public async void Create()
        {
            var mock  = new ServiceMockFacade <IUserRoleRepository>();
            var model = new ApiUserRoleRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <UserRole>())).Returns(Task.FromResult(new UserRole()));
            var service = new UserRoleService(mock.LoggerMock.Object,
                                              mock.RepositoryMock.Object,
                                              mock.ModelValidatorMockFactory.UserRoleModelValidatorMock.Object,
                                              mock.BOLMapperMockFactory.BOLUserRoleMapperMock,
                                              mock.DALMapperMockFactory.DALUserRoleMapperMock);

            CreateResponse <ApiUserRoleResponseModel> response = await service.Create(model);

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.UserRoleModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiUserRoleRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <UserRole>()));
        }
        public void Delete()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(Delete))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var roleService = new UserRoleService(context, config);
                var toAdd       = new UserRolePostDto
                {
                    Name = "GOD"
                };
                UserRole role = roleService.Create(toAdd);
                roleService.Delete(role.Id);

                Assert.IsNull(context.UserRoles.Find(role.Id));
            }
        }
Exemple #9
0
        public void CreateShouldAddAndReturnTheRole()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldAddAndReturnTheRole))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userRoleService = new UserRoleService(context);
                var addUserRole     = userRoleService.Create(new ExamenNet.ViewModels.UserRolePostModel
                {
                    Name        = "Rol testare",
                    Description = "Creat pentru testare"
                });

                var userRole = context.UserRoles.Find(1);
                Assert.AreEqual(addUserRole.Name, userRole.Name);
            }
        }
Exemple #10
0
        public void GetAllShouldReturnUserRoles()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetAllShouldReturnUserRoles))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userRoleService = new UserRoleService(context);
                var addUserRole     = userRoleService.Create(new ExamenNet.ViewModels.UserRolePostModel
                {
                    Name        = "Rol testare",
                    Description = "Creat pentru testare"
                });

                var allUsers = userRoleService.GetAll();
                Assert.IsNotNull(allUsers);
            }
        }
        public void GetById()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetById))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var roleService = new UserRoleService(context, config);
                var toAdd       = new UserRolePostDto
                {
                    Name = "GOD"
                };
                UserRole expected = roleService.Create(toAdd);
                UserRole actual   = roleService.GetById(expected.Id);

                Assert.AreEqual(expected, actual);
            }
        }
        public void GetByIdShouldReturnUserRole()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnUserRole))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var userRoleService = new UserRoleService(context);
                var addUserRole     = userRoleService.Create(new TaskAgendaProj.ViewModels.UserRolePostModel
                {
                    Name        = "Rol testare",
                    Description = "Creat pentru testare"
                });

                var userRole = userRoleService.GetById(1);
                Assert.AreEqual("Rol testare", userRole.Name);
            }
        }
        public void Create()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(Create))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var roleService = new UserRoleService(context, config);

                var toAdd = new UserRolePostDto
                {
                    Name = "GOD"
                };

                UserRole role = roleService.Create(toAdd);

                Assert.AreEqual(toAdd.Name, role.Name);
                Assert.IsNotNull(roleService.GetById(role.Id));
            }
        }
Exemple #14
0
        public void ValidUpsert1()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidUpsert1))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                UserRoleService userRoleService = new UserRoleService(context);
                var             added           = new Lab2Expense.ViewModels.UserRolePostModel
                {
                    Name        = "Regular",
                    Description = "jskds"
                };

                var addedForUpdate = new Lab2Expense.ViewModels.UserRolePostModel
                {
                    Name        = "Admin",
                    Description = "smands"
                };
                var addedForUpdate2 = new Lab2Expense.Models.UserRole
                {
                    Name        = "UserManager",
                    Description = "smands"
                };

                var result = userRoleService.Create(added);
                context.Entry(result).State = EntityState.Detached;
                added.Name = "Admin";

                var resultUpsert = userRoleService.Upsert(result.Id, added);
                var resultNull   = userRoleService.Upsert(8, added);
                Assert.AreEqual("Admin", resultNull.Name);

                Assert.AreEqual("Admin", resultUpsert.Name);
            }
        }
        public void GetByIdShouldReturnUserRoleWithCorrectId()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnUserRoleWithCorrectId))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var userRoleService = new UserRoleService(context);
                var addUserRole     = new UserRolePostDTO()
                {
                    Name        = "Newcomer",
                    Description = "A new guy..."
                };


                var current  = userRoleService.Create(addUserRole);
                var expected = userRoleService.GetById(current.Id);

                Assert.IsNotNull(expected);
                Assert.AreEqual(expected.Name, current.Name);
                Assert.AreEqual(expected.Id, current.Id);
            }
        }
Exemple #16
0
        public void ValidGetCurentRole()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidGetCurentRole))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, config);
                var added        = new Lab2Expense.ViewModels.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "fdsfsdfs",
                    LastName  = "fdsfs",
                    Password  = "******",
                    Username  = "******"
                };

                var userRoleService = new UserRoleService(context);
                var addedRole       = new Lab2Expense.ViewModels.UserRolePostModel
                {
                    Name        = "Regular",
                    Description = "jskds"
                };
                var result = userRoleService.Create(addedRole);

                var resultAdded          = usersService.Register(added);
                var resultAuthentificate = usersService.Authenticate(added.Username, added.Password);

                var user = context.Users.FirstOrDefault(u => u.Id == resultAuthentificate.Id);

                var userRole = usersService.GetCurrentUserRole(user);
                Assert.AreEqual("Regular", userRole.Name);
            }
        }
Exemple #17
0
        public JsonResult CreateUserRole([FromUri] int id, [FromBody] TPermRole[] roles)
        {
            var result = userRoleService.Create(id, roles);

            return(new JsonResult(result));
        }
Exemple #18
0
        public void ValidUpsert()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidUpsert))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator       = new RegisterValidator();
                var usersService    = new UsersService(context, validator, config);
                var userRoleService = new UserRoleService(context);
                var addedRole       = new UserRolePostModel
                {
                    Name        = "Admin",
                    Description = "jskds"
                };
                var result = userRoleService.Create(addedRole);

                var addedRole2 = new UserRolePostModel
                {
                    Name        = "Regular",
                    Description = "jskds"
                };
                var resultRegular = userRoleService.Create(addedRole2);


                var added = new Lab2Expense.Models.User
                {
                    Email         = "[email protected]",
                    FirstName     = "fdsfsdfs",
                    LastName      = "fdsfs",
                    Password      = "******",
                    Username      = "******",
                    UserUserRoles = new List <UserUserRole>()
                };

                var adminRole = context
                                .UserRole
                                .FirstOrDefault(ur => ur.Name == "Admin");

                context.Users.Add(added);
                context.UserUserRole.Add(new UserUserRole
                {
                    User      = added,
                    UserRole  = adminRole,
                    StartTime = DateTime.Now,
                    EndTime   = null,
                });
                var addedRegular = new Lab2Expense.Models.User
                {
                    Email         = "[email protected]",
                    FirstName     = "fdsfsdfs",
                    LastName      = "fdsfs",
                    Password      = "******",
                    Username      = "******",
                    UserUserRoles = new List <UserUserRole>()
                };

                var RegularRole = context
                                  .UserRole
                                  .FirstOrDefault(ur => ur.Name == "Regular");

                context.Users.Add(addedRegular);
                context.UserUserRole.Add(new UserUserRole
                {
                    User      = addedRegular,
                    UserRole  = RegularRole,
                    StartTime = DateTime.Now,
                    EndTime   = null,
                });
                context.SaveChanges();
                context.Entry(added).State        = EntityState.Detached;
                context.Entry(adminRole).State    = EntityState.Detached;
                context.Entry(addedRegular).State = EntityState.Detached;
                var addedRegular2 = new Lab2Expense.Models.User
                {
                    Email         = "*****@*****.**",
                    FirstName     = "fdsfsdfs",
                    LastName      = "fdsfs",
                    Password      = "******",
                    Username      = "******",
                    UserUserRoles = new List <UserUserRole>()
                };
                context.Entry(addedRegular2).State = EntityState.Detached;
                //   var resultAdded = usersService.Register(added);
                //  var resultAuthentificate = usersService.Authenticate(added.Username, added.Password);



                var resultUpdate = usersService.Upsert(addedRegular.Id, addedRegular2, added);
                Assert.AreEqual("*****@*****.**", addedRegular.Email);
            }
        }