public async Task WhenNonceIsRegistered_ReturnsNonce_EvenIfItExpired() { var nonce = new Nonce(_keyId, _value, DateTimeOffset.Now.AddMinutes(-1)); var cacheKey = "Nonce_c1_abc123"; var cacheEntry = _cache.CreateEntry(cacheKey); cacheEntry.Value = nonce; var actual = await _sut.Get(_keyId, _value); actual.Should().Be(nonce); }
/// <summary> /// Registers usage of the specified <see cref="Nonce" /> value. /// </summary> /// <param name="nonce">The <see cref="Nonce" /> that is received from a client.</param> public Task Register(Nonce nonce) { if (nonce == null) { throw new ArgumentNullException(nameof(nonce)); } var cacheKey = CacheKeyCreator(nonce.ClientId, nonce.Value); _cache.Set(cacheKey, nonce, nonce.Expiration); return(Task.CompletedTask); }
public async Task WhenNonceIsInCache_AndItIsNotNull_DoesNotCallDecoratedService() { var cachedNonce = new Nonce("c1", "abc123", _now.AddMinutes(1)); var cacheKey = CacheKeyFactory(cachedNonce); var cacheEntry = _cache.CreateEntry(cacheKey); cacheEntry.Value = cachedNonce; await _sut.Get(cachedNonce.ClientId, cachedNonce.Value); A.CallTo(() => _decorated.Get(A <KeyId> ._, A <string> ._)) .MustNotHaveHappened(); }
public async Task WhenNonceIsAlreadyRegistered_Overwrites() { var cacheKey = "Nonce_c1_abc123"; var cacheEntry = _cache.CreateEntry(cacheKey); cacheEntry.Value = _nonce; var newNonce = new Nonce(_nonce.ClientId, _nonce.Value, _now.AddMinutes(10)); await _sut.Register(newNonce); _cache.TryGetValue(_cacheKey, out var actualNonce).Should().BeTrue(); actualNonce.Should().Be(newNonce); }
public Task Register(Nonce nonce) { if (nonce == null) { throw new ArgumentNullException(nameof(nonce)); } var cacheKey = CacheKeyFactory(nonce.ClientId, nonce.Value); _cache.Set(cacheKey, nonce, nonce.Expiration); return(_decorated.Register(nonce)); }
public async Task AddsToCacheWithExpectedExpiration() { var nonce = new Nonce(new KeyId("c1"), "abc123", _now.AddSeconds(30)); var cacheKey = "CacheEntry_Nonce_c1_abc123"; _cache.TryGetValue(cacheKey, out _).Should().BeFalse(); await _sut.Register(nonce); _cache.TryGetEntry(cacheKey, out var actualEntry).Should().BeTrue(); actualEntry.As <ICacheEntry>().Value.Should().Be(nonce); actualEntry.As <ICacheEntry>().AbsoluteExpiration.Should().Be(nonce.Expiration); }
public async Task WhenNonceIsNotCached_AndNonceIsResolved_ReturnsResolvedNonce() { var cacheKey = CacheKeyFactory("c1", "abc123"); _cache.TryGetValue(cacheKey, out _).Should().BeFalse(); var resolvedNonce = new Nonce("c1", "abc123", _now.AddMinutes(2)); A.CallTo(() => _decorated.Get("c1", "abc123")) .Returns(resolvedNonce); var actual = await _sut.Get("c1", "abc123"); actual.Should().Be(resolvedNonce); }
public async Task WhenNonceIsNotCached_AndItIsResolved_AddsToCacheWithExpectedExpiration() { var cacheKey = CacheKeyFactory("c1", "abc123"); _cache.TryGetValue(cacheKey, out _).Should().BeFalse(); var resolvedNonce = new Nonce("c1", "abc123", _now.AddMinutes(2)); A.CallTo(() => _decorated.Get("c1", "abc123")) .Returns(resolvedNonce); await _sut.Get("c1", "abc123"); _cache.TryGetEntry(cacheKey, out var actualEntry).Should().BeTrue(); actualEntry.As <ICacheEntry>().AbsoluteExpiration.Should().Be(resolvedNonce.Expiration); }
public async Task WhenNonceIsInCache_ButItIsNull_AndNonceIsResolved_ReturnsResolvedNonce() { var cacheKey = CacheKeyFactory("c1", "abc123"); var cacheEntry = _cache.CreateEntry(cacheKey); cacheEntry.Value = null; var resolvedNonce = new Nonce("c1", "abc123", _now.AddMinutes(2)); A.CallTo(() => _decorated.Get("c1", "abc123")) .Returns(resolvedNonce); var actual = await _sut.Get("c1", "abc123"); actual.Should().Be(resolvedNonce); }
private static string CacheKeyFactory(Nonce nonce) { return($"CacheEntry_Nonce_{nonce.ClientId}_{nonce.Value}"); }
public Register() { _now = new DateTimeOffset(2020, 3, 20, 12, 12, 14, TimeSpan.Zero); _cacheKey = "Nonce_c1_abc123"; _nonce = new Nonce(new KeyId("c1"), "abc123", _now.AddMinutes(1)); }