Esempio n. 1
0
        public async Task EditByIdAsyncReturnsCorrect()
        {
            var id   = Guid.NewGuid().ToString();
            var user = new CarpetUser
            {
                Id          = id,
                FirstName   = "Иван",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Email       = "*****@*****.**",
                UserName    = "******",
            };
            var roleAdminId = Guid.NewGuid().ToString();
            var role        = new CarpetRole {
                Id = roleAdminId, Name = GlobalConstants.AdministratorRoleName
            };
            var roleOperatorId = Guid.NewGuid().ToString();
            var roleOperator   = new CarpetRole {
                Id = roleOperatorId, Name = GlobalConstants.OperatorRoleName
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "EditByIdAsyncReturnsCorrect")
                          .Options;
            var dbContext = new ApplicationDbContext(options);

            dbContext.Users.Add(user);
            dbContext.Roles.Add(role);
            dbContext.Roles.Add(roleOperator);
            await dbContext.SaveChangesAsync();

            var repository = new EfDeletableEntityRepository <Employee>(dbContext);
            var service    = new EmployeesService(repository, dbContext);

            var wrongId = Guid.NewGuid().ToString();

            var employee = new EmployeeCreateInputModel
            {
                Id          = id,
                FirstName   = "Иван",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Salary      = 1000m,
                RoleName    = GlobalConstants.OperatorRoleName,
            };

            var result = await service.CreateAsync(employee, string.Empty);

            var employeeEdit = new EmployeeEditInputModel
            {
                Id          = result.Id,
                FirstName   = "Edit",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Salary      = 1000m,
                RoleName    = GlobalConstants.OperatorRoleName,
            };

            var actual = await service.EditByIdAsync(result.Id, employeeEdit, string.Empty);

            Assert.Equal(actual.FirstName, employeeEdit.FirstName);
        }
Esempio n. 2
0
        public async Task EditByIdAsyncWithWrongRoleReturnsError()
        {
            var id   = Guid.NewGuid().ToString();
            var user = new CarpetUser
            {
                Id          = id,
                FirstName   = "Иван",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Email       = "*****@*****.**",
                UserName    = "******",
            };
            var roleAdminId = Guid.NewGuid().ToString();
            var role        = new CarpetRole {
                Id = roleAdminId, Name = GlobalConstants.AdministratorRoleName
            };
            var roleOperatorId = Guid.NewGuid().ToString();
            var roleOperator   = new CarpetRole {
                Id = roleOperatorId, Name = GlobalConstants.OperatorRoleName
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "EditByIdAsyncWithWrongRoleReturnsError")
                          .Options;
            var dbContext = new ApplicationDbContext(options);

            dbContext.Users.Add(user);
            dbContext.Roles.Add(role);
            dbContext.Roles.Add(roleOperator);
            await dbContext.SaveChangesAsync();

            var repository = new EfDeletableEntityRepository <Employee>(dbContext);
            var service    = new EmployeesService(repository, dbContext);

            var wrongId = Guid.NewGuid().ToString();

            var employee = new EmployeeCreateInputModel
            {
                Id          = id,
                FirstName   = "Иван",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Salary      = 1000m,
                RoleName    = GlobalConstants.OperatorRoleName,
            };

            var result = await service.CreateAsync(employee, string.Empty);

            var employeeEdit = new EmployeeEditInputModel
            {
                Id          = result.Id,
                FirstName   = "Иван",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Salary      = 1000m,
                RoleName    = "Wrong",
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(() => service.EditByIdAsync(result.Id, employeeEdit, string.Empty));

            Assert.Equal(string.Format(EmployeeConstants.ArgumentExceptionRoleNotExist, employeeEdit.RoleName), exception.Message);
        }