public async void ShouldAddProduct() { using (var client = new HttpClient()) { await AccountTestHelpers.LoginAsAdmin(ServiceAddress, $"api/account/login", client); var targetUrl = $"{ServiceAddress}{RootAddressProductController}"; //AƱade nuevo producto de Supplier y Categoria existentes //POST //https:// var response = await client.PostAsync(targetUrl, new StringContent( "{\"Name\":\"Product1\",\"CategoryId\":1,\"Description\":\"Description1\",\"Price\":11.0,\"SupplierId\":1}", Encoding.UTF8, "application/json")); Assert.True(response.IsSuccessStatusCode, response.ReasonPhrase); List <Product> products = await ProductTestHelpers.GetProducts(ServiceAddress, RootAddressProductController); // validate the product was added Assert.Equal(4, products.Count); var product = products[products.Count - 1]; Assert.Equal(4, product.Id); Assert.Equal("Product1", product.Name); await AccountTestHelpers.Logout(ServiceAddress, $"api/account/logout", client); } }
public async void ShouldUpdateProduct() { //Change Product(Price): http://localhost:5001/api/shoppingcart/{customerId}/{productId} HTTPPut using (var client = new HttpClient()) { await AccountTestHelpers.LoginAsAdmin(ServiceAddress, $"api/account/login", client); var product = await ProductTestHelpers.GetProduct(_productId, ServiceAddress, RootAddressProductController); product.Price = 44; var json = JsonConvert.SerializeObject(product); //Actualiza producto //Name = "ReplaceProduct" // PUT https://localhost:5001/api/products/1 var targetUrl = $"{ServiceAddress}{RootAddressProductController}/{_productId}"; var response = await client.PutAsync(targetUrl, new StringContent(json, Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { throw new Exception(response.StatusCode.ToString()); } Assert.True(response.IsSuccessStatusCode); // validate product was updated var updatedProduct = await ProductTestHelpers.GetProduct(_productId, ServiceAddress, RootAddressProductController); Assert.Equal(44, updatedProduct.Price); await AccountTestHelpers.Logout(ServiceAddress, $"api/account/logout", client); } }
public async void ShouldDeleteProduct() { using (var client = new HttpClient()) { await AccountTestHelpers.LoginAsAdmin(ServiceAddress, $"api/account/login", client); var product = await ProductTestHelpers.GetProduct(_productId, ServiceAddress, RootAddressProductController); var targetUrl = $"{ServiceAddress}{RootAddressProductController}/{_orderId}"; var response = await client.DeleteAsJsonAsync(targetUrl, product); Assert.True(response.IsSuccessStatusCode, response.ReasonPhrase); List <Product> products = await ProductTestHelpers.GetProducts(ServiceAddress, RootAddressProductController); Assert.Equal(2, products.Count); await AccountTestHelpers.Logout(ServiceAddress, $"api/account/logout", client); } }