Skip to content

Commit

Permalink
Merge pull request #4 from hakimms/abdulhakim/Resolve-response-check
Browse files Browse the repository at this point in the history
Abdulhakim/resolve response check issue and remove script added changes
  • Loading branch information
hakimms authored Jul 28, 2023
2 parents fc9dc59 + 4cd4fc5 commit 6bd0aea
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Task SetValueAsync(object result, CancellationToken cancellationToken)
{
if (result == null)
{
throw new ArgumentNullException(AuthenticationEventResource.Ex_Invalid_Return);
throw new ResponseValidationException(AuthenticationEventResource.Ex_Invalid_Return);
}

if (result is AuthenticationEventResponse action)
Expand All @@ -83,7 +83,7 @@ public Task SetValueAsync(object result, CancellationToken cancellationToken)
}
catch (Exception ex)
{
Response = Request.Failed(ex, !(ex is ValidationException)).Result;
Response = Request.Failed(ex, true).Result;
}

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents
{
/// <summary>
/// Exception class for response validations
/// </summary>
public class ResponseValidationException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="ResponseValidationException"/> class.
/// </summary>
/// <param name="message"></param>
public ResponseValidationException(string message)
: base(message) { }

/// <summary>
/// Initializes a new instance of the <see cref="ResponseValidationException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="innerException"></param>
public ResponseValidationException(string message, Exception innerException)
: base(message, innerException) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ internal AuthenticationEventRequestBase CreateEventRequest(HttpRequestMessage re
// ResponseSchema ?? string.Empty,
ResponseTemplate ?? string.Empty);

responseInfo.SetValue(eventRequest, eventResponse);

if (args != null && args.Length != 0)
{
eventRequest.InstanceCreated(args);
Expand All @@ -76,6 +74,8 @@ internal AuthenticationEventRequestBase CreateEventRequest(HttpRequestMessage re
Helpers.ValidateGraph(eventRequest);
}

responseInfo.SetValue(eventRequest, eventResponse);

return eventRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents.Framework
/// <typeparam name="TData">The EventData model related to the request.</typeparam>
/// <seealso cref="AuthenticationEventResponse" />
/// <seealso cref="AuthenticationEventData" />
public abstract class AuthenticationEventRequest<TResponse, TData> : AuthenticationEventRequestBase where TResponse : AuthenticationEventResponse where TData : AuthenticationEventData
public abstract class AuthenticationEventRequest<TResponse, TData> : AuthenticationEventRequestBase
where TResponse : AuthenticationEventResponse , new()
where TData : AuthenticationEventData
{
/// <summary>Initializes a new instance of the <see cref="AuthenticationEventRequest{T, K}" /> class.</summary>
/// <param name="request">The request.</param>
Expand All @@ -23,7 +25,6 @@ internal AuthenticationEventRequest(HttpRequestMessage request) : base(request)
/// <value>The response.</value>
///
[JsonPropertyName("response")]
[Required]
public TResponse Response { get; set; }

/// <summary>Gets or sets the related EventData model.</summary>
Expand Down Expand Up @@ -62,6 +63,11 @@ public async override Task<AuthenticationEventResponse> Completed()

internal override Task<AuthenticationEventResponse> Failed(Exception exception, bool internalError)
{
if (Response == null)
{
Response = new TResponse();
}

Response.MarkAsFailed(exception, internalError);
return Task.FromResult<AuthenticationEventResponse>((TResponse)Response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents.Framework.Validators;
using System.ComponentModel.DataAnnotations;
using System.Net.Http;
using System.Text.Json.Serialization;

Expand All @@ -11,7 +10,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents.Framework
/// <summary>Abstract class that wraps any request that relies on cloud events.</summary>
/// <typeparam name="TResponse">The Cloud Event Response.</typeparam>
/// <typeparam name="TData">The Cloud Event Data.</typeparam>
public abstract class CloudEventRequest<TResponse, TData> : AuthenticationEventRequest<TResponse, TData> where TResponse : AuthenticationEventResponse where TData : CloudEventData
public abstract class CloudEventRequest<TResponse, TData> : AuthenticationEventRequest<TResponse, TData>
where TResponse : AuthenticationEventResponse, new()
where TData : CloudEventData
{
/// <summary>Gets or sets the source.</summary>
/// <value>The source.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override bool IsValid(object value)
{
return value is not null
&& value is IEnumerable<object> obj
&& !obj.Where(x => x == null).Any();
&& !obj.Any(x => x == null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<EmbeddedResource Include="Payloads\TokenIssuanceStart\ConversionPayload.json" />
<EmbeddedResource Include="Payloads\TokenIssuanceStart\InvalidActionResponse.json" />
<EmbeddedResource Include="Payloads\TokenIssuanceStart\NoActionResponse.json" />
<EmbeddedResource Include="Payloads\TokenIssuanceStart\NullResponse.json" />
<EmbeddedResource Include="Payloads\TokenIssuanceStart\ExpectedPayload.json" />
<EmbeddedResource Include="Payloads\TokenIssuanceStart\QueryParameters.json" />
</ItemGroup>
Expand All @@ -30,15 +31,6 @@
<PackageReference Include="nunit" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit3TestAdapter" />

<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>

<ProjectReference Include="$(AzureCoreTestFramework)" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ public async Task TokenIssuanceStartObjectModelTest()
Assert.True(TestHelper.DoesPayloadMatch(Payload.TokenIssuanceStart.ExpectedPayload, httpResponseMessage.Content.ReadAsStringAsync().Result));
}

/// <summary>Tests the OnTokenIssuanceStart request and response object model when the response is set to null</summary>
[Test]
[Description("Tests the OnTokenIssuanceStart request and response object model when the response is set to null")]
public async Task TokenIssuanceStartObjectModelNullResponseTest()
{
HttpResponseMessage httpResponseMessage = await TestHelper.EventResponseBaseTest(eventsResponseHandler =>
{
if (eventsResponseHandler.Request is TokenIssuanceStartRequest request)
{
request.Response = null;

eventsResponseHandler.SetValueAsync(request.Completed().Result, CancellationToken.None);
}
});

Assert.AreEqual(HttpStatusCode.InternalServerError, httpResponseMessage.StatusCode);
Assert.True(DoesPayloadMatch(Payload.TokenIssuanceStart.NullResponsePayload, httpResponseMessage.Content.ReadAsStringAsync().Result));
}

[Test]
[TestCase(ActionTestTypes.NullClaims)]
[TestCase(ActionTestTypes.EmptyClaims)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"errors": [ "Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return." ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,15 @@ public static string TokenIssuanceStartQueryParameter
return PayloadHelper.GetPayload("TokenIssuanceStart.QueryParameters.json");
}
}

/// <summary> Gets the payload error message when response is set to null </summary>
/// <value> The payload with error message </value>
public static string NullResponsePayload
{
get
{
return PayloadHelper.GetPayload("TokenIssuanceStart.NullResponse.json");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents.Tests
{

internal class TestAuthResponse : AuthenticationEventResponse
{
internal TestAuthResponse(HttpStatusCode code, string content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,44 +342,44 @@ public static (TokenIssuanceAction action, HttpStatusCode expectReturnCode, stri
{
case ActionTestTypes.NullClaims:
return (new ProvideClaimsForToken(null),
HttpStatusCode.BadRequest,
HttpStatusCode.InternalServerError,
"{\"errors\":[\"TokenIssuanceStartResponse: ProvideClaimsForToken: The Claims field is required.\"]}");
case ActionTestTypes.EmptyClaims:
return (new ProvideClaimsForToken(),
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{}}]}}");
case ActionTestTypes.NullClaimId:
return (new ProvideClaimsForToken(new TokenClaim[] { new TokenClaim(null, string.Empty) }),
HttpStatusCode.BadRequest,
"{\"errors\":[\"TokenIssuanceStartResponse: ProvideClaimsForToken: TokenClaim: The Id field is required.\"]}");
HttpStatusCode.InternalServerError,
"{\"errors\":[\"TokenIssuanceStartResponse: ProvideClaimsForToken: TokenClaim: The Id field is required.\"]}");
case ActionTestTypes.EmptyClaimsId:
return (new ProvideClaimsForToken(new TokenClaim[] { new TokenClaim(String.Empty, string.Empty) }),
HttpStatusCode.BadRequest,
"{\"errors\":[\"TokenIssuanceStartResponse: ProvideClaimsForToken: TokenClaim: The Id field is required.\"]}");
HttpStatusCode.InternalServerError,
"{\"errors\":[\"TokenIssuanceStartResponse: ProvideClaimsForToken: TokenClaim: The Id field is required.\"]}");
case ActionTestTypes.EmptyValueString:
return (new ProvideClaimsForToken(new TokenClaim[] { new TokenClaim("key", string.Empty) }),
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":\"\"}}]}}");
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":\"\"}}]}}");
case ActionTestTypes.NullValue:
return (new ProvideClaimsForToken(new TokenClaim[] { new TokenClaim("key", null) }),
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":null}}]}}");
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":null}}]}}");
case ActionTestTypes.EmptyValueArray:
return (new ProvideClaimsForToken(new TokenClaim[] { new TokenClaim("key", new string[] { }) }),
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":[]}}]}}");
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":[]}}]}}");
case ActionTestTypes.EmptyValueStringArray:
return (new ProvideClaimsForToken(new TokenClaim[] { new TokenClaim("key", new string[] { String.Empty, String.Empty }) }),
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":[\"\",\"\"]}}]}}");
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":[\"\",\"\"]}}]}}");
case ActionTestTypes.EmptyMixedArray:
return (new ProvideClaimsForToken(new TokenClaim[] { new TokenClaim("key", new string[] { String.Empty, null, " " }) }),
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":[\"\",null,\" \"]}}]}}");
HttpStatusCode.OK,
"{\"data\":{\"@odata.type\":\"microsoft.graph.onTokenIssuanceStartResponseData\",\"actions\":[{\"@odata.type\":\"microsoft.graph.tokenIssuanceStart.provideClaimsForToken\",\"claims\":{\"key\":[\"\",null,\" \"]}}]}}");
case ActionTestTypes.NullActionItems:
return (null,
HttpStatusCode.BadRequest,
"{\"errors\":[\"TokenIssuanceStartResponse: Actions can not contain null items\"]}");
HttpStatusCode.InternalServerError,
"{\"errors\":[\"TokenIssuanceStartResponse: Actions can not contain null items\"]}");
default:
return (null,
HttpStatusCode.InternalServerError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ namespace Azure.Identity.BrokeredAuthentication
public partial class InteractiveBrowserCredentialBrokerOptions : Azure.Identity.InteractiveBrowserCredentialOptions
{
public InteractiveBrowserCredentialBrokerOptions(System.IntPtr parentWindowHandle) { }
public bool? IsMsaPassthroughEnabled { get { throw null; } set { } }
}
public partial class SharedTokenCacheCredentialBrokerOptions : Azure.Identity.SharedTokenCacheCredentialOptions
{
public SharedTokenCacheCredentialBrokerOptions() { }
public SharedTokenCacheCredentialBrokerOptions(Azure.Identity.TokenCachePersistenceOptions tokenCacheOptions) { }
public bool? IsMsaPassthroughEnabled { get { throw null; } set { } }
}
}

0 comments on commit 6bd0aea

Please sign in to comment.