Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.7 kB
1
Indexable
Never
Controller

using Microsoft.AspNetCore.Mvc;
using Product_Stock_Maintenance.models;
using ProductStockMaintenance.Repo;
namespace ProductStockMaintenance.Controllers;
[ApiController]
    public class LoginController:ControllerBase
    {
      private readonly IRepo _repository;
       
        public LoginController(IRepo repo)
        {
            _repository = repo;
        }
        [HttpPost("/ValidateLoginCredentials")]
        public async Task<IActionResult> Post([FromBody] Logincheck logincheck){
          
        try
          {
                  var result = await _repository.ValidateUser(logincheck.UserName,logincheck.Password);
                  return Ok(result);
        
          }
          catch (Exception)
          {           
              throw ;
          }
        }
    }
   


nunit


// ```csharp
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using Product_Stock_Maintenance.models;
using ProductStockMaintenance.Controllers;
using ProductStockMaintenance.Repo;

namespace UnitTestProject1
{
    [TestFixture]
    public class UnitTest1
    {
        [Test]
        [TestCase("pavan","pavan@123")]
        [TestCase("sjfa","sdfg")]
        [TestCase("bharadwaj","bharadwaj@123")]
        public async Task Test(string Username,string Password)
        {
            //Arrange
            var mockrepo = new Mock<IRepo>();
            var vaildUser = new Login {Username = "bharadwaj" , Password = "bharadwaj@123", AdminAccess = true};
            var vaildAdmin = new Login {Username = "pavan" , Password = "pavan@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");

            //Act
            var LoginController = new LoginController(mockrepo.Object);
            var creds = new Logincheck {UserName = Username , Password = Password};
            var temp = await LoginController.Post(creds);
            var OkResult = (OkObjectResult)temp;
            var Result = OkResult.Value as string;
            //Assert
            if( Result == "Admin"){
                //Console.WriteLine(Username + " " + "AdminLogin");
                Assert.Pass();
            }
            if ( Result == "user" ){
                //Console.WriteLine(Username + " " + "UserLogin");
                Assert.Pass();
            }
            if ( Result == null){
                //Console.WriteLine(Username + "  " +"Not an user");
                Assert.Pass();
            }
            
            else{
                Assert.Fail();
            }
        }
    }
}

// ```
Leave a Comment