-
Notifications
You must be signed in to change notification settings - Fork 3
feat(multiple-auth): added support combining auths as AND/OR groups #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0eac3ab
feat(multiple-auth): added support combining auths as AND/OR groups
asadali214 a4e06fd
refactored authGroupBuilder's apply method
asadali214 e1a45df
Added doc blocks to the public methods in AuthGroupBuilder
asadali214 541e63a
Throwing detailed exception message for AND Auth group
asadali214 a2f1ebf
Merge branch 'main' into 50-add-support-for-multiple-authentication
asadali214 048f9ea
Added unit test to show multiple missing required field validation er…
asadali214 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using APIMatic.Core.Authentication; | ||
| using APIMatic.Core.Test.MockTypes.Authentication; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace APIMatic.Core.Test | ||
| { | ||
| [TestFixture] | ||
| public class AuthenticationTest : TestBase | ||
| { | ||
| [Test] | ||
| public void Multiple_Authentication_Success() | ||
| { | ||
| var basicAuthManager = new BasicAuthManager("username", "password"); | ||
| var globalConfiguration = new GlobalConfiguration.Builder() | ||
| .ServerUrls(new Dictionary<Enum, string> | ||
| { | ||
| {MockServer.Server1, "http://my/path:3000/{one}"}, | ||
| }, MockServer.Server1) | ||
| .AuthManagers(new Dictionary<string, AuthManager>() | ||
| { | ||
| {"basic", basicAuthManager}, | ||
| {"header", new HeaderAuthManager("my api key", "test token")}, | ||
| {"query", new QueryAuthManager("my api key", "test token")} | ||
| }) | ||
| .HttpConfiguration(_clientConfiguration) | ||
| .Build(); | ||
|
|
||
| var request = globalConfiguration.GlobalRequestBuilder() | ||
| .Setup(HttpMethod.Get, "/auth") | ||
| .WithOrAuth(auth => auth | ||
| .Add("basic") | ||
| .AddAndGroup(innerGroup => innerGroup | ||
| .Add("header") | ||
| .Add("query"))) | ||
| .Build(); | ||
|
|
||
| Assert.AreEqual(basicAuthManager.GetBasicAuthHeader(), request.Headers["Authorization"]); | ||
| Assert.AreEqual("my api key", request.Headers["API-KEY"]); | ||
| Assert.AreEqual("test token", request.Headers["TOKEN"]); | ||
| Assert.AreEqual("my api key", request.QueryParameters["API-KEY"]); | ||
| Assert.AreEqual("test token", request.QueryParameters["TOKEN"]); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Multiple_Authentication_OR_Validation_Failure() | ||
| { | ||
| var globalConfiguration = new GlobalConfiguration.Builder() | ||
| .ServerUrls(new Dictionary<Enum, string> | ||
| { | ||
| {MockServer.Server1, "http://my/path:3000/{one}"}, | ||
| }, MockServer.Server1) | ||
| .AuthManagers(new Dictionary<string, AuthManager>() | ||
| { | ||
| {"basic", new BasicAuthManager("username", null)}, | ||
| {"header", new HeaderAuthManager("my api key", null)}, | ||
| {"query", new QueryAuthManager("my api key", "test token")} | ||
| }) | ||
| .HttpConfiguration(_clientConfiguration) | ||
| .Build(); | ||
|
|
||
| var exp = Assert.Throws<ArgumentNullException>(() => globalConfiguration.GlobalRequestBuilder() | ||
| .Setup(HttpMethod.Get, "/auth") | ||
| .WithOrAuth(auth => auth | ||
| .Add("basic") | ||
| .AddAndGroup(innerGroup => innerGroup | ||
| .Add("header") | ||
| .Add("query"))) | ||
| .Build()); | ||
|
|
||
| Assert.AreEqual("Following authentication credentials are required:\n" + | ||
| "-> Missing required header field: Authorization\n" + | ||
| "-> Missing required header field: TOKEN", exp.Message); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Multiple_Authentication_AND_Validation_Failure() | ||
asadali214 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var globalConfiguration = new GlobalConfiguration.Builder() | ||
| .ServerUrls(new Dictionary<Enum, string> | ||
| { | ||
| {MockServer.Server1, "http://my/path:3000/{one}"}, | ||
| }, MockServer.Server1) | ||
| .AuthManagers(new Dictionary<string, AuthManager>() | ||
| { | ||
| {"basic", new BasicAuthManager("username", null)}, | ||
| {"header", new HeaderAuthManager("my api key", null)}, | ||
| {"query", new QueryAuthManager("my api key", "test token")} | ||
| }) | ||
| .HttpConfiguration(_clientConfiguration) | ||
| .Build(); | ||
|
|
||
| var exp = Assert.Throws<ArgumentNullException>(() => globalConfiguration.GlobalRequestBuilder() | ||
| .Setup(HttpMethod.Get, "/auth") | ||
| .WithAndAuth(auth => auth | ||
| .Add("query") | ||
| .Add("header")) | ||
| .Build()); | ||
|
|
||
| Assert.AreEqual("Following authentication credentials are required:\n" + | ||
| "-> Missing required header field: TOKEN", exp.Message); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Multiple_Authentication_AND_All_Missing_Validation_Failure() | ||
| { | ||
| var globalConfiguration = new GlobalConfiguration.Builder() | ||
| .ServerUrls(new Dictionary<Enum, string> | ||
| { | ||
| {MockServer.Server1, "http://my/path:3000/{one}"}, | ||
| }, MockServer.Server1) | ||
| .AuthManagers(new Dictionary<string, AuthManager>() | ||
| { | ||
| {"header", new HeaderAuthManager(null, null)}, | ||
| {"query", new QueryAuthManager(null, null)} | ||
| }) | ||
| .HttpConfiguration(_clientConfiguration) | ||
| .Build(); | ||
|
|
||
| var exp = Assert.Throws<ArgumentNullException>(() => globalConfiguration.GlobalRequestBuilder() | ||
| .Setup(HttpMethod.Get, "/auth") | ||
| .WithAndAuth(auth => auth | ||
| .Add("query") | ||
| .Add("header")) | ||
| .Build()); | ||
|
|
||
| Assert.AreEqual("Following authentication credentials are required:\n" + | ||
| "-> Missing required query field: API-KEY\n" + | ||
| "-> Missing required query field: TOKEN\n" + | ||
| "-> Missing required header field: API-KEY\n" + | ||
| "-> Missing required header field: TOKEN", exp.Message); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Multiple_Authentication_AND_with_nested_OR_Validation_Failure() | ||
| { | ||
| var globalConfiguration = new GlobalConfiguration.Builder() | ||
| .ServerUrls(new Dictionary<Enum, string> | ||
| { | ||
| {MockServer.Server1, "http://my/path:3000/{one}"}, | ||
| }, MockServer.Server1) | ||
| .AuthManagers(new Dictionary<string, AuthManager>() | ||
| { | ||
| {"basic", new BasicAuthManager("username", null)}, | ||
| {"header", new HeaderAuthManager("my api key", null)}, | ||
| {"query", new QueryAuthManager("my api key", "test token")} | ||
| }) | ||
| .HttpConfiguration(_clientConfiguration) | ||
| .Build(); | ||
|
|
||
| var exp = Assert.Throws<ArgumentNullException>(() => globalConfiguration.GlobalRequestBuilder() | ||
| .Setup(HttpMethod.Get, "/auth") | ||
| .WithAndAuth(auth => auth | ||
| .Add("query") | ||
| .AddOrGroup(innerGroup => innerGroup | ||
| .Add("basic") | ||
| .Add("header"))) | ||
| .Build()); | ||
|
|
||
| Assert.AreEqual("Following authentication credentials are required:\n" + | ||
| "-> Missing required header field: Authorization\n" + | ||
| "-> Missing required header field: TOKEN", exp.Message); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
APIMatic.Core.Test/MockTypes/Authentication/HeaderAuthManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // <copyright file="HeaderAuthManager.cs" company="APIMatic"> | ||
| // Copyright (c) APIMatic. All rights reserved. | ||
| // </copyright> | ||
| using APIMatic.Core.Authentication; | ||
|
|
||
| namespace APIMatic.Core.Test.MockTypes.Authentication | ||
| { | ||
| /// <summary> | ||
| /// QueryAuthManager Class. | ||
| /// </summary> | ||
| public class HeaderAuthManager : AuthManager | ||
| { | ||
| public HeaderAuthManager(string apiKey, string token) | ||
| { | ||
| Parameters(paramBuilder => paramBuilder | ||
| .Header(header => header.Setup("API-KEY", apiKey).Required()) | ||
| .Header(header => header.Setup("TOKEN", token).Required())); | ||
| } | ||
| } | ||
| } |
27 changes: 0 additions & 27 deletions
27
APIMatic.Core.Test/MockTypes/Authentication/IBasicAuthCredentials.cs
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
APIMatic.Core.Test/MockTypes/Authentication/QueryAuthManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // <copyright file="QueryAuthManager.cs" company="APIMatic"> | ||
| // Copyright (c) APIMatic. All rights reserved. | ||
| // </copyright> | ||
| using APIMatic.Core.Authentication; | ||
|
|
||
| namespace APIMatic.Core.Test.MockTypes.Authentication | ||
| { | ||
| /// <summary> | ||
| /// QueryAuthManager Class. | ||
| /// </summary> | ||
| public class QueryAuthManager : AuthManager | ||
| { | ||
| public QueryAuthManager(string apiKey, string token) | ||
| { | ||
| Parameters(paramBuilder => paramBuilder | ||
| .Query(query => query.Setup("API-KEY", apiKey).Required()) | ||
| .Query(query => query.Setup("TOKEN", token).Required())); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.