using System; using System.Diagnostics; using System.Collections.Generic; using System.Net; using System.Net.Http; using Xunit; namespace E2ETests { public class SmokeTests { private const string Connection_string_Format = "Server=(localdb)\\v11.0;Database={0};Trusted_Connection=True;MultipleActiveResultSets=true"; private string ApplicationBaseUrl; private HttpClient httpClient; private HttpClientHandler httpClientHandler; [Theory] [InlineData(ServerType.Helios, KreFlavor.DesktopClr, "http://localhost:5001/")] [InlineData(ServerType.WebListener, KreFlavor.DesktopClr, "http://localhost:5002/")] [InlineData(ServerType.Kestrel, KreFlavor.DesktopClr, "http://localhost:5004/")] [InlineData(ServerType.Helios, KreFlavor.CoreClr, "http://localhost:5001/")] [InlineData(ServerType.WebListener, KreFlavor.CoreClr, "http://localhost:5002/")] [InlineData(ServerType.Kestrel, KreFlavor.CoreClr, "http://localhost:5004/")] public void SmokeTestSuite(ServerType hostType, KreFlavor kreFlavor, string applicationBaseUrl) { Console.WriteLine("Variation Details : HostType = {0}, KreFlavor = {1}, applicationBaseUrl = {2}", hostType, kreFlavor, applicationBaseUrl); var testStartTime = DateTime.Now; var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty); Console.WriteLine("Pointing MusicStore DB to '{0}'", string.Format(Connection_string_Format, musicStoreDbName)); //Override the connection strings using environment based configuration Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(Connection_string_Format, musicStoreDbName)); ApplicationBaseUrl = applicationBaseUrl; Process hostProcess = null; bool testSuccessful = false; try { hostProcess = DeploymentUtility.StartApplication(hostType, kreFlavor, musicStoreDbName); httpClientHandler = new HttpClientHandler(); httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) }; //Request to base address and check if various parts of the body are rendered & measure the cold startup time. var response = httpClient.GetAsync(string.Empty).Result; var responseContent = response.Content.ReadAsStringAsync().Result; var initializationCompleteTime = DateTime.Now; Console.WriteLine("[Time]: Approximate time taken for application initialization : '{0}' seconds", (initializationCompleteTime - testStartTime).TotalSeconds); VerifyHomePage(response, responseContent); //Verify the static file middleware can serve static content VerifyStaticContentServed(); //Making a request to a protected resource should automatically redirect to login page AccessStoreWithoutPermissions(); //Register a user - Negative scenario where the Password & ConfirmPassword do not match RegisterUserWithNonMatchingPasswords(); //Register a valid user var generatedUserName = RegisterValidUser(); //Register a user - Negative scenario : Trying to register a user name that's already registered. RegisterExistingUser(generatedUserName); //Logout from this user session - This should take back to the home page SignOutUser(generatedUserName); //Sign in scenarios: Invalid password - Expected an invalid user name password error. SignInWithInvalidPassword(generatedUserName, "InvalidPassword~1"); //Sign in scenarios: Valid user name & password. SignInWithUser(generatedUserName, "Password~1"); //Change password scenario ChangePassword(generatedUserName); //SignIn with old password and verify old password is not allowed and new password is allowed SignOutUser(generatedUserName); SignInWithInvalidPassword(generatedUserName, "Password~1"); SignInWithUser(generatedUserName, "Password~2"); //Making a request to a protected resource that this user does not have access to - should automatically redirect to login page again AccessStoreWithoutPermissions(generatedUserName); //Logout from this user session - This should take back to the home page SignOutUser(generatedUserName); //Login as an admin user SignInWithUser("Administrator", "YouShouldChangeThisPassword1!"); //Now navigating to the store manager should work fine as this user has the necessary permission to administer the store. AccessStoreWithPermissions(); //Create an album var albumName = CreateAlbum(); var albumId = FetchAlbumIdFromName(albumName); //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"); var testCompletionTime = DateTime.Now; Console.WriteLine("[Time]: All tests completed in '{0}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds); Console.WriteLine("[Time]: Total time taken for this test variation '{0}' seconds", (testCompletionTime - testStartTime).TotalSeconds); testSuccessful = true; } finally { if (!testSuccessful) { Console.WriteLine("Some tests failed. Proceeding with cleanup."); } if (hostProcess != null && !hostProcess.HasExited) { //Shutdown the host process hostProcess.Kill(); hostProcess.WaitForExit(5 * 1000); if (!hostProcess.HasExited) { Console.WriteLine("Unable to terminate the host process with process Id '{0}", hostProcess.Id); } else { Console.WriteLine("Successfully terminated host process with process Id '{0}'", hostProcess.Id); } } else { Console.WriteLine("Host process already exited or never started successfully."); } DbUtils.DropDatabase(musicStoreDbName); } } private void VerifyStaticContentServed() { Console.WriteLine("Validating if static contents are served.."); Console.WriteLine("Fetching favicon.ico.."); var response = httpClient.GetAsync("/favicon.ico").Result; ThrowIfResponseStatusNotOk(response); Console.WriteLine("Fetching /Content/bootstrap.css.."); response = httpClient.GetAsync("/Content/bootstrap.css").Result; ThrowIfResponseStatusNotOk(response); Console.WriteLine("Verified static contents are served successfully"); } private void VerifyHomePage(HttpResponseMessage response, string responseContent) { Console.WriteLine("Home page content : {0}", responseContent); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Contains("ASP.NET MVC Music Store", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("
  • Home
  • ", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("Store ", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("