public void AddUserDistinctionAsync_IfNotAdmin_ThrowsUnauthorizedAccessException()
        {
            //Arrange
            mockRepoWrapper
            .Setup(x => x.UserDistinction.CreateAsync(It.IsAny <UserDistinction>()));

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

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

            Assert.AreEqual("Attempted to perform an unauthorized operation.", exception.Message);
        }
Exemplo n.º 2
0
        public void AddUserDistinctionAsync_IfNotAdmin_ThrowsUnauthorizedAccessException()
        {
            //Arrange
            mockRepoWrapper
            .Setup(x => x.UserDistinction.CreateAsync(It.IsAny <UserDistinction>()));

            //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.AddUserDistinctionAsync(It.IsAny <UserDistinctionDTO>(), notAdmin); });

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