public static async Task <Authentication> LoginRandomAsync(this ICremaHost cremaHost, Authority authority, Func <IUser, bool> predicate)
        {
            if (cremaHost.GetService(typeof(IUserCollection)) is IUserCollection userCollection)
            {
                var userFlags  = AuthorityUtility.ToUserFlags(authority) | UserFlags.Offline | UserFlags.NotBanned;
                var userFilter = new UserFilter(userFlags);
                var user       = await userFilter.GetUserAsync(cremaHost);

                var name     = user.ID;
                var password = user.GetPassword();
                var token    = await cremaHost.LoginAsync(name, password);

                return(await cremaHost.AuthenticateAsync(token));
            }
            throw new NotImplementedException();

            bool Test(IUser user)
            {
                if (user.BanInfo.Path != string.Empty)
                {
                    return(false);
                }
                if (user.UserState == UserState.Online)
                {
                    return(false);
                }
                if (user.Authority != authority)
                {
                    return(false);
                }
                return(predicate(user));
            }
        }
        public async Task ItemsDeleted_User_TestAsync()
        {
            var authentication = await this.TestContext.LoginRandomAsync(Authority.Admin);

            var userFilter1 = new UserFilter()
            {
                ExcludedUserIDs = new[] { Authentication.AdminID, authentication.ID }
            };
            var userItem1    = (await userFilter1.GetUserAsync(app)) as IUserItem;
            var actualPath   = string.Empty;
            var expectedPath = userItem1.Path;
            await userContext.AddItemsDeletedEventHandlerAsync(UserContext_ItemsDeleted);

            await userItem1.DeleteAsync(authentication);

            Assert.AreEqual(expectedPath, actualPath);
            await userContext.RemoveItemsDeletedEventHandlerAsync(UserContext_ItemsDeleted);

            var userFilter2 = new UserFilter()
            {
                ExcludedUserIDs = new[] { Authentication.AdminID, authentication.ID }
            };
            var userItem2 = (await userFilter2.GetUserAsync(app)) as IUserItem;
            await userItem2.DeleteAsync(authentication);

            Assert.AreEqual(expectedPath, actualPath);

            void UserContext_ItemsDeleted(object sender, ItemsDeletedEventArgs <IUserItem> e)
            {
                var userItem = e.Items.Single();

                actualPath = e.ItemPaths.Single();
            }
        }
        public async Task UsersLoggedIn_TestAsync()
        {
            var userFilter1 = new UserFilter(UserFlags.Offline | UserFlags.NotBanned);
            var user1       = await userFilter1.GetUserAsync(app);

            var userFilter2 = new UserFilter(UserFlags.Offline | UserFlags.NotBanned)
            {
                ExcludedUserIDs = new[] { user1.ID }
            };
            var user2 = await userFilter2.GetUserAsync(app);

            var actualUserID = string.Empty;
            await userCollection.AddUsersLoggedInEventHandlerAsync(UserCollection_UsersLoggedIn);

            Console.WriteLine(user1.UserState);
            var authentication1 = await this.TestContext.LoginAsync(user1.ID);

            Assert.AreEqual(authentication1.ID, actualUserID);
            await userCollection.RemoveUsersLoggedInEventHandlerAsync(UserCollection_UsersLoggedIn);

            var authentication2 = await this.TestContext.LoginAsync(user2.ID);

            Assert.AreEqual(authentication1.ID, actualUserID);
            Assert.AreNotEqual(authentication2.ID, actualUserID);

            void UserCollection_UsersLoggedIn(object sender, ItemsEventArgs <IUser> e)
            {
                var user = e.Items.Single();

                actualUserID = user.ID;
            }
        }
        public async Task UsersStateChanged_TestAsync()
        {
            var actualState = UserState.None;
            var userFilter  = new UserFilter(UserFlags.Offline | UserFlags.NotBanned);
            var user        = await userFilter.GetUserAsync(app);

            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersStateChanged += UserCollection_UsersStateChanged;
            });

            var authentication = await this.TestContext.LoginAsync(user.ID);

            Assert.AreEqual(user.UserState, actualState);
            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersStateChanged -= UserCollection_UsersStateChanged;
            });

            await this.TestContext.LogoutAsync(authentication);

            Assert.AreNotEqual(user.UserState, actualState);

            void UserCollection_UsersStateChanged(object sender, ItemsEventArgs <IUser> e)
            {
                var user = e.Items.Single();

                actualState = user.UserState;
            }
        }
        public async Task UsersChanged_TestAsync()
        {
            var userFilter = new UserFilter(UserFlags.Offline | UserFlags.NotBanned);
            var user       = await userFilter.GetUserAsync(app);

            var authentication = await this.TestContext.LoginAsync(user.ID);

            var actualUserName = user.UserName;
            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersChanged += UserCollection_UsersChanged;
            });

            var password = user.GetPassword();
            await user.SetUserNameAsync(authentication, password, RandomUtility.NextName());

            Assert.AreEqual(user.UserName, actualUserName);
            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersChanged -= UserCollection_UsersChanged;
            });

            await user.SetUserNameAsync(authentication, password, $"{RandomUtility.NextName()}{RandomUtility.Next(100)}");

            Assert.AreNotEqual(user.UserName, actualUserName);

            void UserCollection_UsersChanged(object sender, ItemsEventArgs <IUser> e)
            {
                var user = e.Items.Single();

                actualUserName = user.UserName;
            }
        }
        public async Task Contains_Dispatcher_FailTestAsync()
        {
            var userFilter = new UserFilter();
            var user       = await userFilter.GetUserAsync(app);

            userCollection.Contains(user.ID);
            Assert.Fail();
        }
        public async Task Contains_TestAsync()
        {
            var userFilter = new UserFilter();
            var user       = await userFilter.GetUserAsync(app);

            var contains = await userCollection.Dispatcher.InvokeAsync(() => userCollection.Contains(user.ID));

            Assert.IsTrue(contains);
        }
        public async Task Indexer_Dispatcher_FailTestAsync()
        {
            var userFilter = new UserFilter();
            var user1      = await userFilter.GetUserAsync(app);

            var user2 = userCollection[user1.ID];

            Assert.Fail();
        }
        public async Task Indexer_TestAsync()
        {
            var userFilter = new UserFilter();
            var user1      = await userFilter.GetUserAsync(app);

            var user2 = await userCollection.Dispatcher.InvokeAsync(() => userCollection[user1.ID]);

            Assert.AreEqual(user1, user2);
        }
Beispiel #10
0
        public async Task DeleteAsync_Online_FailTestAsync()
        {
            var authentication = await this.TestContext.LoginRandomAsync(Authority.Admin);

            var userFilter = new UserFilter(UserFlags.Online)
            {
                ExcludedUserIDs = new[] { Authentication.AdminID, authentication.ID }
            };
            var user = await userFilter.GetUserAsync(app);

            var userItem = user as IUserItem;
            await userItem.DeleteAsync(authentication);
        }
        public async Task UsersMoved_TestAsync()
        {
            var authentication = await this.TestContext.LoginRandomAsync(Authority.Admin);

            var userCategoryCollection = userCollection.GetService(typeof(IUserCategoryCollection)) as IUserCategoryCollection;
            var userFilter             = new UserFilter();
            var user = await userFilter.GetUserAsync(app);

            var categoryFilter = new UserCategoryFilter()
            {
                ExcludedCategories = new[] { user.Category }
            };
            var category = await categoryFilter.GetUserCategoryAsync(app);

            var oldCategory        = user.Category;
            var actualPath         = string.Empty;
            var actualCategoryPath = string.Empty;
            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersMoved += UserCollection_UsersMoved;
            });

            await user.MoveAsync(authentication, category.Path);

            Assert.AreEqual(user.Path, actualPath);
            Assert.AreEqual(user.Category.Path, actualCategoryPath);
            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersMoved -= UserCollection_UsersMoved;
            });

            await user.MoveAsync(authentication, oldCategory.Path);

            Assert.AreNotEqual(user.Path, actualPath);
            Assert.AreNotEqual(user.Category.Path, actualCategoryPath);

            void UserCollection_UsersMoved(object sender, ItemsMovedEventArgs <IUser> e)
            {
                var user = e.Items.Single();

                actualPath         = user.Path;
                actualCategoryPath = user.Category.Path;
            }
        }
        public async Task UsersBanChanged_TestAsync()
        {
            var authentication = await this.TestContext.LoginRandomAsync(Authority.Admin);

            var userFilter1 = new UserFilter(UserFlags.Member | UserFlags.Guest | UserFlags.NotBanned);
            var user1       = await userFilter1.GetUserAsync(app);

            var userFilter2 = new UserFilter(UserFlags.Member | UserFlags.Guest | UserFlags.Banned)
            {
                ExcludedUserIDs = new[] { user1.ID }
            };
            var user2 = await userFilter2.GetUserAsync(app);

            var actualUserID    = string.Empty;
            var actualMessage   = string.Empty;
            var actualBanType   = BanChangeType.Unban;
            var expectedMessage = RandomUtility.NextString();
            await userCollection.AddUsersBanChangedEventHandlerAsync(UserCollection_UsersBanChanged);

            await user1.BanAsync(authentication, expectedMessage);

            Assert.AreEqual(user1.ID, actualUserID);
            Assert.AreEqual(expectedMessage, actualMessage);
            Assert.AreEqual(BanChangeType.Ban, actualBanType);
            await userCollection.RemoveUsersBanChangedEventHandlerAsync(UserCollection_UsersBanChanged);

            await user2.UnbanAsync(authentication);

            Assert.AreEqual(user1.ID, actualUserID);
            Assert.AreEqual(expectedMessage, actualMessage);
            Assert.AreEqual(BanChangeType.Ban, actualBanType);
            Assert.AreNotEqual(actualUserID, user2.ID);

            void UserCollection_UsersBanChanged(object sender, ItemsEventArgs <IUser> e)
            {
                var user     = e.Items.Single();
                var metaData = e.MetaData as object[];

                actualUserID  = user.ID;
                actualBanType = (BanChangeType)metaData[0];
                actualMessage = (metaData[1] as string[]).Single();
            }
        }
        public async Task UsersDeleted_TestAsync()
        {
            var authentication = await this.TestContext.LoginRandomAsync(Authority.Admin);

            var userFilter1 = new UserFilter(UserFlags.Offline | UserFlags.Member | UserFlags.Guest)
            {
                ExcludedUserIDs = new[] { Authentication.AdminID, authentication.ID }
            };
            var user1 = await userFilter1.GetUserAsync(app);

            var actualUserPath = user1.Path;
            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersDeleted += UserCollection_UsersDeleted;
            });

            await user1.DeleteAsync(authentication);

            Assert.AreEqual(string.Empty, actualUserPath);
            Assert.IsNull(user1.Category);
            await userCollection.Dispatcher.InvokeAsync(() =>
            {
                userCollection.UsersDeleted -= UserCollection_UsersDeleted;
            });

            var userFilter2 = new UserFilter(UserFlags.Offline | UserFlags.Member | UserFlags.Guest)
            {
                ExcludedUserIDs = new[] { Authentication.AdminID, authentication.ID }
            };
            var user2 = await userFilter2.GetUserAsync(app);

            var userPath2 = user2.Path;
            await user2.DeleteAsync(authentication);

            Assert.AreNotEqual(string.Empty, userPath2);

            void UserCollection_UsersDeleted(object sender, ItemsDeletedEventArgs <IUser> e)
            {
                actualUserPath = string.Empty;
            }
        }