-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAzureFunctionHandlerTests.cs
131 lines (106 loc) · 4.65 KB
/
AzureFunctionHandlerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using aggregator;
using aggregator.Engine;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.ServiceHooks.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using Newtonsoft.Json;
using NSubstitute;
using unittests_function.TestData;
using Xunit;
using ExecutionContext = Microsoft.Azure.WebJobs.ExecutionContext;
namespace unittests_function
{
public class AzureFunctionHandlerTests
{
private readonly TestAzureFunctionHandler azureFunctionHandler;
public AzureFunctionHandlerTests()
{
ILogger logger;
ExecutionContext context;
HttpContext httpContext;
logger = Substitute.For<ILogger>();
context = Substitute.For<ExecutionContext>();
context.InvocationId = Guid.Empty;
context.FunctionName = "TestRule";
context.FunctionDirectory = "";
context.FunctionAppDirectory = "";
var services = new ServiceCollection()
.AddMvc()
.AddWebApiConventions()
.Services
.BuildServiceProvider();
httpContext = new DefaultHttpContext()
{
RequestServices = services,
};
httpContext.Request.Protocol = "http";
httpContext.Request.Host = new HostString("localhost");
httpContext.Request.Method = HttpMethod.Post.ToString();
azureFunctionHandler = new TestAzureFunctionHandler(logger, context, httpContext);
}
[Fact]
public async Task HandleTestEvent_ReturnAggregatorInformation_Succeeds()
{
IActionResult actionResult = await azureFunctionHandler.RunAsync(ExampleEvents.TestEvent, CancellationToken.None);
var objectResult = actionResult as ObjectResult;
Assert.True(IsSuccessStatusCode(objectResult.StatusCode));
Assert.True(azureFunctionHandler.Response.Headers.TryGetValue("X-Aggregator-Version", out var versions));
Assert.Single(versions);
Assert.True(azureFunctionHandler.Response.Headers.TryGetValue("X-Aggregator-Rule", out var rules));
Assert.Equal("TestRule", rules.Single());
var content = JsonConvert.SerializeObject(objectResult.Value);
Assert.StartsWith("{\"message\":\"Hello from Aggregator v", content);
Assert.EndsWith("executing rule 'TestRule'\"}", content);
}
private static bool IsSuccessStatusCode(int? statusCode)
{
return statusCode.HasValue &&
(statusCode >= 200) && (statusCode <= 299);
}
public static IEnumerable<object[]> Data =>
new List<object[]>
{
new object[] { ExampleEvents.WorkItemUpdateEventResourceVersion10 },
new object[] { ExampleEvents.WorkItemUpdateEventResourceVersion31Preview3 },
new object[] { ExampleEvents.WorkItemUpdateEventResourceVersion51Preview3 },
};
[Theory]
[MemberData(nameof(Data))]
public void HandleEventsWithDifferentResourceVersion_CheckIdentityConversion_Succeeds(WebHookEvent eventData)
{
var eventContext = azureFunctionHandler.InvokeCreateContextFromEvent(eventData);
var workItem = eventContext.WorkItemPayload.WorkItem;
Assert.IsType<IdentityRef>(workItem.Fields[CoreFieldRefNames.AssignedTo]);
Assert.IsType<IdentityRef>(workItem.Fields[CoreFieldRefNames.ChangedBy]);
Assert.IsType<IdentityRef>(workItem.Fields[CoreFieldRefNames.CreatedBy]);
}
}
internal class TestAzureFunctionHandler : AzureFunctionHandler
{
/// <inheritdoc />
public TestAzureFunctionHandler(ILogger logger, ExecutionContext executionContext, HttpContext httpContext)
: base(logger, executionContext, httpContext)
{
helper = new RequestHelper(logger);
}
private readonly RequestHelper helper;
internal WorkItemEventContext InvokeCreateContextFromEvent(WebHookEvent eventData)
{
return helper.CreateContextFromEvent(eventData);
}
internal void InvokeMigrateIdentityInformation(string resourceVersion, WorkItem workItem)
{
helper.MigrateIdentityInformation(resourceVersion, workItem);
}
}
}