Skip to content

Commit 5650b67

Browse files
committed
Token Store Tests
1 parent 4b665a5 commit 5650b67

4 files changed

Lines changed: 868 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using CodeBeam.UltimateAuth.Core.Abstractions;
2+
using CodeBeam.UltimateAuth.Core.MultiTenancy;
3+
using CodeBeam.UltimateAuth.Tokens.EntityFrameworkCore;
4+
using Microsoft.Data.Sqlite;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace CodeBeam.UltimateAuth.Tests.Unit.Tokens.Contracts;
8+
9+
public sealed class EfCoreRefreshTokenStoreContractTests
10+
: RefreshTokenStoreContractTests
11+
{
12+
protected override async Task<IRefreshTokenStoreTestDatabase>
13+
CreateDatabaseAsync()
14+
{
15+
var db = new Database();
16+
await db.InitializeAsync();
17+
return db;
18+
}
19+
20+
private sealed class Database
21+
: IRefreshTokenStoreTestDatabase
22+
{
23+
private readonly SqliteConnection _connection;
24+
private readonly UAuthTokenDbContext _db;
25+
26+
public Database()
27+
{
28+
_connection =
29+
new SqliteConnection("Data Source=:memory:");
30+
31+
var options =
32+
new DbContextOptionsBuilder<UAuthTokenDbContext>()
33+
.UseSqlite(_connection)
34+
.Options;
35+
36+
_db = new UAuthTokenDbContext(options);
37+
}
38+
39+
public async Task InitializeAsync()
40+
{
41+
await _connection.OpenAsync();
42+
await _db.Database.EnsureCreatedAsync();
43+
}
44+
45+
public IRefreshTokenStore CreateStore(TenantKey tenant)
46+
=> new EfCoreRefreshTokenStore<UAuthTokenDbContext>(
47+
_db,
48+
new TenantExecutionContext(tenant));
49+
50+
public async ValueTask DisposeAsync()
51+
{
52+
await _db.DisposeAsync();
53+
await _connection.DisposeAsync();
54+
}
55+
}
56+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using CodeBeam.UltimateAuth.Core.Abstractions;
2+
using CodeBeam.UltimateAuth.Core.MultiTenancy;
3+
4+
namespace CodeBeam.UltimateAuth.Tests.Unit.Tokens.Contracts;
5+
6+
public interface IRefreshTokenStoreTestDatabase : IAsyncDisposable
7+
{
8+
IRefreshTokenStore CreateStore(TenantKey tenant);
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using CodeBeam.UltimateAuth.Core.Abstractions;
2+
using CodeBeam.UltimateAuth.Core.MultiTenancy;
3+
using CodeBeam.UltimateAuth.Tokens.InMemory;
4+
5+
namespace CodeBeam.UltimateAuth.Tests.Unit.Tokens.Contracts;
6+
7+
public sealed class InMemoryRefreshTokenStoreContractTests
8+
: RefreshTokenStoreContractTests
9+
{
10+
protected override Task<IRefreshTokenStoreTestDatabase>
11+
CreateDatabaseAsync()
12+
{
13+
return Task.FromResult<IRefreshTokenStoreTestDatabase>(
14+
new Database());
15+
}
16+
17+
private sealed class Database
18+
: IRefreshTokenStoreTestDatabase
19+
{
20+
private readonly InMemoryRefreshTokenStoreFactory _factory =
21+
new();
22+
23+
public IRefreshTokenStore CreateStore(TenantKey tenant)
24+
=> _factory.Create(tenant);
25+
26+
public ValueTask DisposeAsync()
27+
=> ValueTask.CompletedTask;
28+
}
29+
}

0 commit comments

Comments
 (0)