using NUnit.Framework;
using Funq;
using ServiceStack;
using ServiceStack.Text;
using ServiceStack.Auth;
public class Hello : IReturn<StringResponse>
{
public string Name { get; set; }
}
class MyServices : Service
{
public object Any(Hello request) =>
new StringResponse { Result = $"Hello, {request.Name}!" };
}
public class AppHost : AppSelfHostBase
{
public AppHost() : base("ApiKeys Tests", typeof(MyServices).Assembly) {}
public override void Configure(Container container)
{
SetConfig(new HostConfig {
AdminAuthSecret = "secretz"
});
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[]
{
new ApiKeyAuthProvider(AppSettings) {
KeyTypes = new[] { "secret", "publishable" },
RequireSecureConnection = false
}
}));
container.AddSingleton<IAuthRepository>(c => new InMemoryAuthRepository());
}
}
[TestFixture]
public class ApiKeysTests
{
const string BaseUrl = "http://localhost:2000/";
ServiceStackHost appHost;
[OneTimeSetUp]
public void OneTimeSetUp() => appHost = new AppHost()
.Init()
.Start(BaseUrl);
[OneTimeTearDown]
public void OneTimeTearDown() => appHost.Dispose();
[Test]
public void Can_call_RegenerateApiKeys()
{
var client = new JsonServiceClient(BaseUrl);
client.AddAuthSecret("secretz");
var response = client.Post(new RegenerateApiKeys { Environment = "test" });
response.PrintDump();
}
}
Then I was able to run the test successfully:
$ dotnet test
//Output:
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: 396 ms - ApiKeys.dll (net6.0)
Test run for C:\src\wip\tests\ApiKeys\bin\Debug\net472\ApiKeys.dll (.NETFramework,Version=v4.7.2)
Microsoft (R) Test Execution Command Line Tool Version 17.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: 629 ms - ApiKeys.dll (net472)
I think i have found out what was the issue, because i added api key as the last element it wasent working moving as the first on the list now it works.
from this
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[]
{
new CredentialsAuthProvider(AppSettings)
{
SkipPasswordVerificationForInProcessRequests = true
},
new NetCoreIdentityAuthProvider(AppSettings)
{
// Adapter to enable ServiceStack Auth in MVC
AdminRoles = {"Admin"},
},
new JwtAuthProvider(AppSettings)
{
RequireSecureConnection = false,
AuthKeyBase64 = authKey
},
new ApiKeyAuthProvider(AppSettings) {
KeyTypes = new[] { "secret", "publishable" },
RequireSecureConnection = false
},
}));
to this
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[]
{
new ApiKeyAuthProvider(AppSettings) {
KeyTypes = new[] { "secret", "publishable" },
RequireSecureConnection = false
},
new CredentialsAuthProvider(AppSettings)
{
SkipPasswordVerificationForInProcessRequests = true
},
new NetCoreIdentityAuthProvider(AppSettings)
{
// Adapter to enable ServiceStack Auth in MVC
AdminRoles = {"Admin"},
},
new JwtAuthProvider(AppSettings)
{
RequireSecureConnection = false,
AuthKeyBase64 = authKey
},
}));