// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.ApiAuthorization.IdentityServer.Configuration; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; using System.Linq; using Xunit; namespace Microsoft.AspNetCore.ApiAuthorization.IdentityServer { public class ConfigureApiResourcesTests { [Fact] public void GetApiResources_ReadsApisFromConfiguration() { // Arrange var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["MyAPI:Profile"] = "API" }).Build(); var localApiDescriptor = new TestLocalApiDescriptor(); var configurationLoader = new ConfigureApiResources( configuration, localApiDescriptor, new TestLogger()); // Act var resources = configurationLoader.GetApiResources(); // Assert var resource = Assert.Single(resources); var scope = Assert.Single(resource.Scopes); Assert.Equal("MyAPI", resource.Name); Assert.Equal("MyAPI", scope.Name); } [Fact] public void GetApiResources_ReadsApiScopesFromConfiguration() { // Arrange var expectedScopes = new[] { "First", "Second", "Third" }; var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["MyAPI:Profile"] = "API", ["MyAPI:Scopes"] = "First Second Third" }).Build(); var localApiDescriptor = new TestLocalApiDescriptor(); var configurationLoader = new ConfigureApiResources( configuration, localApiDescriptor, new TestLogger()); // Act var resources = configurationLoader.GetApiResources(); // Assert var resource = Assert.Single(resources); Assert.Equal("MyAPI", resource.Name); Assert.NotNull(resource.Scopes); Assert.Equal(3, resource.Scopes.Count); Assert.Equal(expectedScopes, resource.Scopes.Select(s => s.Name).ToArray()); } [Fact] public void GetApiResources_DetectsLocallyRegisteredApis() { // Arrange var configuration = new ConfigurationBuilder().Build(); var localApiDescriptor = new TestLocalApiDescriptor(new Dictionary { ["MyAPI"] = new ResourceDefinition { Profile = ApplicationProfiles.IdentityServerJwt } }); var configurationLoader = new ConfigureApiResources( configuration, localApiDescriptor, new TestLogger()); // Act var resources = configurationLoader.GetApiResources(); // Assert var resource = Assert.Single(resources); var scope = Assert.Single(resource.Scopes); Assert.Equal("MyAPI", resource.Name); Assert.Equal("MyAPI", scope.Name); } [Fact] public void Configure_AddsResourcesToExistingResourceList() { // Arrange var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["MyAPI:Profile"] = "API" }).Build(); var localApiDescriptor = new TestLocalApiDescriptor(); var configurationLoader = new ConfigureApiResources( configuration, localApiDescriptor, new TestLogger()); var options = new ApiAuthorizationOptions(); // Act configurationLoader.Configure(options); // Assert var resource = Assert.Single(options.ApiResources); var scope = Assert.Single(resource.Scopes); Assert.Equal("MyAPI", resource.Name); Assert.Equal("MyAPI", scope.Name); } private class TestLocalApiDescriptor : IIdentityServerJwtDescriptor { private readonly IDictionary _definitions; public TestLocalApiDescriptor() : this(new Dictionary()) { } public TestLocalApiDescriptor(IDictionary definitions) { _definitions = definitions; } public IDictionary GetResourceDefinitions() { return _definitions; } } } }