using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Net; using System.Net.Http; using Xunit; namespace E2ETests { public class SmokeTests { private string ApplicationBaseUrl = null; private const string Connection_string_Format = "Server=(localdb)\\v11.0;Database={0};Trusted_Connection=True;MultipleActiveResultSets=true"; [Theory] [InlineData(HostType.Helios, KreFlavor.DesktopClr, "http://localhost:5001/")] //[InlineData(HostType.SelfHost, KreFlavor.DesktopClr, "http://localhost:5002/")] public void SmokeTestSuite(HostType hostType, KreFlavor kreFlavor, string applicationBaseUrl) { var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty); var musicStoreIdentityDbName = Guid.NewGuid().ToString().Replace("-", string.Empty); Console.WriteLine("Pointing MusicStore DB to '{0}'", string.Format(Connection_string_Format, musicStoreDbName)); Console.WriteLine("Pointing MusicStoreIdentity DB to '{0}'", string.Format(Connection_string_Format, musicStoreIdentityDbName)); Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(Connection_string_Format, musicStoreDbName)); Environment.SetEnvironmentVariable("SQLAZURECONNSTR_IdentityConnection", string.Format(Connection_string_Format, musicStoreIdentityDbName)); ApplicationBaseUrl = applicationBaseUrl; var hostProcess = DeploymentUtility.StartApplication(hostType, kreFlavor); try { var httpClientHandler = new HttpClientHandler(); var httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) }; //Request to base address and check if various parts of the body are rendered VerifyHomePage(httpClient); //Making a request to a protected resource should automatically redirect to login page AccessStoreWithoutPermissions(httpClient); //Register a user - Negative scenario where the Password & ConfirmPassword do not match RegisterUserWithNonMatchingPasswords(httpClient, httpClientHandler); //Register a valid user var generatedUserName = RegisterValidUser(httpClient, httpClientHandler); //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(); try { Console.WriteLine("Trying to drop the databases created during the test run"); using (var conn = new SqlConnection(@"Server=(localdb)\v11.0;Database=master;Trusted_Connection=True;")) { conn.Open(); var cmd = conn.CreateCommand(); cmd.CommandText = string.Format("DROP DATABASE {0}", musicStoreDbName); cmd.ExecuteNonQuery(); cmd = conn.CreateCommand(); cmd.CommandText = string.Format("DROP DATABASE {0}", musicStoreIdentityDbName); cmd.ExecuteNonQuery(); } } catch (Exception exception) { //Ignore if there is failure in cleanup. Console.WriteLine("Error occured while dropping the databases", exception); } } } 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.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("