Skip to content

Commit

Permalink
Added test for [Authenticate(Provider = "...")] usage
Browse files Browse the repository at this point in the history
  • Loading branch information
steff-mueller committed Mar 27, 2013
1 parent ae4423d commit 1310088
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion tests/ServiceStack.WebHost.Endpoints.Tests/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,45 @@ public override void OnAuthenticated(IServiceBase authService, IAuthSession sess
}
}

public class CustomAuthProvider : AuthProvider
{
public CustomAuthProvider()
{
this.Provider = "custom";
}

public override bool IsAuthorized(IAuthSession session, IOAuthTokens tokens, Auth request = null)
{
return false;
}

public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request)
{
throw new NotImplementedException();
}
}

public class RequiresCustomAuth
{
public string Name { get; set; }
}

public class RequiresCustomAuthResponse
{
public string Result { get; set; }

public ResponseStatus ResponseStatus { get; set; }
}

[Authenticate(Provider="custom")]
public class RequiresCustomAuthService : ServiceInterface.Service
{
public RequiresCustomAuthResponse Any(RequiresCustomAuth request)
{
return new RequiresCustomAuthResponse { Result = request.Name };
}
}

public class AuthTests
{
private const string ListeningOn = "http://localhost:82/";
Expand All @@ -145,7 +184,8 @@ public override void Configure(Container container)
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new AuthProvider[] { //Www-Authenticate should contain basic auth, therefore register this provider first
new BasicAuthProvider(), //Sign-in with Basic Auth
new CredentialsAuthProvider() //HTML Form post of UserName/Password credentials
new CredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
new CustomAuthProvider()
}));

container.Register<ICacheClient>(new MemoryCacheClient());
Expand Down Expand Up @@ -236,6 +276,32 @@ public void No_Credentials_throws_UnAuthorized()
}
}

[Test]
public void Authenticate_attribute_respects_provider()
{
try
{
var client = GetClient();
var authResponse = client.Send(new Auth
{
provider = CredentialsAuthProvider.Name,
UserName = "user",
Password = "p@55word",
RememberMe = true,
});

var request = new RequiresCustomAuth { Name = "test" };
var response = client.Send<RequiresCustomAuthResponse>(request);

Assert.Fail("Shouldn't be allowed");
}
catch (WebServiceException webEx)
{
Assert.That(webEx.StatusCode, Is.EqualTo((int)HttpStatusCode.Unauthorized));
Console.WriteLine(webEx.ResponseDto.Dump());
}
}

[Test]
public void PostFile_with_no_Credentials_throws_UnAuthorized()
{
Expand Down

0 comments on commit 1310088

Please sign in to comment.