Making the tests robust

And some general cleanup
This commit is contained in:
Praburaj 2015-03-11 14:38:40 -07:00
parent b0c989ce90
commit 39fc5ebdc4
14 changed files with 123 additions and 158 deletions

View File

@ -1,16 +1,14 @@
using System;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Diagnostics.Entity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Authentication;
using Microsoft.Framework.Cache.Memory;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using MusicStore.Models;
using Microsoft.AspNet.Authorization;
namespace MusicStore
{

View File

@ -1,19 +1,17 @@
using System;
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Diagnostics.Entity;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Server.WebListener;
using Microsoft.Framework.Cache.Memory;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using Microsoft.Net.Http.Server;
using MusicStore.Models;
using Microsoft.AspNet.Authorization;
namespace MusicStore
{

View File

@ -1,16 +1,14 @@
using System;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Diagnostics.Entity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Authentication;
using Microsoft.Framework.Cache.Memory;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using MusicStore.Models;
using Microsoft.AspNet.Authorization;
namespace MusicStore
{
@ -64,6 +62,12 @@ namespace MusicStore
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.ConfigureOpenIdConnectAuthentication(options =>
{
options.Authority = "https://login.windows.net/[tenantName].onmicrosoft.com";
options.ClientId = "[ClientId]";
});
// Add MVC services to the services container
services.AddMvc();
@ -114,12 +118,7 @@ namespace MusicStore
app.UseIdentity();
// Create an Azure Active directory application and copy paste the following
// https://github.com/aspnet/Security/issues/113
app.UseOpenIdConnectAuthentication(options =>
{
options.Authority = "https://login.windows.net/[tenantName].onmicrosoft.com";
options.ClientId = "[ClientId]";
});
app.UseOpenIdConnectAuthentication();
// Add MVC to the request pipeline
app.UseMvc(routes =>

View File

@ -302,9 +302,9 @@ namespace E2ETests
private static string SwitchPathToRuntimeFlavor(RuntimeFlavor runtimeFlavor, RuntimeArchitecture runtimeArchitecture, ILogger logger)
{
var currentRuntime = Environment.GetCommandLineArgs().First();
var runtimePath = Environment.GetCommandLineArgs().First();
logger.LogInformation(string.Empty);
logger.LogInformation("Current runtime is : {0}", currentRuntime);
logger.LogInformation("Current runtime path is : {0}", runtimePath);
var replaceStr = new StringBuilder().
Append("dnx").
@ -313,13 +313,21 @@ namespace E2ETests
Append((runtimeArchitecture == RuntimeArchitecture.x86) ? "-x86" : "-x64").
ToString();
currentRuntime = Regex.Replace(currentRuntime, "dnx-(clr|coreclr)-win-(x86|x64)", replaceStr, RegexOptions.IgnoreCase);
currentRuntime = Path.GetDirectoryName(currentRuntime);
runtimePath = Regex.Replace(runtimePath, "dnx-(clr|coreclr)-win-(x86|x64)", replaceStr, RegexOptions.IgnoreCase);
runtimePath = Path.GetDirectoryName(runtimePath);
// Tweak the %PATH% to the point to the right RUNTIMEFLAVOR.
Environment.SetEnvironmentVariable("PATH", currentRuntime + ";" + Environment.GetEnvironmentVariable("PATH"));
Environment.SetEnvironmentVariable("PATH", runtimePath + ";" + Environment.GetEnvironmentVariable("PATH"));
var runtimeName = new DirectoryInfo(currentRuntime).Parent.Name;
var runtimeDirectoryInfo = new DirectoryInfo(runtimePath);
if (!runtimeDirectoryInfo.Exists)
{
throw new Exception(
string.Format("Requested runtime at location '{0}' does not exist. Please make sure it is installed before running test.",
runtimeDirectoryInfo.FullName));
}
var runtimeName = runtimeDirectoryInfo.Parent.Name;
logger.LogInformation(string.Empty);
logger.LogInformation("Changing to use runtime : {runtimeName}", runtimeName);
return runtimeName;

View File

@ -32,7 +32,7 @@ namespace E2ETests
SiteName = "MusicStoreNtlmAuthentication" //This is configured in the NtlmAuthentication.config
};
var testStartTime = DateTime.Now;
var stopwatch = Stopwatch.StartNew();
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
_logger.LogInformation("Pointing MusicStore DB to '{connString}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
@ -53,19 +53,15 @@ namespace E2ETests
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
Helpers.Retry(() =>
{
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds", stopwatch.Elapsed.TotalSeconds);
}, logger: _logger);
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds",
(initializationCompleteTime - testStartTime).TotalSeconds);
VerifyHomePage(response, responseContent, true);
//Check if the user name appears in the page
@ -76,9 +72,8 @@ namespace E2ETests
//Should be able to access the store as the Startup adds necessary permissions for the current user
AccessStoreWithPermissions();
var testCompletionTime = DateTime.Now;
_logger.LogInformation("[Time]: All tests completed in '{t}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds);
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds", (testCompletionTime - testStartTime).TotalSeconds);
stopwatch.Stop();
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds", stopwatch.Elapsed.TotalSeconds);
testSuccessful = true;
}
finally

View File

@ -41,7 +41,7 @@ namespace E2ETests
EnvironmentName = "OpenIdConnectTesting"
};
var testStartTime = DateTime.Now;
var stopwatch = Stopwatch.StartNew();
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
_logger.LogInformation("Pointing MusicStore DB to '{connString}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
@ -68,27 +68,22 @@ namespace E2ETests
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
Helpers.Retry(() =>
{
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds", stopwatch.Elapsed.TotalSeconds);
}, logger: _logger);
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds",
(initializationCompleteTime - testStartTime).TotalSeconds);
VerifyHomePage(response, responseContent);
// OpenIdConnect login.
LoginWithOpenIdConnect();
var testCompletionTime = DateTime.Now;
_logger.LogInformation("[Time]: All tests completed in '{t}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds);
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds", (testCompletionTime - testStartTime).TotalSeconds);
stopwatch.Stop();
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds", stopwatch.Elapsed.TotalSeconds);
testSuccessful = true;
}
finally
@ -103,4 +98,4 @@ namespace E2ETests
}
}
}
}
}

View File

@ -51,7 +51,7 @@ namespace E2ETests
BundleApplicationBeforeStart = true
};
var testStartTime = DateTime.Now;
var stopwatch = Stopwatch.StartNew();
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
_logger.LogInformation("Pointing MusicStore DB to '{connString}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
@ -72,7 +72,6 @@ namespace E2ETests
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
//Add retry logic since tests are flaky on mono due to connection issues
@ -80,12 +79,9 @@ namespace E2ETests
{
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds", stopwatch.Elapsed.TotalSeconds);
}, logger: _logger);
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds",
(initializationCompleteTime - testStartTime).TotalSeconds);
VerifyHomePage(response, responseContent, true);
//Static files are served?
@ -99,9 +95,8 @@ namespace E2ETests
}
}
var testCompletionTime = DateTime.Now;
_logger.LogInformation("[Time]: All tests completed in '{t}' seconds.", (testCompletionTime - initializationCompleteTime).TotalSeconds);
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds.", (testCompletionTime - testStartTime).TotalSeconds);
stopwatch.Stop();
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds.", stopwatch.Elapsed.TotalSeconds);
testSuccessful = true;
}
finally

View File

@ -3,7 +3,6 @@ using System.Diagnostics;
using System.Net.Http;
using Microsoft.AspNet.Testing.xunit;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using Xunit;
namespace E2ETests
@ -105,7 +104,7 @@ namespace E2ETests
EnvironmentName = "SocialTesting"
};
var testStartTime = DateTime.Now;
var stopwatch = Stopwatch.StartNew();
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
_logger.LogInformation("Pointing MusicStore DB to '{connString}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
@ -132,19 +131,15 @@ namespace E2ETests
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
Helpers.Retry(() =>
{
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds", stopwatch.Elapsed.TotalSeconds);
}, logger: _logger);
_logger.LogInformation("[Time]: Approximate time taken for application initialization : '{t}' seconds",
(initializationCompleteTime - testStartTime).TotalSeconds);
VerifyHomePage(response, responseContent);
//Verify the static file middleware can serve static content
@ -228,9 +223,8 @@ namespace E2ETests
//MicrosoftAccountLogin
LoginWithMicrosoftAccount();
var testCompletionTime = DateTime.Now;
_logger.LogInformation("[Time]: All tests completed in '{t}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds);
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds", (testCompletionTime - testStartTime).TotalSeconds);
stopwatch.Stop();
_logger.LogInformation("[Time]: Total time taken for this test variation '{t}' seconds", stopwatch.Elapsed.TotalSeconds);
testSuccessful = true;
}
finally
@ -245,4 +239,4 @@ namespace E2ETests
}
}
}
}
}

View File

@ -1,5 +1,5 @@
using Microsoft.AspNet.Http.Authentication;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Http.Authentication;
using Newtonsoft.Json;
namespace MusicStore.Mocks.Common

View File

@ -2,9 +2,9 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Authentication.Facebook;
using Microsoft.AspNet.Authentication.OAuth;
using Microsoft.AspNet.Identity;
using MusicStore.Mocks.Common;
namespace MusicStore.Mocks.Facebook

View File

@ -2,9 +2,9 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Authentication.Google;
using Microsoft.AspNet.Authentication.OAuth;
using Microsoft.AspNet.Identity;
using MusicStore.Mocks.Common;
namespace MusicStore.Mocks.Google

View File

@ -2,9 +2,9 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Authentication.MicrosoftAccount;
using Microsoft.AspNet.Authentication.OAuth;
using Microsoft.AspNet.Identity;
using MusicStore.Mocks.Common;
namespace MusicStore.Mocks.MicrosoftAccount

View File

@ -9,7 +9,6 @@ using Microsoft.Framework.Cache.Memory;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using MusicStore.Mocks.Common;
using MusicStore.Mocks.OpenIdConnect;
using MusicStore.Models;
@ -53,6 +52,28 @@ namespace MusicStore
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.ConfigureOpenIdConnectAuthentication(options =>
{
options.Authority = "https://login.windows.net/[tenantName].onmicrosoft.com";
options.ClientId = "c99497aa-3ee2-4707-b8a8-c33f51323fef";
options.BackchannelHttpHandler = new OpenIdConnectBackChannelHttpHandler();
options.StringDataFormat = new CustomStringDataFormat();
options.StateDataFormat = new CustomStateDataFormat();
options.TokenValidationParameters.ValidateLifetime = false;
options.ProtocolValidator.RequireNonce = true;
options.ProtocolValidator.NonceLifetime = TimeSpan.FromDays(36500);
options.UseTokenLifetime = false;
options.Notifications = new OpenIdConnectAuthenticationNotifications
{
MessageReceived = OpenIdConnectNotifications.MessageReceived,
AuthorizationCodeReceived = OpenIdConnectNotifications.AuthorizationCodeReceived,
RedirectToIdentityProvider = OpenIdConnectNotifications.RedirectToIdentityProvider,
SecurityTokenReceived = OpenIdConnectNotifications.SecurityTokenReceived,
SecurityTokenValidated = OpenIdConnectNotifications.SecurityTokenValidated
};
});
// Add MVC services to the services container
services.AddMvc();
@ -103,28 +124,7 @@ namespace MusicStore
app.UseIdentity();
// Create an Azure Active directory application and copy paste the following
// https://github.com/aspnet/Security/issues/113
app.UseOpenIdConnectAuthentication(options =>
{
options.Authority = "https://login.windows.net/[tenantName].onmicrosoft.com";
options.ClientId = "c99497aa-3ee2-4707-b8a8-c33f51323fef";
options.BackchannelHttpHandler = new OpenIdConnectBackChannelHttpHandler();
options.StringDataFormat = new CustomStringDataFormat();
options.StateDataFormat = new CustomStateDataFormat();
options.TokenValidationParameters.ValidateLifetime = false;
options.ProtocolValidator.RequireNonce = true;
options.ProtocolValidator.NonceLifetime = TimeSpan.FromDays(36500);
options.UseTokenLifetime = false;
options.Notifications = new OpenIdConnectAuthenticationNotifications
{
MessageReceived = OpenIdConnectNotifications.MessageReceived,
AuthorizationCodeReceived = OpenIdConnectNotifications.AuthorizationCodeReceived,
RedirectToIdentityProvider = OpenIdConnectNotifications.RedirectToIdentityProvider,
SecurityTokenReceived = OpenIdConnectNotifications.SecurityTokenReceived,
SecurityTokenValidated = OpenIdConnectNotifications.SecurityTokenValidated
};
});
app.UseOpenIdConnectAuthentication();
// Add MVC to the request pipeline
app.UseMvc(routes =>

View File

@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication.Facebook;
using Microsoft.AspNet.Authentication.Google;
using Microsoft.AspNet.Authentication.MicrosoftAccount;
@ -9,13 +8,11 @@ using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Diagnostics.Entity;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Identity;
using Microsoft.Framework.Cache.Memory;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using Microsoft.Framework.Runtime;
using MusicStore.Mocks.Common;
using MusicStore.Mocks.Facebook;
@ -89,6 +86,55 @@ namespace MusicStore
options.Scope.Add("user_checkins");
});
services.ConfigureGoogleAuthentication(options =>
{
options.ClientId = "[ClientId]";
options.ClientSecret = "[ClientSecret]";
options.AccessType = "offline";
options.Notifications = new GoogleAuthenticationNotifications()
{
OnAuthenticated = GoogleNotifications.OnAuthenticated,
OnReturnEndpoint = GoogleNotifications.OnReturnEndpoint,
OnApplyRedirect = GoogleNotifications.OnApplyRedirect
};
options.StateDataFormat = new CustomStateDataFormat();
options.BackchannelHttpHandler = new GoogleMockBackChannelHttpHandler();
});
services.ConfigureTwitterAuthentication(options =>
{
options.ConsumerKey = "[ConsumerKey]";
options.ConsumerSecret = "[ConsumerSecret]";
options.Notifications = new TwitterAuthenticationNotifications()
{
OnAuthenticated = TwitterNotifications.OnAuthenticated,
OnReturnEndpoint = TwitterNotifications.OnReturnEndpoint,
OnApplyRedirect = TwitterNotifications.OnApplyRedirect
};
options.StateDataFormat = new CustomTwitterStateDataFormat();
options.BackchannelHttpHandler = new TwitterMockBackChannelHttpHandler();
#if DNX451
options.BackchannelCertificateValidator = null;
#endif
});
services.ConfigureMicrosoftAccountAuthentication(options =>
{
options.Caption = "MicrosoftAccount - Requires project changes";
options.ClientId = "[ClientId]";
options.ClientSecret = "[ClientSecret]";
options.Notifications = new MicrosoftAccountAuthenticationNotifications()
{
OnAuthenticated = MicrosoftAccountNotifications.OnAuthenticated,
OnReturnEndpoint = MicrosoftAccountNotifications.OnReturnEndpoint,
OnApplyRedirect = MicrosoftAccountNotifications.OnApplyRedirect
};
options.BackchannelHttpHandler = new MicrosoftAccountMockBackChannelHandler();
options.StateDataFormat = new CustomStateDataFormat();
options.Scope.Add("wl.basic");
options.Scope.Add("wl.signin");
});
// Add MVC services to the services container
services.AddMvc();
@ -129,26 +175,6 @@ namespace MusicStore
// Configure Session.
app.UseSession();
//To gracefully shutdown the server - Not for production scenarios
app.Map("/shutdown", shutdown =>
{
shutdown.Run(async context =>
{
var appShutdown = context.ApplicationServices.GetService<IApplicationShutdown>();
appShutdown.RequestShutdown();
await Task.Delay(10 * 1000, appShutdown.ShutdownRequested);
if (appShutdown.ShutdownRequested.IsCancellationRequested)
{
await context.Response.WriteAsync("Shutting down gracefully");
}
else
{
await context.Response.WriteAsync("Shutting down token not fired");
}
});
});
//Configure SignalR
app.UseSignalR();
@ -160,54 +186,11 @@ namespace MusicStore
app.UseFacebookAuthentication();
app.UseGoogleAuthentication(options =>
{
options.ClientId = "[ClientId]";
options.ClientSecret = "[ClientSecret]";
options.AccessType = "offline";
options.Notifications = new GoogleAuthenticationNotifications()
{
OnAuthenticated = GoogleNotifications.OnAuthenticated,
OnReturnEndpoint = GoogleNotifications.OnReturnEndpoint,
OnApplyRedirect = GoogleNotifications.OnApplyRedirect
};
options.StateDataFormat = new CustomStateDataFormat();
options.BackchannelHttpHandler = new GoogleMockBackChannelHttpHandler();
});
app.UseGoogleAuthentication();
app.UseTwitterAuthentication(options =>
{
options.ConsumerKey = "[ConsumerKey]";
options.ConsumerSecret = "[ConsumerSecret]";
options.Notifications = new TwitterAuthenticationNotifications()
{
OnAuthenticated = TwitterNotifications.OnAuthenticated,
OnReturnEndpoint = TwitterNotifications.OnReturnEndpoint,
OnApplyRedirect = TwitterNotifications.OnApplyRedirect
};
options.StateDataFormat = new CustomTwitterStateDataFormat();
options.BackchannelHttpHandler = new TwitterMockBackChannelHttpHandler();
#if DNX451
options.BackchannelCertificateValidator = null;
#endif
});
app.UseTwitterAuthentication();
app.UseMicrosoftAccountAuthentication(options =>
{
options.Caption = "MicrosoftAccount - Requires project changes";
options.ClientId = "[ClientId]";
options.ClientSecret = "[ClientSecret]";
options.Notifications = new MicrosoftAccountAuthenticationNotifications()
{
OnAuthenticated = MicrosoftAccountNotifications.OnAuthenticated,
OnReturnEndpoint = MicrosoftAccountNotifications.OnReturnEndpoint,
OnApplyRedirect = MicrosoftAccountNotifications.OnApplyRedirect
};
options.BackchannelHttpHandler = new MicrosoftAccountMockBackChannelHandler();
options.StateDataFormat = new CustomStateDataFormat();
options.Scope.Add("wl.basic");
options.Scope.Add("wl.signin");
});
app.UseMicrosoftAccountAuthentication();
// Add MVC to the request pipeline
app.UseMvc(routes =>
@ -231,4 +214,4 @@ namespace MusicStore
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
}
}
}
}