コード例 #1
0
        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();
 }
コード例 #3
0
 public StockAdminAuthorizationServiceTest()
 {
     _authorizationService = MockAuthorizationService.BuildAuthorizationService(services =>
     {
         services.AddScoped <IAuthorizationHandler, AdminAuthorizationHandler>();
     });
     _user = new TestClaimsPrincipal();
 }
コード例 #4
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
        protected override void Given()
        {
            base.Given();

            MockAuthorizationService
            .Setup(
                m => m.AuthorizeAsync(
                    It.IsAny <ClaimsPrincipal>(),
                    It.IsAny <object>(),
                    PolicyNames.CanSubmitReport))
            .Returns(
                Task.FromResult(AuthorizationResult.Failed()));
            MockAuthorizationService
            .Setup(
                m => m.AuthorizeAsync(
                    It.IsAny <ClaimsPrincipal>(),
                    It.IsAny <object>(),
                    PolicyNames.CanEditReport))
            .Returns(
                Task.FromResult(AuthorizationResult.Failed()));
        }
コード例 #11
0
        public void TestWebAuthorizationExceptionThrownIfNotAuthorized()
        {
            MockWebClientApplication mockApplication          = new MockWebClientApplication();
            MockAuthorizationService mockAuthorizationService =
                mockApplication.RootContainer.Services.AddNew <MockAuthorizationService, IAuthorizationService>();
            MockVirtualPathUtility mockVirtualPathUtility =
                mockApplication.RootContainer.Services.AddNew <MockVirtualPathUtility, IVirtualPathUtilityService>();

            mockVirtualPathUtility.ToAppRelativeResult = "~/files.asx";
            mockAuthorizationService.ShouldAuthorize   = false;
            MockAuthorizationRuleCatalogService mockRulesCatalog =
                mockApplication.RootContainer.Services.AddNew <MockAuthorizationRuleCatalogService, IAuthorizationRulesService>();

            mockRulesCatalog.GetRuleReturnValue = new string[] { "AuthorizationRule" };
            MockHttpContext mockContext = new MockHttpContext();

            mockContext.Request = new HttpRequest("file.aspx", "http://MyWebApp/files.aspx", null);

            TestableWebClientAuthorizationModule module = new TestableWebClientAuthorizationModule();

            module.TestHandleAuthorization(mockApplication, mockContext);
        }
コード例 #12
0
        SpaceInteractionServiceReturnsAuthFailureWithInvalidTokens()
        {
            // setup
            var invalidAuthorizationService =
                new MockAuthorizationService(false, null);
            var spaceInteractionService = new SpaceInteractionService(
                LoggerFactory.Create((config) => {})
                .CreateLogger <SpaceInteractionService>(),
                null !, // TODO: add mocks for IConfig/IndexerDBC
                null !,
                invalidAuthorizationService);

            // actions
            var result = await spaceInteractionService.CreateSpaceAsync(
                new Models.V1.Space.CreateOrUpdateSpaceRequest
            {
                SpaceId = "asdf"
            });

            // assertions
            Assert.Equal("INVALID_SCOPE", result.Right?.PropertyErrorCode);
        }
コード例 #13
0
        public void TestAuthorizationServiceIsAskedForAuthorizationRule()
        {
            MockWebClientApplication mockApplication          = new MockWebClientApplication();
            MockAuthorizationService mockAuthorizationService =
                mockApplication.RootContainer.Services.AddNew <MockAuthorizationService, IAuthorizationService>();
            MockAuthorizationRuleCatalogService mockRulesCatalog =
                mockApplication.RootContainer.Services.AddNew <MockAuthorizationRuleCatalogService, IAuthorizationRulesService>();
            MockVirtualPathUtility mockVirtualPathUtility =
                mockApplication.RootContainer.Services.AddNew <MockVirtualPathUtility, IVirtualPathUtilityService>();

            mockVirtualPathUtility.ToAppRelativeResult = "~/files.asx";
            mockRulesCatalog.GetRuleReturnValue        = new string[] { "AuthorizationRule" };
            MockHttpContext mockContext = new MockHttpContext();

            mockContext.Request = new HttpRequest("file.aspx", "http://MyWebApp/files.aspx", null);

            TestableWebClientAuthorizationModule module = new TestableWebClientAuthorizationModule();

            module.TestHandleAuthorization(mockApplication, mockContext);

            Assert.AreEqual("~/files.asx", mockRulesCatalog.LastGetRuleUrl);
            Assert.AreEqual("AuthorizationRule", mockAuthorizationService.LastContext);
        }
コード例 #14
0
 protected void SetupAuthorization(bool isAuthorized)
 {
     MockAuthorizationService
     .Setup(x => x.AuthorizeAsync(It.IsAny <ClaimsPrincipal>(), null, It.IsAny <IEnumerable <IAuthorizationRequirement> >()))
     .ReturnsAsync(isAuthorized ? AuthorizationResult.Success() : AuthorizationResult.Failed());
 }