-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathOpenApiTestBootstrap.cs
More file actions
119 lines (107 loc) · 5.05 KB
/
OpenApiTestBootstrap.cs
File metadata and controls
119 lines (107 loc) · 5.05 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Net.Http;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.ObjectModel;
using Microsoft.AspNetCore.TestHost;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
namespace Azure.DataApiBuilder.Service.Tests.OpenApiIntegration
{
// Defines helpers used to help generate an OpenApiDocument object which
// can be validated in tests so that a constantly running DAB instance
// isn't necessary.
internal class OpenApiTestBootstrap
{
/// <summary>
/// Bootstraps a test server instance using a runtime config file generated
/// from the provided entity collection. The test server is only used to generate
/// and return the OpenApiDocument for use this method's callers.
/// </summary>
/// <param name="runtimeEntities"></param>
/// <param name="configFileName"></param>
/// <param name="databaseEnvironment"></param>
/// <returns>Generated OpenApiDocument</returns>
internal static async Task<OpenApiDocument> GenerateOpenApiDocumentAsync(
RuntimeEntities runtimeEntities,
string configFileName,
string databaseEnvironment)
{
return await GenerateOpenApiDocumentAsync(
runtimeEntities: runtimeEntities,
configFileName: configFileName,
databaseEnvironment: databaseEnvironment,
restOptions: null);
}
/// <summary>
/// Bootstraps a test server instance using a runtime config file generated
/// from the provided entity collection and REST options. The test server is only used to generate
/// and return the OpenApiDocument for use this method's callers.
/// </summary>
/// <param name="runtimeEntities"></param>
/// <param name="configFileName"></param>
/// <param name="databaseEnvironment"></param>
/// <param name="restOptions">Optional REST runtime options to customize request-body-strict setting.</param>
/// <returns>Generated OpenApiDocument</returns>
internal static async Task<OpenApiDocument> GenerateOpenApiDocumentAsync(
RuntimeEntities runtimeEntities,
string configFileName,
string databaseEnvironment,
RestRuntimeOptions restOptions)
{
TestHelper.SetupDatabaseEnvironment(databaseEnvironment);
FileSystem fileSystem = new();
FileSystemRuntimeConfigLoader loader = new(fileSystem);
loader.TryLoadKnownConfig(out RuntimeConfig config);
// Create custom REST options if provided, otherwise use existing config
RuntimeOptions runtimeWithRestOptions = restOptions is not null
? config.Runtime with { Rest = restOptions, Host = config.Runtime?.Host with { Mode = HostMode.Production } }
: config.Runtime with { Host = config.Runtime?.Host with { Mode = HostMode.Production } };
RuntimeConfig configWithCustomHostMode = config with
{
Runtime = runtimeWithRestOptions,
Entities = runtimeEntities
};
File.WriteAllText(configFileName, configWithCustomHostMode.ToJson());
string[] args = new[]
{
$"--ConfigFileName={configFileName}"
};
using TestServer server = new(Program.CreateWebHostBuilder(args));
using HttpClient client = server.CreateClient();
{
HttpRequestMessage request = new(HttpMethod.Get, "/api/openapi");
HttpResponseMessage response = await client.SendAsync(request);
Stream responseStream = await response.Content.ReadAsStreamAsync();
// Read V3 as YAML
OpenApiDocument openApiDocument = new OpenApiStreamReader().Read(responseStream, out OpenApiDiagnostic diagnostic);
TestHelper.UnsetAllDABEnvironmentVariables();
return openApiDocument;
}
}
/// <summary>
/// Creates basic permissions collection with the anonymous and authenticated roles
/// where all actions are permitted.
/// </summary>
/// <returns>Array of EntityPermission objects.</returns>
internal static EntityPermission[] CreateBasicPermissions()
{
List<EntityPermission> permissions = new()
{
new EntityPermission(Role: "anonymous", Actions: new EntityAction[]
{
new(Action: EntityActionOperation.All, Fields: null, Policy: new())
}),
new EntityPermission(Role: "authenticated", Actions: new EntityAction[]
{
new(Action: EntityActionOperation.All, Fields: null, Policy: new())
})
};
return permissions.ToArray();
}
}
}