示例#1
0
        public void GetPrincipalFromExpiredToken_Test()
        {
            IStorage storage = new InMemoryStaticStorage();

            var config = new Mock <IConfiguration>();

            config.SetupGet(x => x["Jwt:Key"]).Returns("9e41f7cd-b71e-4d9c-a6a4-0a5c4aa0f66a");

            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyQGRvbWFpbi5jb20iLCJlbWFpbCI6InVzZXJAZG9tYWluLmNvbSIsImp0aSI6IjM0NjkwOWU3LTNmOTktNGY3Ni1iMjg2LWQ4YzUxNzViZTg2YyIsImV4cCI6MTUzMDU0MjQ5MywiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo2MzM1NC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjYzMzU0LyJ9.1LughzKLTFWyJijEVhtOaT-zAVvKRKU-koNX_Zc095w";

            AuthService svc       = new AuthService(storage, config.Object);
            var         principal = svc.GetPrincipalFromExpiredToken(token);

            Assert.IsNotNull(principal);
        }
        public async Task Register_Ok()
        {
            IStorage storage = new InMemoryStaticStorage();

            User u = new User()
            {
                Email = "*****@*****.**", Password = "******"
            };
            AccountService svc = new AccountService(storage);

            User newUser = await svc.RegisterUserAsync(u);

            Assert.IsNotNull(newUser);
            Assert.IsFalse(newUser.IsEmailConfirmed);
        }
        public async Task RegisterEmailConfirmationCode_Ok()
        {
            IStorage storage = new InMemoryStaticStorage();

            User u = new User()
            {
                Email = "*****@*****.**", Password = "******"
            };
            AccountService svc = new AccountService(storage);

            string code = await svc.RegisterEmailConfirmationCodeAsync(u);

            string res = await storage.GetEmailByConfirmationCodeAsync(code);

            Assert.AreEqual(u.Email, res);
        }
        public async Task FireCannonTest()
        {
            GameState              game;
            FireResult             res1, res2, res3, res4, res5;
            IGameLogic             gameLogic = new GameLogic();
            IStorage               storage   = new InMemoryStaticStorage();
            IOptions <GameOptions> opt       = new Mock <IOptions <GameOptions> >().Object;

            GameService svc = new GameService(storage, gameLogic, null, opt);

            game = await svc.StartNewGameAsync("connectionid");

            //find first ship with 4 cells
            var ship4CellsInfo = game.ServerBoard.Ships[0];
            //find index of fisrt element where cell value is 0
            int emptyIdx = game.ServerBoard.Board.Select((number, index) => new { index, i = number }).Where(n => n.i == 0).FirstOrDefault().index;

            res1 = await svc.FireCannon(game.GameId, ship4CellsInfo.Cells[0]);

            res2 = await svc.FireCannon(game.GameId, ship4CellsInfo.Cells[1]);

            res3 = await svc.FireCannon(game.GameId, ship4CellsInfo.Cells[2]);

            res4 = await svc.FireCannon(game.GameId, ship4CellsInfo.Cells[3]);

            res5 = await svc.FireCannon(game.GameId, emptyIdx);

            Assert.IsTrue(res1.IsHit);
            Assert.IsTrue(game.IsAwaitingServerTurn);
            Assert.IsNull(res1.ShipDestroyed);

            Assert.IsTrue(res2.IsHit);
            Assert.IsNull(res2.ShipDestroyed);

            Assert.IsTrue(res3.IsHit);
            Assert.IsNull(res3.ShipDestroyed);

            Assert.IsTrue(res4.IsHit);
            Assert.IsNotNull(res4.ShipDestroyed);
            Assert.IsFalse(res4.IsGameOver);

            Assert.IsFalse(res5.IsHit);
            Assert.IsNull(res5.ShipDestroyed);
        }
        public async Task StartNewGameTest()
        {
            GameState  dbObj;
            GameState  game;
            IGameLogic gameLogic   = new GameLogic();
            IStorage   storage     = new InMemoryStaticStorage();
            var        optAccessor = new Mock <IOptions <GameOptions> >().Object;

            GameService svc = new GameService(storage, gameLogic, null, optAccessor);

            game = await svc.StartNewGameAsync("connectionid");

            dbObj = await storage.FindActiveGameAsync(game.GameId);

            Assert.IsNotNull(game);
            Assert.IsTrue(game.ServerBoard.Board.Length == 100);
            Assert.IsTrue(game.ClientBoard.Board.Length == 100);
            Assert.IsNull(game.DateEnd);
            Assert.IsNotNull(dbObj);
        }
        public async Task CleanupTest()
        {
            InMemoryStaticStorage s = new InMemoryStaticStorage
            {
                HoursToLive = 0
            };


            //clean results of older runs
            await s.Cleanup();

            for (int i = 0; i < 1000; i++)
            {
                var game = PeerToPeerGameState.CreateNew();
                await s.AddP2PGameAsync(game);
            }

            for (int i = 0; i < 1000; i++)
            {
                var session = new PeerToPeerSessionState()
                {
                    Code = i.ToString(),
                };
                await s.AddP2PSessionAsync(session);
            }

            for (int i = 0; i < 1000; i++)
            {
                var game = new GameState
                {
                    GameId = Guid.NewGuid().ToString()
                };
                await s.AddGameAsync(game);
            }

            await s.Cleanup();

            Assert.AreEqual(0, s.PeerToPeerGamesCount);
            Assert.AreEqual(0, s.PeerToPeerSessionsCount);
            Assert.AreEqual(0, s.GamesCount);
        }
        public async Task GetUserByResetPasswordToken_Ok()
        {
            IStorage storage = new InMemoryStaticStorage();
            await storage.Prepare();

            User u = new User()
            {
                Email = "*****@*****.**", Password = "******"
            };
            AccountService svc = new AccountService(storage);

            string code = await svc.RegisterResetPasswordCodeAsync(u);

            User res = await svc.GetUserByResetPasswordTokenAsync(code);

            //now should return null as code already used
            var code2 = await storage.GetEmailByResetPasswordCodeAsync(code);

            Assert.AreEqual(u.Email, res.Email);
            Assert.IsNull(code2);
        }
        public async Task ConfirmEmail_Ok()
        {
            IStorage storage = new InMemoryStaticStorage();

            User u = new User()
            {
                Email = "*****@*****.**", Password = "******"
            };
            AccountService svc = new AccountService(storage);

            User newUser = await svc.RegisterUserAsync(u);

            var res1 = newUser.IsEmailConfirmed;

            await svc.ConfirmEmailAsync(u);

            var res2 = newUser.IsEmailConfirmed;

            Assert.IsNotNull(newUser);
            Assert.IsFalse(res1);
            Assert.IsTrue(res2);
        }
        public async Task StopGameTest()
        {
            GameState       game;
            IGameLogic      gameLogic   = new GameLogic();
            IStorage        storage     = new InMemoryStaticStorage();
            var             optAccessor = new Mock <IOptions <GameOptions> >().Object;
            List <ShipInfo> ships       = new List <ShipInfo> {
                new ShipInfo(false, new [] { 3, 4, 5, 6 }),
                new ShipInfo(false, new [] { 26, 27, 28 }),
                new ShipInfo(false, new [] { 30, 31, 32 }),
            };

            GameService svc = new GameService(storage, gameLogic, null, optAccessor);

            game = await svc.StartNewGameAsync("connectionid");

            game = await svc.StopGameAsync(game.GameId, ships);


            Assert.IsNotNull(game);
            Assert.IsNotNull(game.DateEnd);
            Assert.IsNotNull(game.ClientBoard.Ships);
            Assert.AreEqual(3, game.ClientBoard.Ships.Count);
        }