I've got a class inside a class library that should be used by Windows application nor Web application.
it's defined as
public class CredentialProviderPipelineStep
{
internal static string GetDomainFullName(string friendlyName)
{
var context = new DirectoryContext(DirectoryContextType.Domain, friendlyName);
var domain = Domain.GetDomain(context);
return domain?.Name;
}
public static Task<IDictionary<string, object>> Action(IConfiguration configuration)
{
return Task.Run<IDictionary<string, object>>(() =>
{
var context = new Dictionary<string, object>
{
[Resources.DomainName] = GetDomainFullName(Environment.UserDomainName),
[Resources.DomainUser] = Environment.UserName
};
if (configuration.AppSettings[Resources.SSOApplicationId] == null)
throw new KeyNotFoundException(Resources.SSOApplicationId);
context[Resources.ApplicationId] =
Convert.ToInt32(configuration.AppSettings[Resources.SSOApplicationId]);
return context;
});
}
internal static IDictionary<string, object> GetUserAndDomain()
{
//It's to check if it's a web app, so I've to ask for principal data <see href="http://stackoverflow.com/questions/3179716/how-determine-if-application-is-web-application/"></see>
return Assembly.GetEntryAssembly() == null ? GetUserAndDomainFromWebApp() : GetUserAndDomainFromEnviorment();
}
private static IDictionary<string,object> GetUserAndDomainFromEnviorment()
{
var httpContext = System.Web.HttpContext.Current.User.Identity.Name;
string[] splitted = httpContext.Split('\\');
var result = new Dictionary<string, object>
{
[Resources.DomainName] = GetDomainFullName(Environment.UserDomainName),
[Resources.DomainUser] = Environment.UserName
};
return result;
}
private static IDictionary<string, object> GetUserAndDomainFromWebApp()
{
var result = new Dictionary<string, object>
{
[Resources.DomainName] = GetDomainFullName(Environment.UserDomainName),
[Resources.DomainUser] = Environment.UserName
};
return result;
}
}
The canonical UnitTest (using NUnit) cover the GetUserAndDomainFromEnviorment part , but how can I write a unit test that cover the GetUserAndDomainFromWebApp method? so it has the System.Web.HttpContext.Current.User.Identity.Name filled?
Thanks