public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            using (var projectContext = new ProjectContext())
            {
                using (var unitOfWork = new UnitOfWork(projectContext))
                {
                    IdentityUser user = await unitOfWork.Users.FindUser(context.UserName, context.Password);

                    if (user == null)
                    {
                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                        return;
                    }
                }
            }


            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim("sub", context.UserName));
            identity.AddClaim(new Claim("role", "user"));

            context.Validated(identity);

        }
Пример #2
0
 public ProductsController()
 {
     _unitOfWork = new UnitOfWork(new ProjectContext());
     _productRepository = _unitOfWork.Products;
     _categoryRepository = _unitOfWork.Categories;
     _authRepository = _unitOfWork.Users;
 }
Пример #3
0
        public void TestInit()
        {
            var mockSet = new Mock<DbSet<Product>>();
            mockSet.As<IDbAsyncEnumerable<Product>>()
                .Setup(m => m.GetAsyncEnumerator())
                .Returns(new TestDbAsyncEnumerator<Product>(_data.GetEnumerator()));

            mockSet.As<IQueryable<Product>>()
                .Setup(m => m.Provider)
                .Returns(new TestDbAsyncQueryProvider<Product>(_data.Provider));

            mockSet.As<IQueryable<Product>>().Setup(m => m.Expression).Returns(_data.Expression);
            mockSet.As<IQueryable<Product>>().Setup(m => m.ElementType).Returns(_data.ElementType);
            mockSet.As<IQueryable<Product>>().Setup(m => m.GetEnumerator()).Returns(_data.GetEnumerator());

            var mockContext = new Mock<ProjectContext>();
            mockContext.Setup(c => c.Products).Returns(mockSet.Object);
            mockContext.Setup(e => e.Set<Product>()).Returns(mockSet.Object);

            _unitOfWork = new UnitOfWork(mockContext.Object);
        }
Пример #4
0
        public async Task UnitOfWork_GetAllAsync_ReturnProducts()
        {
            var data = new List<Product> 
            { 
                new Product { Name = "BBB" }, 
                new Product { Name = "ZZZ" }, 
                new Product { Name = "AAA" }, 
            }.AsQueryable();

            var mockSet = new Mock<DbSet<Product>>();
            mockSet.As<IDbAsyncEnumerable<Product>>()
                .Setup(m => m.GetAsyncEnumerator())
                .Returns(new TestDbAsyncEnumerator<Product>(data.GetEnumerator()));

            mockSet.As<IQueryable<Product>>()
                .Setup(m => m.Provider)
                .Returns(new TestDbAsyncQueryProvider<Product>(data.Provider));

            mockSet.As<IQueryable<Product>>().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As<IQueryable<Product>>().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As<IQueryable<Product>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

            var mockContext = new Mock<ProjectContext>();
            mockContext.Setup(c => c.Products).Returns(mockSet.Object);
            mockContext.Setup(e => e.Set<Product>()).Returns(mockSet.Object);

            var unitOfWork = new UnitOfWork(mockContext.Object);
            
            var products = await unitOfWork.Products.GetAllAsync();

            var listProducts = products.ToList();
            Assert.AreEqual(3, listProducts.Count);
            Assert.AreEqual(1, listProducts.Count(p=>p.Name=="BBB"));
            Assert.AreEqual(1, listProducts.Count(p => p.Name == "ZZZ"));
            Assert.AreEqual(1, listProducts.Count(p => p.Name == "AAA"));


        }
Пример #5
0
 public void TestInit()
 {
     _unitOfWork = new UnitOfWork(new ProjectContext());
 }
Пример #6
0
 public OrderController()
 {
     _unitOfWork = new UnitOfWork(new ProjectContext());
     _orderRepository = _unitOfWork.Orders;
     _authRepository = _unitOfWork.Users;
 }
Пример #7
0
 public CartController()
 {
     _unitOfWork = new UnitOfWork(new ProjectContext());
     _cartRepository = _unitOfWork.Carts;
     _authRepository = _unitOfWork.Users;
 }
Пример #8
0
 public void TestInit()
 {
     _unitOfWork = new UnitOfWork(new ProjectContext());
     _controller = new CategoriesController();
 }