public void OpenIdConnectRequestClient_Constructor()
        {
            // Arrange
            var helper                = new TestHelper();
            var response              = new HttpResponseMessage();
            var clientFactory         = helper.CreateHttpClientFactory(response);
            var tokenHandler          = new JwtSecurityTokenHandler();
            var authClientOptions     = new AuthClientOptions();
            var mockAuthClientOptions = new Mock <IOptionsMonitor <AuthClientOptions> >();

            mockAuthClientOptions.Setup(m => m.CurrentValue).Returns(authClientOptions);
            var openIdConnectOptions     = new OpenIdConnectOptions();
            var mockOpenIdConnectOptions = new Mock <IOptionsMonitor <OpenIdConnectOptions> >();

            mockOpenIdConnectOptions.Setup(m => m.CurrentValue).Returns(openIdConnectOptions);
            var mockJsonSerializeOptions = new Mock <IOptionsMonitor <JsonSerializerOptions> >();
            var mockLogger = new Mock <ILogger <OpenIdConnectRequestClient> >();

            // Act
            var client = new OpenIdConnectRequestClient(clientFactory, tokenHandler, mockAuthClientOptions.Object, mockOpenIdConnectOptions.Object, mockJsonSerializeOptions.Object, mockLogger.Object);

            // Assert
            Assert.NotNull(client);
            client.AuthClientOptions.Should().Be(authClientOptions);
            client.OpenIdConnectOptions.Should().Be(openIdConnectOptions);
            client.Client.Should().NotBeNull();
        }
        public void AddPimsKeycloakService_Success()
        {
            // Arrange
            var services = new ServiceCollection();

            var mockPimsService = new Mock <IPimsRepository>();

            services.AddScoped((s) => mockPimsService.Object);
            var mockMapper = new Mock <IMapper>();

            services.AddScoped((s) => mockMapper.Object);
            var mockLogger = new Mock <ILogger <IPimsKeycloakService> >();

            services.AddScoped((s) => mockLogger.Object);
            var user = PrincipalHelper.CreateForPermission();

            services.AddScoped((s) => user);

            var mockConfig = new Mock <IOptions <KeycloakOptions> >();
            var options    = new KeycloakOptions()
            {
                Authority = "test",
                Audience  = "test",
                Client    = "test",
                Admin     = new KeycloakAdminOptions
                {
                    Authority = "test",
                    Users     = "test"
                },
                OpenIdConnect = new OpenIdConnectOptions
                {
                    Token    = "test",
                    UserInfo = "test"
                },
                ServiceAccount = new KeycloakServiceAccountOptions
                {
                    Client = "test",
                    Secret = "test"
                }
            };

            mockConfig.Setup(m => m.Value).Returns(options);
            services.AddScoped((s) => mockConfig.Object);
            var mockOpenIdConnectRequestClient = new Mock <IOpenIdConnectRequestClient>();
            var clientOptions = new AuthClientOptions();

            mockOpenIdConnectRequestClient.Setup(m => m.AuthClientOptions).Returns(clientOptions);
            services.AddScoped((s) => mockOpenIdConnectRequestClient.Object);

            // Act
            var result   = services.AddPimsKeycloakService();
            var provider = result.BuildServiceProvider();
            var service  = provider.GetService <IPimsKeycloakService>();

            // Assert
            result.Should().NotBeNull();
            result.Count.Should().Be(8);
            provider.Should().NotBeNull();
            service.Should().NotBeNull();
        }
示例#3
0
 public void Validate_ConfigurationException(AuthClientOptions options)
 {
     // Arrange
     // Act
     // Assert
     Assert.Throws <ConfigurationException>(() => options.Validate());
 }
示例#4
0
        public void Validate_Success()
        {
            // Arrange
            var options = new AuthClientOptions()
            {
                Audience  = "Audience",
                Authority = "Authority",
                Client    = "Client",
            };

            // Act
            options.Validate();

            // Assert
            Assert.NotNull(options);
        }
        public void AddKeycloakService_Success()
        {
            // Arrange
            var services   = new ServiceCollection();
            var mockConfig = new Mock <IOptions <KeycloakOptions> >();
            var options    = new KeycloakOptions()
            {
                Authority = "test",
                Audience  = "test",
                Client    = "test",
                Admin     = new KeycloakAdminOptions
                {
                    Authority = "test",
                    Users     = "test"
                },
                OpenIdConnect = new OpenIdConnectOptions
                {
                    Token    = "test",
                    UserInfo = "test"
                },
                ServiceAccount = new KeycloakServiceAccountOptions
                {
                    Client = "test",
                    Secret = "test"
                }
            };

            mockConfig.Setup(m => m.Value).Returns(options);
            services.AddScoped((s) => mockConfig.Object);
            var mockOpenIdConnectRequestClient = new Mock <IOpenIdConnectRequestClient>();
            var clientOptions = new AuthClientOptions();

            mockOpenIdConnectRequestClient.Setup(m => m.AuthClientOptions).Returns(clientOptions);
            services.AddScoped((s) => mockOpenIdConnectRequestClient.Object);

            // Act
            var result   = services.AddKeycloakService();
            var provider = result.BuildServiceProvider();
            var service  = provider.GetService <IKeycloakService>();

            // Assert
            result.Should().NotBeNull();
            result.Count.Should().Be(3);
            provider.Should().NotBeNull();
            service.Should().NotBeNull();
        }