Skip to content

Commit dba434e

Browse files
justinyooDerich367
authored andcommitted
Add more test cases for OpenApiHttpTriggerContext (Azure#463)
1 parent ed081c1 commit dba434e

File tree

6 files changed

+506
-27
lines changed

6 files changed

+506
-27
lines changed

src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/OpenApiHttpTriggerContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ private async Task<string> GetRuntimeFilenameAsync(string functionAppDirectory)
281281

282282
var runtimes = dependencyManifests
283283
.Select(manifest => manifest.Targets[manifest.RuntimeTarget.Name].First())
284+
.Where(manifest => manifest.Value.Dependencies != null)
284285
.Select(target => new
285286
{
286287
Name = target.Key.Split('/').First(),

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Filters/DocumentFilterCollection.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22

33
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
4-
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
54

65
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Filters
76
{
@@ -10,21 +9,13 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Filters
109
/// </summary>
1110
public class DocumentFilterCollection
1211
{
13-
/// <summary>
14-
/// Initializes a new instance of the <see cref="DocumentFilterCollection"/> class.
15-
/// </summary>
16-
public DocumentFilterCollection()
17-
{
18-
this.DocumentFilters = new List<IDocumentFilter>();
19-
}
20-
2112
/// <summary>
2213
/// Initializes a new instance of the <see cref="DocumentFilterCollection"/> class.
2314
/// </summary>
2415
/// <param name="documentFilters">List of <see cref="IDocumentFilter"/> instances.</param>
25-
public DocumentFilterCollection(List<IDocumentFilter> documentFilters)
16+
public DocumentFilterCollection(List<IDocumentFilter> documentFilters = null)
2617
{
27-
this.DocumentFilters = documentFilters.ThrowIfNullOrDefault();
18+
this.DocumentFilters = documentFilters ?? new List<IDocumentFilter>();
2819
}
2920

3021
/// <summary>

test/Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Tests/OpenApiHttpTriggerContextTests.cs

Lines changed: 232 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@
99
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Functions;
1010
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Tests.Fakes;
1111
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
12+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
1213
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
1314
using Microsoft.OpenApi;
1415
using Microsoft.VisualStudio.TestTools.UnitTesting;
1516

1617
using Moq;
1718

19+
using Newtonsoft.Json.Serialization;
20+
1821
namespace Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Tests
1922
{
2023
[TestClass]
2124
public class OpenApiHttpTriggerContextTests
2225
{
26+
[TestCleanup]
27+
public void CleanUp()
28+
{
29+
Environment.SetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT", null);
30+
Environment.SetEnvironmentVariable("OpenApi__AuthLevel__Document", null);
31+
Environment.SetEnvironmentVariable("OpenApi__AuthLevel__UI", null);
32+
Environment.SetEnvironmentVariable("OpenApi__ApiKey", null);
33+
}
34+
2335
[DataTestMethod]
2436
[DataRow(typeof(OpenApiHttpTriggerContextTests))]
2537
public async Task Given_Type_When_Initiated_Then_It_Should_Return_ApplicationAssemblyWithGivenType(Type type)
@@ -112,8 +124,60 @@ public async Task Given_Type_When_Initiated_Then_It_Should_Return_HttpSettings()
112124
var settings = (await context.SetApplicationAssemblyAsync(location, false))
113125
.HttpSettings;
114126

127+
settings.Should().NotBeNull();
115128
settings.RoutePrefix.Should().Be("api");
129+
}
130+
131+
[TestMethod]
132+
public async Task Given_Type_When_Initiated_Then_It_Should_Return_Document()
133+
{
134+
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
135+
var context = new OpenApiHttpTriggerContext();
136+
137+
var document = (await context.SetApplicationAssemblyAsync(location, false))
138+
.Document;
139+
140+
document.Should().NotBeNull();
141+
}
142+
143+
[TestMethod]
144+
public async Task Given_Type_When_Initiated_Then_It_Should_Return_SwaggerUI()
145+
{
146+
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
147+
var context = new OpenApiHttpTriggerContext();
148+
149+
var swaggerUI = (await context.SetApplicationAssemblyAsync(location, false))
150+
.SwaggerUI;
151+
152+
swaggerUI.Should().NotBeNull();
153+
}
154+
155+
[TestMethod]
156+
public async Task Given_Type_When_Initiated_Then_It_Should_Return_NamingStrategy()
157+
{
158+
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
159+
var context = new OpenApiHttpTriggerContext();
160+
161+
var namingStrategy = (await context.SetApplicationAssemblyAsync(location, false))
162+
.NamingStrategy;
163+
164+
namingStrategy.Should().NotBeNull();
165+
namingStrategy.Should().BeOfType<CamelCaseNamingStrategy>();
166+
}
167+
168+
[DataTestMethod]
169+
[DataRow("Development", true)]
170+
[DataRow("Production", false)]
171+
public async Task Given_Type_When_Initiated_Then_It_Should_Return_IsDevelopment(string environment, bool expected)
172+
{
173+
Environment.SetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT", environment);
174+
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
175+
var context = new OpenApiHttpTriggerContext();
116176

177+
var isDevelopment = (await context.SetApplicationAssemblyAsync(location, false))
178+
.IsDevelopment;
179+
180+
isDevelopment.Should().Be(expected);
117181
}
118182

119183
[TestMethod]
@@ -131,32 +195,193 @@ public async Task Given_Authorization_When_AuthorizeAsync_Invoked_Then_It_Should
131195
result.Payload.Should().Be(FakeOpenApiHttpTriggerAuthorization.Payload);
132196
}
133197

198+
[TestMethod]
199+
public void Given_Type_When_GetVisitorCollection_Invoked_Then_It_Should_Return_Result()
200+
{
201+
var context = new OpenApiHttpTriggerContext();
202+
203+
var result = context.GetVisitorCollection();
204+
205+
result.Should().NotBeNull();
206+
result.Visitors.Should().NotBeNull();
207+
result.Visitors.Should().HaveCountGreaterThan(0);
208+
}
209+
210+
[TestMethod]
211+
public void Given_NoVersion_When_GetOpenApiVersionType_Invoked_Then_It_Should_Return_V2()
212+
{
213+
var context = new OpenApiHttpTriggerContext();
214+
215+
var result = context.GetOpenApiVersionType();
216+
217+
result.Should().Be(OpenApiVersionType.V2);
218+
}
219+
220+
[TestMethod]
221+
public void Given_InvalidVersion_When_GetOpenApiVersionType_Invoked_Then_It_Should_Throw_Exception()
222+
{
223+
var context = new OpenApiHttpTriggerContext();
224+
225+
Action action = () => context.GetOpenApiVersionType("v1");
226+
227+
action.Should().Throw<InvalidOperationException>();
228+
}
229+
230+
[DataTestMethod]
231+
[DataRow("v2", OpenApiVersionType.V2)]
232+
[DataRow("V2", OpenApiVersionType.V2)]
233+
[DataRow("v3", OpenApiVersionType.V3)]
234+
[DataRow("V3", OpenApiVersionType.V3)]
235+
public void Given_ValidVersion_When_GetOpenApiVersionType_Invoked_Then_It_Should_Return_V2(string version, OpenApiVersionType expected)
236+
{
237+
var context = new OpenApiHttpTriggerContext();
238+
239+
var result = context.GetOpenApiVersionType(version);
240+
241+
result.Should().Be(expected);
242+
}
243+
244+
[TestMethod]
245+
public void Given_InvalidVersion_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Throw_Exception()
246+
{
247+
var context = new OpenApiHttpTriggerContext();
248+
249+
Action action = () => context.GetOpenApiSpecVersion("v1");
250+
251+
action.Should().Throw<InvalidOperationException>();
252+
}
253+
134254
[DataTestMethod]
135255
[DataRow("v2", OpenApiSpecVersion.OpenApi2_0)]
256+
[DataRow("V2", OpenApiSpecVersion.OpenApi2_0)]
136257
[DataRow("v3", OpenApiSpecVersion.OpenApi3_0)]
137-
public async Task Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string version, OpenApiSpecVersion expected)
258+
[DataRow("V3", OpenApiSpecVersion.OpenApi3_0)]
259+
public void Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string version, OpenApiSpecVersion expected)
138260
{
139-
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
140261
var context = new OpenApiHttpTriggerContext();
141262

142-
var result = (await context.SetApplicationAssemblyAsync(location, false))
143-
.GetOpenApiSpecVersion(version);
263+
var result = context.GetOpenApiSpecVersion(version);
144264

145265
result.Should().Be(expected);
146266
}
147267

268+
[TestMethod]
269+
public void Given_InvalidVersion_When_GetOpenApiFormat_Invoked_Then_It_Should_Throw_Exception()
270+
{
271+
var context = new OpenApiHttpTriggerContext();
272+
273+
Action action = () => context.GetOpenApiFormat("text");
274+
275+
action.Should().Throw<InvalidOperationException>();
276+
}
277+
148278
[DataTestMethod]
279+
[DataRow("yml", OpenApiFormat.Yaml)]
280+
[DataRow("YML", OpenApiFormat.Yaml)]
149281
[DataRow("yaml", OpenApiFormat.Yaml)]
282+
[DataRow("YAML", OpenApiFormat.Yaml)]
150283
[DataRow("json", OpenApiFormat.Json)]
151-
public async Task Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string format, OpenApiFormat expected)
284+
[DataRow("JSON", OpenApiFormat.Json)]
285+
public void Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string format, OpenApiFormat expected)
286+
{
287+
var context = new OpenApiHttpTriggerContext();
288+
289+
var result = context.GetOpenApiFormat(format);
290+
291+
result.Should().Be(expected);
292+
}
293+
294+
[TestMethod]
295+
public void Given_NoAuthLevel_When_GetDocumentAuthLevel_Invoked_Then_It_Should_Return_Anonymous()
296+
{
297+
var context = new OpenApiHttpTriggerContext();
298+
299+
var result = context.GetDocumentAuthLevel();
300+
301+
result.Should().Be(OpenApiAuthLevelType.Anonymous);
302+
}
303+
304+
[DataTestMethod]
305+
[DataRow("Anonymous", OpenApiAuthLevelType.Anonymous)]
306+
[DataRow("User", OpenApiAuthLevelType.User)]
307+
[DataRow("Function", OpenApiAuthLevelType.Function)]
308+
[DataRow("System", OpenApiAuthLevelType.System)]
309+
[DataRow("Admin", OpenApiAuthLevelType.Admin)]
310+
[DataRow("ServiceAccount", OpenApiAuthLevelType.Anonymous)]
311+
public void Given_AuthLevel_When_GetDocumentAuthLevel_Invoked_Then_It_Should_Return_Result(string authLevel, OpenApiAuthLevelType expected)
312+
{
313+
Environment.SetEnvironmentVariable("OpenApi__AuthLevel__Document", authLevel);
314+
315+
var context = new OpenApiHttpTriggerContext();
316+
317+
var result = context.GetDocumentAuthLevel();
318+
319+
result.Should().Be(expected);
320+
}
321+
322+
[TestMethod]
323+
public void Given_NoAuthLevel_When_GetUIAuthLevel_Invoked_Then_It_Should_Return_Anonymous()
324+
{
325+
var context = new OpenApiHttpTriggerContext();
326+
327+
var result = context.GetUIAuthLevel();
328+
329+
result.Should().Be(OpenApiAuthLevelType.Anonymous);
330+
}
331+
332+
[DataTestMethod]
333+
[DataRow("Anonymous", OpenApiAuthLevelType.Anonymous)]
334+
[DataRow("User", OpenApiAuthLevelType.User)]
335+
[DataRow("Function", OpenApiAuthLevelType.Function)]
336+
[DataRow("System", OpenApiAuthLevelType.System)]
337+
[DataRow("Admin", OpenApiAuthLevelType.Admin)]
338+
[DataRow("ServiceAccount", OpenApiAuthLevelType.Anonymous)]
339+
public void Given_AuthLevel_When_GetUIAuthLevel_Invoked_Then_It_Should_Return_Result(string authLevel, OpenApiAuthLevelType expected)
340+
{
341+
Environment.SetEnvironmentVariable("OpenApi__AuthLevel__UI", authLevel);
342+
343+
var context = new OpenApiHttpTriggerContext();
344+
345+
var result = context.GetUIAuthLevel();
346+
347+
result.Should().Be(expected);
348+
}
349+
350+
[TestMethod]
351+
public void Given_NoApiKey_When_GetSwaggerAuthKey_Invoked_Then_It_Should_Return_Empty()
352+
{
353+
var context = new OpenApiHttpTriggerContext();
354+
355+
var result = context.GetSwaggerAuthKey();
356+
357+
result.Should().BeEmpty();
358+
}
359+
360+
[DataTestMethod]
361+
[DataRow("hello world")]
362+
public void Given_ApiKey_When_GetSwaggerAuthKey_Invoked_Then_It_Should_Return_Result(string apiKey)
363+
{
364+
Environment.SetEnvironmentVariable("OpenApi__ApiKey", apiKey);
365+
366+
var context = new OpenApiHttpTriggerContext();
367+
368+
var result = context.GetSwaggerAuthKey();
369+
370+
result.Should().Be(apiKey);
371+
}
372+
373+
[TestMethod]
374+
public async Task Given_Type_When_GetDocumentFilterCollection_Invoked_Then_It_Should_Return_Result()
152375
{
153376
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
154377
var context = new OpenApiHttpTriggerContext();
155378

156379
var result = (await context.SetApplicationAssemblyAsync(location, false))
157-
.GetOpenApiFormat(format);
380+
.GetDocumentFilterCollection();
158381

159-
result.Should().Be(expected);
382+
result.Should().NotBeNull();
383+
result.DocumentFilters.Should().NotBeNull();
384+
result.DocumentFilters.Should().HaveCount(0);
160385
}
161386
}
162387
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Linq;
2+
3+
using FluentAssertions;
4+
5+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
6+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Filters;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
9+
using Moq;
10+
11+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Filters
12+
{
13+
[TestClass]
14+
public class DocumentFilterCollectionTests
15+
{
16+
[TestMethod]
17+
public void Given_Null_When_Instantated_Then_It_Should_Return_Result()
18+
{
19+
var collection = new DocumentFilterCollection();
20+
21+
collection.DocumentFilters.Should().NotBeNull();
22+
collection.DocumentFilters.Should().HaveCount(0);
23+
}
24+
25+
[DataTestMethod]
26+
[DataRow(1)]
27+
[DataRow(2)]
28+
public void Given_Filters_When_Instantated_Then_It_Should_Return_Result(int count)
29+
{
30+
var filters = Enumerable.Range(0, count).Select(i => new Mock<IDocumentFilter>().Object).ToList();
31+
var collection = new DocumentFilterCollection(filters);
32+
33+
collection.DocumentFilters.Should().NotBeNull();
34+
collection.DocumentFilters.Should().HaveCount(count);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)