Exemplo n.º 1
0
        public async Task GET_DailyNutritionLogControllerForbidsIncorrectUser()
        {
            var options = SqliteInMemory
                          .CreateOptions <ApplicationDbContext>();

            using (var context = new ApplicationDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedTestDatabaseUsersWithDailyNutritionLogs();
            }

            using (var context = new ApplicationDbContext(options))
            {
                DailyNutritionLogController controller = new DailyNutritionLogController(
                    context,
                    TestUtilities.SetUpMockClaimsManager(1).Object,
                    TestUtilities.GenericSetUpControllerStubLogger <DailyNutritionLogController>().Object);

                controller.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext {
                        User = TestUtilities.setUpSampleHttpContextUser()
                    }
                };

                var result = await controller.Get(2);

                Assert.IsType <ForbidResult>(result);
            }
        }
Exemplo n.º 2
0
        public async Task GET_DailyNutritionLogControllerReturnsLogs(int userId, int numberOfLogsResult)
        {
            var options = SqliteInMemory
                          .CreateOptions <ApplicationDbContext>();

            using (var context = new ApplicationDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedTestDatabaseUsersWithDailyNutritionLogs();
            }

            using (var context = new ApplicationDbContext(options))
            {
                DailyNutritionLogController controller = new DailyNutritionLogController(
                    context,
                    TestUtilities.SetUpMockClaimsManager(userId).Object,
                    TestUtilities.GenericSetUpControllerStubLogger <DailyNutritionLogController>().Object);

                controller.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext {
                        User = TestUtilities.setUpSampleHttpContextUser()
                    }
                };

                var result = await controller.Get(userId) as OkObjectResult;

                var resultData = result.Value as List <DailyNutritionLogDTO>;

                Assert.Equal(numberOfLogsResult, resultData.Count);
                foreach (DailyNutritionLogDTO dto in resultData)
                {
                    Assert.Equal(userId, dto.UserId);
                }
            }
        }