Skip to content

Commit 19618d6

Browse files
committed
TBA integration test
1 parent 7f93841 commit 19618d6

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

tests/NRedisStack.Tests/NRedisStack.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<PackageReference Include="xunit" Version="2.4.1" />
3232
<PackageReference Include="xunit.assert" Version="2.4.1" />
3333
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.0" />
34+
<PackageReference Include="Microsoft.Azure.StackExchangeRedis" Version="3.1.0" />
3435
</ItemGroup>
3536

3637
<ItemGroup>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Xunit;
2+
using StackExchange.Redis;
3+
using Azure.Identity;
4+
using NRedisStack.RedisStackCommands;
5+
using NRedisStack.Search;
6+
7+
namespace NRedisStack.Tests.TokenBasedAuthentication
8+
{
9+
public class AuthenticationTests
10+
{
11+
static readonly string key = "myKey";
12+
static readonly string value = "myValue";
13+
static readonly string index = "myIndex";
14+
static readonly string field = "myField";
15+
static readonly string alias = "myAlias";
16+
17+
string env0Host = "localhost:6379";
18+
19+
[Fact]
20+
public void TestTokenBasedAuthentication()
21+
{
22+
23+
// NOTE: ConnectionMultiplexer instances should be as long-lived as possible. Ideally a single ConnectionMultiplexer per cache is reused over the lifetime of the client application process.
24+
ConnectionMultiplexer? connectionMultiplexer = null;
25+
StringWriter connectionLog = new();
26+
27+
var configurationOptions = ConfigurationOptions.Parse($"{env0Host}").ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()).Result!;
28+
configurationOptions.Ssl = false;
29+
configurationOptions.AbortOnConnectFail = true; // Fail fast for the purposes of this sample. In production code, this should remain false to retry connections on startup
30+
31+
connectionMultiplexer = ConnectionMultiplexer.ConnectAsync(configurationOptions, connectionLog).Result;
32+
33+
IDatabase db = connectionMultiplexer.GetDatabase();
34+
35+
db.KeyDelete(key);
36+
try
37+
{
38+
db.FT().DropIndex(index);
39+
}
40+
catch { }
41+
42+
db.StringSet(key, value);
43+
string result = db.StringGet(key);
44+
Assert.Equal(value, result);
45+
46+
var ft = db.FT();
47+
Schema sc = new Schema().AddTextField(field);
48+
Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));
49+
50+
db.HashSet(index, new HashEntry[] { new HashEntry(field, value) });
51+
52+
Assert.True(ft.AliasAdd(alias, index));
53+
SearchResult res1 = ft.Search(alias, new Query("*").ReturnFields(field));
54+
Assert.Equal(1, res1.TotalResults);
55+
Assert.Equal(value, res1.Documents[0][field]);
56+
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)