예제 #1
0
        public async Task TestVehicleController_PostVehicle()
        {
            using (var transaction = Fixture.Connection.BeginTransaction())
            {
                using (var context = Fixture.CreateContext(transaction))
                {
                    using var serviceScope = _serviceProvider.CreateScope();
                    var appEnvirontent = serviceScope.ServiceProvider.GetRequiredService <IWebHostEnvironment>();
                    var seed           = new SeedData(new VehicleImageRetriever(), appEnvirontent);


                    var userManager     = serviceScope.ServiceProvider.GetRequiredService <ICustomUserManager>();
                    var userRoleManager = serviceScope.ServiceProvider.GetRequiredService <ICustomRoleManager>();
                    await seed.Initialize(userManager, userRoleManager, context);

                    var carsRepo     = new CarsRepository(context);
                    var carOwnerRepo = new CarOwnersRepository(context);


                    var identitySevice = serviceScope.ServiceProvider.GetRequiredService <IIdentityService>();

                    var userLoginRequest = new UserLoginRequest()
                    {
                        Email    = "*****@*****.**",
                        Password = "******"
                    };

                    var loginResponse = await identitySevice
                                        .LoginAsync(userLoginRequest.Email, userLoginRequest.Password);

                    var carService = new CarService(carsRepo, carOwnerRepo, identitySevice);

                    var mockLogger = new Mock <ILogger <VehiclesController> >();
                    ILogger <VehiclesController> logger = mockLogger.Object;

                    var mockWebHostingEnvironment = new Mock <IWebHostEnvironment>();

                    var mockCustomAuthorizationService = new Mock <ICustomAuthorizationService>();
                    mockCustomAuthorizationService
                    .Setup(m => m.AuthorizeAsync(
                               It.IsAny <ClaimsPrincipal>(),
                               It.IsAny <object>(),
                               It.IsAny <IAuthorizationRequirement>()))
                    .ReturnsAsync(AuthorizationResult.Success);

                    var controller = new VehiclesController(
                        carService,
                        logger,
                        new UriService("https://localhost:5010/"),
                        new CustomMapper(new VehicleImageRetriever()),
                        mockWebHostingEnvironment.Object,
                        new VehicleImageRetriever(),
                        mockCustomAuthorizationService.Object
                        );

                    var testCar = new CarRequest
                    {
                        Brand        = "Test",
                        Price        = 23.4m,
                        Date         = DateTime.Now,
                        CarEngine    = 3,
                        Color        = "Grey",
                        Description  = "test",
                        Drive        = "Mixed",
                        Transmision  = "Auto",
                        UniqueNumber = SeedData.GenerateRandomRegistrationPlateNumber(),
                        Token        = loginResponse.Token
                    };

                    var res = await controller.PostCarItem(testCar);

                    Assert.NotNull(res);
                    var car = await context.Cars.AsNoTracking().SingleOrDefaultAsync(c => c.UniqueNumber == testCar.UniqueNumber);

                    Assert.NotNull(car);
                }
            }
        }