Skip to content

Commit b467cba

Browse files
committed
Authentication Store Tests
1 parent 1a34fc9 commit b467cba

6 files changed

Lines changed: 736 additions & 2 deletions

File tree

‎src/authentication/CodeBeam.UltimateAuth.Authentication.EntityFrameworkCore/Stores/EfCoreAuthenticationSecurityStateStore.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ public EfCoreAuthenticationSecurityStateStore(TDbContext db, TenantExecutionCont
3838

3939
public async Task AddAsync(AuthenticationSecurityState state, CancellationToken ct = default)
4040
{
41+
ct.ThrowIfCancellationRequested();
42+
43+
if (state.Tenant != _tenant)
44+
throw new InvalidOperationException("Tenant mismatch.");
45+
46+
var exists = await DbSet.AnyAsync(x =>
47+
x.Tenant == _tenant &&
48+
x.UserKey == state.UserKey &&
49+
x.Scope == state.Scope &&
50+
x.CredentialType == state.CredentialType,
51+
ct);
52+
53+
if (exists)
54+
throw new UAuthConflictException("security_state_already_exists");
55+
4156
var entity = AuthenticationSecurityStateMapper.ToProjection(state);
4257

4358
DbSet.Add(entity);
@@ -47,6 +62,11 @@ public async Task AddAsync(AuthenticationSecurityState state, CancellationToken
4762

4863
public async Task UpdateAsync(AuthenticationSecurityState state, long expectedVersion, CancellationToken ct = default)
4964
{
65+
ct.ThrowIfCancellationRequested();
66+
67+
if (state.Tenant != _tenant)
68+
throw new InvalidOperationException("Tenant mismatch.");
69+
5070
var entity = await DbSet
5171
.SingleOrDefaultAsync(x =>
5272
x.Tenant == _tenant &&

‎src/authentication/CodeBeam.UltimateAuth.Authentication.InMemory/InMemoryAuthenticationSecurityStateStore.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ public Task UpdateAsync(AuthenticationSecurityState state, long expectedVersion,
7070

7171
var key = (state.UserKey, state.Scope, state.CredentialType);
7272

73-
if (!_index.TryGetValue(key, out var id) || id != state.Id)
73+
if (!_index.TryGetValue(key, out var id))
74+
throw new UAuthNotFoundException("security_state_not_found");
75+
76+
if (id != state.Id)
7477
throw new UAuthConflictException("security_state_index_corrupted");
7578

7679
if (!_byId.TryGetValue(state.Id, out var current))
77-
throw new UAuthNotFoundException("security_state_not_found");
80+
throw new UAuthConflictException("security_state_index_corrupted");
7881

7982
if (current.SecurityVersion != expectedVersion)
8083
throw new UAuthConflictException("security_state_version_conflict");

0 commit comments

Comments
 (0)