From 0b0b5837fda2b8a263a874e1d3629b6dafa609c0 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Thu, 3 Jul 2014 16:05:58 -0700 Subject: [PATCH] A few changes with this checkin 1. Fixed the delete album action 2. Added a test for add to cart, checkout & delete an album scenarios. With this the scenario set becomes complete. --- .../Controllers/CheckoutController.cs | 1 + .../Controllers/StoreManagerController.cs | 25 ++++--- src/MusicStore/MusicStore.kproj | 2 +- .../Views/Checkout/AddressAndPayment.cshtml | 1 + .../Views/StoreManager/Index.cshtml | 2 +- .../{Delete.cshtml => RemoveAlbum.cshtml} | 0 test/E2ETests/SmokeTests.cs | 66 +++++++++++++++++++ 7 files changed, 85 insertions(+), 12 deletions(-) rename src/MusicStore/Views/StoreManager/{Delete.cshtml => RemoveAlbum.cshtml} (100%) diff --git a/src/MusicStore/Controllers/CheckoutController.cs b/src/MusicStore/Controllers/CheckoutController.cs index 02c6b01eba..f0349e34e7 100644 --- a/src/MusicStore/Controllers/CheckoutController.cs +++ b/src/MusicStore/Controllers/CheckoutController.cs @@ -31,6 +31,7 @@ namespace MusicStore.Controllers // POST: /Checkout/AddressAndPayment [HttpPost] + [ValidateAntiForgeryToken] public async Task AddressAndPayment(Order order) { var formCollection = await Context.Request.GetFormAsync(); diff --git a/src/MusicStore/Controllers/StoreManagerController.cs b/src/MusicStore/Controllers/StoreManagerController.cs index a714c507d2..1f8143f189 100644 --- a/src/MusicStore/Controllers/StoreManagerController.cs +++ b/src/MusicStore/Controllers/StoreManagerController.cs @@ -109,8 +109,8 @@ namespace MusicStore.Controllers } // - // GET: /StoreManager/Delete/5 - public IActionResult Delete(int id = 0) + // GET: /StoreManager/RemoveAlbum/5 + public IActionResult RemoveAlbum(int id = 0) { Album album = db.Albums.Single(a => a.AlbumId == id); if (album == null) @@ -121,13 +121,12 @@ namespace MusicStore.Controllers } // - // POST: /StoreManager/Delete/5 - [HttpPost, ActionName("Delete")] - public IActionResult DeleteConfirmed(int id) + // POST: /StoreManager/RemoveAlbum/5 + [HttpPost, ActionName("RemoveAlbum")] + public IActionResult RemoveAlbumConfirmed(int id) { Album album = db.Albums.Single(a => a.AlbumId == id); - // TODO [EF] Replace with DbSet.Remove when querying attaches instances - db.ChangeTracker.Entry(album).State = EntityState.Deleted; + db.Albums.Remove(album); db.SaveChanges(); return RedirectToAction("Index"); } @@ -136,10 +135,16 @@ namespace MusicStore.Controllers // GET: /StoreManager/GetAlbumIdFromName // Note: Added for automated testing purpose. Application does not use this. [HttpGet] - public int GetAlbumIdFromName(string albumName) + public IActionResult GetAlbumIdFromName(string albumName) { - var album = db.Albums.Single(a => a.Title == albumName); - return album.AlbumId; + var album = db.Albums.Where(a => a.Title == albumName).FirstOrDefault(); + + if (album == null) + { + return new HttpStatusCodeResult(404); + } + + return new ContentResult { Content = album.AlbumId.ToString(), ContentType = "text/plain" }; } } } \ No newline at end of file diff --git a/src/MusicStore/MusicStore.kproj b/src/MusicStore/MusicStore.kproj index ce45ee8fd8..7bfe181973 100644 --- a/src/MusicStore/MusicStore.kproj +++ b/src/MusicStore/MusicStore.kproj @@ -66,7 +66,7 @@ - + diff --git a/src/MusicStore/Views/Checkout/AddressAndPayment.cshtml b/src/MusicStore/Views/Checkout/AddressAndPayment.cshtml index d45711ac3c..2fe088a9a9 100644 --- a/src/MusicStore/Views/Checkout/AddressAndPayment.cshtml +++ b/src/MusicStore/Views/Checkout/AddressAndPayment.cshtml @@ -15,6 +15,7 @@ @using (Html.BeginForm()) { + @Html.AntiForgeryToken()

Address And Payment

Shipping Information diff --git a/src/MusicStore/Views/StoreManager/Index.cshtml b/src/MusicStore/Views/StoreManager/Index.cshtml index 4956c08af2..9215cb846c 100644 --- a/src/MusicStore/Views/StoreManager/Index.cshtml +++ b/src/MusicStore/Views/StoreManager/Index.cshtml @@ -58,7 +58,7 @@ @Html.ActionLink("Edit", "Edit", new { id = item.AlbumId }) | @Html.ActionLink("Details", "Details", new { id = item.AlbumId }) | - @Html.ActionLink("Delete", "Delete", new { id = item.AlbumId }) + @Html.ActionLink("Delete", "RemoveAlbum", new { id = item.AlbumId }) } diff --git a/src/MusicStore/Views/StoreManager/Delete.cshtml b/src/MusicStore/Views/StoreManager/RemoveAlbum.cshtml similarity index 100% rename from src/MusicStore/Views/StoreManager/Delete.cshtml rename to src/MusicStore/Views/StoreManager/RemoveAlbum.cshtml diff --git a/test/E2ETests/SmokeTests.cs b/test/E2ETests/SmokeTests.cs index 2844c86865..ea241f9126 100644 --- a/test/E2ETests/SmokeTests.cs +++ b/test/E2ETests/SmokeTests.cs @@ -105,6 +105,13 @@ namespace E2ETests //Get details of the album VerifyAlbumDetails(albumId, albumName); + //Add an album to cart and checkout the same + AddAlbumToCart(albumId, albumName); + CheckOutCartItems(); + + //Delete the album from store + DeleteAlbum(albumId, albumName); + //Logout from this user session - This should take back to the home page SignOutUser("Administrator"); @@ -407,6 +414,65 @@ namespace E2ETests Assert.Contains("Back to List", responseContent, StringComparison.OrdinalIgnoreCase); } + private void AddAlbumToCart(string albumId, string albumName) + { + Console.WriteLine("Adding album id '{0}' to the cart", albumId); + var response = httpClient.GetAsync(string.Format("/ShoppingCart/AddToCart?id={0}", albumId)).Result; + ThrowIfResponseStatusNotOk(response); + var responseContent = response.Content.ReadAsStringAsync().Result; + Assert.Contains(albumName, responseContent, StringComparison.OrdinalIgnoreCase); + Assert.Contains("", responseContent, StringComparison.OrdinalIgnoreCase); + Console.WriteLine("Verified that album is added to cart"); + } + + private void CheckOutCartItems() + { + Console.WriteLine("Checking out the cart contents..."); + var response = httpClient.GetAsync("/Checkout/AddressAndPayment").Result; + ThrowIfResponseStatusNotOk(response); + var responseContent = response.Content.ReadAsStringAsync().Result; + + var formParameters = new List> + { + new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Checkout/AddressAndPayment")), + new KeyValuePair("FirstName", "FirstNameValue"), + new KeyValuePair("LastName", "LastNameValue"), + new KeyValuePair("Address", "AddressValue"), + new KeyValuePair("City", "Redmond"), + new KeyValuePair("State", "WA"), + new KeyValuePair("PostalCode", "98052"), + new KeyValuePair("Country", "USA"), + new KeyValuePair("Phone", "PhoneValue"), + new KeyValuePair("Email", "email@email.com"), + new KeyValuePair("PromoCode", "FREE"), + }; + + var content = new FormUrlEncodedContent(formParameters.ToArray()); + response = httpClient.PostAsync("/Checkout/AddressAndPayment", content).Result; + responseContent = response.Content.ReadAsStringAsync().Result; + Assert.Contains("

Checkout Complete

", responseContent, StringComparison.OrdinalIgnoreCase); + Assert.StartsWith(ApplicationBaseUrl + "Checkout/Complete/", response.RequestMessage.RequestUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase); + } + + private void DeleteAlbum(string albumId, string albumName) + { + Console.WriteLine("Deleting album '{0}' from the store..", albumName); + + var formParameters = new List> + { + new KeyValuePair("id", albumId) + }; + + var content = new FormUrlEncodedContent(formParameters.ToArray()); + var response = httpClient.PostAsync("/StoreManager/RemoveAlbum", content).Result; + ThrowIfResponseStatusNotOk(response); + + Console.WriteLine("Verifying if the album '{0}' is deleted from store", albumName); + response = httpClient.GetAsync(string.Format("/StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result; + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + Console.WriteLine("Album is successfully deleted from the store.", albumName, albumId); + } + private void ThrowIfResponseStatusNotOk(HttpResponseMessage response) { if (response.StatusCode != HttpStatusCode.OK)