public async Task ChefAuthorization_ShowAllowStockCreateWhenChef()
        {
            //Arrange
            Meal meal = new Meal
            {
                Id             = 1,
                DayOfSesshinId = 1,
                Type           = MealType.Breakfast
            };
            var             userManager = MockIdentity.MockUserManager <AppUser>().Object;
            MockMealService mockMeal    = new MockMealService();

            mockMeal.MockGetById(meal);
            mockMeal.MockGetSesshinOwner("1");
            MockSesshinService mockSesshin = new MockSesshinService();
            var authorizationService       = MockAuthorizationService.BuildAuthorizationService(services =>
            {
                services.AddScoped <IMealService>(sp => mockMeal.Object);
                services.AddScoped <IAuthorizationHandler>(sp => new ChefAuthorizationHandler(userManager, mockMeal.Object, mockSesshin.Object));
            });
            var user = new ClaimsPrincipal(
                new ClaimsIdentity(
                    new Claim[] {
                new Claim(ClaimTypes.Name, "homer.simpson"),
                new Claim(ClaimTypes.Role, Constants.UserChefRole),
                new Claim("AccountStatus", "Approved")
            }));

            //Act
            var allowed = await authorizationService.AuthorizeAsync(user, new Stock(), UserOperations.Create);

            // Assert
            Assert.True(allowed.Succeeded);
        }
예제 #2
0
 public DayOfSesshinAccountantAuthorizationServiceTest()
 {
     _authorizationService = MockAuthorizationService.BuildAuthorizationService(services =>
     {
         services.AddScoped <IAuthorizationHandler, AccountantAuthorizationHandler>();
     });
     _user = new TestClaimsPrincipal();
 }
 public StockAdminAuthorizationServiceTest()
 {
     _authorizationService = MockAuthorizationService.BuildAuthorizationService(services =>
     {
         services.AddScoped <IAuthorizationHandler, AdminAuthorizationHandler>();
     });
     _user = new TestClaimsPrincipal();
 }
        public SesshinChefAuthorizationServiceTest()
        {
            _userManager    = new MockUserManager();
            _mealService    = new MockMealService();
            _sesshinService = new MockSesshinService();

            _authorizationService = MockAuthorizationService.BuildAuthorizationService(services =>
            {
                services.AddScoped <IAuthorizationHandler>(sp => new ChefAuthorizationHandler(_userManager.Object, _mealService.Object, _sesshinService.Object));
            });
            _user = new TestClaimsPrincipal();
        }
예제 #5
0
        private AccountsController GetAccountsControllerNoRole(MockAccountService mockService, MockUserManager mockUserManager, MockUserValidator mockUserValidator)
        {
            var authService = MockAuthorizationService.BuildAuthorizationService(services =>
            {
                services.AddScoped <IAccountService>(sp => mockService.Object);
                services.AddScoped <IAuthorizationHandler, AdminAuthorizationHandler>();
            });

            var controller = new AccountsController(authService, mockUserManager.Object, mockUserValidator.Object, mockService.Object);

            return(controller);
        }
예제 #6
0
        private SesshinsController GetSesshinsController(MockSesshinService mockSesshin)
        {
            var authService = MockAuthorizationService.BuildAuthorizationService(services =>
            {
                services.AddScoped <ISesshinService>(sp => mockSesshin.Object);
                services.AddScoped <IAuthorizationHandler, AdminAuthorizationHandler>();
            });

            var controller = new SesshinsController(mockSesshin.Object, authService);

            MockAuthorizationService.SetupUserWithRole(controller, Constants.UserAdministratorsRole);

            return(controller);
        }
예제 #7
0
        private MealsController GetMealsController(MockMealService mockService, bool addRole)
        {
            var authService = MockAuthorizationService.BuildAuthorizationService(services =>
            {
                services.AddScoped <IMealService>(sp => mockService.Object);
                services.AddScoped <IAuthorizationHandler, AdminAuthorizationHandler>();
            });

            var controller = new MealsController(mockService.Object, authService);

            if (addRole)
            {
                MockAuthorizationService.SetupUserWithRole(controller, Constants.UserAdministratorsRole);
            }

            return(controller);
        }
        public async Task AccountantAuthorization_ShowAllowStockCreateWhenAccountant()
        {
            //Arrange
            var authorizationService = MockAuthorizationService.BuildAuthorizationService(services =>
            {
                services.AddScoped <IAuthorizationHandler, AccountantAuthorizationHandler>();
            });
            var user = new ClaimsPrincipal(
                new ClaimsIdentity(
                    new Claim[] {
                new Claim(ClaimTypes.Name, "homer.simpson"),
                new Claim(ClaimTypes.Role, Constants.UserAccountantRole),
                new Claim("AccountStatus", "Approved")
            }));

            //Act
            var allowed = await authorizationService.AuthorizeAsync(user, new Stock(), UserOperations.Create);

            // Assert
            Assert.True(allowed.Succeeded);
        }
        public async Task AdminAuthorization_ShowNotAllowProductDeleteWhenChef()
        {
            //Arrange
            var authorizationService = MockAuthorizationService.BuildAuthorizationService(services =>
            {
                services.AddScoped <IAuthorizationHandler, AdminAuthorizationHandler>();
            });
            var user = new ClaimsPrincipal(
                new ClaimsIdentity(
                    new Claim[] {
                new Claim(ClaimTypes.Name, "homer.simpson"),
                new Claim(ClaimTypes.Role, Constants.UserChefRole),
                new Claim("AccountStatus", "Approved")
            }));

            //Act
            var allowed = await authorizationService.AuthorizeAsync(user, new Product(), UserOperations.Delete);

            // Assert
            Assert.False(allowed.Succeeded);
        }