vendredi 15 mai 2020

ASP.NET Core Identity XUnit and mocking - Login functionnality

I am facing a problem in testing login API, first thing i am creating a fakeUserManager with a fakeUser to set userManager and to instanciate service and Controller then i created a test method to check out if the user exist or not but it cannot find, this is my test class :

public class UserControllerTest : IClassFixture<TestFixture<Startup>>
    {


        private AuthController Controller { get; }

        private IAuthenticationService Service { get; }

        public UserControllerTest(TestFixture<Startup> fixture)
        {
            var users = new List<User>();

            var user = new User();

            user.UserName = "test";
            user.Id = 1000;
            user.Password = "test";

            users.Add(user);

            var fakeUserManager = MockUserManager<User>(users);

            var j = new Mock<IOptions<JwtOptions>>();
            var JwtManager = new Mock<JwtManager>(j.Object);


            Service = new AuthenticationService<User>(JwtManager.Object, fakeUserManager.Object);
            Controller = new AuthController(Service);
        }

        public static Mock<UserManager<TUser>> MockUserManager<TUser>(List<TUser> ls) where TUser : class
        {
            var store = new Mock<IUserStore<TUser>>();
            var mgr = new Mock<UserManager<TUser>>(store.Object, null, null, null, null, null, null, null, null);

            mgr.Object.UserValidators.Add(new UserValidator<TUser>());
            mgr.Object.PasswordValidators.Add(new PasswordValidator<TUser>());

            mgr.Setup(x => x.DeleteAsync(It.IsAny<TUser>())).ReturnsAsync(IdentityResult.Success);
            mgr.Setup(x => x.CreateAsync(It.IsAny<TUser>(), It.IsAny<string>())).ReturnsAsync(IdentityResult.Success).Callback<TUser, string>((x, y) => ls.Add(x));
            mgr.Setup(x => x.UpdateAsync(It.IsAny<TUser>())).ReturnsAsync(IdentityResult.Success);



            return mgr;
        }

        [Theory]
        [InlineData("test", "test")]
        public async Task Login(string password, string name)
        {

            var testUser = new User
            {
                UserName = name,
                Password = password
            };

            var loggedUser = await Controller.Login(testUser);


            var okResult = loggedUser as ObjectResult;
            Assert.Equal(200, okResult.StatusCode);
        }

  }

And here is my authentificationService class :

public class AuthenticationService<TUser> : IAuthenticationService
        where TUser : User, new()
    {
        protected readonly UserManager<TUser> userManager;
        protected readonly JwtManager jwtManager;

        public AuthenticationService(JwtManager jwtManager, UserManager<TUser> userManager)
        { 
            this.userManager = userManager;
            this.jwtManager = jwtManager;
        }

        public async Task<AuthResult<Token>> Login(User loginDto)
        {
            // code returns token in dev
        }
}

Aucun commentaire:

Enregistrer un commentaire