示例#1
0
        public void ChangeDistinctionAsync_IfNotAdmin_ThrowsUnauthorizedAccessException()
        {
            //Arrange
            mockRepoWrapper
            .Setup(x => x.Distinction.GetFirstAsync(It.IsAny <Expression <Func <Distinction, bool> > >(),
                                                    It.IsAny <Func <IQueryable <Distinction>, IIncludableQueryable <Distinction, object> > >()))
            .ReturnsAsync(distinction);

            userManager.Setup(m => m.GetRolesAsync(It.IsAny <User>())).ReturnsAsync(GetRolesWithoutAdmin());

            //Assert
            Exception exception = Assert.ThrowsAsync(typeof(UnauthorizedAccessException),
                                                     async() => { await distinctionService.ChangeDistinctionAsync(distinctionDTO, It.IsAny <User>()); });

            Assert.AreEqual("Attempted to perform an unauthorized operation.", exception.Message);
        }
        public void ChangeDistinctionAsync_IfNotAdmin_ThrowsUnauthorizedAccessException()
        {
            //Arrange
            mockRepoWrapper
            .Setup(x => x.Distinction.GetFirstAsync(It.IsAny <Expression <Func <Distinction, bool> > >(),
                                                    It.IsAny <Func <IQueryable <Distinction>, IIncludableQueryable <Distinction, object> > >()))
            .ReturnsAsync(distinction);

            //Act
            ClaimsIdentity  claimsIdentity = new ClaimsIdentity();
            ClaimsPrincipal notAdmin       = new ClaimsPrincipal();

            claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, "Htos`"));
            notAdmin.AddIdentity(claimsIdentity);

            //Assert
            Exception exception = Assert.ThrowsAsync(typeof(UnauthorizedAccessException),
                                                     async() => { await distinctionService.ChangeDistinctionAsync(distinctionDTO, notAdmin); });

            Assert.AreEqual("Attempted to perform an unauthorized operation.", exception.Message);
        }