Exemplo n.º 1
0
        public async Task <IActionResult> PostResults(ChallengeResultViewModel challengeResultViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(challengeResultViewModel)); // Retry
            }
            var user = await GetCurrentUserAsync();

            var dto = _mapper.Map <ChallengeResultDto>(challengeResultViewModel);

            if (Tempdata != null && Tempdata.ContainsKey(user.Id))
            {
                var tempData = Tempdata[user.Id];
                Tempdata.Remove(user.Id); //Dispose data
                dto.ChallengeId = tempData.Id;
                dto.UserId      = user.Id;
                dto.IsComplete  = true;
            }
            // else return View("/Error"); // TODO unit tests rely on Tempdata

            var isSuccess = await _challengeService.SetResultAsync(dto);

            if (!isSuccess)
            {
                return(StatusCode(500));
            }

            return(RedirectToAction(nameof(Challenges)));
        }
Exemplo n.º 2
0
        /// <exception cref="NotImplementedException">Always.</exception>
        public ActionResult Challenge(string id)
        {
            using (var db = new DatabaseContext())
            {
                var challege = db.ChallengeDb
                               .Include(a => a.Faction)
                               .Include(a => a.Cards)
                               .FirstOrDefault(a => a.Id == id);
                if (challege == null)
                {
                    Response.StatusCode = 404;
                    ViewBag.ErrorCode   = 404;
                    return(View("Error"));
                }
                var cards = new ConcurrentBag <OneCard>();
                challege.Cards.AsParallel().ForAll(a =>
                {
                    cards.Add(new NetrunnerApi().GetCard(a.Code).First());
                });
                var viewModel = new ChallengeResultViewModel {
                    Cards = cards, Challenge = challege
                };

                return(View("Challenge", viewModel));
            }
        }
        public async Task PostResults_ReturnsViewResultAgain_WhenModelStateIsInvalid()
        {
            // Arrange
            var mockUserManager   = new FakeUserManager();
            var mockLoggerFactory = new Mock <ILoggerFactory>();
            var mockLogger        = new Mock <ILogger>();

            mockLoggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>()))
            .Returns(mockLogger.Object);
            var mockMapper           = new Mock <IMapper>();
            var mockChallengeService = new Mock <IChallengeService>();

            var challengeResultViewModel = new ChallengeResultViewModel();

            var challengeController = new ChallengeController(
                mockUserManager,
                mockLoggerFactory.Object,
                mockMapper.Object,
                mockChallengeService.Object
                );

            challengeController.ModelState.AddModelError("", "");

            // Act
            var result = await challengeController.PostResults(challengeResultViewModel) as ViewResult;

            // Assert
            Assert.IsType <ViewResult>(result);
            var model = Assert.IsType <ChallengeResultViewModel>(result.Model);

            Assert.Equal(false, result.ViewData.ModelState.IsValid);
        }
        public async Task PostResults_ReturnsStatusCode500_IfChallengeEmpty()
        {
            // Arrange
            var mockUserManager   = new FakeUserManager();
            var mockLoggerFactory = new Mock <ILoggerFactory>();
            var mockLogger        = new Mock <ILogger>();

            mockLoggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>()))
            .Returns(mockLogger.Object);
            var mockMapper               = new Mock <IMapper>();
            var mockChallengeService     = new Mock <IChallengeService>();
            var challengeResultViewModel = new ChallengeResultViewModel();

            var challengeController = new ChallengeController(
                mockUserManager,
                mockLoggerFactory.Object,
                mockMapper.Object,
                mockChallengeService.Object
                );

            // Act
            var result = await challengeController.PostResults(It.IsAny <int>()) as StatusCodeResult;

            // Assert
            Assert.IsType <StatusCodeResult>(result);
            Assert.Equal(500, result.StatusCode);
        }
        public async Task PostResults_RedirectToAction_WhenDatabaseSuccess()
        {
            // Arrange
            var mockUserManager   = new FakeUserManager();
            var mockLoggerFactory = new Mock <ILoggerFactory>();
            var mockLogger        = new Mock <ILogger>();

            mockLoggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>()))
            .Returns(mockLogger.Object);
            var mockMapper = new Mock <IMapper>();

            mockMapper.Setup(m => m.Map <ChallengeResultDto>(It.IsAny <ChallengeResultViewModel>()))
            .Returns(new ChallengeResultDto())
            .Verifiable();
            var mockChallengeService = new Mock <IChallengeService>();

            mockChallengeService.Setup(c => c.SetResultAsync(It.IsAny <ChallengeResultDto>()))
            .ReturnsAsync(true)
            .Verifiable();
            var challengeResultViewModel = new ChallengeResultViewModel();

            var challengeController = new ChallengeController(
                mockUserManager,
                mockLoggerFactory.Object,
                mockMapper.Object,
                mockChallengeService.Object
                );

            // Act
            var result = await challengeController.PostResults(challengeResultViewModel) as RedirectToActionResult;

            // Assert
            Assert.IsType <RedirectToActionResult>(result);
            Assert.Equal("Challenges", result.ActionName);
        }
        public async Task PostResults_ReturnsViewResult_IfChallengeExists()
        {
            // Arrange
            var mockUserManager   = new FakeUserManager();
            var mockLoggerFactory = new Mock <ILoggerFactory>();
            var mockLogger        = new Mock <ILogger>();

            mockLoggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>()))
            .Returns(mockLogger.Object);
            var challengeResultViewModel = new ChallengeResultViewModel();
            var mockMapper = new Mock <IMapper>();

            mockMapper.Setup(m => m.Map <ChallengeResultViewModel>(It.IsAny <ChallengeDto>()))
            .Returns(challengeResultViewModel)
            .Verifiable();
            var mockChallengeService = new Mock <IChallengeService>();

            mockChallengeService.Setup(c => c.GetChallengeAsync(It.IsAny <int>()))
            .ReturnsAsync(new ChallengeDto())
            .Verifiable();


            var challengeController = new ChallengeController(
                mockUserManager,
                mockLoggerFactory.Object,
                mockMapper.Object,
                mockChallengeService.Object
                );

            // Act
            var result = await challengeController.PostResults(It.IsAny <int>()) as ViewResult;

            // Assert
            Assert.IsType <ViewResult>(result);
            var model = Assert.IsType <ChallengeResultViewModel>(result.Model);
            // Assert.Equal(0, model.Id); TODO test model properties
        }
Exemplo n.º 7
0
        private async Task <ChallengeResultViewModel> PostChallengeAsync(string achievementString)
        {
            ChallengeResultViewModel vm = new ChallengeResultViewModel();

            try
            {
                PostAchievementResult response = await _achievementClient.PostAsync(achievementString);

                if (response != null)
                {
                    switch (response.Status)
                    {
                    case AchievementStatus.Added:
                        vm.result = ChallengeResult.Added;
                        vm.Title  = response.ViewModel.Name;
                        vm.Points = response.ViewModel.Value;
                        break;

                    case AchievementStatus.Duplicate:
                        vm.result = ChallengeResult.Duplicate;
                        vm.Title  = "Duplicate";
                        vm.Points = 0;
                        break;

                    case AchievementStatus.Error:
                        vm.result = ChallengeResult.Error;
                        vm.Title  = "Error";
                        vm.Points = 0;
                        break;

                    case AchievementStatus.NotFound:
                        vm.result = ChallengeResult.NotFound;
                        vm.Title  = "Unrecognised";
                        vm.Points = 0;
                        break;
                    }
                }
                else
                {
                    vm.result = ChallengeResult.Error;
                }
            }
            catch (ApiException e)
            {
                if (e.StatusCode == 401)
                {
                    await App.Current.MainPage.DisplayAlert("Authentication Failure", "Looks like your session has expired. Choose OK to go back to the login screen.", "OK");

                    Application.Current.MainPage = new SSW.Rewards.Views.LoginPage();
                }
                else
                {
                    vm.result = ChallengeResult.Error;
                }
            }
            catch
            {
                vm.result = ChallengeResult.Error;
            }

            return(vm);
        }