Skip to content

Commit

Permalink
Boosted Authenticator test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
analogrelay committed Nov 18, 2013
1 parent 7b753e2 commit fe2d154
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/NuGetGallery/Authentication/Providers/Authenticator.cs
Expand Up @@ -61,9 +61,16 @@ public static string GetName(Type authenticator)
internal static IEnumerable<Authenticator> GetAllAvailable()
{
// Find all available auth providers
var configTypes = typeof(ConfigurationService)
return GetAllAvailable(typeof(Authenticator)
.Assembly
.GetExportedTypes()
.GetExportedTypes());
}

internal static IEnumerable<Authenticator> GetAllAvailable(IEnumerable<Type> typesToSearch)
{
// Find all available auth providers
var configTypes =
typesToSearch
.Where(t => !t.IsAbstract && typeof(Authenticator).IsAssignableFrom(t))
.ToList();
var providers = configTypes
Expand All @@ -72,7 +79,7 @@ internal static IEnumerable<Authenticator> GetAllAvailable()
return providers;
}

protected virtual AuthenticatorConfiguration CreateConfigObject()
protected internal virtual AuthenticatorConfiguration CreateConfigObject()
{
return new AuthenticatorConfiguration();
}
Expand All @@ -93,7 +100,7 @@ public abstract class Authenticator<TConfig> : Authenticator
{
public TConfig Config { get; private set; }

protected override AuthenticatorConfiguration CreateConfigObject()
protected internal override AuthenticatorConfiguration CreateConfigObject()
{
Config = new TConfig();
return Config;
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetGallery/Configuration/ConfigurationService.cs
Expand Up @@ -66,7 +66,7 @@ public virtual IAppConfiguration ResolveSettings()
return ResolveConfigObject(new AppConfiguration(), SettingPrefix);
}

public T ResolveConfigObject<T>(T instance, string prefix)
public virtual T ResolveConfigObject<T>(T instance, string prefix)
{
// Iterate over the properties
foreach (var property in GetConfigProperties<T>(instance))
Expand Down
143 changes: 143 additions & 0 deletions tests/NuGetGallery.Facts/Authentication/AuthenticatorFacts.cs
@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NuGetGallery.Authentication.Providers;
using NuGetGallery.Configuration;
using NuGetGallery.Framework;
using Owin;
using Xunit;

namespace NuGetGallery.Authentication
{
public class AuthenticatorFacts
{
public class TheNameProperty
{
[Fact]
public void GivenANonMatchingTypeName_ItUsesTheFullTypeName()
{
Assert.Equal("AMisnamedAuthicator", new AMisnamedAuthicator().Name);
}

[Fact]
public void GivenAMatchingTypeName_ItUsesTheShortenedName()
{
Assert.Equal("ATest", new ATestAuthenticator().Name);
}
}

public class TheConstructor
{
[Fact]
public void DefaultsConfigurationToDisabled()
{
var auther = new ATestAuthenticator();
Assert.NotNull(auther.BaseConfig);
Assert.False(auther.BaseConfig.Enabled);
Assert.Null(auther.BaseConfig.AuthenticationType);
}
}

public class TheStartupMethod : TestContainer
{
[Fact]
public void LoadsConfigFromConfigurationService()
{
// Arrange
var authConfig = new AuthenticatorConfiguration();
GetMock<ConfigurationService>()
.Setup(c => c.ResolveConfigObject(It.IsAny<AuthenticatorConfiguration>(), "Auth.ATest."))
.Returns(authConfig);
var auther = new ATestAuthenticator();

// Act
auther.Startup(Get<ConfigurationService>(), Get<IAppBuilder>());

// Assert
Assert.Same(authConfig, auther.BaseConfig);
}

[Fact]
public void DoesNotAttachToOwinAppIfDisabled()
{
// Arrange
var authConfig = new AuthenticatorConfiguration()
{
Enabled = false
};
GetMock<ConfigurationService>()
.Setup(c => c.ResolveConfigObject(It.IsAny<AuthenticatorConfiguration>(), "Auth.ATest."))
.Returns(authConfig);
var auther = new ATestAuthenticator();

// Act
auther.Startup(Get<ConfigurationService>(), Get<IAppBuilder>());

// Assert
Assert.Null(auther.AttachedTo);
}

[Fact]
public void AttachesToOwinAppIfEnabled()
{
// Arrange
var authConfig = new AuthenticatorConfiguration()
{
Enabled = true
};
GetMock<ConfigurationService>()
.Setup(c => c.ResolveConfigObject(It.IsAny<AuthenticatorConfiguration>(), "Auth.ATest."))
.Returns(authConfig);
var auther = new ATestAuthenticator();

// Act
auther.Startup(Get<ConfigurationService>(), Get<IAppBuilder>());

// Assert
Assert.Same(Get<IAppBuilder>(), auther.AttachedTo);
}
}

public class TheGetAllAvailableMethod
{
[Fact]
public void IgnoresAbstractAndNonAuthenticatorTypes()
{
// Act
var authers = Authenticator.GetAllAvailable(new [] {
typeof(ATestAuthenticator),
typeof(Authenticator),
typeof(TheGetAllAvailableMethod)
}).ToArray();

Assert.Equal(1, authers.Length);
Assert.IsType<ATestAuthenticator>(authers[0]);
}
}

public class TheAuthenticatorOfTCreateConfigObjectOverride
{
[Fact]
public void ReturnsInstanceOfGenericParameter()
{
Assert.IsType<AMisnamedConfig>(new AMisnamedAuthicator().CreateConfigObject());
}
}

private class ATestAuthenticator : Authenticator {
public IAppBuilder AttachedTo { get; private set; }

protected override void AttachToOwinApp(ConfigurationService config, IAppBuilder app)
{
AttachedTo = app;
base.AttachToOwinApp(config, app);
}
}

private class AMisnamedAuthicator : Authenticator<AMisnamedConfig> { }
private class AMisnamedConfig : AuthenticatorConfiguration { }
}
}
1 change: 1 addition & 0 deletions tests/NuGetGallery.Facts/NuGetGallery.Facts.csproj
Expand Up @@ -214,6 +214,7 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="AppConfigIsCorrectlyApplied.cs" />
<Compile Include="Authentication\AuthenticatorFacts.cs" />
<Compile Include="Authentication\Providers\ApiKey\ApiKeyAuthenticationHandlerFacts.cs" />
<Compile Include="Authentication\AuthenticationServiceFacts.cs" />
<Compile Include="Authentication\ForceSslWhenAuthenticatedMiddlewareFacts.cs" />
Expand Down

0 comments on commit fe2d154

Please sign in to comment.