Skip to content

Commit ced9e58

Browse files
committed
Bundle Tests
1 parent 6781fcd commit ced9e58

5 files changed

Lines changed: 278 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("CodeBeam.UltimateAuth.Tests.Unit")]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("CodeBeam.UltimateAuth.Tests.Unit")]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using CodeBeam.UltimateAuth.Authentication.EntityFrameworkCore;
2+
using CodeBeam.UltimateAuth.Authorization.EntityFrameworkCore;
3+
using CodeBeam.UltimateAuth.Credentials.EntityFrameworkCore;
4+
using CodeBeam.UltimateAuth.EntityFrameworkCore;
5+
using CodeBeam.UltimateAuth.Sessions.EntityFrameworkCore;
6+
using CodeBeam.UltimateAuth.Tokens.EntityFrameworkCore;
7+
using CodeBeam.UltimateAuth.Users.EntityFrameworkCore;
8+
using FluentAssertions;
9+
using Microsoft.EntityFrameworkCore;
10+
11+
namespace CodeBeam.UltimateAuth.Tests.Unit.EntityFrameworkCore;
12+
13+
public sealed class UAuthDbContextTests
14+
{
15+
[Fact]
16+
public void Model_ShouldContainAllUltimateAuthProjectionTypes()
17+
{
18+
var options =
19+
new DbContextOptionsBuilder<UAuthDbContext>()
20+
.UseInMemoryDatabase(Guid.NewGuid().ToString())
21+
.Options;
22+
23+
using var context = new UAuthDbContext(options);
24+
25+
var model = context.Model;
26+
27+
model.FindEntityType(typeof(UserLifecycleProjection))
28+
.Should().NotBeNull();
29+
30+
model.FindEntityType(typeof(UserProfileProjection))
31+
.Should().NotBeNull();
32+
33+
model.FindEntityType(typeof(UserIdentifierProjection))
34+
.Should().NotBeNull();
35+
36+
model.FindEntityType(typeof(PasswordCredentialProjection))
37+
.Should().NotBeNull();
38+
39+
model.FindEntityType(typeof(RoleProjection))
40+
.Should().NotBeNull();
41+
42+
model.FindEntityType(typeof(RolePermissionProjection))
43+
.Should().NotBeNull();
44+
45+
model.FindEntityType(typeof(UserRoleProjection))
46+
.Should().NotBeNull();
47+
48+
model.FindEntityType(typeof(SessionRootProjection))
49+
.Should().NotBeNull();
50+
51+
model.FindEntityType(typeof(SessionChainProjection))
52+
.Should().NotBeNull();
53+
54+
model.FindEntityType(typeof(SessionProjection))
55+
.Should().NotBeNull();
56+
57+
model.FindEntityType(typeof(RefreshTokenProjection))
58+
.Should().NotBeNull();
59+
60+
model.FindEntityType(typeof(AuthenticationSecurityStateProjection))
61+
.Should().NotBeNull();
62+
}
63+
64+
[Fact]
65+
public void DbSets_ShouldBeAvailable()
66+
{
67+
var options =
68+
new DbContextOptionsBuilder<UAuthDbContext>()
69+
.UseInMemoryDatabase(Guid.NewGuid().ToString())
70+
.Options;
71+
72+
using var context = new UAuthDbContext(options);
73+
74+
context.UserLifecycles.Should().NotBeNull();
75+
context.UserProfiles.Should().NotBeNull();
76+
context.UserIdentifiers.Should().NotBeNull();
77+
context.PasswordCredentials.Should().NotBeNull();
78+
79+
context.Roles.Should().NotBeNull();
80+
context.UserRoleAssignments.Should().NotBeNull();
81+
context.UserPermissions.Should().NotBeNull();
82+
83+
context.Roots.Should().NotBeNull();
84+
context.Chains.Should().NotBeNull();
85+
context.Sessions.Should().NotBeNull();
86+
87+
context.RefreshTokens.Should().NotBeNull();
88+
context.AuthenticationSecurityStates.Should().NotBeNull();
89+
}
90+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using CodeBeam.UltimateAuth.EntityFrameworkCore;
2+
using FluentAssertions;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace CodeBeam.UltimateAuth.Tests.Unit.EntityFrameworkCore;
6+
7+
public sealed class UAuthEfCoreOptionsTests
8+
{
9+
[Fact]
10+
public void Resolve_WithSpecificConfiguration_ShouldReturnSpecific()
11+
{
12+
Action<DbContextOptionsBuilder> defaultConfig = _ => { };
13+
Action<DbContextOptionsBuilder> specificConfig = _ => { };
14+
15+
var options = new UAuthEfCoreOptions
16+
{
17+
Default = defaultConfig
18+
};
19+
20+
var result = options.Resolve(specificConfig);
21+
22+
result.Should().BeSameAs(specificConfig);
23+
}
24+
25+
[Fact]
26+
public void Resolve_WithoutSpecificConfiguration_ShouldReturnDefault()
27+
{
28+
Action<DbContextOptionsBuilder> defaultConfig = _ => { };
29+
30+
var options = new UAuthEfCoreOptions
31+
{
32+
Default = defaultConfig
33+
};
34+
35+
var result = options.Resolve(null);
36+
37+
result.Should().BeSameAs(defaultConfig);
38+
}
39+
40+
[Fact]
41+
public void Resolve_WithoutAnyConfiguration_ShouldThrow()
42+
{
43+
var options = new UAuthEfCoreOptions();
44+
45+
var act = () => options.Resolve(null);
46+
47+
act.Should()
48+
.Throw<InvalidOperationException>()
49+
.WithMessage(
50+
"No database configuration provided for UltimateAuth EFCore.*");
51+
}
52+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using CodeBeam.UltimateAuth.Authentication.EntityFrameworkCore;
2+
using CodeBeam.UltimateAuth.Authorization.EntityFrameworkCore;
3+
using CodeBeam.UltimateAuth.Credentials.EntityFrameworkCore;
4+
using CodeBeam.UltimateAuth.EntityFrameworkCore;
5+
using CodeBeam.UltimateAuth.Sessions.EntityFrameworkCore;
6+
using CodeBeam.UltimateAuth.Tokens.EntityFrameworkCore;
7+
using CodeBeam.UltimateAuth.Users.EntityFrameworkCore;
8+
using FluentAssertions;
9+
using Microsoft.EntityFrameworkCore;
10+
using Microsoft.Extensions.DependencyInjection;
11+
12+
namespace CodeBeam.UltimateAuth.Tests.Unit.EntityFrameworkCore;
13+
14+
public sealed class UltimateAuthEntityFrameworkCoreExtensionsTests
15+
{
16+
[Fact]
17+
public void AddUltimateAuthEntityFrameworkCore_WithUnifiedContext_ShouldRegisterUAuthDbContext()
18+
{
19+
var services = new ServiceCollection();
20+
21+
services.AddUltimateAuthEntityFrameworkCore(options =>
22+
options.UseInMemoryDatabase(Guid.NewGuid().ToString()));
23+
24+
using var provider = services.BuildServiceProvider();
25+
26+
using var scope = provider.CreateScope();
27+
28+
var context =
29+
scope.ServiceProvider.GetRequiredService<UAuthDbContext>();
30+
31+
context.Should().NotBeNull();
32+
}
33+
34+
[Fact]
35+
public void AddUltimateAuthEntityFrameworkCore_WithUnifiedContext_ShouldConfigureDatabaseProvider()
36+
{
37+
var services = new ServiceCollection();
38+
39+
services.AddUltimateAuthEntityFrameworkCore(options =>
40+
options.UseInMemoryDatabase("uauth-test"));
41+
42+
using var provider = services.BuildServiceProvider();
43+
44+
using var scope = provider.CreateScope();
45+
46+
var context =
47+
scope.ServiceProvider.GetRequiredService<UAuthDbContext>();
48+
49+
context.Database.ProviderName
50+
.Should()
51+
.Be("Microsoft.EntityFrameworkCore.InMemory");
52+
}
53+
54+
[Fact]
55+
public void AddUltimateAuthEntityFrameworkCore_WithDefaultConfiguration_ShouldResolveAllContexts()
56+
{
57+
var services = new ServiceCollection();
58+
59+
services.AddUltimateAuthEntityFrameworkCore(options =>
60+
{
61+
options.Default = builder =>
62+
builder.UseInMemoryDatabase(
63+
Guid.NewGuid().ToString());
64+
});
65+
66+
using var provider = services.BuildServiceProvider();
67+
68+
using var scope = provider.CreateScope();
69+
70+
scope.ServiceProvider
71+
.GetRequiredService<UAuthUserDbContext>()
72+
.Should().NotBeNull();
73+
74+
scope.ServiceProvider
75+
.GetRequiredService<UAuthCredentialDbContext>()
76+
.Should().NotBeNull();
77+
78+
scope.ServiceProvider
79+
.GetRequiredService<UAuthAuthorizationDbContext>()
80+
.Should().NotBeNull();
81+
82+
scope.ServiceProvider
83+
.GetRequiredService<UAuthSessionDbContext>()
84+
.Should().NotBeNull();
85+
86+
scope.ServiceProvider
87+
.GetRequiredService<UAuthTokenDbContext>()
88+
.Should().NotBeNull();
89+
90+
scope.ServiceProvider
91+
.GetRequiredService<UAuthAuthenticationDbContext>()
92+
.Should().NotBeNull();
93+
}
94+
95+
[Fact]
96+
public void AddUltimateAuthEntityFrameworkCore_SpecificConfiguration_ShouldOverrideDefault()
97+
{
98+
var services = new ServiceCollection();
99+
100+
services.AddUltimateAuthEntityFrameworkCore(options =>
101+
{
102+
options.Default = builder =>
103+
builder.UseSqlite("Data Source=default.db");
104+
105+
options.Users = builder =>
106+
builder.UseSqlite("Data Source=users.db");
107+
});
108+
109+
using var provider = services.BuildServiceProvider();
110+
using var scope = provider.CreateScope();
111+
112+
var users =
113+
scope.ServiceProvider
114+
.GetRequiredService<UAuthUserDbContext>();
115+
116+
var sessions =
117+
scope.ServiceProvider
118+
.GetRequiredService<UAuthSessionDbContext>();
119+
120+
users.Database.GetDbConnection()
121+
.DataSource
122+
.Should()
123+
.Be("users.db");
124+
125+
sessions.Database.GetDbConnection()
126+
.DataSource
127+
.Should()
128+
.Be("default.db");
129+
}
130+
}

0 commit comments

Comments
 (0)