public async Task Test_Search_Products_By_Criteria()
        {
            var filteringOptions = new List <FilteringOption>()
            {
                new FilteringOption()
                {
                    Field = "name", Operator = FilteringOption.FilteringOperator.Contains, Value = "Camera"
                }
            };
            var sortingOption = new List <SortingOption>()
            {
                new SortingOption()
                {
                    Field = "name", Direction = SortingOption.SortingDirection.ASC, Priority = 1
                }
            };
            var pageSearchArgs = new Core.Pagination.SearchArgs()
            {
                FilteringOptions = filteringOptions, PagingStrategy = PagingStrategy.WithCount, SortingOptions = sortingOption, PageIndex = 1, PageSize = 10
            };
            var searchPageRequest = new PageSearchArgs()
            {
                Args = pageSearchArgs
            };

            var provider     = ClaimsProviderTest.WithAdminClaims();
            var client       = Factory.CreateClientWithTokenAuth(provider);
            var httpResponse = await client.PostAsync("/api/product/search", new StringContent(JsonConvert.SerializeObject(searchPageRequest), Encoding.UTF8, "application/json"));

            httpResponse.EnsureSuccessStatusCode();
            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
        }
        public async Task Test_CreateProduct_WithInvalidProductDetails()
        {
            var provider     = ClaimsProviderTest.WithAdminClaims();
            var client       = Factory.CreateClientWithTokenAuth(provider);
            var httpResponse = await client.PostAsync("/api/product/create", new StringContent(JsonConvert.SerializeObject(new CreateProductCommand()
            {
                CategoryId = 4, UnitPrice = 200
            }), Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
        }
        public async Task Test_CreateUser_WithValidUserDetails()
        {
            var provider     = ClaimsProviderTest.WithAdminClaims();
            var client       = Factory.CreateClientWithTokenAuth(provider);
            var httpResponse = await client.PostAsync("/api/user/create", new StringContent(JsonConvert.SerializeObject(new CreateUserCommand()
            {
                UserName = "******", Password = "******", ConfirmPassword = "******"
            }), Encoding.UTF8, "application/json"));

            httpResponse.EnsureSuccessStatusCode();
            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
        }
        public async Task Test_CreateProduct_WithValidProductDetails()
        {
            var createProductRequest = new CreateProductCommand()
            {
                Code = "P-REDME001", Name = "Redme Note 8 Pro", CategoryId = 4, UnitPrice = 200
            };
            var provider     = ClaimsProviderTest.WithAdminClaims();
            var client       = Factory.CreateClientWithTokenAuth(provider);
            var httpResponse = await client.PostAsync("/api/product/create", new StringContent(JsonConvert.SerializeObject(createProductRequest), Encoding.UTF8, "application/json"));

            httpResponse.EnsureSuccessStatusCode();
            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            var product = JsonConvert.DeserializeObject <ProductResponse>(stringResponse);

            Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
            Assert.True(product.ProductId > 0);
            Assert.True(product.Name == "Redme Note 8 Pro");
            Assert.True(product.Category.CategoryId == 4);
            Assert.True(product.UnitPrice == 200);
        }
        public static HttpClient CreateClientWithTokenAuth <T>(this WebApplicationFactory <T> factory, ClaimsProviderTest claimsProvider) where T : class
        {
            var configuration           = factory.Services.GetRequiredService <IConfiguration>();
            var jwtIssuerOptionsSection = configuration.GetSection(nameof(JwtIssuerOptions));
            var jwtSettings             = jwtIssuerOptionsSection.Get <JwtIssuerOptions>();
            var signingCredentials      = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SigningKey)), SecurityAlgorithms.HmacSha256Signature);
            var token = new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(jwtSettings.Issuer, jwtSettings.Audience, claimsProvider.Claims, null, DateTime.UtcNow.AddMinutes(30), signingCredentials));

            var client = factory.CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(JwtBearerDefaults.AuthenticationScheme, token);
            return(client);
        }