public void GetOrdersOfUserTest() { OrdersOfUserController ordersController = CreateFakeOrdersOfUserController(_users[0]); //Retrieving all orders of same logged user (regular) var response = ordersController.GetAllOrdersOfUser(_users[0].Id); Assert.IsType <OkObjectResult>(response.Result); Assert.Equal(_orders.FindAll(o => o.UserId == _users[0].Id).Count, ((IEnumerable <GetOrderModel>)((OkObjectResult)response.Result).Value).Count()); //Retrieving all orders of different logged user (regular) response = ordersController.GetAllOrdersOfUser(2); Assert.IsType <UnauthorizedObjectResult>(response.Result); ordersController = CreateFakeOrdersOfUserController(_users[2]); //Retrieving all orders of different logged user (admin) response = ordersController.GetAllOrdersOfUser(_users[0].Id); Assert.IsType <OkObjectResult>(response.Result); Assert.Equal(_orders.FindAll(o => o.UserId == _users[0].Id).Count, ((IEnumerable <GetOrderModel>)((OkObjectResult)response.Result).Value).Count()); }
private OrdersOfUserController CreateFakeOrdersOfUserController(User loggedUser = null) { //Create fake DBContext var context = new GlovoDbContext(ContextOptions); //Create fake HttpContextAccessor var httpContext = new DefaultHttpContext(); var httpContextAccessor = new HttpContextAccessor { HttpContext = httpContext }; //Add logged user to HttpContextAccessor in case it is needed if (loggedUser != null) { httpContextAccessor.HttpContext.Items["User"] = loggedUser; } //Create RestApiRestaurantsService instance with fake DBContext and HttpContextAccessor _ordersService = new RestApiOrdersService(context, httpContextAccessor); //Create mapper with UsersProfile var mapper = new MapperConfiguration(cfg => { cfg.AddProfile <LocationsProfile>(); cfg.AddProfile <OrdersProductsProfile>(); cfg.AddProfile <OrdersProfile>(); cfg.AddProfile <ProductsProfile>(); cfg.AddProfile <RestaurantsProfile>(); cfg.AddProfile <UsersProfile>(); }).CreateMapper(); //Create UsersController instance with the RestApiRestaurantsService instance and the mapper var ordersController = new OrdersOfUserController(_ordersService, mapper) { ControllerContext = { HttpContext = httpContext } }; return(ordersController); }