Skip to content

Instantly share code, notes, and snippets.

@wayne-o
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wayne-o/6e00fdc43a3a204a8bba to your computer and use it in GitHub Desktop.
Save wayne-o/6e00fdc43a3a204a8bba to your computer and use it in GitHub Desktop.
namespace Sonatribe.Tests.RegressionTests
{
public class SelfHostedBaseTest
{
protected IServiceClient Client;
protected AuthAppHostHttpListener AppHost;
protected virtual string ListeningOn
{
get { return "http://localhost:82/"; }
}
protected virtual string WebHostUrl
{
get { return "http://some.api.sonatribe.com"; }
}
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
AppHost = new AuthAppHostHttpListener(WebHostUrl, Configure);
AppHost.Init();
AppHost.Start(ListeningOn);
//other set up stuff
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
AppHost.Dispose();
}
protected IServiceClient GetClient()
{
return new JsonServiceClient(ListeningOn)
{
UserName = "xxxxx",
Password = "xxxxx"
};
}
public virtual void Configure(Container container)
{
container.Register<IServiceClient>(Client);
}
public class AuthAppHostHttpListener : AppHostHttpListenerBase
{
private readonly string _webHostUrl;
private Action<Container> _configureFn;
public AuthAppHostHttpListener(string webHostUrl, Action<Container> configureFn = null)
: base("Validation Tests", typeof(OneOfYourServices).Assembly)
{
_webHostUrl = webHostUrl;
_configureFn = configureFn;
}
public override void Configure(Funq.Container container)
{
JsConfig.EmitCamelCaseNames = true;
ConfigureAuth(container);
ConfigureContainer(container);
InstallRaven(container);
}
public void InstallRaven(Funq.Container container)
{
var store = new EmbeddableDocumentStore
{
Configuration =
{
RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true,
RunInMemory = true
}
}.Initialize();
//you can switch to a live/deployed API here if that tickles your fancy
//var store = new DocumentStore()
//{
// ConnectionStringName = "SonatribeConnectionString"
//}.Initialize();
store.Conventions.IdentityPartsSeparator = "-";
container.Register<IDocumentStore>(store);
container.Register<IDocumentSession>(c => c.Resolve<IDocumentStore>().OpenSession()).ReusedWithin(ReuseScope.Request);
}
public static void DoInitialisation(IDocumentStore store)
{
store.Initialize();
store.Conventions.IdentityPartsSeparator = "-";
store.Conventions.DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite;
//install transformers and indexes here
}
private void ConfigureContainer(Container container)
{
container.Register(c => c.Resolve<IDocumentStore>().OpenSession()).ReusedWithin(ReuseScope.Default);
container.Register(c => c.Resolve<IDocumentStore>().OpenAsyncSession()).ReusedWithin(ReuseScope.Default);
//configure the dependancies that are needed by your services here
//this is generally the same set up as your actual service host
}
private void ConfigureAuth(Container container)
{
var appSettings = new AppSettings();
SetConfig(new HostConfig
{
WebHostUrl = _webHostUrl,
StripApplicationVirtualPath = true,
DebugMode = true,
GlobalResponseHeaders =
{
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"},
{"Access-Control-Allow-Headers", "Content-Type"},
}
});
Plugins.Add(new AuthFeature(
() => new CustomUserSession(),
new IAuthProvider[]
{
new CredentialsAuthProvider(),
new TwitterAuthProvider(appSettings),
new FacebookAuthProvider(appSettings),
new DigestAuthProvider(appSettings),
new BasicAuthProvider()
}) { HtmlRedirect = null });
container.Register<IAuthRepository>(
c => new RavenDbUserAuthRepository(container.Resolve<IDocumentStore>()));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment