Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.5 kB
2
Indexable
Never
 [Test]
        [TestCase("pavan","pavan@123")]
        [TestCase("sjfa","sdfg")]
        [TestCase("bharadwaj","bharadwaj@123")]
        [TestCase("1234", "1234@123")]
        public async Task Test(string Username,string Password)
        {
            try{
            //Arrange
            var Dbcontext = new Mock<TemplateContext>();
            var mockrepo = new Mock<IRepo>(Dbcontext.Object);
            var vaildUser = new Login {AdminId = 1 ,Username = "bharadwaj" , Password = "bharadwaj@123", AdminAccess = true};
            var vaildAdmin = new Login {AdminId = 2,Username = "pavan" , Password = "pavan@123" , AdminAccess = false };
            var invalidLogin = new Login { AdminId = 3, Username = "1234", Password = "1234@123", AdminAccess = false };
            mockrepo.Setup(repo => repo.ValidateUser(vaildAdmin.Username,vaildAdmin.Password)).ReturnsAsync("Admin");
            mockrepo.Setup(repo => repo.ValidateUser(vaildUser.Username,vaildUser.Password)).ReturnsAsync("user");
            mockrepo.Setup(repo => repo.ValidateUser(invalidLogin.Username, invalidLogin.Password)).Throws(new Exception("error"));
            //Act
            var LoginController = new Repo(mockrepo.Object);
            var temp = await LoginController.ValidateUser(Username,Password);
            //Assert
            
            switch(temp){
                case "admin":
                case "users" :
                case null :
                    Assert.Pass();
                    break;
                default:
                    Assert.Fail();
                    break;
            }
        }
        catch(Exception){
            Assert.Pass();
        }
        }

repo

public async Task<string> ValidateUser(string username,string password)
    {

        try
        {
          var  users = await (from data in _context.Logins
                            where data.Username==username && data.Password==password 
                            select data).FirstOrDefaultAsync();
                
                
                if(users != null )
                {
                    return (bool)users.AdminAccess ? "admin" : "users";
                }
                else
                {
                    return "Invalid login Credentials ";
                }
        }

        catch (Exception ex)
        {
          throw new Exception("error occured",ex);
        }
    }
Leave a Comment