public async Task OnPostAsync_InvalidModel() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <OidcDbContext>() .UseInMemoryDatabase(databaseName) .Options; IActionResult post; var client = new Client { Id = Random.Next() }; using (var context = new OidcDbContext(options)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new OidcDbContext(options)) { var idPRestrictions = new IdPRestrictionsModel(context) { Client = new Client { Id = Random.Next() } }; post = await idPRestrictions.OnPostAsync().ConfigureAwait(false); } // Assert Assert.IsType <PageResult>(post); }
public async Task OnPostAsync_AllRemoved() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; IdPRestrictionsModel idPRestrictions; IActionResult post; var clientId = Random.Next(); var client = new Client { Id = clientId, IdentityProviderRestrictions = new List <ClientIdPRestriction> { new ClientIdPRestriction { Id = Random.Next() } } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { idPRestrictions = new IdPRestrictionsModel(context) { Client = new Client { Id = clientId } }; post = await idPRestrictions.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { client = await context.Clients .Include(x => x.IdentityProviderRestrictions) .SingleOrDefaultAsync(x => x.Id.Equals(clientId)) .ConfigureAwait(false); Assert.Empty(client.IdentityProviderRestrictions); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/IdPRestrictions", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Client.Id), key); Assert.Equal(idPRestrictions.Client.Id, value); }); }
public async Task OnPostAsync_InvalidId() { // Arrange var context = new Mock <IConfigurationDbContext>(); var idPRestrictions = new IdPRestrictionsModel(context.Object) { Client = new Client { Id = 0 } }; // Act var post = await idPRestrictions.OnPostAsync().ConfigureAwait(false); // Assert context.Verify(x => x.SaveChangesAsync(), Times.Never); Assert.IsType <PageResult>(post); }
public async Task OnPostAsync() { // Arrange const string idPRestriction1OriginalProvider = "Original Provider"; const string idPRestriction1EditedProvider = "Edited Provider"; const string newIdPRestrictionProvider = "New Provider"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <OidcDbContext>() .UseInMemoryDatabase(databaseName) .Options; IdPRestrictionsModel idPRestrictions; IActionResult post; var idPRestriction1 = new ClientIdPRestriction { Id = Random.Next(), Provider = idPRestriction1OriginalProvider }; var idPRestriction2 = new ClientIdPRestriction { Id = Random.Next() }; var client = new Client { Id = Random.Next(), IdentityProviderRestrictions = new List <ClientIdPRestriction> { idPRestriction1, idPRestriction2 } }; using (var context = new OidcDbContext(options)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new OidcDbContext(options)) { idPRestrictions = new IdPRestrictionsModel(context) { Client = new Client { Id = client.Id, IdentityProviderRestrictions = new List <ClientIdPRestriction> { new ClientIdPRestriction { Id = idPRestriction1.Id, Provider = idPRestriction1EditedProvider }, new ClientIdPRestriction { Provider = newIdPRestrictionProvider } } } }; post = await idPRestrictions.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new OidcDbContext(options)) { client = await context.Clients .Include(x => x.IdentityProviderRestrictions) .SingleOrDefaultAsync(x => x.Id.Equals(client.Id)) .ConfigureAwait(false); idPRestriction1 = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Id.Equals(idPRestriction1.Id)); idPRestriction2 = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Id.Equals(idPRestriction2.Id)); var newIdPRestriction = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Provider.Equals(newIdPRestrictionProvider)); Assert.NotNull(idPRestriction1); Assert.Equal(idPRestriction1EditedProvider, idPRestriction1.Provider); Assert.Null(idPRestriction2); Assert.NotNull(newIdPRestriction); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/IdPRestrictions", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Client.Id), key); Assert.Equal(idPRestrictions.Client.Id, value); }); }