Exemplo n.º 1
0
        public void AuthenticatePlayerTest()
        {
            var options = new DbContextOptionsBuilder <VisokiNaponDbContext>()
                          .UseInMemoryDatabase(databaseName: "FakeDataBase")
                          .Options;


            using (var context = new VisokiNaponDbContext(options))
            {
                var service = new PlayerHandler(context);

                // UNEXPECTED NULL
                Assert.False(service.Authenticate(null, "Password123").Result);
                Assert.False(service.Authenticate("username", null).Result);

                // UNREGISTRATED USERNAME
                Assert.False(service.Confirm("username", "Password1234").Result);

                context.Players.Add(new Player()
                {
                    Email        = "*****@*****.**",
                    Name         = "Jovana",
                    Surname      = "Jovanovic",
                    UsernameId   = "jovana",
                    PasswordHash = null,
                    PasswordSalt = null,
                    Verified     = "1234",
                    DateAndTime  = new DateTime(2020, 2, 19)
                });
                context.Players.Add(new Player()
                {
                    Email        = "*****@*****.**",
                    Name         = "Ana",
                    Surname      = "Anic",
                    UsernameId   = "anica",
                    PasswordHash = null,
                    PasswordSalt = null,
                    Verified     = "1234",
                    DateAndTime  = DateTime.Now
                });

                context.SaveChanges();


                // UNCONFIRMED PLAYER  Email : "*****@*****.**", username : "******"
                var player = context.Players.Find("jovana");
                Assert.False(service.Authenticate(player.UsernameId, "Password").Result);
                player = context.Players.Find("jovana");
                Assert.True(player == null);

                // UNCONFIRMED PLAYER, BUT YET HAVE TIME TO CONFIM IT Email : "*****@*****.**", username : "******"
                player = context.Players.Find("anica");
                Assert.False(service.Authenticate(player.UsernameId, "Password").Result);

                // CHECK COMPLETELY VALID CONFIRMATION
                //email: "*****@*****.**", username : "******" password : "******"

                var q = service.Create("*****@*****.**", "milos", "Milos", "Milosevic", "Milos1234", "Milos1234").Result;
                player          = context.Players.Find("milos");
                player.Verified = "1";
                context.Players.Update(player);
                player = context.Players.Find("milos");
                Assert.True(player.PasswordHash != null && player.PasswordSalt != null);
                Assert.True(player.Verified == "1");
                Assert.True(service.Authenticate(player.UsernameId, "Milos1234").Result);

                //CHECK CONFIRMED PLAYER WITH WRONG PASSWORD SUBMITTED
                Assert.False(service.Authenticate(player.UsernameId, "Milos4321").Result);
            }
        }