Adding some more test scenarios to MusicStore and moving the scenarios in methods.

This commit is contained in:
Praburaj 2014-05-23 16:11:56 -07:00
parent 8409e9518b
commit 11a71e950d
3 changed files with 244 additions and 112 deletions

View File

@ -23,7 +23,7 @@ namespace MusicStore.Controllers
// //
// GET: /Account/Login // GET: /Account/Login
[AllowAnonymous] [AllowAnonymous]
public IActionResult Login(string returnUrl=null) public IActionResult Login(string returnUrl = null)
{ {
ViewBag.ReturnUrl = returnUrl; ViewBag.ReturnUrl = returnUrl;
return View(); return View();
@ -34,7 +34,7 @@ namespace MusicStore.Controllers
[HttpPost] [HttpPost]
[AllowAnonymous] [AllowAnonymous]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl=null) public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
@ -93,7 +93,7 @@ namespace MusicStore.Controllers
// //
// GET: /Account/Manage // GET: /Account/Manage
public IActionResult Manage(ManageMessageId? message=null) public IActionResult Manage(ManageMessageId? message = null)
{ {
ViewBag.StatusMessage = ViewBag.StatusMessage =
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."

View File

@ -3,10 +3,10 @@
"DefaultAdminPassword": "YouShouldChangeThisPassword1!", "DefaultAdminPassword": "YouShouldChangeThisPassword1!",
"Data": { "Data": {
"DefaultConnection": { "DefaultConnection": {
"Connectionstring": "Server=(localdb)\\v11.0;Database=MusicStore7;Trusted_Connection=True;MultipleActiveResultSets=true" "Connectionstring": "Server=(localdb)\\v11.0;Database=MusicStore;Trusted_Connection=True;MultipleActiveResultSets=true"
}, },
"IdentityConnection": { "IdentityConnection": {
"Connectionstring": "Server=(localdb)\\v11.0;Database=MusicStoreIdentity7;Trusted_Connection=True;MultipleActiveResultSets=true" "Connectionstring": "Server=(localdb)\\v11.0;Database=MusicStoreIdentity;Trusted_Connection=True;MultipleActiveResultSets=true"
} }
} }
} }

View File

@ -8,11 +8,14 @@ namespace E2ETests
{ {
public class SmokeTests public class SmokeTests
{ {
private string ApplicationBaseUrl = null;
[Theory] [Theory]
[InlineData(HostType.Helios, KreFlavor.DesktopClr, "http://localhost:5001/")] [InlineData(HostType.Helios, KreFlavor.DesktopClr, "http://localhost:5001/")]
//[InlineData(HostType.SelfHost, KreFlavor.DesktopClr, "http://localhost:5002/")] //[InlineData(HostType.SelfHost, KreFlavor.DesktopClr, "http://localhost:5002/")]
public void SmokeTestSuite(HostType hostType, KreFlavor kreFlavor, string applicationBaseUrl) public void SmokeTestSuite(HostType hostType, KreFlavor kreFlavor, string applicationBaseUrl)
{ {
ApplicationBaseUrl = applicationBaseUrl;
var hostProcess = DeploymentUtility.StartApplication(hostType, kreFlavor); var hostProcess = DeploymentUtility.StartApplication(hostType, kreFlavor);
try try
@ -21,32 +24,127 @@ namespace E2ETests
var httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) }; var httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) };
//Request to base address and check if various parts of the body are rendered //Request to base address and check if various parts of the body are rendered
var response = httpClient.GetAsync(string.Empty).Result; VerifyHomePage(httpClient);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Home page content : {0}", responseContent);
Assert.Equal<HttpStatusCode>(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("ASP.NET MVC Music Store", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Register", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Login", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("mvcmusicstore.codeplex.com", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Application initialization successful.");
//Making a request to a protected resource should automatically redirect to login page //Making a request to a protected resource should automatically redirect to login page
Console.WriteLine("Trying to access StoreManager without signing in.."); AccessStoreWithoutPermissions(httpClient);
response = httpClient.GetAsync("/StoreManager/").Result;
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains("<h4>Use a local account to log in.</h4>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Equal<string>(applicationBaseUrl + "Account/Login?ReturnUrl=%2FStoreManager%2F", response.RequestMessage.RequestUri.AbsoluteUri);
Console.WriteLine("Redirected to login page as expected.");
//Register a user - Need a way to get the antiforgery token and send it in the request as a form encoded parameter //Register a user - Negative scenario where the Password & ConfirmPassword do not match
response = httpClient.GetAsync("/Account/Register").Result; RegisterUserWithNonMatchingPasswords(httpClient, httpClientHandler);
responseContent = response.Content.ReadAsStringAsync().Result;
var generatedUserName = Guid.NewGuid().ToString().Replace("-", string.Empty); //Register a valid user
Console.WriteLine("Creating a new user with name '{0}'", generatedUserName); var generatedUserName = RegisterValidUser(httpClient, httpClientHandler);
var formParameters = new List<KeyValuePair<string, string>>
//Register a user - Negative scenario : Trying to register a user name that's already registered.
RegisterExistingUser(httpClient, httpClientHandler, generatedUserName);
//Logout from this user session - This should take back to the home page
SignOutUser(httpClient, httpClientHandler, generatedUserName);
//Sign in scenarios: Invalid password - Expected an invalid user name password error.
SignInWithInvalidPassword(httpClient, httpClientHandler, generatedUserName);
//Sign in scenarios: Valid user name & password.
SignInWithUser(httpClient, httpClientHandler, generatedUserName, "Password~1");
//Change password scenario
ChangePassword(httpClient, httpClientHandler, generatedUserName);
//Making a request to a protected resource that this user does not have access to - should automatically redirect to login page again
AccessStoreWithoutPermissions(httpClient, generatedUserName);
//Logout from this user session - This should take back to the home page
SignOutUser(httpClient, httpClientHandler, generatedUserName);
//Login as an admin user
SignInWithUser(httpClient, httpClientHandler, "Administrator", "YouShouldChangeThisPassword1!");
//Now navigating to the store manager should work fine as this user has the necessary permission to administer the store.
AccessStoreWithPermissions(httpClient);
//Create an album
CreateAlbum(httpClient, httpClientHandler);
//Logout from this user session - This should take back to the home page
SignOutUser(httpClient, httpClientHandler, "Administrator");
}
finally
{
//Shutdown the host process
hostProcess.Kill();
}
}
private void VerifyHomePage(HttpClient httpClient)
{
var response = httpClient.GetAsync(string.Empty).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Home page content : {0}", responseContent);
Assert.Equal<HttpStatusCode>(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("ASP.NET MVC Music Store", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<li><a href=\"/\">Home</a></li>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<a class=\"dropdown-toggle\" data-toggle=\"dropdown\">Store <b class=\"caret\"></b></a>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<ul class=\"dropdown-menu\">", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<li class=\"divider\"></li>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<a href=\"/Store/Details/", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Register", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Login", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("mvcmusicstore.codeplex.com", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Application initialization successful.");
}
private void AccessStoreWithoutPermissions(HttpClient httpClient, string generatedUserName = null)
{
Console.WriteLine("Trying to access StoreManager that needs ManageStore claim with the current user : {0}", generatedUserName ?? "Anonymous");
var response = httpClient.GetAsync("/StoreManager/").Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains("<h4>Use a local account to log in.</h4>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Equal<string>(ApplicationBaseUrl + "Account/Login?ReturnUrl=%2FStoreManager%2F", response.RequestMessage.RequestUri.AbsoluteUri);
Console.WriteLine("Redirected to login page as expected.");
}
private void AccessStoreWithPermissions(HttpClient httpClient)
{
Console.WriteLine("Trying to access the store inventory..");
var response = httpClient.GetAsync("/StoreManager/").Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Equal<string>(ApplicationBaseUrl + "StoreManager/", response.RequestMessage.RequestUri.AbsoluteUri);
Console.WriteLine("Successfully acccessed the store inventory");
}
private void RegisterUserWithNonMatchingPasswords(HttpClient httpClient, HttpClientHandler httpClientHandler)
{
Console.WriteLine("Trying to create user with not matching password and confirm password");
var response = httpClient.GetAsync("/Account/Register").Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
var generatedUserName = Guid.NewGuid().ToString().Replace("-", string.Empty);
Console.WriteLine("Creating a new user with name '{0}'", generatedUserName);
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("UserName", generatedUserName),
new KeyValuePair<string, string>("Password", "Password~1"),
new KeyValuePair<string, string>("ConfirmPassword", "Password~2"),
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Register")),
};
var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/Account/Register", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Null(httpClientHandler.CookieContainer.GetCookies(new Uri(ApplicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application"));
Assert.Contains("The password and confirmation password do not match.", responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Server side model validator rejected the user '{0}''s registration as passwords do not match.", generatedUserName);
}
private string RegisterValidUser(HttpClient httpClient, HttpClientHandler httpClientHandler)
{
var response = httpClient.GetAsync("/Account/Register").Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
var generatedUserName = Guid.NewGuid().ToString().Replace("-", string.Empty);
Console.WriteLine("Creating a new user with name '{0}'", generatedUserName);
var formParameters = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("UserName", generatedUserName), new KeyValuePair<string, string>("UserName", generatedUserName),
new KeyValuePair<string, string>("Password", "Password~1"), new KeyValuePair<string, string>("Password", "Password~1"),
@ -54,73 +152,132 @@ namespace E2ETests
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Register")), new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Register")),
}; };
var content = new FormUrlEncodedContent(formParameters.ToArray()); var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/Account/Register", content).Result; response = httpClient.PostAsync("/Account/Register", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result; responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains(string.Format("Hello {0}!", generatedUserName), responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains(string.Format("Hello {0}!", generatedUserName), responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent //Verify cookie sent
Assert.NotNull(httpClientHandler.CookieContainer.GetCookies(new Uri(applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application")); Assert.NotNull(httpClientHandler.CookieContainer.GetCookies(new Uri(ApplicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application"));
Console.WriteLine("Successfully registered user '{0}' and signed in", generatedUserName); Console.WriteLine("Successfully registered user '{0}' and signed in", generatedUserName);
return generatedUserName;
}
//Making a request to a protected resource that this user does not have access to - should automatically redirect to login page again private void RegisterExistingUser(HttpClient httpClient, HttpClientHandler httpClientHandler, string generatedUserName)
Console.WriteLine("Trying to access StoreManager that needs special permissions that {0} does not claim", generatedUserName); {
response = httpClient.GetAsync("/StoreManager/").Result; Console.WriteLine("Trying to register a user with name '{0}' again", generatedUserName);
responseContent = response.Content.ReadAsStringAsync().Result; var response = httpClient.GetAsync("/Account/Register").Result;
Assert.Contains("<h4>Use a local account to log in.</h4>", responseContent, StringComparison.OrdinalIgnoreCase); var responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Equal<string>(applicationBaseUrl + "Account/Login?ReturnUrl=%2FStoreManager%2F", response.RequestMessage.RequestUri.AbsoluteUri); Console.WriteLine("Creating a new user with name '{0}'", generatedUserName);
Console.WriteLine("Redirected to login page as expected."); var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("UserName", generatedUserName),
new KeyValuePair<string, string>("Password", "Password~1"),
new KeyValuePair<string, string>("ConfirmPassword", "Password~1"),
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Register")),
};
//Logout from this user session - This should take back to the home page var content = new FormUrlEncodedContent(formParameters.ToArray());
Console.WriteLine("Signing out from '{0}''s session", generatedUserName); response = httpClient.PostAsync("/Account/Register", content).Result;
formParameters = new List<KeyValuePair<string, string>> responseContent = response.Content.ReadAsStringAsync().Result;
//Bug? Registering the same user again does not throw this error
Assert.Contains(string.Format("Name {0} is already taken.", generatedUserName), responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Identity threw a valid exception that user '{0}' already exists in the system", generatedUserName);
}
private void SignOutUser(HttpClient httpClient, HttpClientHandler httpClientHandler, string generatedUserName)
{
Console.WriteLine("Signing out from '{0}''s session", generatedUserName);
var response = httpClient.GetAsync(string.Empty).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
var formParameters = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/LogOff")), new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/LogOff")),
}; };
content = new FormUrlEncodedContent(formParameters.ToArray()); var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/Account/LogOff", content).Result; response = httpClient.PostAsync("/Account/LogOff", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result; responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains("ASP.NET MVC Music Store", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("ASP.NET MVC Music Store", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Register", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("Register", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Login", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("Login", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("mvcmusicstore.codeplex.com", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("mvcmusicstore.codeplex.com", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie cleared on logout //Verify cookie cleared on logout
Assert.Null(httpClientHandler.CookieContainer.GetCookies(new Uri(applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application")); Assert.Null(httpClientHandler.CookieContainer.GetCookies(new Uri(ApplicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application"));
Console.WriteLine("Successfully signed out of '{0}''s session", generatedUserName); Console.WriteLine("Successfully signed out of '{0}''s session", generatedUserName);
}
//Login as an admin user private void SignInWithInvalidPassword(HttpClient httpClient, HttpClientHandler httpClientHandler, string generatedUserName)
Console.WriteLine("Signing in as '{0}'", "Administrator"); {
response = httpClient.GetAsync("/Account/Login").Result; var response = httpClient.GetAsync("/Account/Login").Result;
responseContent = response.Content.ReadAsStringAsync().Result; var responseContent = response.Content.ReadAsStringAsync().Result;
formParameters = new List<KeyValuePair<string, string>> Console.WriteLine("Signing in with user '{0}'", generatedUserName);
var formParameters = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("UserName", "Administrator"), new KeyValuePair<string, string>("UserName", generatedUserName),
new KeyValuePair<string, string>("Password", "YouShouldChangeThisPassword1!"), new KeyValuePair<string, string>("Password", "InvalidPassword~1"),
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Login")), new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Login")),
}; };
content = new FormUrlEncodedContent(formParameters.ToArray()); var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/Account/Login", content).Result; response = httpClient.PostAsync("/Account/Login", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result; responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains(string.Format("Hello {0}!", "Administrator"), responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("<div class=\"validation-summary-errors\"><ul><li>Invalid username or password.</li>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase); //Verify cookie not sent
Console.WriteLine("Successfully signed in as '{0}'", "Administrator"); Assert.Null(httpClientHandler.CookieContainer.GetCookies(new Uri(ApplicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application"));
Console.WriteLine("Identity successfully prevented an invalid user login.");
}
//Now navigating to the store manager should work fine as this user has the necessary permission to administer the store. private void SignInWithUser(HttpClient httpClient, HttpClientHandler httpClientHandler, string generatedUserName, string password)
Console.WriteLine("Trying to access the store inventory.."); {
response = httpClient.GetAsync("/StoreManager/").Result; var response = httpClient.GetAsync("/Account/Login").Result;
responseContent = response.Content.ReadAsStringAsync().Result; var responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Equal<string>(applicationBaseUrl + "StoreManager/", response.RequestMessage.RequestUri.AbsoluteUri); Console.WriteLine("Signing in with user '{0}'", generatedUserName);
Console.WriteLine("Successfully acccessed the store inventory"); var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("UserName", generatedUserName),
new KeyValuePair<string, string>("Password", password),
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Login")),
};
//Create an album var content = new FormUrlEncodedContent(formParameters.ToArray());
var albumName = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 12); response = httpClient.PostAsync("/Account/Login", content).Result;
Console.WriteLine("Trying to create an album with name '{0}'", albumName); responseContent = response.Content.ReadAsStringAsync().Result;
response = httpClient.GetAsync("/StoreManager/create").Result; Assert.Contains(string.Format("Hello {0}!", generatedUserName), responseContent, StringComparison.OrdinalIgnoreCase);
responseContent = response.Content.ReadAsStringAsync().Result; Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
formParameters = new List<KeyValuePair<string, string>> //Verify cookie sent
Assert.NotNull(httpClientHandler.CookieContainer.GetCookies(new Uri(ApplicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application"));
Console.WriteLine("Successfully signed in with user '{0}'", generatedUserName);
}
private void ChangePassword(HttpClient httpClient, HttpClientHandler httpClientHandler, string generatedUserName)
{
var response = httpClient.GetAsync("/Account/Manage").Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("OldPassword", "Password~1"),
new KeyValuePair<string, string>("NewPassword", "Password~2"),
new KeyValuePair<string, string>("ConfirmPassword", "Password~2"),
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/Manage")),
};
var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/Account/Manage", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains("Your password has been changed.", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.NotNull(httpClientHandler.CookieContainer.GetCookies(new Uri(ApplicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application"));
Console.WriteLine("Successfully changed the password for user '{0}'", generatedUserName);
}
private void CreateAlbum(HttpClient httpClient, HttpClientHandler httpClientHandler)
{
var albumName = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 12);
Console.WriteLine("Trying to create an album with name '{0}'", albumName);
var response = httpClient.GetAsync("/StoreManager/create").Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
var formParameters = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/StoreManager/create")), new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/StoreManager/create")),
new KeyValuePair<string, string>("GenreId", "1"), new KeyValuePair<string, string>("GenreId", "1"),
@ -130,37 +287,12 @@ namespace E2ETests
new KeyValuePair<string, string>("AlbumArtUrl", "TestUrl"), new KeyValuePair<string, string>("AlbumArtUrl", "TestUrl"),
}; };
content = new FormUrlEncodedContent(formParameters.ToArray()); var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/StoreManager/create", content).Result; response = httpClient.PostAsync("/StoreManager/create", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result; responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Equal<string>(applicationBaseUrl + "StoreManager", response.RequestMessage.RequestUri.AbsoluteUri); Assert.Equal<string>(ApplicationBaseUrl + "StoreManager", response.RequestMessage.RequestUri.AbsoluteUri);
Assert.Contains(albumName, responseContent); Assert.Contains(albumName, responseContent);
Console.WriteLine("Successfully created an album with name '{0}' in the store", albumName); Console.WriteLine("Successfully created an album with name '{0}' in the store", albumName);
//Logout from this user session - This should take back to the home page
Console.WriteLine("Signing out of '{0}''s session", "Administrator");
formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/LogOff")),
};
content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("/Account/LogOff", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains("ASP.NET MVC Music Store", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Register", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Login", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("mvcmusicstore.codeplex.com", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie cleared on logout
Assert.Null(httpClientHandler.CookieContainer.GetCookies(new Uri(applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Security.Application"));
Console.WriteLine("Successfully signed out of '{0}''s session", "Administrator");
}
finally
{
//Shutdown the host process
hostProcess.Kill();
}
} }
} }
} }