public async Task RemoveByDeviceCodeAsync_should_delete_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", 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); await sut.RemoveByDeviceCodeAsync("code"); await sut.RemoveByDeviceCodeAsync("code"); using var s2 = store.OpenAsyncSession(); Assert.Null(await s2.LoadAsync <Entity.DeviceCode>($"{nameof(Entity.DeviceCode)}/test")); }
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 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 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 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 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()}"); } }
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 RemoveByDeviceCodeAsync_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.RemoveByDeviceCodeAsync(null)); }
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_Remove_Existing_DeviceCode(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var serializer = new PersistentGrantSerializer(); 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, 12, 7, 14, 15, 16), Lifetime = 42, IsOpenId = true }; using (var session = testDb.SessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { await session.SaveAsync(new DeviceFlowCodes { DeviceCode = testDeviceCode, ID = testUserCode, ClientId = existingDeviceCode.ClientId, CreationTime = existingDeviceCode.CreationTime, Expiration = existingDeviceCode.CreationTime.AddSeconds(existingDeviceCode.Lifetime), Data = serializer.Serialize(existingDeviceCode) }); await tx.CommitAsync(); } } using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, serializer, loggerMock.Object); await store.RemoveByDeviceCodeAsync(testDeviceCode); } using (var session = testDb.SessionFactory.OpenSession()) { (await session.GetAsync <DeviceFlowCodes>(testUserCode)).Should().BeNull(); } await CleanupTestDataAsync(testDb); }
public async Task Removing_An_Unexisting_DeviceCode_Does_Not_Throw_Exception(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var serializer = new PersistentGrantSerializer(); using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, serializer, loggerMock.Object); Func <Task> act = async() => await store.RemoveByDeviceCodeAsync($"device_{Guid.NewGuid()}"); await act.Should().NotThrowAsync(); } await CleanupTestDataAsync(testDb); }
public async Task RemoveByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDeviceCodeDeleted() { using var ravenStore = GetDocumentStore(); await new DeviceFlowCodeIndex().ExecuteAsync(ravenStore); 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 }; using (var session = ravenStore.OpenSession()) { session.Store(new DeviceFlowCode { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = existingDeviceCode.ClientId, CreationTime = existingDeviceCode.CreationTime, Expiration = existingDeviceCode.CreationTime.AddSeconds(existingDeviceCode.Lifetime), Data = serializer.Serialize(existingDeviceCode) }); session.SaveChanges(); } WaitForIndexing(ravenStore); using (var session = ravenStore.OpenAsyncSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.RemoveByDeviceCodeAsync(testDeviceCode); } using (var session = ravenStore.OpenSession()) { session.Query <DeviceFlowCode>().FirstOrDefault(x => x.UserCode == testUserCode) .Should().BeNull(); } }
public async Task RemoveByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDeviceCodeDeleted(DbContextOptions <PersistedGrantDbContext> options) { 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 }; using (var context = new PersistedGrantDbContext(options)) { context.DeviceFlowCodes.Add(new DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = existingDeviceCode.ClientId, CreationTime = existingDeviceCode.CreationTime, Expiration = existingDeviceCode.CreationTime.AddSeconds(existingDeviceCode.Lifetime), Data = serializer.Serialize(existingDeviceCode) }); context.SaveChanges(); } using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); await store.RemoveByDeviceCodeAsync(testDeviceCode); } using (var context = new PersistedGrantDbContext(options)) { context.DeviceFlowCodes.FirstOrDefault(x => x.UserCode == testUserCode).Should().BeNull(); } }
public async Task RemoveByDeviceCodeAsync_WhenDeviceCodeDoesNotExists_ExpectSuccess() { var store = new DeviceFlowStore(_context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.RemoveByDeviceCodeAsync($"device_{Guid.NewGuid()}"); }