Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Oct 19, 2024
1 parent 93816ec commit 07de3e5
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/Ocelot/Configuration/AuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public AuthenticationOptions(List<string> allowedScopes, string authenticationPr
public AuthenticationOptions(List<string> allowedScopes, string[] authenticationProviderKeys, List<string> requiredRole, string scopeKey, string roleKey, string policyName)
{
AllowedScopes = allowedScopes;
AuthenticationProviderKey = string.Empty;
AuthenticationProviderKeys = authenticationProviderKeys ?? Array.Empty<string>();
BuildAuthenticationProviderKeys(null, authenticationProviderKeys);
PolicyName = policyName;
RequiredRole = requiredRole;
ScopeKey = scopeKey;
Expand All @@ -41,6 +40,8 @@ private void BuildAuthenticationProviderKeys(string legacyKey, string[] keys)
keys ??= Array.Empty<string>();
if (string.IsNullOrEmpty(legacyKey))
{
AuthenticationProviderKeys = keys;
AuthenticationProviderKey = string.Empty;
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Ocelot.Configuration.Creator;

public class AuthenticationOptionsCreator : IAuthenticationOptionsCreator
{
public AuthenticationOptions Create(FileRoute route) => new(route.AuthenticationOptions);
public AuthenticationOptions Create(FileRoute route) => new(route?.AuthenticationOptions ?? new());
}
27 changes: 5 additions & 22 deletions src/Ocelot/Configuration/File/FileAuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,11 @@ public FileAuthenticationOptions(FileAuthenticationOptions from)
public override string ToString() => new StringBuilder()
.Append($"{nameof(AuthenticationProviderKey)}:'{AuthenticationProviderKey}',")
.Append($"{nameof(AuthenticationProviderKeys)}:[{string.Join(',', AuthenticationProviderKeys.Select(x => $"'{x}'"))}],")
.Append($"{nameof(AllowedScopes)}:[{string.Join(',', AllowedScopes.Select(x => $"'{x}'"))}]")
.Append($"{nameof(AllowedScopes)}:[{string.Join(',', AllowedScopes.Select(x => $"'{x}'"))}],")
.Append($"{nameof(RequiredRole)}:[").AppendJoin(',', RequiredRole).Append("],")
.Append($"{nameof(ScopeKey)}:[").AppendJoin(',', ScopeKey).Append("],")
.Append($"{nameof(RoleKey)}:[").AppendJoin(',', RoleKey).Append("],")
.Append($"{nameof(PolicyName)}:[").AppendJoin(',', PolicyName).Append(']')
.ToString();

public string ToString2()
{
var sb = new StringBuilder();
sb.Append($"{nameof(AuthenticationProviderKey)}:{AuthenticationProviderKey},{nameof(AllowedScopes)}:[");
sb.AppendJoin(',', AllowedScopes);
sb.Append("]");
sb.Append($",{nameof(RequiredRole)}:[");
sb.AppendJoin(',', RequiredRole);
sb.Append("]");
sb.Append($",{nameof(ScopeKey)}:[");
sb.AppendJoin(',', ScopeKey);
sb.Append("]");
sb.Append($",{nameof(RoleKey)}:[");
sb.AppendJoin(',', RoleKey);
sb.Append("]");
sb.Append($",{nameof(PolicyName)}:[");
sb.AppendJoin(',', PolicyName);
sb.Append("]");
return sb.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Create_OptionsObjIsNotNull_CreatedSuccessfully(bool isAuthentication
// Arrange
string authenticationProviderKey = !isAuthenticationProviderKeys ? "Test" : null;
string[] authenticationProviderKeys = isAuthenticationProviderKeys ?
new string[] { "Test #1", "Test #2" } : null;
new string[] { "Test #1", "Test #2" } : Array.Empty<string>();
var fileRoute = new FileRoute()
{
AuthenticationOptions = new FileAuthenticationOptions
Expand All @@ -56,11 +56,13 @@ public void Create_OptionsObjIsNotNull_CreatedSuccessfully(bool isAuthentication
AuthenticationProviderKeys = authenticationProviderKeys,
},
};
var expected = new AuthenticationOptionsBuilder()
.WithAllowedScopes(fileRoute.AuthenticationOptions?.AllowedScopes)
.WithAuthenticationProviderKey(authenticationProviderKey)
.WithAuthenticationProviderKeys(authenticationProviderKeys)
.Build();

var b = new AuthenticationOptionsBuilder()
.WithAllowedScopes(fileRoute.AuthenticationOptions?.AllowedScopes);
b = isAuthenticationProviderKeys
? b.WithAuthenticationProviderKeys(authenticationProviderKeys)
: b.WithAuthenticationProviderKey(authenticationProviderKey);
var expected = b.Build();

// Act
var actual = _authOptionsCreator.Create(fileRoute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ public void Configuration_is_invalid_with_invalid_authentication_provider()
this.Given(x => x.GivenAConfiguration(route))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "Authentication Options AuthenticationProviderKey:'Test',AuthenticationProviderKeys:['Test #1','Test #2'],AllowedScopes:[] is unsupported authentication provider"))
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "Authentication Options AuthenticationProviderKey:Test,AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider"))
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "Authentication Options AuthenticationProviderKey:'Test',AuthenticationProviderKeys:['Test #1','Test #2'],AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider"))
.BDDfy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ public void should_not_be_valid_if_specified_authentication_provider_isnt_regist
this.Given(_ => GivenThe(fileRoute))
.When(_ => WhenIValidate())
.Then(_ => ThenTheResultIsInvalid())
.And(_ => ThenTheErrorsContains($"Authentication Options AuthenticationProviderKey:'JwtLads',AuthenticationProviderKeys:[],AllowedScopes:[] is unsupported authentication provider"))
.And(_ => ThenTheErrorsContains($"Authentication Options AuthenticationProviderKey:JwtLads,AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider"))
.And(_ => ThenTheErrorsContains($"Authentication Options AuthenticationProviderKey:'JwtLads',AuthenticationProviderKeys:[],AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider"))
.BDDfy();
}

Expand Down

0 comments on commit 07de3e5

Please sign in to comment.