|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| 3 | + * See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers |
| 4 | + * for more information concerning the license and the contributors participating to this project. |
| 5 | + */ |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace AspNet.Security.OAuth.Keycloak |
| 12 | +{ |
| 13 | + public static class KeycloakAuthenticationOptionsTests |
| 14 | + { |
| 15 | + public static IEnumerable<object[]> AccessTypes => new object[][] |
| 16 | + { |
| 17 | + new object[] { KeycloakAuthenticationAccessType.BearerOnly }, |
| 18 | + new object[] { KeycloakAuthenticationAccessType.Confidential }, |
| 19 | + new object[] { KeycloakAuthenticationAccessType.Public }, |
| 20 | + }; |
| 21 | + |
| 22 | + [Theory] |
| 23 | + [InlineData(null)] |
| 24 | + [InlineData("")] |
| 25 | + public static void Validate_Does_Not_Throw_If_ClientSecret_Is_Not_Provided_For_Public_Access_Type(string clientSecret) |
| 26 | + { |
| 27 | + // Arrange |
| 28 | + var options = new KeycloakAuthenticationOptions() |
| 29 | + { |
| 30 | + AccessType = KeycloakAuthenticationAccessType.Public, |
| 31 | + ClientId = "my-client-id", |
| 32 | + ClientSecret = clientSecret, |
| 33 | + }; |
| 34 | + |
| 35 | + // Act (no Assert) |
| 36 | + options.Validate(); |
| 37 | + } |
| 38 | + |
| 39 | + [Theory] |
| 40 | + [InlineData(KeycloakAuthenticationAccessType.BearerOnly)] |
| 41 | + [InlineData(KeycloakAuthenticationAccessType.Confidential)] |
| 42 | + public static void Validate_Throws_If_ClientSecret_Is_Null(KeycloakAuthenticationAccessType accessType) |
| 43 | + { |
| 44 | + // Arrange |
| 45 | + var options = new KeycloakAuthenticationOptions() |
| 46 | + { |
| 47 | + AccessType = accessType, |
| 48 | + ClientId = "my-client-id", |
| 49 | + ClientSecret = null, |
| 50 | + }; |
| 51 | + |
| 52 | + // Act and Assert |
| 53 | + Assert.Throws<ArgumentException>("ClientSecret", () => options.Validate()); |
| 54 | + } |
| 55 | + |
| 56 | + [Theory] |
| 57 | + [MemberData(nameof(AccessTypes))] |
| 58 | + public static void Validate_Throws_If_AuthorizationEndpoint_Is_Null(KeycloakAuthenticationAccessType accessType) |
| 59 | + { |
| 60 | + // Arrange |
| 61 | + var options = new KeycloakAuthenticationOptions() |
| 62 | + { |
| 63 | + AccessType = accessType, |
| 64 | + AuthorizationEndpoint = null, |
| 65 | + ClientId = "my-client-id", |
| 66 | + ClientSecret = "my-client-secret", |
| 67 | + }; |
| 68 | + |
| 69 | + // Act and Assert |
| 70 | + Assert.Throws<ArgumentException>("AuthorizationEndpoint", () => options.Validate()); |
| 71 | + } |
| 72 | + |
| 73 | + [Theory] |
| 74 | + [MemberData(nameof(AccessTypes))] |
| 75 | + public static void Validate_Throws_If_TokenEndpoint_Is_Null(KeycloakAuthenticationAccessType accessType) |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + var options = new KeycloakAuthenticationOptions() |
| 79 | + { |
| 80 | + AccessType = accessType, |
| 81 | + ClientId = "my-client-id", |
| 82 | + ClientSecret = "my-client-secret", |
| 83 | + TokenEndpoint = null, |
| 84 | + }; |
| 85 | + |
| 86 | + // Act and Assert |
| 87 | + Assert.Throws<ArgumentException>("TokenEndpoint", () => options.Validate()); |
| 88 | + } |
| 89 | + |
| 90 | + [Theory] |
| 91 | + [MemberData(nameof(AccessTypes))] |
| 92 | + public static void Validate_Throws_If_CallbackPath_Is_Null(KeycloakAuthenticationAccessType accessType) |
| 93 | + { |
| 94 | + // Arrange |
| 95 | + var options = new KeycloakAuthenticationOptions() |
| 96 | + { |
| 97 | + AccessType = accessType, |
| 98 | + CallbackPath = null, |
| 99 | + ClientId = "my-client-id", |
| 100 | + ClientSecret = "my-client-secret", |
| 101 | + }; |
| 102 | + |
| 103 | + // Act and Assert |
| 104 | + Assert.Throws<ArgumentException>("CallbackPath", () => options.Validate()); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments