public async Task DeviceFlowStore_GetByCodesNegativeTest() { var storageContext = Services.BuildServiceProvider().GetService <DeviceFlowStorageContext>(); Assert.IsNotNull(storageContext); var store = new DeviceFlowStore(storageContext, _persistentGrantSerializer, _logger); Assert.IsNotNull(store); string userCode = Guid.NewGuid().ToString("n"); string deviceCode = Guid.NewGuid().ToString("n"); Console.WriteLine($"userCode: {userCode}"); Console.WriteLine($"deviceCode: {deviceCode}"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var findByDeviceCode = await store.FindByDeviceCodeAsync(deviceCode); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.FindByDeviceCodeAsync({deviceCode}): {stopwatch.ElapsedMilliseconds} ms"); Assert.IsNull(findByDeviceCode); stopwatch.Reset(); stopwatch.Start(); var findByUserCode = await store.FindByUserCodeAsync(userCode); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.FindByUserCodeAsync({userCode}): {stopwatch.ElapsedMilliseconds} ms"); Assert.IsNull(findByUserCode); }
public async Task FindByDeviceCodeAsync_WhenDeviceCodeDoesNotExist_ExpectNull() { var store = new DeviceFlowStore(_context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); var code = await store.FindByDeviceCodeAsync($"device_{Guid.NewGuid()}"); code.Should().BeNull(); }
public async Task FindByDeviceCodeAsync_should_find_device_code_by_code() { using var store = new RavenDbTestDriverWrapper().GetDocumentStore(); var serializer = new PersistentGrantSerializer(); var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); using var s1 = store.OpenAsyncSession(); await s1.StoreAsync(new Entity.DeviceCode { Id = "test", Code = "code", Data = serializer.Serialize(new DeviceCode { AuthorizedScopes = new[] { "client_credential" } }) }, $"{nameof(Entity.DeviceCode)}/test"); await s1.SaveChangesAsync(); var sut = new DeviceFlowStore(new ScopedAsynDocumentcSession(store.OpenAsyncSession()), serializer, loggerMock.Object); var result = await sut.FindByDeviceCodeAsync("code"); Assert.NotNull(result); }
public async Task DeviceFlowStore_SaveUpdateGetByCodesTest() { var storageContext = Services.BuildServiceProvider().GetService <DeviceFlowStorageContext>(); Assert.IsNotNull(storageContext); var store = new DeviceFlowStore(storageContext, _persistentGrantSerializer, _logger); Assert.IsNotNull(store); string userCode = Guid.NewGuid().ToString("n"); string deviceCode = Guid.NewGuid().ToString("n"); Console.WriteLine($"userCode: {userCode}"); Console.WriteLine($"deviceCode: {deviceCode}"); DeviceCode deviceCodeData = CreateTestObject(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, deviceCodeData); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.StoreDeviceAuthorizationAsync({deviceCode},{userCode}): {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); string oldClientId = deviceCodeData.ClientId; string newClientId = Guid.NewGuid().ToString("n"); deviceCodeData.ClientId = newClientId; await store.UpdateByUserCodeAsync(userCode, deviceCodeData); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.UpdateByUserCodeAsync({userCode}): {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); var findByDeviceCode = await store.FindByDeviceCodeAsync(deviceCode); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.FindByDeviceCodeAsync({deviceCode}): {stopwatch.ElapsedMilliseconds} ms"); AssertDeviceCodesEqual(deviceCodeData, findByDeviceCode); Assert.AreEqual <string>(newClientId, findByDeviceCode.ClientId); Assert.AreNotEqual <string>(oldClientId, findByDeviceCode.ClientId); stopwatch.Reset(); stopwatch.Start(); var findByUserCode = await store.FindByUserCodeAsync(userCode); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.FindByUserCodeAsync({userCode}): {stopwatch.ElapsedMilliseconds} ms"); AssertDeviceCodesEqual(deviceCodeData, findByUserCode); Assert.AreEqual <string>(newClientId, findByUserCode.ClientId); Assert.AreNotEqual <string>(oldClientId, findByUserCode.ClientId); AssertDeviceCodesEqual(findByDeviceCode, findByUserCode); }
public async Task Can_call_DeviceFlowStore_FindByDeviceCodeAsync_and_RemoveByDeviceCodeAsync() => await ExecuteWithStrategyInTransactionAsync( async context => { await SaveDevices(context); }, async context => { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), new FakeLogger <DeviceFlowStore>()); Assert.Equal("D2", (await store.FindByDeviceCodeAsync("DC2")).Description); await store.RemoveByDeviceCodeAsync("DC2"); Assert.Null(await store.FindByDeviceCodeAsync("DC2")); await store.RemoveByDeviceCodeAsync("DC2"); Assert.Null(await store.FindByDeviceCodeAsync("DC2")); } );
public async Task Should_Retrieve_Data_By_DeviceCode(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var serializer = new PersistentGrantSerializer(); var testDeviceCode = $"device_{Guid.NewGuid()}"; var testUserCode = $"user_{Guid.NewGuid()}"; var expectedSubject = $"sub_{Guid.NewGuid()}"; var expectedDeviceCodeData = new DeviceCode() { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 12, 7, 14, 15, 16), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal( new ClaimsIdentity( new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) } ) ) }; using (var session = testDb.SessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { await session.SaveAsync(new DeviceFlowCodes { DeviceCode = testDeviceCode, ID = testUserCode, ClientId = expectedDeviceCodeData.ClientId, SubjectId = expectedDeviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = expectedDeviceCodeData.CreationTime, Expiration = expectedDeviceCodeData.CreationTime.AddSeconds(expectedDeviceCodeData.Lifetime), Data = serializer.Serialize(expectedDeviceCodeData) }); await tx.CommitAsync(); } } DeviceCode code; using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, serializer, loggerMock.Object); code = await store.FindByDeviceCodeAsync(testDeviceCode); } code.Should().BeEquivalentTo(expectedDeviceCodeData, assertionOptions => assertionOptions.Excluding(x => x.Subject)); code.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull(); await CleanupTestDataAsync(testDb); }
public async Task FindByDeviceCodeAsync_WhenDeviceCodeDoesNotExist_ExpectNull() { var storeHolder = GetOperationalDocumentStoreHolder(); var store = new DeviceFlowStore(storeHolder, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); var code = await store.FindByDeviceCodeAsync($"device_{Guid.NewGuid().ToString()}"); code.Should().BeNull(); }
public async Task FindByDeviceCodeAsync_WhenDeviceCodeDoesNotExist_ExpectNull(DbContextOptions <PersistedGrantDbContext> options) { using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); var code = await store.FindByDeviceCodeAsync($"device_{Guid.NewGuid().ToString()}"); code.Should().BeNull(); } }
public async Task FindByDeviceCodeAsync_should_throw_when_code_is_null() { using var store = new RavenDbTestDriverWrapper().GetDocumentStore(); var serializer = new PersistentGrantSerializer(); var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var sut = new DeviceFlowStore(new ScopedAsynDocumentcSession(store.OpenAsyncSession()), serializer, loggerMock.Object); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.FindByDeviceCodeAsync(null)); }
public async Task FindByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDataRetrievedCorrectly() { using var ravenStore = GetDocumentStore(); await new DeviceFlowCodeIndex().ExecuteAsync(ravenStore); var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var expectedSubject = $"sub_{Guid.NewGuid().ToString()}"; var expectedDeviceCodeData = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) })) }; using (var session = ravenStore.OpenSession()) { session.Store(new DeviceFlowCode { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = expectedDeviceCodeData.ClientId, SubjectId = expectedDeviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = expectedDeviceCodeData.CreationTime, Expiration = expectedDeviceCodeData.CreationTime.AddSeconds(expectedDeviceCodeData.Lifetime), Data = serializer.Serialize(expectedDeviceCodeData) }); session.SaveChanges(); } WaitForIndexing(ravenStore); DeviceCode code; using (var session = ravenStore.OpenAsyncSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); code = await store.FindByDeviceCodeAsync(testDeviceCode); } code.Should().BeEquivalentTo(expectedDeviceCodeData, assertionOptions => assertionOptions.Excluding(x => x.Subject)); code.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject) .Should().NotBeNull(); }
public async Task Should_Retrieve_Null_If_DeviceCode_Does_Not_Exist(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), loggerMock.Object); var code = await store.FindByDeviceCodeAsync($"device_{Guid.NewGuid()}"); code.Should().BeNull(); } await CleanupTestDataAsync(testDb); }
public async Task FindByDeviceCodeAsync_WhenDeviceCodeDoesNotExist_ExpectNull() { using var ravenStore = GetDocumentStore(); await new DeviceFlowCodeIndex().ExecuteAsync(ravenStore); using (var session = ravenStore.OpenAsyncSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); var code = await store.FindByDeviceCodeAsync($"device_{Guid.NewGuid().ToString()}"); code.Should().BeNull(); } }
public async Task FindByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDataRetrievedCorrectly(DbContextOptions <PersistedGrantDbContext> options) { var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var expectedSubject = $"sub_{Guid.NewGuid().ToString()}"; var expectedDeviceCodeData = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) })) }; using (var context = new PersistedGrantDbContext(options)) { context.DeviceFlowCodes.Add(new DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = expectedDeviceCodeData.ClientId, SubjectId = expectedDeviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = expectedDeviceCodeData.CreationTime, Expiration = expectedDeviceCodeData.CreationTime.AddSeconds(expectedDeviceCodeData.Lifetime), Data = serializer.Serialize(expectedDeviceCodeData) }); context.SaveChanges(); } DeviceCode code; using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); code = await store.FindByDeviceCodeAsync(testDeviceCode); } code.Should().BeEquivalentTo(expectedDeviceCodeData, assertionOptions => assertionOptions.Excluding(x => x.Subject)); code.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull(); }
public async Task FindByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDataRetrievedCorrectly() { var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var expectedSubject = $"sub_{Guid.NewGuid().ToString()}"; var expectedDeviceCodeData = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) })) }; var repo = g.operationalDb.GetRepository <Storage.Entities.DeviceFlowCodes>(); var entity = new Storage.Entities.DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = expectedDeviceCodeData.ClientId, SubjectId = expectedDeviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = expectedDeviceCodeData.CreationTime, Expiration = expectedDeviceCodeData.CreationTime.AddSeconds(expectedDeviceCodeData.Lifetime), Data = serializer.Serialize(expectedDeviceCodeData) }; repo.Insert(entity); DeviceCode code; var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); code = await store.FindByDeviceCodeAsync(testDeviceCode); code.Should().BeEquivalentTo(expectedDeviceCodeData, assertionOptions => assertionOptions.Excluding(x => x.Subject)); code.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull(); }