Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Sep 28, 2023
1 parent cec765c commit 9cccc98
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 41 deletions.
4 changes: 3 additions & 1 deletion src/Ydb.Sdk/src/Auth/StaticCredentialsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class StaticCredentialsProvider : ICredentialsProvider, IUseDriverConfig

private static readonly TimeSpan RefreshGap = TimeSpan.FromMinutes(1);

private const int MaxRetries = 5;
public int MaxRetries = 5;

private readonly object _lock = new();

Expand Down Expand Up @@ -123,7 +123,9 @@ private async Task<TokenData> ReceiveToken()
}

await Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));

++retryAttempt;
_logger.LogInformation($"Failed to fetch token, attempt {retryAttempt}");
}
}
}
Expand Down
95 changes: 55 additions & 40 deletions src/Ydb.Sdk/tests/Auth/TestStaticAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class TestStaticAuth : IDisposable
private readonly ITestOutputHelper _output;
private readonly ILoggerFactory? _loggerFactory;

private readonly Driver _anonDriver;
private readonly TableClient _anonTableClient;

public TestStaticAuth(ITestOutputHelper output)
{
_output = output;
Expand All @@ -23,70 +26,82 @@ public TestStaticAuth(ITestOutputHelper output)
database: "/local"
);

using var anonDriver = new Driver(driverConfig, _loggerFactory);
anonDriver.Initialize().Wait();

using var anonTableClient = new TableClient(anonDriver);
_anonDriver = new Driver(driverConfig, _loggerFactory);
_anonDriver.Initialize().Wait();

Utils.ExecuteSchemeQuery(anonTableClient, "DROP USER IF EXISTS testuser", ensureSuccess: false).Wait();
Utils.ExecuteSchemeQuery(anonTableClient, "CREATE USER testuser PASSWORD 'test_password'").Wait();
Utils.ExecuteSchemeQuery(anonTableClient, "DROP USER IF EXISTS testnopass", ensureSuccess: false).Wait();
Utils.ExecuteSchemeQuery(anonTableClient, "CREATE USER testnopass").Wait();
_anonTableClient = new TableClient(_anonDriver);
}

public void Dispose()
{
_anonTableClient.Dispose();
_anonDriver.Dispose();
GC.SuppressFinalize(this);
}


[Fact]
public async Task GoodAuth()
private async Task DoAuth(string? passwordCreate, string? passwordAuth, int maxRetries = 5)
{
var driverConfig = new DriverConfig(
var anonDriverConfig = new DriverConfig(
endpoint: "grpc://localhost:2136",
database: "/local",
new StaticCredentialsProvider("testuser", "test_password")
database: "/local"
);

await using var driver = await Driver.CreateInitialized(driverConfig, _loggerFactory);
await using var anonDriver = await Driver.CreateInitialized(anonDriverConfig, _loggerFactory);

using var anonTableClient = new TableClient(anonDriver);

using var tableClient = new TableClient(driver);

var response = await Utils.ExecuteDataQuery(tableClient, "SELECT 1");
var row = response.Result.ResultSets[0].Rows[0];
Assert.Equal(1, row[0].GetInt32());
var user = $"test{Guid.NewGuid():n}";

if (passwordCreate is null or "")
{
await Utils.ExecuteSchemeQuery(anonTableClient, $"CREATE USER {user}");
}
else
{
await Utils.ExecuteSchemeQuery(anonTableClient, $"CREATE USER {user} PASSWORD '{passwordCreate}'");
}

try
{
var driverConfig = new DriverConfig(
endpoint: "grpc://localhost:2136",
database: "/local",
new StaticCredentialsProvider(user, passwordAuth){MaxRetries = maxRetries}
);

await using var driver = await Driver.CreateInitialized(driverConfig, _loggerFactory);

using var tableClient = new TableClient(driver);

var response = await Utils.ExecuteDataQuery(tableClient, "SELECT 1");
var row = response.Result.ResultSets[0].Rows[0];
Assert.Equal(1, row[0].GetInt32());
}
finally
{
await Utils.ExecuteSchemeQuery(anonTableClient, $"DROP USER {user}");
}
}


[Fact]
public async Task NoPasswordAuth()
public async Task GoodAuth()
{
var driverConfig = new DriverConfig(
endpoint: "grpc://localhost:2136",
database: "/local",
new StaticCredentialsProvider("testnopass", "")
);

await using var driver = await Driver.CreateInitialized(driverConfig, _loggerFactory);

using var tableClient = new TableClient(driver);
await DoAuth("test_password", "test_password");
}

var response = await Utils.ExecuteDataQuery(tableClient, "SELECT 1");
var row = response.Result.ResultSets[0].Rows[0];
Assert.Equal(1, row[0].GetInt32());
[Fact]
public async Task NoPasswordAuth()
{
await DoAuth(null, null);
}

[Fact]
public async Task WrongPassword()
{
var driverConfig = new DriverConfig(
endpoint: "grpc://localhost:2136",
database: "/local",
new StaticCredentialsProvider("testuser", "wrong")
);

await Assert.ThrowsAsync<InvalidCredentialsException>(
async () => await Driver.CreateInitialized(driverConfig, _loggerFactory));
async () => await DoAuth("good_password", "wrong_password", maxRetries:1));
}

[Fact]
Expand All @@ -95,7 +110,7 @@ public async Task NotExistAuth()
var driverConfig = new DriverConfig(
endpoint: "grpc://localhost:2136",
database: "/local",
new StaticCredentialsProvider("notexists", "nopass")
new StaticCredentialsProvider("notexists", "nopass"){MaxRetries = 1}
);

await Assert.ThrowsAsync<InvalidCredentialsException>(
Expand Down

0 comments on commit 9cccc98

Please sign in to comment.