public async Task TestGetPermissions()
        {
            var camId       = 1;
            var isUserValid = true;
            var camUser     = new User
            {
                PrincipalId = camId,
            };
            var user = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            var permissions = new List <IPermission>();
            var userCache   = new UserCache(user, camUser, isUserValid, permissions);

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(userCache);

            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);
            Action <IEnumerable <IPermission> > tester = (testPermissions) =>
            {
                Assert.IsTrue(Object.ReferenceEquals(permissions, testPermissions));
            };

            tester(provider.GetPermissions(user));
            tester(await provider.GetPermissionsAsync(user));
        }
        public void TestGetCurrentUser_NoCurrentUser()
        {
            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);
            var user     = provider.GetCurrentUser();

            Assert.IsInstanceOfType(user, typeof(AnonymousUser));
        }
        public async Task TestGetBusinessUser()
        {
            var camId   = 1;
            var camUser = new User
            {
                PrincipalId = camId,
            };
            var user = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            var isUserValid = true;
            var userCache   = new UserCache(user, camUser, isUserValid);

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(userCache);

            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);
            Action <ECA.Business.Service.User> tester = (testUser) =>
            {
                Assert.AreEqual(camId, testUser.Id);
            };

            tester(provider.GetBusinessUser(user));
            tester(await provider.GetBusinessUserAsync(user));
        }
        public async Task TestHasSevisUserAccount_UserDoesNotHaveAccount()
        {
            var camId       = 1;
            var isUserValid = true;
            var camUser     = new User
            {
                PrincipalId = camId,
            };
            var user = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            var permissions = new List <IPermission>();
            var userCache   = new UserCache(user, camUser, isUserValid, permissions);

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(userCache);

            Action <bool> tester = (result) =>
            {
                Assert.IsFalse(result);
            };
            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            tester(provider.HasSevisUserAccount(user, "user", "org"));
            tester(await provider.HasSevisUserAccountAsync(user, "user", "org"));
        }
        public void TestClear_UserCacheIsPresent()
        {
            var camId       = 1;
            var isUserValid = true;
            var camUser     = new User
            {
                PrincipalId = camId,
            };
            var user = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            var permissions = new List <IPermission>();
            var userCache   = new UserCache(user, camUser, isUserValid, permissions);

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(userCache);
            cacheService.Setup(x => x.Remove(It.IsAny <IWebApiUser>()));
            cacheService.Setup(x => x.Remove(It.IsAny <Guid>()));

            permissionService.Setup(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>())).ReturnsAsync(permissions);
            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            provider.Clear(user);
            cacheService.Verify(x => x.Remove(It.IsAny <IWebApiUser>()), Times.Never());
            cacheService.Verify(x => x.Remove(It.IsAny <Guid>()), Times.Once());
        }
        public async Task TestGetPrincipalId()
        {
            var camId       = 1;
            var isUserValid = true;
            var camUser     = new User
            {
                PrincipalId = camId,
            };
            var user = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            var permissions = new List <IPermission>();
            var userCache   = new UserCache(user, camUser, isUserValid, permissions);

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(userCache);
            userService.Setup(x => x.GetUserById(It.IsAny <Guid>())).Returns(camUser);
            userService.Setup(x => x.GetUserByIdAsync(It.IsAny <Guid>())).ReturnsAsync(camUser);

            permissionService.Setup(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>())).ReturnsAsync(permissions);
            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            Action <int> tester = (testId) =>
            {
                Assert.AreEqual(camId, testId);
            };

            tester(provider.GetPrincipalId(user));
            tester(await provider.GetPrincipalIdAsync(user));
        }
        public async Task TestGetUserCache_UserIsNotCached_UserDoesNotExistInCam()
        {
            var isUserValid = true;
            var user        = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            User nullUser             = null;
            var  permissions          = new List <IPermission>();
            Action <UserCache> tester = (c) =>
            {
                Assert.AreEqual(0, c.Permissions.Count());
            };

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(default(UserCache));
            cacheService.Setup(x => x.Add(It.IsAny <UserCache>())).Callback(tester);
            permissionService.Setup(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>())).ReturnsAsync(permissions);

            userService.Setup(x => x.GetUserById(It.IsAny <Guid>())).Returns(nullUser);
            userService.Setup(x => x.GetUserByIdAsync(It.IsAny <Guid>())).ReturnsAsync(nullUser);
            userService.Setup(x => x.IsUserValid(It.IsAny <Guid>())).Returns(isUserValid);
            userService.Setup(x => x.IsUserValidAsync(It.IsAny <Guid>())).ReturnsAsync(isUserValid);

            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            provider.GetUserCache(user);
            userService.Verify(x => x.GetUserById(It.IsAny <Guid>()), Times.Once());

            await provider.GetUserCacheAsync(user);

            userService.Verify(x => x.GetUserByIdAsync(It.IsAny <Guid>()), Times.Once());

            permissionService.Verify(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>()), Times.Exactly(0));
        }
Ejemplo n.º 8
0
        public async Task TestImpersonateAsync_ImpersonatedUserIsValid()
        {
            Guid impersonatorId             = Guid.NewGuid();
            var  isImpersonatorCamUserValid = false;
            var  impersonatorCamUser        = new User
            {
                PrincipalId = 1,
            };
            var impersonator = new SimpleUser
            {
                Id       = impersonatorId,
                Username = "******"
            };
            var impersonatorUserCache = new UserCache(impersonator, impersonatorCamUser, isImpersonatorCamUserValid);

            Guid impersonatedId              = Guid.NewGuid();
            var  isImpersonatedCamUserValid  = true;
            var  impersonatedUserPermissions = new List <IPermission>
            {
                new CAM.Business.Service.SimplePermission
                {
                    IsAllowed    = true,
                    PermissionId = 1,
                    PrincipalId  = 2,
                    ResourceId   = 3
                }
            };
            var impersonatedCamUser = new User
            {
                PrincipalId = 2,
            };
            var impersonatedUser = new SimpleUser
            {
                Id       = impersonatedId,
                Username = "******"
            };

            permissionService.Setup(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>())).ReturnsAsync(impersonatedUserPermissions);

            userService.Setup(x => x.GetUserByIdAsync(It.Is <Guid>((id) => id == impersonatedId))).ReturnsAsync(impersonatedCamUser);
            userService.Setup(x => x.GetUserByIdAsync(It.Is <Guid>((id) => id == impersonatorId))).ReturnsAsync(impersonatorCamUser);
            userService.Setup(x => x.IsUserValidAsync(It.Is <Guid>((id) => id == impersonatedId))).ReturnsAsync(isImpersonatedCamUserValid);
            userService.Setup(x => x.IsUserValidAsync(It.Is <Guid>((id) => id == impersonatorId))).ReturnsAsync(isImpersonatorCamUserValid);

            cacheDictionary.Add(impersonatorId.ToString(), new UserCache(impersonatedUser, impersonatedCamUser, isImpersonatedCamUserValid, impersonatedUserPermissions));

            var provider = new BearerTokenUserProvider(userService.Object, cacheService, permissionService.Object);
            await provider.ImpersonateAsync(impersonator, impersonatedId);

            var userCache = provider.GetUserCache(impersonator);

            Assert.IsNotNull(userCache);
            CollectionAssert.AreEqual(impersonatedUserPermissions.ToList(), userCache.Permissions.ToList());
            Assert.AreEqual(impersonatorCamUser.PrincipalId, userCache.CamPrincipalId);
            Assert.AreEqual(impersonator.Id, userCache.UserId);
            Assert.AreEqual(impersonator.Username, userCache.UserName);
            Assert.AreEqual(isImpersonatedCamUserValid, userCache.IsValidCamUser);
        }
        public void TestGetCurrentUser_HasCurrentUser()
        {
            var debugUser = SetDebugUser();
            var provider  = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);
            var user      = provider.GetCurrentUser();

            Assert.IsInstanceOfType(user, typeof(WebApiUser));
            Assert.AreEqual(debugUser.Id, user.Id);
        }
        public void TestDispose_DisposingAgainShouldNotThrow()
        {
            userService.As <IDisposable>();
            cacheService.As <IDisposable>();
            permissionService.As <IDisposable>();
            var testService = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            testService.Dispose();
            testService.Invoking(x => x.Dispose()).ShouldNotThrow();
        }
        public void TestDispose_PermissionService()
        {
            userService.As <IDisposable>();
            cacheService.As <IDisposable>();
            permissionService.As <IDisposable>();
            var testService = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            var serviceField = typeof(BearerTokenUserProvider).GetField("permissionService", BindingFlags.Instance | BindingFlags.NonPublic);
            var serviceValue = serviceField.GetValue(testService);

            Assert.IsNotNull(serviceField);
            Assert.IsNotNull(serviceValue);

            testService.Dispose();
            serviceValue = serviceField.GetValue(testService);
            Assert.IsNull(serviceValue);
        }
        public async Task TestGetUserCache_UserIsNotCached_UserExistsInCam()
        {
            var camId       = 1;
            var isUserValid = true;
            var camUser     = new User
            {
                PrincipalId = camId,
            };
            var user = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            var permissions = new List <IPermission>();
            Action <UserCache> addCallback = (c) =>
            {
                Assert.IsNotNull(c);
                Assert.AreEqual(camId, c.CamPrincipalId);
                Assert.AreEqual(isUserValid, c.IsValidCamUser);
                Assert.AreEqual(user.Id, c.UserId);
            };

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(default(UserCache));
            cacheService.Setup(x => x.Add(It.IsAny <UserCache>())).Callback(addCallback);

            permissionService.Setup(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>())).ReturnsAsync(permissions);
            userService.Setup(x => x.GetUserById(It.IsAny <Guid>())).Returns(camUser);
            userService.Setup(x => x.GetUserByIdAsync(It.IsAny <Guid>())).ReturnsAsync(camUser);
            userService.Setup(x => x.IsUserValid(It.IsAny <Guid>())).Returns(isUserValid);
            userService.Setup(x => x.IsUserValidAsync(It.IsAny <Guid>())).ReturnsAsync(isUserValid);

            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            provider.GetUserCache(user);
            await provider.GetUserCacheAsync(user);

            //be sure we make it through the IsValidUser check and load the user permissions.
            permissionService.Verify(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>()), Times.Exactly(1));
            permissionService.Verify(x => x.GetAllowedPermissionsByPrincipalId(It.IsAny <int>()), Times.Exactly(1));
        }
        public async Task TestHasSevisUserAccount_CheckCaseInsensitive()
        {
            var camId        = 1;
            var isUserValid  = true;
            var sevisAccount = new SevisUserAccount
            {
                OrgId    = "org",
                Username = "******"
            };
            var camUser = new User
            {
                PrincipalId = camId,
            };

            camUser.SevisUserAccounts = new List <SevisUserAccount>
            {
                sevisAccount
            };
            var user = new SimpleUser
            {
                Id = Guid.NewGuid()
            };
            var permissions = new List <IPermission>();
            var userCache   = new UserCache(user, camUser, isUserValid, permissions);

            cacheService.Setup(x => x.GetUserCache(It.IsAny <IWebApiUser>())).Returns(userCache);

            Action <bool> tester = (result) =>
            {
                Assert.IsTrue(result);
            };
            var provider = new BearerTokenUserProvider(userService.Object, cacheService.Object, permissionService.Object);

            tester(provider.HasSevisUserAccount(user, sevisAccount.Username.ToUpper(), sevisAccount.OrgId.ToUpper()));
            tester(await provider.HasSevisUserAccountAsync(user, sevisAccount.Username.ToUpper(), sevisAccount.OrgId.ToUpper()));
        }
Ejemplo n.º 14
0
        public void TestImpersonate_ImpersonatedUserIsNotValid()
        {
            Guid impersonatorId             = Guid.NewGuid();
            var  isImpersonatorCamUserValid = false;
            var  impersonatorCamUser        = new User
            {
                PrincipalId = 1,
            };
            var impersonator = new SimpleUser
            {
                Id       = impersonatorId,
                Username = "******"
            };
            var impersonatorUserCache = new UserCache(impersonator, impersonatorCamUser, isImpersonatorCamUserValid);

            Guid impersonatedId              = Guid.NewGuid();
            var  isImpersonatedCamUserValid  = false;
            var  impersonatedUserPermissions = new List <IPermission>
            {
                new CAM.Business.Service.SimplePermission
                {
                    IsAllowed    = true,
                    PermissionId = 1,
                    PrincipalId  = 2,
                    ResourceId   = 3
                }
            };
            var impersonatedCamUser = new User
            {
                PrincipalId = 2,
            };
            var impersonatedUser = new SimpleUser
            {
                Id       = impersonatedId,
                Username = "******"
            };

            permissionService.Setup(x => x.GetAllowedPermissionsByPrincipalIdAsync(It.IsAny <int>())).ReturnsAsync(impersonatedUserPermissions);
            Func <Guid, User> getUserById = (id) =>
            {
                if (id == impersonatorId)
                {
                    return(impersonatorCamUser);
                }
                else
                {
                    return(impersonatedCamUser);
                }
            };
            Func <Guid, bool> getIsUserValid = (id) =>
            {
                if (id == impersonatorId)
                {
                    return(isImpersonatorCamUserValid);
                }
                else
                {
                    return(isImpersonatedCamUserValid);
                }
            };

            userService.Setup(x => x.GetUserById(It.IsAny <Guid>())).Returns(getUserById);
            userService.Setup(x => x.IsUserValid(It.IsAny <Guid>())).Returns(getIsUserValid);
            cacheDictionary.Add(impersonatorId.ToString(), new UserCache(impersonatedUser, impersonatedCamUser, isImpersonatedCamUserValid, impersonatedUserPermissions));

            var provider = new BearerTokenUserProvider(userService.Object, cacheService, permissionService.Object);

            provider.Impersonate(impersonator, impersonatedId);

            var userCache = provider.GetUserCache(impersonator);

            Assert.IsNotNull(userCache);
            Assert.AreEqual(0, userCache.Permissions.Count());
        }