예제 #1
0
        public async Task OnPostAsync_InvalidModel()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            IActionResult post;
            var           apiResource = new ApiResource {
                Id = Random.Next()
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(apiResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                var secrets = new SecretsModel(context)
                {
                    ApiResource = new ApiResource {
                        Id = Random.Next()
                    }
                };
                post = await secrets.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            Assert.IsType <PageResult>(post);
        }
예제 #2
0
        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 secrets = new SecretsModel(context)
                {
                    Client = new Client {
                        Id = Random.Next()
                    }
                };
                post = await secrets.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            Assert.IsType <PageResult>(post);
        }
예제 #3
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            SecretsModel  secrets;
            IActionResult post;
            var           apiResourceId = Random.Next();
            var           apiResource   = new ApiResource
            {
                Id      = apiResourceId,
                Secrets = new List <ApiSecret>
                {
                    new ApiSecret {
                        Id = Random.Next()
                    }
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(apiResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                secrets = new SecretsModel(context)
                {
                    ApiResource = new ApiResource {
                        Id = apiResourceId
                    }
                };
                post = await secrets.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                apiResource = await context.ApiResources
                              .Include(x => x.Secrets)
                              .SingleOrDefaultAsync(x => x.Id.Equals(apiResourceId))
                              .ConfigureAwait(false);

                Assert.Empty(apiResource.Secrets);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Secrets", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(ApiResource.Id), key);
                Assert.Equal(secrets.ApiResource.Id, value);
            });
        }
예제 #4
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <OidcDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            SecretsModel  secrets;
            IActionResult post;
            var           client = new Client
            {
                Id            = Random.Next(),
                ClientSecrets = new List <ClientSecret>
                {
                    new ClientSecret {
                        Id = Random.Next()
                    }
                }
            };

            using (var context = new OidcDbContext(options))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new OidcDbContext(options))
            {
                secrets = new SecretsModel(context)
                {
                    Client = new Client {
                        Id = client.Id
                    }
                };
                post = await secrets.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new OidcDbContext(options))
            {
                client = await context.Clients
                         .Include(x => x.ClientSecrets)
                         .SingleOrDefaultAsync(x => x.Id.Equals(client.Id))
                         .ConfigureAwait(false);

                Assert.Empty(client.ClientSecrets);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Secrets", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(secrets.Client.Id, value);
            });
        }
예제 #5
0
        public async Task OnPostAsync_InvalidId()
        {
            // Arrange
            var context = new Mock <IConfigurationDbContext>();
            var secrets = new SecretsModel(context.Object)
            {
                Client = new Client {
                    Id = 0
                }
            };

            // Act
            var post = await secrets.OnPostAsync().ConfigureAwait(false);

            // Assert
            context.Verify(x => x.SaveChangesAsync(), Times.Never);
            Assert.IsType <PageResult>(post);
        }
예제 #6
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string secret1OriginalValue = "Original Value";
            const string secret1EditedValue   = "Edited Value";
            const string newSecretValue       = "New Value";
            var          databaseName         = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options = new DbContextOptionsBuilder <OidcDbContext>()
                                   .UseInMemoryDatabase(databaseName)
                                   .Options;
            SecretsModel  secrets;
            IActionResult post;
            var           secret1 = new ClientSecret
            {
                Id    = Random.Next(),
                Value = secret1OriginalValue
            };
            var secret2 = new ClientSecret {
                Id = Random.Next()
            };
            var client = new Client
            {
                Id            = Random.Next(),
                ClientSecrets = new List <ClientSecret>
                {
                    secret1,
                    secret2
                }
            };

            using (var context = new OidcDbContext(options))
            {
                context.Add(client);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new OidcDbContext(options))
            {
                secrets = new SecretsModel(context)
                {
                    Client = new Client
                    {
                        Id            = client.Id,
                        ClientSecrets = new List <ClientSecret>
                        {
                            new ClientSecret
                            {
                                Id    = secret1.Id,
                                Value = secret1EditedValue
                            },
                            new ClientSecret {
                                Value = newSecretValue
                            }
                        }
                    }
                };
                post = await secrets.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new OidcDbContext(options))
            {
                client = await context.Clients
                         .Include(x => x.ClientSecrets)
                         .SingleOrDefaultAsync(x => x.Id.Equals(client.Id))
                         .ConfigureAwait(false);

                secret1 = client.ClientSecrets.SingleOrDefault(x => x.Id.Equals(secret1.Id));
                secret2 = client.ClientSecrets.SingleOrDefault(x => x.Id.Equals(secret2.Id));
                var newSecret = client.ClientSecrets.SingleOrDefault(x => x.Value.Equals(newSecretValue));

                Assert.NotNull(secret1);
                Assert.Equal(secret1EditedValue, secret1.Value);
                Assert.Null(secret2);
                Assert.NotNull(newSecret);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Secrets", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(secrets.Client.Id, value);
            });
        }
예제 #7
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string secret1OriginalValue = "Original Value";
            const string secret1EditedValue   = "Edited Value";
            const string newSecretValue       = "New Value";
            var          databaseName         = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                   .UseInMemoryDatabase(databaseName)
                                   .Options;
            SecretsModel  secrets;
            IActionResult post;
            var           secret1 = new ApiSecret
            {
                Id    = Random.Next(),
                Value = secret1OriginalValue
            };
            var secret2 = new ApiSecret {
                Id = Random.Next()
            };
            var apiResourceId = Random.Next();
            var apiResource   = new ApiResource
            {
                Id      = apiResourceId,
                Secrets = new List <ApiSecret>
                {
                    secret1,
                    secret2
                }
            };

            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                context.Add(apiResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                secrets = new SecretsModel(context)
                {
                    ApiResource = new ApiResource
                    {
                        Id      = apiResourceId,
                        Secrets = new List <ApiSecret>
                        {
                            new ApiSecret
                            {
                                Id    = secret1.Id,
                                Value = secret1EditedValue
                            },
                            new ApiSecret {
                                Value = newSecretValue
                            }
                        }
                    }
                };
                post = await secrets.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                apiResource = await context.ApiResources
                              .Include(x => x.Secrets)
                              .SingleOrDefaultAsync(x => x.Id.Equals(apiResourceId))
                              .ConfigureAwait(false);

                secret1 = apiResource.Secrets.SingleOrDefault(x => x.Id.Equals(secret1.Id));
                secret2 = apiResource.Secrets.SingleOrDefault(x => x.Id.Equals(secret2.Id));
                var newSecret = apiResource.Secrets.SingleOrDefault(x => x.Value.Equals(newSecretValue));

                Assert.NotNull(secret1);
                Assert.Equal(secret1EditedValue, secret1.Value);
                Assert.Null(secret2);
                Assert.NotNull(newSecret);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/Secrets", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(ApiResource.Id), key);
                Assert.Equal(secrets.ApiResource.Id, value);
            });
        }