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.
This commit is contained in:
Praburaj 2014-07-03 16:05:58 -07:00
parent 3fdb2e3e57
commit 0b0b5837fd
7 changed files with 85 additions and 12 deletions

View File

@ -31,6 +31,7 @@ namespace MusicStore.Controllers
// POST: /Checkout/AddressAndPayment
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddressAndPayment(Order order)
{
var formCollection = await Context.Request.GetFormAsync();

View File

@ -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" };
}
}
}

View File

@ -66,7 +66,7 @@
<Content Include="Views\Shared\_LoginPartial.cshtml" />
<Content Include="Views\ShoppingCart\Index.cshtml" />
<Content Include="Views\StoreManager\Create.cshtml" />
<Content Include="Views\StoreManager\Delete.cshtml" />
<Content Include="Views\StoreManager\RemoveAlbum.cshtml" />
<Content Include="Views\StoreManager\Details.cshtml" />
<Content Include="Views\StoreManager\Edit.cshtml" />
<Content Include="Views\StoreManager\Index.cshtml" />

View File

@ -15,6 +15,7 @@
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<h2>Address And Payment</h2>
<fieldset>
<legend>Shipping Information</legend>

View File

@ -58,7 +58,7 @@
<td>
@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 })
</td>
</tr>
}

View File

@ -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("<a href=\"/StoreManager\">Back to List</a>", 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("<span class=\"glyphicon glyphicon glyphicon-shopping-cart\"></span>", 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<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Checkout/AddressAndPayment")),
new KeyValuePair<string, string>("FirstName", "FirstNameValue"),
new KeyValuePair<string, string>("LastName", "LastNameValue"),
new KeyValuePair<string, string>("Address", "AddressValue"),
new KeyValuePair<string, string>("City", "Redmond"),
new KeyValuePair<string, string>("State", "WA"),
new KeyValuePair<string, string>("PostalCode", "98052"),
new KeyValuePair<string, string>("Country", "USA"),
new KeyValuePair<string, string>("Phone", "PhoneValue"),
new KeyValuePair<string, string>("Email", "email@email.com"),
new KeyValuePair<string, string>("PromoCode", "FREE"),
};
var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/Checkout/AddressAndPayment", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains("<h2>Checkout Complete</h2>", 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<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("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>(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)