internal void OnActionExecuting( ActionExecutingContext filterContext, ApplicationUserManager userManager, ClientIdCalculator clientIdCalculator) { if (filterContext.ActionParameters.ContainsKey(USER_CONTEXT_KEY)) { ApplicationUser applicationUser; if (filterContext.HttpContext.User.Identity.IsAuthenticated) { string userId = filterContext.HttpContext.User.Identity.GetUserId(); applicationUser = userManager.FindByIdAsync(userId).Result; } else { applicationUser = new AnonymousApplicationUser(); if (this.RequiresGamingGroup) { filterContext.Result = new RedirectToRouteResult(MVC.Account.Login().GetRouteValueDictionary()); } } applicationUser.AnonymousClientId = clientIdCalculator.GetClientId(filterContext.HttpContext.Request, applicationUser); filterContext.ActionParameters[USER_CONTEXT_KEY] = applicationUser; } base.OnActionExecuting(filterContext); }
public void It_Processes_Analytics_Events_Then_Badges_Then_Champions_Then_Nemeses_Then_Achievements() { //--arrange var playersInGame = new List <int> { 10, 11 }; var playedGameEvent = new PlayedGameCreatedEvent(1, 2, playersInGame, TransactionSource.RestApi, new AnonymousApplicationUser()); var anonymousApplicationUser = new AnonymousApplicationUser(); //--act _autoMocker.ClassUnderTest.Handle(playedGameEvent); //--assert //--make sure event tracking was called but not yet the champion recalculator _autoMocker.Get <INemeStatsEventTracker>().AssertWasCalled(mock => mock.TrackPlayedGame( Arg <ApplicationUser> .Is.Equal(anonymousApplicationUser), Arg <TransactionSource> .Is.Equal(playedGameEvent.TransactionSource)), options => options.WhenCalled(y => _autoMocker.Get <IChampionRecalculator>().AssertWasNotCalled( mock => mock.RecalculateChampion(Arg <int> .Is.Anything, Arg <ApplicationUser> .Is.Anything, Arg <IDataContext> .Is.Anything)))); //--make sure the champion recalculator was called but not yet the nemesis recalculator _autoMocker.Get <IChampionRecalculator>().AssertWasCalled( mock => mock.RecalculateChampion( Arg <int> .Is.Equal(playedGameEvent.GameDefinitionId), Arg <ApplicationUser> .Is.Equal(anonymousApplicationUser), Arg <IDataContext> .Is.Anything, Arg <bool> .Is.Anything), options => options.WhenCalled(y => _autoMocker.Get <INemesisRecalculator>().AssertWasNotCalled( mock => mock.RecalculateNemesis(Arg <int> .Is.Equal(playersInGame[0]), Arg <ApplicationUser> .Is.Equal(anonymousApplicationUser), Arg <IDataContext> .Is.Anything)))); //--make sure the nemesis recalculator was called for the first player in the game, but not yet the achievement processor _autoMocker.Get <INemesisRecalculator>().AssertWasCalled( mock => mock.RecalculateNemesis( Arg <int> .Is.Equal(playersInGame[0]), Arg <ApplicationUser> .Is.Equal(anonymousApplicationUser), Arg <IDataContext> .Is.Anything), options => options.WhenCalled(y => _autoMocker.Get <IAchievementProcessor>().AssertWasNotCalled( mock => mock.ProcessAchievements(playedGameEvent.TriggerEntityId)))); //--make sure the nemesis recalculator was called for the second player in the game _autoMocker.Get <INemesisRecalculator>().AssertWasCalled( mock => mock.RecalculateNemesis( Arg <int> .Is.Equal(playersInGame[1]), Arg <ApplicationUser> .Is.Equal(anonymousApplicationUser), Arg <IDataContext> .Is.Anything)); _autoMocker.Get <IGamingGroupChampionRecalculator>().AssertWasCalled(mock => mock.RecalculateGamingGroupChampion( Arg <int> .Is.Equal(playedGameEvent.TriggerEntityId)), options => options.WhenCalled(y => _autoMocker.Get <IAchievementProcessor>().AssertWasNotCalled( mock => mock.ProcessAchievements(playedGameEvent.TriggerEntityId)))); //--make sure the achievement processor gets called _autoMocker.Get <IAchievementProcessor>().AssertWasCalled( mock => mock.ProcessAchievements(playedGameEvent.TriggerEntityId)); }
public void It_Shows_The_Login_Widget_If_The_User_Is_Unauthenticated() { var user = new AnonymousApplicationUser(); var viewResult = _autoMocker.ClassUnderTest.Index(user) as ViewResult; var model = viewResult.Model as HomeIndexViewModel; model.ShowLoginPartial.ShouldBe(true); }
public void The_Anonymous_User_Will_Have_No_Achievements_Unlocked() { //--arrange var anonymousUser = new AnonymousApplicationUser(); //--act var results = _autoMocker.ClassUnderTest.GetAllAchievementSummaries(anonymousUser); //--assert results.Any(x => x.CurrentPlayerUnlockedThisAchievement).ShouldBeFalse(); }
internal void OnActionExecuting( ActionExecutingContext filterContext, ApplicationUserManager userManager, ClientIdCalculator clientIdCalculator) { if (filterContext.ActionParameters.ContainsKey(UserContextKey)) { ApplicationUser applicationUser; if (filterContext.HttpContext.User.Identity.IsAuthenticated) { var userId = filterContext.HttpContext.User.Identity.GetUserId(); applicationUser = userManager.FindByIdAsync(userId).Result; if (RequiresGamingGroup && !applicationUser.CurrentGamingGroupId.HasValue && !filterContext.IsChildAction) { var url = CreateManageAccountUrl(filterContext.RequestContext); filterContext.Result = new RedirectResult(url); } } else { applicationUser = new AnonymousApplicationUser(); if (RequiresGamingGroup) { filterContext.Result = new RedirectToRouteResult(MVC.Account.Login().GetRouteValueDictionary()); } } applicationUser.AnonymousClientId = clientIdCalculator.GetClientId(filterContext.HttpContext.Request, applicationUser); filterContext.ActionParameters[UserContextKey] = applicationUser; } base.OnActionExecuting(filterContext); }
private int UpdateBoardGameGeekDefinitions(List <BoardGameGeekGameDefinition> boardGameGeekGameDefinitions) { var anonymousUser = new AnonymousApplicationUser(); int totalGamesUpdated = 0; foreach (var existingBoardGameGeekGameDefinition in boardGameGeekGameDefinitions) { //delay between BGG calls to decrease likelyhood of getting blocked by BGG Thread.Sleep(400); var gameDetails = _boardGameGeekApiClient.GetGameDetails(existingBoardGameGeekGameDefinition.Id); if (gameDetails != null) { existingBoardGameGeekGameDefinition.DateUpdated = DateTime.UtcNow; existingBoardGameGeekGameDefinition.AverageWeight = gameDetails.AverageWeight; existingBoardGameGeekGameDefinition.Description = gameDetails.Description; existingBoardGameGeekGameDefinition.MaxPlayTime = gameDetails.MaxPlayTime; existingBoardGameGeekGameDefinition.MinPlayTime = gameDetails.MinPlayTime; existingBoardGameGeekGameDefinition.MaxPlayers = gameDetails.MaxPlayers; existingBoardGameGeekGameDefinition.MinPlayers = gameDetails.MinPlayers; existingBoardGameGeekGameDefinition.Name = gameDetails.Name; existingBoardGameGeekGameDefinition.Thumbnail = gameDetails.Thumbnail; existingBoardGameGeekGameDefinition.Image = gameDetails.Image; existingBoardGameGeekGameDefinition.YearPublished = gameDetails.YearPublished; foreach (var gameCategory in gameDetails.Categories) { if ( existingBoardGameGeekGameDefinition.Categories.All( c => !c.CategoryName.Equals(gameCategory.Category, StringComparison.InvariantCultureIgnoreCase))) { var existentCategory = _dataContext.GetQueryable <BoardGameGeekGameCategory>() .FirstOrDefault( c => c.CategoryName.Equals(gameCategory.Category, StringComparison.InvariantCultureIgnoreCase)); if (existentCategory == null) { existentCategory = new BoardGameGeekGameCategory() { BoardGameGeekGameCategoryId = gameCategory.Id, CategoryName = gameCategory.Category }; } existingBoardGameGeekGameDefinition.Categories.Add(existentCategory); } } foreach (var gameMechanic in gameDetails.Mechanics) { if ( existingBoardGameGeekGameDefinition.Mechanics.All( c => !c.MechanicName.Equals(gameMechanic.Mechanic, StringComparison.InvariantCultureIgnoreCase))) { var existentMechanic = _dataContext.GetQueryable <BoardGameGeekGameMechanic>() .FirstOrDefault( c => c.MechanicName.Equals(gameMechanic.Mechanic, StringComparison.InvariantCultureIgnoreCase)); if (existentMechanic == null) { existentMechanic = new BoardGameGeekGameMechanic() { BoardGameGeekGameMechanicId = gameMechanic.Id, MechanicName = gameMechanic.Mechanic }; } existingBoardGameGeekGameDefinition.Mechanics.Add(existentMechanic); } } _dataContext.Save(existingBoardGameGeekGameDefinition, anonymousUser); if (totalGamesUpdated++ % 10 == 0) { _dataContext.CommitAllChanges(); Console.WriteLine("{0} BoardGameGeekGameDefinitions updated so far...", totalGamesUpdated); } } } _dataContext.CommitAllChanges(); return(totalGamesUpdated); }