Skip to content

Commit

Permalink
Add unit test for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lapellaniz committed Apr 24, 2024
1 parent c2e2911 commit 4a55556
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Microsoft.Azure.Functions.Worker.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Moq;
using Function.Domain.Helpers;
using Microsoft.Azure.Functions.Worker;
using System.IO;
using System.Net;
using System.Security.Claims;
using UnitTests.Mocks;

namespace UnitTests.Function.Domain.Helpers
{
public class HttpHelperTests
{
private HttpHelper _util = new();

[Fact]
public void ValidateRequestHeaders_Should_Return_True_When_SourceHeader_Matches()
{
// Arrange
Mock<FakeHttpRequestData> mockRequest = new();
mockRequest.Setup(x => x.Headers).Returns([]); // Set up the headers
mockRequest.Object.Headers.Add("x-teladoc-udf-ol-source", "ExpectedValue"); // Set the expected header value

// Act
var result = _util.ValidateRequestHeaders(mockRequest.Object, "ExpectedValue");

// Assert
Assert.True(result, "Expected header value should match.");
}

[Fact]
public void ValidateRequestHeaders_Should_Return_False_When_SourceHeader_Does_Not_Match()
{
// Arrange
Mock<FakeHttpRequestData> mockRequest = new();
mockRequest.Setup(x => x.Headers).Returns([]); // Set up the headers
mockRequest.Object.Headers.Add("x-teladoc-udf-ol-source", "DifferentValue"); // Set a different header value

// Act
var result = _util.ValidateRequestHeaders(mockRequest.Object, "ExpectedValue");

// Assert
Assert.False(result, "Expected header value should not match.");
}

[Fact]
public void ValidateRequestHeaders_Should_Return_False_When_SourceHeader_Is_Not_Present()
{
// Arrange
Mock<FakeHttpRequestData> mockRequest = new();
mockRequest.Setup(x => x.Headers).Returns([]); // Set up the headers

// Act
var result = _util.ValidateRequestHeaders(mockRequest.Object, "ExpectedValue");

// Assert
Assert.False(result, "Header should not be present.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Azure.Functions.Worker;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTests.Mocks
{
public class FakeFunctionContext : FunctionContext
{
public override string InvocationId => Guid.NewGuid().ToString();

public override string FunctionId => Guid.NewGuid().ToString();

public override TraceContext TraceContext => throw new NotImplementedException();

public override BindingContext BindingContext => throw new NotImplementedException();

public override RetryContext RetryContext => throw new NotImplementedException();

public override IServiceProvider InstanceServices { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public override FunctionDefinition FunctionDefinition => throw new NotImplementedException();

public override IDictionary<object, object> Items { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public override IInvocationFeatures Features => throw new NotImplementedException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Claims;

namespace UnitTests.Mocks
{
public class FakeHttpRequestData : HttpRequestData
{
public FakeHttpRequestData() : this(new FakeFunctionContext(), new Uri("http://fakeurl.test"))
{

}

public FakeHttpRequestData(FunctionContext functionContext, Uri url, Stream body = null) : base(functionContext)

Check warning on line 20 in function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpRequestData.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 20 in function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpRequestData.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Cookies' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 20 in function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpRequestData.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Identities' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 20 in function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpRequestData.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Method' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
Url = url;
Body = body ?? new MemoryStream();
}

public override Stream Body { get; } = new MemoryStream();

public override HttpHeadersCollection Headers { get; } = [];

public override IReadOnlyCollection<IHttpCookie> Cookies { get; }

public override Uri Url { get; }

public override IEnumerable<ClaimsIdentity> Identities { get; }

public override string Method { get; }

public override HttpResponseData CreateResponse()
{
return new FakeHttpResponseData(FunctionContext);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace UnitTests.Mocks
{
public class FakeHttpResponseData : HttpResponseData
{
public FakeHttpResponseData(FunctionContext functionContext) : base(functionContext)

Check warning on line 15 in function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpResponseData.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Cookies' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
}

public override HttpStatusCode StatusCode { get; set; }
public override HttpHeadersCollection Headers { get; set; } = [];
public override Stream Body { get; set; } = new MemoryStream();
public override HttpCookies Cookies { get; }
}
}
22 changes: 14 additions & 8 deletions function-app/adb-to-purview/tests/unit-tests/unit-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.17.0" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="xunit" Version="2.4.2-pre.12" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\adb-to-purview.csproj" />
Expand Down

0 comments on commit 4a55556

Please sign in to comment.