Skip to content

Commit

Permalink
Decompose CheckAuth method
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Oct 3, 2023
1 parent 98473e8 commit add7151
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/Ydb.Sdk/tests/Auth/TestStaticAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,26 @@ private async Task DropUser(TableClient tableClient, string user)
_logger.LogInformation($"User {user} dropped successfully");
}

private async Task DoAuth(string? passwordCreate, string? passwordAuth, int maxRetries = 5)
private async Task Authorize(string user, string? password, int maxRetries)
{
var driverConfig = new DriverConfig(
endpoint: "grpc://localhost:2136",
database: "/local",
new StaticCredentialsProvider(user, password, _loggerFactory) { MaxRetries = maxRetries }
);

_logger.LogInformation($"DriverConfig for {user} created");

await using var driver = await Driver.CreateInitialized(driverConfig, _loggerFactory);
_logger.LogInformation($"Driver for {user} created and initialized");
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());
}

private async Task CheckAuth(string? passwordCreate, string? passwordAuth, int maxRetries = 5)
{
_logger.LogInformation("Creating anon driver");
var anonDriverConfig = new DriverConfig(
Expand All @@ -60,21 +79,7 @@ private async Task DoAuth(string? passwordCreate, string? passwordAuth, int maxR

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

_logger.LogInformation($"DriverConfig for {user} created");

await using var driver = await Driver.CreateInitialized(driverConfig, _loggerFactory);
_logger.LogInformation($"Driver for {user} created and initialized");
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());
await Authorize(user, passwordAuth, maxRetries);
}
finally
{
Expand All @@ -86,20 +91,20 @@ private async Task DoAuth(string? passwordCreate, string? passwordAuth, int maxR
[Fact]
public async Task GoodAuth()
{
await DoAuth("test_password", "test_password");
await CheckAuth("test_password", "test_password");
}

[Fact]
public async Task NoPasswordAuth()
{
await DoAuth(null, null);
await CheckAuth(null, null);
}

[Fact]
public async Task WrongPassword()
{
await Assert.ThrowsAsync<InvalidCredentialsException>(
async () => await DoAuth("good_password", "wrong_password", maxRetries: 1));
async () => await CheckAuth("good_password", "wrong_password", maxRetries: 1));
}

[Fact]
Expand Down

0 comments on commit add7151

Please sign in to comment.