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 StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDataStored() { var storeHolder = GetOperationalDocumentStoreHolder(); var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; var store = new DeviceFlowStore(storeHolder, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); using (var session = storeHolder.OpenAsyncSession()) { var foundDeviceFlowCodes = await session.Query <DeviceFlowCode>().FirstOrDefaultAsync(x => x.DeviceCode == deviceCode); foundDeviceFlowCodes.Should().NotBeNull(); var deserializedData = new PersistentGrantSerializer().Deserialize <DeviceCode>(foundDeviceFlowCodes?.Data); deserializedData.CreationTime.Should().BeCloseTo(data.CreationTime); deserializedData.ClientId.Should().Be(data.ClientId); deserializedData.Lifetime.Should().Be(data.Lifetime); } }
public async Task RemoveByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDeviceCodeDeleted() { var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var existingDeviceCode = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true }; var repo = g.operationalDb.GetRepository <Storage.Entities.DeviceFlowCodes>(); var entity = new Storage.Entities.DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = existingDeviceCode.ClientId, CreationTime = existingDeviceCode.CreationTime, Expiration = existingDeviceCode.CreationTime.AddSeconds(existingDeviceCode.Lifetime), Data = serializer.Serialize(existingDeviceCode) }; repo.Insert(entity); var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.RemoveByDeviceCodeAsync(testDeviceCode); repo.Select.Where(x => x.UserCode == testUserCode).First().Should().BeNull(); }
public async Task UpdateByUserCodeAsync_WhenDeviceCodeAuthorized_ExpectSubjectAndDataUpdated() { var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var expectedSubject = $"sub_{Guid.NewGuid().ToString()}"; var unauthorizedDeviceCode = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true }; var repo = g.operationalDb.GetRepository <Storage.Entities.DeviceFlowCodes>(); var entity = new Storage.Entities.DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = unauthorizedDeviceCode.ClientId, CreationTime = unauthorizedDeviceCode.CreationTime, Expiration = unauthorizedDeviceCode.CreationTime.AddSeconds(unauthorizedDeviceCode.Lifetime), Data = serializer.Serialize(unauthorizedDeviceCode) }; repo.Insert(entity); var authorizedDeviceCode = new DeviceCode { ClientId = unauthorizedDeviceCode.ClientId, RequestedScopes = unauthorizedDeviceCode.RequestedScopes, AuthorizedScopes = unauthorizedDeviceCode.RequestedScopes, Subject = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) })), IsAuthorized = true, IsOpenId = true, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300 }; var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.UpdateByUserCodeAsync(testUserCode, authorizedDeviceCode); Storage.Entities.DeviceFlowCodes updatedCodes; updatedCodes = repo.Where(x => x.UserCode == testUserCode).First(); // should be unchanged updatedCodes.DeviceCode.Should().Be(testDeviceCode); updatedCodes.ClientId.Should().Be(unauthorizedDeviceCode.ClientId); updatedCodes.CreationTime.Should().Be(unauthorizedDeviceCode.CreationTime); updatedCodes.Expiration.Should().Be(unauthorizedDeviceCode.CreationTime.AddSeconds(authorizedDeviceCode.Lifetime)); // should be changed var parsedCode = serializer.Deserialize <DeviceCode>(updatedCodes.Data); parsedCode.Should().BeEquivalentTo(authorizedDeviceCode, assertionOptions => assertionOptions.Excluding(x => x.Subject)); parsedCode.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull(); }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDataStored() { var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); var foundDeviceFlowCodes = g.operationalDb.Select <Storage.Entities.DeviceFlowCodes>().Where(x => x.DeviceCode == deviceCode).First(); foundDeviceFlowCodes.Should().NotBeNull(); var deserializedData = new PersistentGrantSerializer().Deserialize <DeviceCode>(foundDeviceFlowCodes?.Data); deserializedData.CreationTime.Should().BeCloseTo(data.CreationTime); deserializedData.ClientId.Should().Be(data.ClientId); deserializedData.Lifetime.Should().Be(data.Lifetime); }
public async Task FindByUserCodeAsync_WhenUserCodeDoesNotExist_ExpectNull() { var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); var code = await store.FindByUserCodeAsync($"user_{Guid.NewGuid().ToString()}"); code.Should().BeNull(); }
public async Task DeviceFlowStore_RemoveByKeyNegativeTest() { 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.RemoveByDeviceCodeAsync(deviceCode); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.RemoveByDeviceCodeAsync({deviceCode}): {stopwatch.ElapsedMilliseconds} ms"); }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDeviceCodeAndUserCodeStored() { using var ravenStore = GetDocumentStore(); await new DeviceFlowCodeIndex().ExecuteAsync(ravenStore); var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; using (var session = ravenStore.OpenAsyncSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); } using (var session = ravenStore.OpenSession()) { var foundDeviceFlowCodes = session.Query <DeviceFlowCode>().FirstOrDefault(x => x.DeviceCode == deviceCode); foundDeviceFlowCodes.Should().NotBeNull(); foundDeviceFlowCodes?.DeviceCode.Should().Be(deviceCode); foundDeviceFlowCodes?.UserCode.Should().Be(userCode); } }
public async Task Should_Store_DeviceCode_And_UserCode_When_Authorization_Successful(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 42 }; using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), loggerMock.Object); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); } using (var session = testDb.SessionFactory.OpenSession()) { var foundDeviceFlowCodes = await session.QueryOver <DeviceFlowCodes>() .Where(c => c.DeviceCode == deviceCode) .SingleOrDefaultAsync(); foundDeviceFlowCodes.Should().NotBeNull(); foundDeviceFlowCodes?.DeviceCode.Should().Be(deviceCode); foundDeviceFlowCodes?.ID.Should().Be(userCode); } await CleanupTestDataAsync(testDb); }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDataStored(DbContextOptions <PersistedGrantDbContext> options) { var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); } using (var context = new PersistedGrantDbContext(options)) { var foundDeviceFlowCodes = context.DeviceFlowCodes.FirstOrDefault(x => x.DeviceCode == deviceCode); foundDeviceFlowCodes.Should().NotBeNull(); var deserializedData = new PersistentGrantSerializer().Deserialize <DeviceCode>(foundDeviceFlowCodes?.Data); deserializedData.CreationTime.Should().BeCloseTo(data.CreationTime); deserializedData.ClientId.Should().Be(data.ClientId); deserializedData.Lifetime.Should().Be(data.Lifetime); } }
public async Task FindByUserCodeAsync_should_find_device_code_by_user_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", UserCode = "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.FindByUserCodeAsync("code"); Assert.NotNull(result); }
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 RemoveByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDeviceCodeDeleted() { var testDeviceCode = $"device_{Guid.NewGuid()}"; var testUserCode = $"user_{Guid.NewGuid()}"; var existingDeviceCode = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29).AsUtc(), Lifetime = 300, IsOpenId = true }; await _context.DeviceFlowCodes.AddAsync(new DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = existingDeviceCode.ClientId, CreationTime = existingDeviceCode.CreationTime, Expiration = existingDeviceCode.CreationTime.AddSeconds(existingDeviceCode.Lifetime), Data = _serializer.Serialize(existingDeviceCode) }); var store = new DeviceFlowStore(_context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.RemoveByDeviceCodeAsync(testDeviceCode); (await _context.DeviceFlowCodes.WhereEqualTo("UserCode", testUserCode).GetSnapshotAsync()).Count.Should().Be(0); }
public async Task UpdateByUserCodeAsync_should_update_device_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", UserCode = "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); await sut.UpdateByUserCodeAsync("code", new DeviceCode { ClientId = "test" }); using var s2 = store.OpenAsyncSession(); var updated = await s2.LoadAsync <Entity.DeviceCode>($"{nameof(Entity.DeviceCode)}/test"); var data = serializer.Deserialize <DeviceCode>(updated.Data); Assert.Equal("test", data.ClientId); }
public async Task RemoveByDeviceCodeAsync_WhenDeviceCodeDoesNotExists_ExpectSuccess(DbContextOptions <PersistedGrantDbContext> options) { using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); await store.RemoveByDeviceCodeAsync($"device_{Guid.NewGuid().ToString()}"); } }
private static void CreateSut(out Mock <IAdminStore <DeviceCode> > storeMock, out DeviceFlowStore sut) { storeMock = new Mock <IAdminStore <DeviceCode> >(); var serializerMock = new Mock <IPersistentGrantSerializer>(); sut = new DeviceFlowStore(storeMock.Object, serializerMock.Object); }
public async Task RemoveByDeviceCodeAsync_WhenDeviceCodeDoesNotExists_ExpectSuccess() { var storeHolder = GetOperationalDocumentStoreHolder(); var store = new DeviceFlowStore(storeHolder, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.RemoveByDeviceCodeAsync($"device_{Guid.NewGuid().ToString()}"); }
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 FindByUserCodeAsync_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.FindByUserCodeAsync(null)); }
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 void DeviceFlowStore_CtorsTest() { var storageContext = Services.BuildServiceProvider().GetService <DeviceFlowStorageContext>(); Assert.IsNotNull(storageContext); var store = new DeviceFlowStore(storageContext, _persistentGrantSerializer, _logger); Assert.IsNotNull(store); }
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 RemoveByDeviceCodeAsync_WhenDeviceCodeDoesNotExists_ExpectSuccess() { using var ravenStore = GetDocumentStore(); await new DeviceFlowCodeIndex().ExecuteAsync(ravenStore); using (var session = ravenStore.OpenAsyncSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.RemoveByDeviceCodeAsync($"device_{Guid.NewGuid().ToString()}"); } }
public async Task Should_Throw_Exception_If_DeviceCode_Already_Exists(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var serializer = new PersistentGrantSerializer(); var existingUserCode = $"user_{Guid.NewGuid()}"; var deviceCodeData = new DeviceCode() { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 12, 07, 8, 00, 00), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal( new ClaimsIdentity( new List <Claim> { new Claim(JwtClaimTypes.Subject, $"sub_{Guid.NewGuid()}") } ) ) }; using (var session = testDb.SessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { await session.SaveAsync(new DeviceFlowCodes() { DeviceCode = $"device_{Guid.NewGuid()}", ID = existingUserCode, ClientId = deviceCodeData.ClientId, SubjectId = deviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = deviceCodeData.CreationTime, Expiration = deviceCodeData.CreationTime.AddSeconds(deviceCodeData.Lifetime), Data = serializer.Serialize(deviceCodeData) }); await tx.CommitAsync(); } } using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), loggerMock.Object); Func <Task> act = async() => await store.StoreDeviceAuthorizationAsync($"device_{Guid.NewGuid()}", existingUserCode, deviceCodeData); await act.Should().ThrowAsync <HibernateException>(); } await CleanupTestDataAsync(testDb); }
public async Task Can_call_DeviceFlowStore_FindByUserCodeAsync() => await ExecuteWithStrategyInTransactionAsync( async context => { await SaveDevices(context); }, async context => { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), new FakeLogger <DeviceFlowStore>()); Assert.Equal("D2", (await store.FindByUserCodeAsync("U2")).Description); } );
public async Task UpdateByUserCodeAsync_should_throw_when_code_not_found() { 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 <InvalidOperationException>(() => sut.UpdateByUserCodeAsync("code", new DeviceCode { ClientId = "test" })); }
public async Task FindByUserCodeAsync_WhenUserCodeDoesNotExist_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.FindByUserCodeAsync($"user_{Guid.NewGuid().ToString()}"); code.Should().BeNull(); } }
public async Task StoreDeviceAuthorizationAsync_should_check_parameters() { 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.StoreDeviceAuthorizationAsync(null, null, null)); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.StoreDeviceAuthorizationAsync("code", null, null)); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.StoreDeviceAuthorizationAsync("code", "userCode", null)); }
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); }