public async Task HttpPost_WhenCorrectToken_And_NullRequestProductsPassed_ReturnsZeroTrollyTotal() { // Arrange string strToken = "e3b5361b-e85c-4c62-85fd-4c9e2af2c053"; Trolly request = FakeTrollyRequest.GetRequest(); // remove request products request.Products = null; string strJson = JsonConvert.SerializeObject(request); var stringContent = new StringContent(strJson, System.Text.Encoding.UTF8, "application/json"); string strError = ""; // Act var response = await httpClient.PostAsync("api/wooliesx/TrollyTotal?token=" + strToken, stringContent); // Assert // shoud return 400 BadRequest // should return error products required var stringResponse = await response.Content.ReadAsStringAsync(); JObject dynJson = (JObject)JsonConvert.DeserializeObject(stringResponse); foreach (var item in dynJson) { JArray jarray = (JArray)item.Value; strError = (string)((JValue)jarray.First).Value; } Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Equal("products required", strError); }
public async Task HttpPost_WhenCorrectToken_And_EqualRequestProductsSpecialsAndQuantitiesPassed_ReturnsOKTrollyTotal() { // Arrange string strToken = "e3b5361b-e85c-4c62-85fd-4c9e2af2c053"; Trolly request = FakeTrollyRequest.GetRequest(); string strJson = JsonConvert.SerializeObject(request); var stringContent = new StringContent(strJson, System.Text.Encoding.UTF8, "application/json"); string strData = ""; // Act var response = await httpClient.PostAsync("api/wooliesx/TrollyTotal?token=" + strToken, stringContent); // Assert // normal price (6*6 + 2*4.5 + 1*3) = 48 // special price 40 // shoud return 200 OK // should return trolly total 40 var stringResponse = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(40.0f, Convert.ToSingle(stringResponse)); }
public void HttpPost_WhenCorrectToken_And_NullRequestProductsPassed_ReturnsZeroTrollyTotal() { // Arrange string strToken = "e3b5361b-e85c-4c62-85fd-4c9e2af2c053"; Trolly request = FakeTrollyRequest.GetRequest(); _mockService.Setup(repo => repo.GetTrollyTotal(request)) .Returns(FakeTrollyRequest.GetTrollyTotal(request)); _controller = new TrollyTotalController(_mockService.Object); // Act // remove request products request.Products = null; _controller = new TrollyTotalController(_mockService.Object); var modelState = _controller.ModelState; var actionResult = _controller.Post(strToken, request); var result = (BadRequestObjectResult)actionResult.Result; // Assert // should return model state error products required // should return trolly total 0 // shoud return 400 BadRequest Assert.Equal("products required", modelState["products"].Errors[0].ErrorMessage); Assert.Equal(0, actionResult.Value); Assert.Equal(400, result.StatusCode); }
public void HttpPost_WhenEmptyTokenPassed_ReturnsZeroTrollyTotal() { // Arrange string strToken = string.Empty; Trolly request = FakeTrollyRequest.GetRequest(); _mockService.Setup(repo => repo.GetTrollyTotal(request)) .Returns(FakeTrollyRequest.GetTrollyTotal(request)); _controller = new TrollyTotalController(_mockService.Object); // Act _controller = new TrollyTotalController(_mockService.Object); var modelState = _controller.ModelState; var actionResult = _controller.Post(strToken, request); var result = (BadRequestObjectResult)actionResult.Result; // Assert // should return model state error token is required // should return trolly total 0 // shoud return 400 BadRequest Assert.Equal("token is required", modelState["token"].Errors[0].ErrorMessage); Assert.Equal(0, actionResult.Value); Assert.Equal(400, result.StatusCode); }
public void HttpPost_WhenCorrectToken_And_NotEqualRequestProductsAndQuantitiesPassed_ReturnsZeroTrollyTotal() { // Arrange string strToken = "e3b5361b-e85c-4c62-85fd-4c9e2af2c053"; Trolly request = FakeTrollyRequest.GetRequest(); _mockService.Setup(repo => repo.GetTrollyTotal(request)) .Returns(FakeTrollyRequest.GetTrollyTotal(request)); _controller = new TrollyTotalController(_mockService.Object); // Act // add request product to make products and quantities not equal request.Products.Add(new TrollyProduct { Name = "Additional Product", Price = 100.00f }); _controller = new TrollyTotalController(_mockService.Object); var modelState = _controller.ModelState; var actionResult = _controller.Post(strToken, request); var result = (BadRequestObjectResult)actionResult.Result; // Assert // should return model state error same number of products and quantities required // should return trolly total 0 // shoud return 400 BadRequest Assert.Equal("same number of products and quantities required", modelState["mismatch"].Errors[0].ErrorMessage); Assert.Equal(0, actionResult.Value); Assert.Equal(400, result.StatusCode); }
public void HttpPost_WhenCorrectToken_And_EqualRequestProductsSpecialsAndQuantitiesPassed_ReturnsOKTrollyTotal() { // Arrange string strToken = "e3b5361b-e85c-4c62-85fd-4c9e2af2c053"; Trolly request = FakeTrollyRequest.GetRequest(); _mockService.Setup(repo => repo.GetTrollyTotal(request)) .Returns(FakeTrollyRequest.GetTrollyTotal(request)); _controller = new TrollyTotalController(_mockService.Object); // Act _controller = new TrollyTotalController(_mockService.Object); var actionResult = _controller.Post(strToken, request); var result = (OkObjectResult)actionResult.Result; // Assert // normal price (6*6 + 2*4.5 + 1*3) = 48 // special price 40 // shoud return 200 OK // should return trolly total 40 Assert.Equal(200, result.StatusCode); Assert.Equal(40, (float)result.Value); }