コード例 #1
0
        public async Task <ActionResult <Bet> > PlayDice(
            CancellationToken cancellationToken,
            [FromBody] DiceBetValidationModel diceBet,
            [FromServices] IUserService dbUserService,
            [FromServices] IDiceService diceService)
        {
            var userLogin = HttpContext.User.Identity?.Name;

            if (string.IsNullOrEmpty(userLogin))
            {
                return(BadRequest("Invalid user!"));
            }

            var diceRoll = new Random().Next(1, 7);
            var result   = await diceService.GetBetResultAsync(diceRoll, (BetType)diceBet.BetType);

            var bet = new Bet
            {
                Id            = Guid.NewGuid().ToString(),
                BetType       = (BetType)diceBet.BetType,
                DroppedNumber = diceRoll,
                IsBetWon      = result,
                DateTime      = diceBet.DateTime
            };

            await dbUserService.AddBetAsync(userLogin, bet, cancellationToken);

            await dbUserService.AddRecentAsync(userLogin, "original_dice_game", Platform,
                                               cancellationToken);

            return(Ok(bet));
        }
コード例 #2
0
        public async Task Should_Return_BadRequest_When_Login_Null()
        {
            //Arrange
            var cancellationToken = new CancellationToken();
            var platform          = "android";
            var diceBet           = new DiceBetValidationModel {
                BetType = 0
            };
            var login = "******";

            var logger      = new Mock <ILogger <GamesController> >();
            var mapper      = new Mock <IMapper>();
            var rootService = new Mock <IRootService>();
            var userService = new Mock <IUserService>();
            var diceService = new Mock <IDiceService>();

            var controller = new GamesController(logger.Object, mapper.Object, rootService.Object)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                },
                Platform = platform
            };
            //Act
            var result = await controller.PlayDice(cancellationToken, diceBet, userService.Object, diceService.Object);

            //Assert

            for (var i = 1; i <= 6; i++)
            {
                diceService.Verify(d => d.GetBetResultAsync(i, (BetType)diceBet.BetType), Times.Never);
            }

            userService.Verify(u =>
                               u.AddRecentAsync(login, "original_dice_game", platform, cancellationToken), Times.Never);
            var resultBet = Assert.IsType <BadRequestObjectResult>(result.Result);

            Assert.Equal("Invalid user!", resultBet.Value);
        }
コード例 #3
0
        public async Task Should_Return_Ok()
        {
            //Arrange
            var cancellationToken = new CancellationToken();
            var platform          = "android";
            var diceBet           = new DiceBetValidationModel {
                BetType = 0
            };
            var login = "******";

            var logger      = new Mock <ILogger <GamesController> >();
            var mapper      = new Mock <IMapper>();
            var rootService = new Mock <IRootService>();
            var userService = new Mock <IUserService>();
            var diceService = new Mock <IDiceService>();

            for (var i = 1; i <= 6; i++)
            {
                diceService.Setup(d => d.GetBetResultAsync(i, (BetType)diceBet.BetType));
            }

            userService.Setup(u =>
                              u.AddRecentAsync(login, "original_dice_game", platform, cancellationToken));

            var claims = new List <Claim>
            {
                new(ClaimsIdentity.DefaultNameClaimType, login),
            };
            var claimsIdentity = new ClaimsIdentity(claims, "Token", ClaimsIdentity.DefaultNameClaimType,
                                                    ClaimsIdentity.DefaultRoleClaimType);
            var controller = new GamesController(logger.Object, mapper.Object, rootService.Object)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = new ClaimsPrincipal(new List <ClaimsIdentity>()
                        {
                            claimsIdentity
                        }.AsEnumerable())
                    }
                },
                Platform = platform
            };
            //Act
            var result = await controller.PlayDice(cancellationToken, diceBet, userService.Object, diceService.Object);

            //Assert

            for (var i = 1; i <= 6; i++)
            {
                diceService.Verify(d => d.GetBetResultAsync(i, (BetType)diceBet.BetType), Times.AtMostOnce);
            }

            userService.Verify(u =>
                               u.AddRecentAsync(login, "original_dice_game", platform, cancellationToken), Times.Once);
            var resultBet = Assert.IsType <OkObjectResult>(result.Result);
            var bet       = Assert.IsType <Bet>(resultBet.Value);

            Assert.Equal((BetType)diceBet.BetType, bet.BetType);
        }