private CartController GetControllerWithNotAuthenticatedUser()
        {
            var testProduct     = DomainTestUtil.GetTestProducts(1)[0];
            var userManagerMock = new Mock <IUserManager>();
            var dataManagerMock = new Mock <IDataManager>();

            dataManagerMock.Setup(x => x.ProductRepository.GetEntity(It.IsAny <Guid>()))
            .Returns(testProduct);

            var identityMock = new Mock <GenericIdentity>("test");

            identityMock.SetupGet(x => x.IsAuthenticated).Returns(false);

            return(new CartController(dataManagerMock.Object, userManagerMock.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext
                    {
                        User = new ClaimsPrincipal(identityMock.Object),
                        Session = new MockHttpSession
                        {
                            ["cart"] = _testJsonString
                        }
                    }
                },
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
Beispiel #2
0
        private ProductController GetControllerWithNotAuthenticatedUser(bool returnsNullProduct = false)
        {
            var userManagerMock       = new Mock <IUserManager>();
            var userManagerHelperMock = new Mock <IUserManagerHelper>();
            var dataManagerMock       = new Mock <IDataManager>();

            dataManagerMock.Setup(x => x.ProductRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => returnsNullProduct ? null : _testProduct);

            dataManagerMock.Setup(x => x.ReviewRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestReviews());

            var identityMock = new Mock <GenericIdentity>("test");

            identityMock.SetupGet(x => x.IsAuthenticated).Returns(false);

            return(new ProductController(dataManagerMock.Object, userManagerMock.Object, userManagerHelperMock.Object)
            {
                TempData = new TempDataDictionary(new DefaultHttpContext(), Mock.Of <ITempDataProvider>()),
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext
                    {
                        User = new ClaimsPrincipal(identityMock.Object),
                    }
                },
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
Beispiel #3
0
        private UsersController GetController(bool creatableUser = true, bool updatableUser = true)
        {
            var userManagerMock       = new Mock <IUserManager>();
            var userManagerHelperMock = new Mock <IUserManagerHelper>();

            userManagerMock.Setup(x => x.GetAllUsers())
            .Returns(_testUsers);
            userManagerMock.Setup(x => x.FindUserById(It.IsAny <Guid>()))
            .Returns <Guid>(a => Task.FromResult(_testUsers[0]));
            userManagerMock.Setup(x => x.FindUserByName(It.IsAny <string>()))
            .Returns <string>(a => Task.FromResult(_testUsers[1]));
            userManagerMock.Setup(x => x.CreateUser(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <Guid>()))
            .Returns <User, string, Guid>((a, b, c) => Task.FromResult(Guid.NewGuid()));
            userManagerMock.Setup(x => x.UpdateUser(It.IsAny <User>()))
            .Returns <User>(a => Task.FromResult(updatableUser));
            userManagerHelperMock.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>()))
            .Returns <ClaimsPrincipal>(a => _testUsers[0].Id);

            var environmentMock = new Mock <IWebHostEnvironment>();

            environmentMock.SetupGet(x => x.WebRootPath).Returns(Directory.GetCurrentDirectory());

            var imageServiceMock = new Mock <IImageService>();

            return(new UsersController(
                       userManagerMock.Object,
                       userManagerHelperMock.Object,
                       environmentMock.Object,
                       imageServiceMock.Object
                       )
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
        private ProductsController GetController()
        {
            var testProduct = DomainTestUtil.GetTestProducts(1)[0];

            testProduct.Category = DomainTestUtil.GetTestCategories(1)[0];
            var dataManagerMock = new Mock <IDataManager>();

            dataManagerMock.Setup(x => x.ProductRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestProducts());
            dataManagerMock.Setup(x => x.ProductRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => testProduct);
            dataManagerMock.Setup(x => x.ProductRepository.SaveEntity(It.IsAny <Product>()))
            .Returns <Product>(a => Guid.NewGuid());
            dataManagerMock.Setup(x => x.CategoryRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestCategories());
            dataManagerMock.Setup(x => x.CategoryRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => DomainTestUtil.GetTestCategories(1)[0]);

            var imageServiceMock = new Mock <IImageService>();
            var environmentMock  = new Mock <IWebHostEnvironment>();

            return(new ProductsController(dataManagerMock.Object, imageServiceMock.Object, environmentMock.Object)
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
        private CartController GetControllerWithAuthenticatedUser()
        {
            var testProduct     = DomainTestUtil.GetTestProducts(1)[0];
            var userManagerMock = new Mock <IUserManager>();
            var dataManagerMock = new Mock <IDataManager>();

            userManagerMock.Setup(x => x.FindUserByName(It.IsAny <string>()))
            .Returns <string>(a => Task.FromResult(DomainTestUtil.GetTestUsers(1)[0]));

            dataManagerMock.Setup(x => x.ProductRepository.GetEntity(It.IsAny <Guid>()))
            .Returns(testProduct);

            dataManagerMock.Setup(x => x.OrderRepository.SaveEntity(It.IsAny <Order>()))
            .Returns <Order>(a => Guid.NewGuid());

            dataManagerMock.Setup(x => x.PurchaseDetailRepository.SaveEntity(It.IsAny <PurchaseDetail>()))
            .Returns <PurchaseDetail>(a => Guid.NewGuid());

            return(new CartController(dataManagerMock.Object, userManagerMock.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext
                    {
                        User = new ClaimsPrincipal(new GenericIdentity("test")),
                        Session = new MockHttpSession
                        {
                            ["cart"] = _testJsonString
                        }
                    }
                },
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
Beispiel #6
0
        private ReviewsController GetController()
        {
            var dataManagerMock = new Mock <IDataManager>();

            dataManagerMock.Setup(x => x.ReviewRepository.GetAllEntities())
            .Returns(_testReviews);
            dataManagerMock.Setup(x => x.ReviewRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => DomainTestUtil.GetTestReviews(1)[0]);
            dataManagerMock.Setup(x => x.ReviewRepository.SaveEntity(It.IsAny <Review>()))
            .Returns <Review>(a => Guid.NewGuid());
            dataManagerMock.Setup(x => x.ProductRepository.GetAllEntities())
            .Returns(_testProducts);

            var userManagerMock = new Mock <IUserManager>();

            userManagerMock.Setup(x => x.GetAllUsers())
            .Returns(DomainTestUtil.GetTestUsers());
            userManagerMock.Setup(x => x.FindUserById(It.IsAny <Guid>()))
            .Returns <Guid>(a => Task.FromResult(_testUser));

            return(new ReviewsController(dataManagerMock.Object, userManagerMock.Object)
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
        private ProfileController GetController(
            bool changeablePassword     = true,
            bool confirmablePhoneNumber = true,
            bool findsByPhoneNumber     = true
            )
        {
            var testUser = DomainTestUtil.GetTestUsers(1)[0];

            var dataManagerMock       = new Mock <IDataManager>();
            var userManagerMock       = new Mock <IUserManager>();
            var userManagerHelperMock = new Mock <IUserManagerHelper>();
            var smsServiceMock        = new Mock <ISmsService>();
            var imageServiceMock      = new Mock <IImageService>();
            var environmentMock       = new Mock <IWebHostEnvironment>();

            dataManagerMock.Setup(x => x.OrderRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestOrders());

            userManagerMock.Setup(x => x.FindUserByName(It.IsAny <string>()))
            .Returns <string>(a => Task.FromResult(testUser));

            userManagerMock.Setup(x => x.FindUserById(It.IsAny <Guid>()))
            .Returns <Guid>(a => Task.FromResult(DomainTestUtil.GetTestUsers(1)[0]));

            userManagerMock.Setup(x => x.FindUserByPhoneNumber(It.IsAny <string>()))
            .Returns <string>(a => Task.FromResult(findsByPhoneNumber ? testUser : null));

            userManagerMock.Setup(x => x.ChangeUserPassword(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns <Guid, string, string>((a, b, c) => Task.FromResult(changeablePassword));

            userManagerMock.Setup(x => x.ConfirmPhoneNumber(It.IsAny <Guid>(), It.IsAny <string>()))
            .Returns <Guid, string>((a, b) => Task.FromResult(confirmablePhoneNumber));

            return(new ProfileController(
                       dataManagerMock.Object,
                       userManagerMock.Object,
                       userManagerHelperMock.Object,
                       smsServiceMock.Object,
                       imageServiceMock.Object,
                       environmentMock.Object
                       )
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object,
                TempData = new TempDataDictionary(new DefaultHttpContext(), Mock.Of <ITempDataProvider>()),
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext
                    {
                        User = new ClaimsPrincipal(new GenericIdentity("test"))
                    }
                }
            });
        }
        private CategoriesController GetController()
        {
            var dataManagerMock = new Mock <IDataManager>();

            dataManagerMock.Setup(x => x.CategoryRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestCategories());
            dataManagerMock.Setup(x => x.CategoryRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => DomainTestUtil.GetTestCategories(1)[0]);
            dataManagerMock.Setup(x => x.CategoryRepository.SaveEntity(It.IsAny <Category>()))
            .Returns <Category>(a => Guid.NewGuid());

            return(new CategoriesController(dataManagerMock.Object)
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
        private AccountController GetControllerWithAuthenticatedUser()
        {
            var userManagerMock       = new Mock <IUserManager>();
            var emailServiceMock      = new Mock <IEmailService>();
            var userManagerHelperMock = new Mock <IUserManagerHelper>();

            return(new AccountController(userManagerMock.Object, userManagerHelperMock.Object, emailServiceMock.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext
                    {
                        User = new ClaimsPrincipal(new GenericIdentity("test"))
                    }
                },
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
        private PurchaseDetailsController GetController()
        {
            var dataManagerMock = new Mock <IDataManager>();

            dataManagerMock.Setup(x => x.PurchaseDetailRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestPurchaseDetails());
            dataManagerMock.Setup(x => x.PurchaseDetailRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => DomainTestUtil.GetTestPurchaseDetails(1)[0]);
            dataManagerMock.Setup(x => x.PurchaseDetailRepository.SaveEntity(It.IsAny <PurchaseDetail>()))
            .Returns <PurchaseDetail>(a => Guid.NewGuid());
            dataManagerMock.Setup(x => x.OrderRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestOrders());
            dataManagerMock.Setup(x => x.ProductRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestProducts());
            dataManagerMock.Setup(x => x.ProductRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => DomainTestUtil.GetTestProducts(1)[0]);

            return(new PurchaseDetailsController(dataManagerMock.Object)
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
Beispiel #11
0
        public void SearchTest()
        {
            var searchViewModel = new SearchViewModel {
                Query = "test", Filter = "test"
            };
            var homeViewModel = new HomeViewModel
            {
                SearchViewModel = searchViewModel
            };
            var mock       = new Mock <IDataManager>();
            var controller = new HomeController(mock.Object)
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            };

            searchViewModel.Query = "test";
            var result = controller.Search(homeViewModel);

            var redirectResult = Assert.IsType <RedirectResult>(result);

            Assert.Equal("/search?query=test&filter=test", redirectResult.Url);
        }
Beispiel #12
0
        private ManagingController GetController()
        {
            var dataManagerMock = new Mock <IDataManager>();

            dataManagerMock.Setup(x => x.OrderRepository.GetAllEntities())
            .Returns(DomainTestUtil.GetTestOrders());
            dataManagerMock.Setup(x => x.OrderRepository.GetEntity(It.IsAny <Guid>()))
            .Returns <Guid>(a => DomainTestUtil.GetTestOrders(1)[0]);

            var notificationHubContextMock = new Mock <IHubContext <NotificationHub> >();
            var userConnectionManagerMock  = new Mock <IUserConnectionManager>();
            var emailServiceMock           = new Mock <IEmailService>();

            return(new ManagingController(
                       dataManagerMock.Object,
                       notificationHubContextMock.Object,
                       userConnectionManagerMock.Object,
                       emailServiceMock.Object
                       )
            {
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object
            });
        }
        private AccountController GetControllerWithNotAuthenticatedUser(
            bool loginSuccess          = false,
            bool externalLoginSuccess  = false,
            bool findsById             = true,
            bool findsByUsername       = false,
            bool findsByEmail          = false,
            bool confirmableEmail      = true,
            bool resetablePassword     = true,
            bool nullExternalLoginInfo = true
            )
        {
            var testUser = DomainTestUtil.GetTestUsers(1)[0];

            var userManagerMock = new Mock <IUserManager>();

            userManagerMock.Setup(x => x.SignIn(It.IsAny <string>(), It.IsAny <string>()))
            .Returns <string, string>((a, b) => Task.FromResult(loginSuccess));
            userManagerMock.Setup(x => x.FindUserById(It.IsAny <Guid>()))
            .Returns <Guid>(a => Task.FromResult(findsById ? testUser : null));
            userManagerMock.Setup(x => x.FindUserByName(It.IsAny <string>()))
            .Returns <string>(a => Task.FromResult(findsByUsername ? testUser : null));
            userManagerMock.Setup(x => x.FindUserByEmail(It.IsAny <string>()))
            .Returns <string>(a => Task.FromResult(findsByEmail ? testUser : null));
            userManagerMock.Setup(x => x.CreateUser(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <Guid>()))
            .Returns <User, string, Guid>((a, b, c) => Task.FromResult(Guid.NewGuid()));
            userManagerMock.Setup(x => x.GenerateEmailConfirmationToken(It.IsAny <Guid>()))
            .Returns <Guid>(a => Task.FromResult(string.Empty));
            userManagerMock.Setup(x => x.ConfirmEmail(It.IsAny <Guid>(), It.IsAny <string>()))
            .Returns <Guid, string>((a, b) => Task.FromResult(confirmableEmail));

            userManagerMock.Setup(x => x.ResetPassword(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns <Guid, string, string>((a, b, c) => Task.FromResult(resetablePassword));

            var emailServiceMock = new Mock <IEmailService>();
            var identityMock     = new Mock <GenericIdentity>("test");

            identityMock.SetupGet(x => x.IsAuthenticated).Returns(false);

            var urlHelperMock = new Mock <IUrlHelper>(MockBehavior.Strict);

            urlHelperMock.Setup(x => x.Action(It.IsAny <UrlActionContext>()))
            .Returns("callbackUrl");

            var externalLoginInfoMock = new Mock <ExternalLoginInfo>(new ClaimsPrincipal(), "test", "test", "test");
            var authSchemeMock        = new Mock <AuthenticationScheme>("test", "test", typeof(IAuthenticationHandler));

            var userManagerHelperMock = new Mock <IUserManagerHelper>();

            userManagerHelperMock.Setup(x => x.GetExternalLoginInfo())
            .Returns(Task.FromResult(nullExternalLoginInfo ? null : externalLoginInfoMock.Object));
            userManagerHelperMock.Setup(x => x.AddExternalLogin(It.IsAny <Guid>(), It.IsAny <ExternalLoginInfo>()))
            .Returns <Guid, ExternalLoginInfo>((a, b) => Task.FromResult(0));
            userManagerHelperMock.Setup(x => x.GetExternalAuthenticationSchemes())
            .Returns(Task.FromResult(new List <AuthenticationScheme> {
                authSchemeMock.Object
            }));
            userManagerHelperMock.Setup(x =>
                                        x.ConfigureExternalAuthenticationProperties(It.IsAny <string>(), It.IsAny <string>()))
            .Returns <string, string>((a, b) => new AuthenticationProperties {
                RedirectUri = "test"
            });
            userManagerHelperMock.Setup(x => x.ExternalLoginSignIn(It.IsAny <ExternalLoginInfo>()))
            .Returns(Task.FromResult(externalLoginSuccess));

            return(new AccountController(userManagerMock.Object, userManagerHelperMock.Object, emailServiceMock.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext
                    {
                        User = new ClaimsPrincipal(identityMock.Object)
                    }
                },
                ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object,
                Url = urlHelperMock.Object
            });
        }