Skip to content

Commit 6b95588

Browse files
committed
feat(sso-persisted-grants) [PM-23572]: Add additional tests.
1 parent 03c09a3 commit 6b95588

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

bitwarden_license/src/Sso/IdentityServer/DistributedCachePersistedGrantStore.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ public DistributedCachePersistedGrantStore(
3939

4040
var grant = result.Value;
4141

42-
// Check expiration
43-
if (!grant.Expiration.HasValue || grant.Expiration.Value >= DateTime.UtcNow) return grant;
44-
await RemoveAsync(key);
45-
return null;
42+
// Check if grant has expired - remove expired grants from cache
43+
if (grant.Expiration.HasValue && grant.Expiration.Value < DateTime.UtcNow)
44+
{
45+
await RemoveAsync(key);
46+
return null;
47+
}
4648

49+
return grant;
4750
}
4851

4952
public Task<IEnumerable<PersistedGrant>> GetAllAsync(PersistedGrantFilter filter)

bitwarden_license/test/SSO.Test/IdentityServer/DistributedCachePersistedGrantStoreTests.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,74 @@ await _cache.Received(1).SetAsync(
8585
opts.SkipDistributedCacheReadWhenStale == false));
8686
}
8787

88+
[Fact]
89+
public async Task GetAsync_WithValidGrant_ReturnsGrant()
90+
{
91+
// Arrange
92+
var grant = CreateTestGrant("valid-key", expiration: DateTime.UtcNow.AddMinutes(5));
93+
_cache.TryGetAsync<PersistedGrant>("valid-key")
94+
.Returns(MaybeValue<PersistedGrant>.FromValue(grant));
95+
96+
// Act
97+
var result = await _sut.GetAsync("valid-key");
98+
99+
// Assert
100+
Assert.NotNull(result);
101+
Assert.Equal("valid-key", result.Key);
102+
Assert.Equal("authorization_code", result.Type);
103+
Assert.Equal("test-subject", result.SubjectId);
104+
await _cache.DidNotReceive().RemoveAsync(Arg.Any<string>());
105+
}
106+
107+
[Fact]
108+
public async Task GetAsync_WithNonExistentKey_ReturnsNull()
109+
{
110+
// Arrange
111+
_cache.TryGetAsync<PersistedGrant>("nonexistent-key")
112+
.Returns(MaybeValue<PersistedGrant>.None);
113+
114+
// Act
115+
var result = await _sut.GetAsync("nonexistent-key");
116+
117+
// Assert
118+
Assert.Null(result);
119+
await _cache.DidNotReceive().RemoveAsync(Arg.Any<string>());
120+
}
121+
122+
[Fact]
123+
public async Task GetAsync_WithExpiredGrant_RemovesAndReturnsNull()
124+
{
125+
// Arrange
126+
var expiredGrant = CreateTestGrant("expired-key", expiration: DateTime.UtcNow.AddMinutes(-1));
127+
_cache.TryGetAsync<PersistedGrant>("expired-key")
128+
.Returns(MaybeValue<PersistedGrant>.FromValue(expiredGrant));
129+
130+
// Act
131+
var result = await _sut.GetAsync("expired-key");
132+
133+
// Assert
134+
Assert.Null(result);
135+
await _cache.Received(1).RemoveAsync("expired-key");
136+
}
137+
138+
[Fact]
139+
public async Task GetAsync_WithNoExpiration_ReturnsGrant()
140+
{
141+
// Arrange
142+
var grant = CreateTestGrant("no-expiry-key", expiration: null);
143+
_cache.TryGetAsync<PersistedGrant>("no-expiry-key")
144+
.Returns(MaybeValue<PersistedGrant>.FromValue(grant));
145+
146+
// Act
147+
var result = await _sut.GetAsync("no-expiry-key");
148+
149+
// Assert
150+
Assert.NotNull(result);
151+
Assert.Equal("no-expiry-key", result.Key);
152+
Assert.Null(result.Expiration);
153+
await _cache.DidNotReceive().RemoveAsync(Arg.Any<string>());
154+
}
155+
88156
[Fact]
89157
public async Task RemoveAsync_RemovesGrantFromCache()
90158
{

0 commit comments

Comments
 (0)