using System;
using System.Collections.Generic;
using System.Linq;
using BVC.SpotBuyCenter.Core.Infrastructure.Database;
using Raven.Abstractions.Data;
using Raven.Client;
using Raven.Client.Indexes;
using ServiceStack.CacheAccess.Providers;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.WebHost.Endpoints.Support;
namespace BVC.SpotBuyCenter.Tests.Integration
{
public abstract class IntegrationBaseTest
{
protected string HostUrl
{
get { return GlobalSetupFixture.HostUrl; }
}
protected HttpListenerBase Host
{
get { return GlobalSetupFixture.Host; }
}
protected IDocumentStore Store
{
get { return GlobalSetupFixture.Store; }
}
protected JsonServiceClient Client
{
get { return GlobalSetupFixture.Client; }
}
protected IUserAuthRepository AuthRepository
{
get { return GlobalSetupFixture.AuthRepository; }
}
protected void Seed(Action<IDocumentSession> seedAction)
{
using (var session = Store.OpenSession())
{
seedAction.Invoke(session);
session.SaveChanges();
}
Store.ClearStaleIndexes();
}
protected void ClearDb()
{
new RavenDocumentsByEntityName().Execute(Store);
Store.ClearStaleIndexes();
Store.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName", new IndexQuery { Query = "Tag: *" });
GlobalSetupFixture.CacheClient.ClearCaches();
}
protected UserAuth Login(string role = null, string email = "testuser@example.com", string password = "testpass")
{
var roles = new List<string>();
if (role != null)
{
roles.Add(role);
}
return Login(roles, email, password);
}
protected UserAuth Login(IEnumerable<string> roles = null, string email = "testuser@example.com", string password = "testpass")
{
var user = new UserAuth
{
Email = email,
Roles = (roles ?? new string[] { }).ToList()
};
AuthRepository.CreateUserAuth(user, password);
Client.Post(new Auth {
provider = CredentialsAuthProvider.Name,
UserName = email,
Password = password
});
return user;
}
}
}