Exemple #1
0
        protected void Application_Start()
        {
            

            Action<User> saveUserToSession = (account) =>
            {
                HttpContext.Current.Session.Remove("User");
                HttpContext.Current.Session.Add("User", account);
            };
            Func<User> getFromSession = () => (User)HttpContext.Current.Session["User"];

            var memoryDataSourceUser = new Dictionary<string, User>();

            //sample initialization
            memoryDataSourceUser.Add("wow", new User() { Id = Guid.Parse("537c8609-ddbe-434f-9e74-b2f55b9a35da"),  Name = "wow", Password = "******" });
            memoryDataSourceUser.Add("wah", new User() { Id = Guid.Parse("2939c3e6-0ee6-4167-a8aa-f5e95e58edee"),  Name = "wah", Password = "******" }); 
            

            //var repoAccount = new MemoryRepoAccount(new Dictionary<Guid, Account>());
            var repoAccount = new FileSystemRepoAccount();
            var serviceAccount = new ServiceAccount(repoAccount);

            var repoUser = new MemoryRepoUser(memoryDataSourceUser);
            var serviceUser = new ServiceUser(repoUser, saveUserToSession, getFromSession);

            var filterConfig = new FilterConfig(serviceUser);

            var controllerFactory = new PoorManControllerFactory(serviceUser,serviceAccount);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);

            AreaRegistration.RegisterAllAreas();
            filterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
        public async Task Get_will_fetch_only_data_for_the_Account()
        {
            // Arrange
            var wowId = Guid.NewGuid();
            var wowId2 = Guid.NewGuid();
            IRepoAccount repoAccount = createFakeRepoAccount(wowId, wowId2);
            IRepoUser repoUser = createFakeRepoUser(wowId, wowId2);
            User mutableAccount = null;
            Action<User> saveUserToSession = (account) => mutableAccount = account;
            Func<User> getFromSession = () => mutableAccount;
            IServiceUser serviceUser = new ServiceUser(repoUser, saveUserToSession, getFromSession);
            IServiceAccount serviceAccount = new ServiceAccount(repoAccount);

            serviceUser.authenticate("wow", "wow");
            AccountController controller = new AccountController(serviceUser, serviceAccount);

            // Act
            var result = await controller.GetAsync();

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count());
            Assert.AreEqual(result.ElementAt(1).Name, "Lor'themar Theron");
        }
        public async Task Add_Correct_Character_To_Account()
        {
            // Arrange
            var wowId = Guid.NewGuid();
            var wowId2 = Guid.NewGuid();
            IRepoAccount repoAccount = createFakeRepoAccount(wowId, wowId2);
            IRepoUser repoUser = createFakeRepoUser(wowId, wowId2);
            User mutableAccount = null;
            Action<User> saveUserToSession = (account) => mutableAccount = account;
            Func<User> getFromSession = () => mutableAccount;
            IServiceUser serviceUser = new ServiceUser(repoUser, saveUserToSession, getFromSession);
            IServiceAccount serviceAccount = new ServiceAccount(repoAccount);

            serviceUser.authenticate("wow", "wow");
            AccountController controller = new AccountController(serviceUser, serviceAccount);
            var cmd = new CreateCharacterCmd()
            {
                Id = Guid.NewGuid(),
                Name = "Orgrim Doomhammer",
                Class = ClassFactory.Warrior.ToString(),
                Faction = FactionFactory.Horde.ToString(),
                Level = 100,
                Race = RaceFactory.Orc.ToString()
            };

            // Act
            var result = await controller.AddNewCharacterAsync(cmd);

            // Assert
            var response = AssertResponse(result);
            Assert.IsTrue(response.IsOk);
        }
        public async Task retrieve_empty_guid_character_from_account()
        {
            // Arrange
            var wowId = Guid.NewGuid();
            var wowId2 = Guid.NewGuid();
            IRepoAccount repoAccount = createFakeRepoAccount(wowId, wowId2);
            IRepoUser repoUser = createFakeRepoUser(wowId, wowId2);
            User mutableAccount = null;
            Action<User> saveUserToSession = (account) => mutableAccount = account;
            Func<User> getFromSession = () => mutableAccount;
            IServiceUser serviceUser = new ServiceUser(repoUser, saveUserToSession, getFromSession);
            IServiceAccount serviceAccount = new ServiceAccount(repoAccount);

            serviceUser.authenticate("wow", "wow");
            AccountController controller = new AccountController(serviceUser, serviceAccount);
            var cmd = new CreateCharacterCmd()
            {
                Id = Guid.NewGuid(),
                Name = "Orgrim Doomhammer",
                Class = ClassFactory.Warrior.ToString(),
                Faction = FactionFactory.Horde.ToString(),
                Level = 100,
                Race = RaceFactory.Orc.ToString()
            };
            await controller.AddNewCharacterAsync(cmd);
            var currentUser = serviceUser.getCurrentUser();
            var characters = await serviceAccount.GetCharactersAsync(currentUser.Id);
            await controller.RemoveCharacterAsync(new RemoveCharacterCmd() { Id = characters.ElementAt(0).Id });
            var retrieveCmd = new RetrieveCharacterCmd() { Id = Guid.Empty };

            // Act
            var result = await controller.RetrieveCharacterAsync(retrieveCmd);

            // Assert
            var response = AssertResponse(result);
            Assert.IsFalse(response.IsOk);
        }
        public async Task send_invalid_command_to_create_character(Guid id, string name, string @class, string faction, int level, string race)
        {
            // Arrange
            var wowId = Guid.NewGuid();
            var wowId2 = Guid.NewGuid();
            IRepoAccount repoAccount = createFakeRepoAccount(wowId, wowId2);
            IRepoUser repoUser = createFakeRepoUser(wowId, wowId2);
            User mutableAccount = null;
            Action<User> saveUserToSession = (account) => mutableAccount = account;
            Func<User> getFromSession = () => mutableAccount;
            IServiceUser serviceUser = new ServiceUser(repoUser, saveUserToSession, getFromSession);
            IServiceAccount serviceAccount = new ServiceAccount(repoAccount);

            serviceUser.authenticate("wow", "wow");
            AccountController controller = new AccountController(serviceUser, serviceAccount);
            var cmd = new CreateCharacterCmd()
            {
                Id = id,
                Name = name,
                Class = @class,
                Faction = faction,
                Level = level,
                Race = race
            };

            // Act
            var result = await controller.AddNewCharacterAsync(cmd);

            // Assert
            var response = AssertResponse(result);
            Assert.IsFalse(response.IsOk);
        }