Esempio n. 1
0
        public async Task OnPostAsync_InvalidModel()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            IActionResult post;
            var           client = new Client {
                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))
            {
                var scopes = new ScopesModel(context)
                {
                    Client = new Client {
                        Id = Random.Next()
                    }
                };
                post = await scopes.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            Assert.IsType <PageResult>(post);
        }
Esempio n. 2
0
        public async Task OnPostAsync_AllRemoved()
        {
            // Arrange
            var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}";
            var options      = new DbContextOptionsBuilder <IdentityServerDbContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;
            ScopesModel   scopes;
            IActionResult post;
            var           clientId = Random.Next();
            var           client   = new Client
            {
                Id            = clientId,
                AllowedScopes = new List <ClientScope>
                {
                    new ClientScope {
                        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))
            {
                scopes = new ScopesModel(context)
                {
                    Client = new Client {
                        Id = clientId
                    }
                };
                post = await scopes.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                client = await context.Clients
                         .Include(x => x.AllowedScopes)
                         .SingleOrDefaultAsync(x => x.Id.Equals(clientId))
                         .ConfigureAwait(false);

                Assert.Empty(client.AllowedScopes);
            }

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

            Assert.Equal("../Details/Scopes", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(scopes.Client.Id, value);
            });
        }
Esempio n. 3
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 ApiResource {
                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 scopes = new ScopesModel(context)
                {
                    ApiResource = new ApiResource {
                        Id = Random.Next()
                    }
                };
                post = await scopes.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            Assert.IsType <PageResult>(post);
        }
Esempio n. 4
0
        public async Task OnPostAsync_InvalidId()
        {
            // Arrange
            var context = new Mock <IConfigurationDbContext>();
            var scopes  = new ScopesModel(context.Object)
            {
                Client = new Client {
                    Id = 0
                }
            };

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

            // Assert
            context.Verify(x => x.SaveChangesAsync(), Times.Never);
            Assert.IsType <PageResult>(post);
        }
Esempio n. 5
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string scope1OriginalScope = "Original Scope";
            const string scope1EditedScope   = "Edited Scope";
            const string newScopeScope       = "New Scope";
            var          databaseName        = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options             = new DbContextOptionsBuilder <IdentityServerDbContext>()
                                               .UseInMemoryDatabase(databaseName)
                                               .Options;
            ScopesModel   scopes;
            IActionResult post;
            var           scope1 = new ClientScope
            {
                Id    = Random.Next(),
                Scope = scope1OriginalScope
            };
            var scope2 = new ClientScope {
                Id = Random.Next()
            };
            var clientId = Random.Next();
            var client   = new Client
            {
                Id            = clientId,
                AllowedScopes = new List <ClientScope>
                {
                    scope1,
                    scope2
                }
            };

            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))
            {
                scopes = new ScopesModel(context)
                {
                    Client = new Client
                    {
                        Id            = clientId,
                        AllowedScopes = new List <ClientScope>
                        {
                            new ClientScope
                            {
                                Id    = scope1.Id,
                                Scope = scope1EditedScope
                            },
                            new ClientScope {
                                Scope = newScopeScope
                            }
                        }
                    }
                };
                post = await scopes.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object))
            {
                client = await context.Clients
                         .Include(x => x.AllowedScopes)
                         .SingleOrDefaultAsync(x => x.Id.Equals(clientId))
                         .ConfigureAwait(false);

                scope1 = client.AllowedScopes.SingleOrDefault(x => x.Id.Equals(scope1.Id));
                scope2 = client.AllowedScopes.SingleOrDefault(x => x.Id.Equals(scope2.Id));
                var newScope = client.AllowedScopes.SingleOrDefault(x => x.Scope.Equals(newScopeScope));

                Assert.NotNull(scope1);
                Assert.Equal(scope1EditedScope, scope1.Scope);
                Assert.Null(scope2);
                Assert.NotNull(newScope);
            }

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

            Assert.Equal("../Details/Scopes", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(Client.Id), key);
                Assert.Equal(scopes.Client.Id, value);
            });
        }
Esempio n. 6
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string scope1OriginalName = "Original Name";
            const string scope1EditedName   = "Edited Name";
            const string newScopeName       = "New Name";
            var          databaseName       = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options            = new DbContextOptionsBuilder <OidcDbContext>()
                                              .UseInMemoryDatabase(databaseName)
                                              .Options;
            ScopesModel   scopes;
            IActionResult post;
            var           scope1 = new ApiScope
            {
                Id   = Random.Next(),
                Name = scope1OriginalName
            };
            var scope2 = new ApiScope {
                Id = Random.Next()
            };
            var client = new ApiResource
            {
                Id     = Random.Next(),
                Scopes = new List <ApiScope>
                {
                    scope1,
                    scope2
                }
            };

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

            // Act
            using (var context = new OidcDbContext(options))
            {
                scopes = new ScopesModel(context)
                {
                    ApiResource = new ApiResource
                    {
                        Id     = client.Id,
                        Scopes = new List <ApiScope>
                        {
                            new ApiScope
                            {
                                Id   = scope1.Id,
                                Name = scope1EditedName
                            },
                            new ApiScope {
                                Name = newScopeName
                            }
                        }
                    }
                };
                post = await scopes.OnPostAsync().ConfigureAwait(false);
            }

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

                scope1 = client.Scopes.SingleOrDefault(x => x.Id.Equals(scope1.Id));
                scope2 = client.Scopes.SingleOrDefault(x => x.Id.Equals(scope2.Id));
                var newScope = client.Scopes.SingleOrDefault(x => x.Name.Equals(newScopeName));

                Assert.NotNull(scope1);
                Assert.Equal(scope1EditedName, scope1.Name);
                Assert.Null(scope2);
                Assert.NotNull(newScope);
            }

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

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