Exemplo n.º 1
0
        public TerminiControllerTest()
        {
            #region DBContext
            _context = new MojContext(new DbContextOptionsBuilder <MojContext>()
                                      .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                      .Options);
            #endregion

            #region Identity
            _userManager = new UserManager <Korisnik>(
                new UserStore <Korisnik>(_context),
                null,
                new PasswordHasher <Korisnik>(),
                new List <UserValidator <Korisnik> > {
                new UserValidator <Korisnik>()
            },
                null, null, null, null,
                new Mock <ILogger <UserManager <Korisnik> > >().Object
                );

            _roleManager = new RoleManager <IdentityRole>(
                new RoleStore <IdentityRole>(_context),
                new List <RoleValidator <IdentityRole> > {
                new RoleValidator <IdentityRole>()
            },
                null, null,
                new Mock <ILogger <RoleManager <IdentityRole> > >().Object
                );
            #endregion

            #region ControllerContext
            _context.PopuniPodacimaAsync(_userManager, _roleManager);

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Name, "KorisnikKupac2"),
                new Claim(ClaimTypes.NameIdentifier, "1"),
                new Claim("name", "KorisnikKupac2"),
            };

            var identity        = new ClaimsIdentity(claims, "TestAuthType");
            var claimsPrincipal = new ClaimsPrincipal(identity);

            var mockPrincipal = new Mock <IPrincipal>();
            mockPrincipal.Setup(x => x.Identity).Returns(identity);
            mockPrincipal.Setup(x => x.IsInRole(It.IsAny <string>())).Returns(true);

            var mockHttpContext = new Mock <HttpContext>();
            mockHttpContext.Setup(m => m.User).Returns(claimsPrincipal);

            _controllerContext = new ControllerContext
            {
                HttpContext = mockHttpContext.Object
            };
            #endregion
        }
Exemplo n.º 2
0
        public PredstaveControllerTest()
        {
            #region DBContext

            MojContext Context = new MojContext(new DbContextOptionsBuilder <MojContext>()
                                                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                                .Options);

            #endregion

            #region Identity

            UserManager <Korisnik> UserManager = new UserManager <Korisnik>(
                new UserStore <Korisnik>(Context),
                null,
                new PasswordHasher <Korisnik>(),
                new List <UserValidator <Korisnik> > {
                new UserValidator <Korisnik>()
            },
                null, null, null, null,
                new Mock <ILogger <UserManager <Korisnik> > >().Object
                );

            RoleManager <IdentityRole> RoleManager = new RoleManager <IdentityRole>(
                new RoleStore <IdentityRole>(Context),
                new List <RoleValidator <IdentityRole> > {
                new RoleValidator <IdentityRole>()
            },
                null, null,
                new Mock <ILogger <RoleManager <IdentityRole> > >().Object
                );

            #endregion

            #region ControllerContext

            // ReSharper disable once ConstantConditionalAccessQualifier
            Context?.PopuniPodacimaAsync(UserManager, RoleManager);

            List <Claim> Claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, "KorisnikKupac1"),
                new Claim(ClaimTypes.NameIdentifier, "1"),
                new Claim("name", "KorisnikKupac1")
            };

            ClaimsIdentity  Identity        = new ClaimsIdentity(Claims, "TestAuthType");
            ClaimsPrincipal ClaimsPrincipal = new ClaimsPrincipal(Identity);

            Mock <IPrincipal> MockPrincipal = new Mock <IPrincipal>();
            MockPrincipal.Setup(x => x.Identity).Returns(Identity);
            MockPrincipal.Setup(x => x.IsInRole(It.IsAny <string>())).Returns(true);

            Mock <HttpContext> MockHttpContext = new Mock <HttpContext>();
            MockHttpContext.Setup(m => m.User).Returns(ClaimsPrincipal);

            ControllerContext ControllerContext = new ControllerContext
            {
                HttpContext = MockHttpContext.Object
            };

            #endregion

            #region Controller

            _predstaveController = new PredstaveController(Context)
            {
                ControllerContext = ControllerContext
            };

            #endregion
        }