-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathParameterValidationTests.cs
More file actions
300 lines (272 loc) · 17.6 KB
/
ParameterValidationTests.cs
File metadata and controls
300 lines (272 loc) · 17.6 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Core.Authorization;
using Azure.DataApiBuilder.Core.Services.OpenAPI;
using Microsoft.OpenApi.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Azure.DataApiBuilder.Service.Tests.OpenApiIntegration;
/// <summary>
/// Integration tests validating path and query parameters are created
/// for different entities in DAB's REST API endpoint.
/// Path parameters are defined in the path of a URL using curly braces {}.
/// For example, in the URL /books/{id}, id is a path parameter.
/// Query parameters are defined in the query string of a URL using the ? character
/// followed by a list of key-value pairs separated by & characters.
/// For example, in the URL /books?author=John+Doe&page=2, author and page are query parameters.
/// </summary>
[TestCategory(TestCategory.MSSQL)]
[TestClass]
public class ParameterValidationTests
{
private const string CUSTOM_CONFIG = "parameter-config.MsSql.json";
private const string MSSQL_ENVIRONMENT = TestCategory.MSSQL;
/// <summary>
/// Validates the path parameters for GET methods.
/// </summary>
/// <param name="entityName">The name of the entity.</param>
/// <param name="objectName">The name of the database object.</param>
/// <param name="entitySourceType">The source type of the entity.</param>
[DataTestMethod]
[DataRow("BooksTable", "books", EntitySourceType.Table, DisplayName = "Table with path parameter id")]
[DataRow("BooksView", "books_view_all", EntitySourceType.View, DisplayName = "View with path parameter id")]
public async Task TestPathParametersForTablesAndViews(string entityName, string objectName, EntitySourceType entitySourceType)
{
EntitySource entitySource = new(Object: objectName, entitySourceType, null, null);
OpenApiDocument openApiDocument = await GenerateOpenApiDocumentForGivenEntityAsync(entityName, entitySource);
Assert.AreEqual(2, openApiDocument.Paths.Count);
Assert.IsTrue(openApiDocument.Paths.ContainsKey($"/{entityName}"));
Assert.IsTrue(openApiDocument.Paths.ContainsKey($"/{entityName}/id/{{id}}"));
foreach ((string pathName, OpenApiPathItem pathItem) in openApiDocument.Paths)
{
string apiPathWithParam = $"/{entityName}/id/{{id}}";
if (pathName.Equals(apiPathWithParam))
{
Assert.IsTrue(pathItem.Parameters.Count is 1);
OpenApiParameter pathParameter = pathItem.Parameters.First();
Assert.AreEqual("id", pathParameter.Name);
Assert.AreEqual(ParameterLocation.Path, pathParameter.In);
Assert.IsTrue(pathParameter.Required);
}
else
{
// Get All and POST method with path /entityName, will have no path parameters.
Assert.IsTrue(pathItem.Parameters.Count is 0);
}
}
}
/// <summary>
/// Validates that the default set of query parameters are generated for GET methods in Table/Views.
/// $select, $filter, $orderby, $first, $after are the query parameters.
/// </summary>
/// <param name="entityName">The name of the entity.</param>
/// <param name="objectName">The name of the database object.</param>
/// <param name="entitySourceType">The source type of the entity.</param>
[DataTestMethod]
[DataRow("BooksTable", "books", EntitySourceType.Table, DisplayName = "Table with query parameters")]
[DataRow("BooksView", "books_view_all", EntitySourceType.View, DisplayName = "View with query parameters")]
public async Task TestQueryParametersAddedForGEToperationOnTablesAndViews(string entityName, string objectName, EntitySourceType entitySourceType)
{
EntitySource entitySource = new(Object: objectName, entitySourceType, null, null);
OpenApiDocument openApiDocument = await GenerateOpenApiDocumentForGivenEntityAsync(entityName, entitySource);
Assert.AreEqual(2, openApiDocument.Paths.Count);
Assert.IsTrue(openApiDocument.Paths.ContainsKey($"/{entityName}"));
Assert.IsTrue(openApiDocument.Paths.ContainsKey($"/{entityName}/id/{{id}}"));
// Asserting on all the parameters for Get All operation.
Assert.IsTrue(openApiDocument.Paths[$"/{entityName}"].Operations.ContainsKey(OperationType.Get));
OpenApiOperation openApiOperationForGetAll = openApiDocument.Paths[$"/{entityName}"].Operations[OperationType.Get];
AssertOnAllDefaultQueryParameters(openApiOperationForGetAll.Parameters.ToList());
// Assert that path with id has all the query parameters.
Assert.IsTrue(openApiDocument.Paths[$"/{entityName}/id/{{id}}"].Operations.ContainsKey(OperationType.Get));
OpenApiOperation openApiOperationForGetById = openApiDocument.Paths[$"/{entityName}/id/{{id}}"].Operations[OperationType.Get];
AssertOnAllDefaultQueryParameters(openApiOperationForGetById.Parameters.ToList());
}
/// <summary>
/// Validates that the default set of query parameters are generated for GET methods in Table/Views.
/// $select, $filter, $orderby, $first, $after are the query parameters.
/// </summary>
/// <param name="entityName">The name of the entity.</param>
/// <param name="objectName">The name of the database object.</param>
/// <param name="entitySourceType">The source type of the entity.</param>
[DataTestMethod]
[DataRow("BooksTable", "books", EntitySourceType.Table, DisplayName = "Table with query parameters")]
[DataRow("BooksView", "books_view_all", EntitySourceType.View, DisplayName = "View with query parameters")]
public async Task TestQueryParametersExcludedFromNonReadOperationsOnTablesAndViews(string entityName, string objectName, EntitySourceType entitySourceType)
{
EntitySource entitySource = new(Object: objectName, entitySourceType, null, null);
OpenApiDocument openApiDocument = await GenerateOpenApiDocumentForGivenEntityAsync(entityName, entitySource);
Assert.AreEqual(2, openApiDocument.Paths.Count);
Assert.IsTrue(openApiDocument.Paths.ContainsKey($"/{entityName}"));
Assert.IsTrue(openApiDocument.Paths.ContainsKey($"/{entityName}/id/{{id}}"));
// Assert that Query Parameters Excluded From NonReadOperations for path without id.
OpenApiPathItem pathWithouId = openApiDocument.Paths[$"/{entityName}"];
Assert.IsTrue(pathWithouId.Operations.ContainsKey(OperationType.Post));
Assert.IsFalse(pathWithouId.Operations[OperationType.Post].Parameters.Any(param => param.In is ParameterLocation.Query));
// With keyless PUT/PATCH support, PUT and PATCH operations are present on the base path
// for entities with auto-generated primary keys. Validate they don't have query parameters.
Assert.IsTrue(pathWithouId.Operations.ContainsKey(OperationType.Put));
Assert.IsFalse(pathWithouId.Operations[OperationType.Put].Parameters.Any(param => param.In is ParameterLocation.Query));
Assert.IsTrue(pathWithouId.Operations.ContainsKey(OperationType.Patch));
Assert.IsFalse(pathWithouId.Operations[OperationType.Patch].Parameters.Any(param => param.In is ParameterLocation.Query));
Assert.IsFalse(pathWithouId.Operations.ContainsKey(OperationType.Delete));
// Assert that Query Parameters Excluded From NonReadOperations for path with id.
OpenApiPathItem pathWithId = openApiDocument.Paths[$"/{entityName}/id/{{id}}"];
Assert.IsFalse(pathWithId.Operations.ContainsKey(OperationType.Post));
Assert.IsTrue(pathWithId.Operations.ContainsKey(OperationType.Put));
Assert.IsFalse(pathWithId.Operations[OperationType.Put].Parameters.Any(param => param.In is ParameterLocation.Query));
Assert.IsTrue(pathWithId.Operations.ContainsKey(OperationType.Patch));
Assert.IsFalse(pathWithId.Operations[OperationType.Patch].Parameters.Any(param => param.In is ParameterLocation.Query));
Assert.IsTrue(pathWithId.Operations.ContainsKey(OperationType.Delete));
Assert.IsFalse(pathWithId.Operations[OperationType.Delete].Parameters.Any(param => param.In is ParameterLocation.Query));
}
/// <summary>
/// Validates that Input parameters are generated for Stored Procedures with GET operation.
/// It also validates parameter metadata like type, name, location, required, etc.
/// </summary>
/// <param name="entityName">The name of the entity.</param>
/// <param name="objectName">The name of the database object.</param>
[TestMethod]
public async Task TestInputParametersForStoredProcedures()
{
string entityName = "UpdateBookTitle";
string objectName = "update_book_title";
// Adding parameter metadata with a default value.
List<ParameterMetadata> parameterMetadata = new()
{
new ParameterMetadata
{
Name = "id",
Required = false
},
new ParameterMetadata
{
Name = "title",
Required = false,
Default = "Test Title"
}
};
EntitySource entitySource = new(Object: objectName, Type: EntitySourceType.StoredProcedure, Parameters: parameterMetadata, KeyFields: null);
OpenApiDocument openApiDocument = await GenerateOpenApiDocumentForGivenEntityAsync(
entityName,
entitySource,
supportedHttpMethods: new SupportedHttpVerb[] { SupportedHttpVerb.Get });
Assert.AreEqual(1, openApiDocument.Paths.Count);
Assert.IsTrue(openApiDocument.Paths.ContainsKey($"/{entityName}"));
OpenApiPathItem pathItem = openApiDocument.Paths.First().Value;
Assert.AreEqual(1, pathItem.Operations.Count);
Assert.IsTrue(pathItem.Operations.ContainsKey(OperationType.Get));
OpenApiOperation operation = pathItem.Operations[OperationType.Get];
Assert.AreEqual(2, operation.Parameters.Where(param => param.In is ParameterLocation.Query).Count());
Assert.IsTrue(operation.Parameters.Any(param =>
param.In is ParameterLocation.Query
&& param.Name.Equals("id")
&& param.Schema.Type.Equals("integer")
&& param.Required is false));
// Parameter with default value will be an optional query parameter.
Assert.IsTrue(operation.Parameters.Any(param =>
param.In is ParameterLocation.Query
&& param.Name.Equals("title")
&& param.Schema.Type.Equals("string")
&& param.Required is false));
}
/// <summary>
/// Validates that input query parameters are not generated for Stored Procedures irrespective of whether the operation is a GET operation
/// or any other supported http REST operation.
/// </summary>
[DataTestMethod]
[DataRow("CountBooks", "count_books", new SupportedHttpVerb[] { SupportedHttpVerb.Get }, DisplayName = "StoredProcudure with no input parameters results in 0 created input query params.")]
[DataRow("InsertBook", "insert_book", new SupportedHttpVerb[] { SupportedHttpVerb.Post }, DisplayName = "StoredProcedure without GET operations will results in 0 created input query params.")]
public async Task TestStoredProcedureForNoQueryParameters(string entityName, string objectName, SupportedHttpVerb[] supportedHttpVerbs)
{
EntitySource entitySource = new(Object: objectName, EntitySourceType.StoredProcedure, Parameters: null, KeyFields: null);
OpenApiDocument openApiDocument = await GenerateOpenApiDocumentForGivenEntityAsync(entityName, entitySource, supportedHttpVerbs);
Assert.AreEqual(1, openApiDocument.Paths.Count);
OpenApiPathItem pathItem = openApiDocument.Paths.First().Value;
Assert.AreEqual(1, pathItem.Operations.Count);
Assert.AreEqual(supportedHttpVerbs.First().ToString(), pathItem.Operations.Keys.First().ToString());
Assert.IsFalse(pathItem.Operations.Values.First().Parameters.Any(param => param.In is ParameterLocation.Query));
}
/// <summary>
/// validate that $select, $filter, $orderby, $first, $after query parameters are present in the openAPI document.
/// </summary>
private static void AssertOnAllDefaultQueryParameters(List<OpenApiParameter> openApiParameters)
{
int countOfDefaultQueryParams = openApiParameters.Where(openApiParameter => openApiParameter.In is ParameterLocation.Query).Count();
Assert.AreEqual(5, countOfDefaultQueryParams);
Assert.IsTrue(openApiParameters.Any(param => param.In is ParameterLocation.Query && param.Name.Equals("$select") && param.Schema.Type.Equals("string")));
Assert.IsTrue(openApiParameters.Any(param => param.In is ParameterLocation.Query && param.Name.Equals("$filter") && param.Schema.Type.Equals("string")));
Assert.IsTrue(openApiParameters.Any(param => param.In is ParameterLocation.Query && param.Name.Equals("$orderby") && param.Schema.Type.Equals("string")));
Assert.IsTrue(openApiParameters.Any(param => param.In is ParameterLocation.Query && param.Name.Equals("$first") && param.Schema.Type.Equals("integer")));
Assert.IsTrue(openApiParameters.Any(param => param.In is ParameterLocation.Query && param.Name.Equals("$after") && param.Schema.Type.Equals("string")));
}
/// <summary>
/// Generate and return the OpenApiDocument for a single entity.
/// </summary>
/// <param name="entityName">The name of the entity.</param>
/// <param name="entitySource">Database source for entity.</param>
/// <param name="supportedMethods">Supported HTTP verbs for the entity.</param>
private async static Task<OpenApiDocument> GenerateOpenApiDocumentForGivenEntityAsync(
string entityName,
EntitySource entitySource,
SupportedHttpVerb[] supportedHttpMethods = null)
{
Entity entity = new(
Source: entitySource,
Fields: null,
GraphQL: new(Singular: null, Plural: null, Enabled: false),
Rest: new(Methods: supportedHttpMethods ?? EntityRestOptions.DEFAULT_SUPPORTED_VERBS),
Permissions: OpenApiTestBootstrap.CreateBasicPermissions(),
Mappings: null,
Relationships: null);
Dictionary<string, Entity> entities = new()
{
{ entityName, entity }
};
RuntimeEntities runtimeEntities = new(entities);
return await OpenApiTestBootstrap.GenerateOpenApiDocumentAsync(
runtimeEntities: runtimeEntities,
configFileName: CUSTOM_CONFIG,
databaseEnvironment: MSSQL_ENVIRONMENT);
}
/// <summary>
/// Test to validate that the custom header parameters are present for each of the operation irrespective of the operation and the type
/// of the entity.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="objectName">Name of the database object backing the entity.</param>
/// <param name="entitySourceType">Sourcetype of the entity.</param>
/// <returns></returns>
[DataTestMethod]
[DataRow("BooksTable", "books", EntitySourceType.Table, DisplayName = "Assert custom header presence in header parameters for table.")]
[DataRow("BooksView", "books_view_all", EntitySourceType.View, DisplayName = "Assert custom header presence in header parameters for view.")]
[DataRow("UpdateBookTitle", "update_book_title", EntitySourceType.StoredProcedure, DisplayName = "Assert custom header presence in header parameters for stored procedure.")]
[DataRow("BooksTable", "books", EntitySourceType.Table, DisplayName = "Validate custom header presence in header parameters for table.")]
[DataRow("BooksView", "books_view_all", EntitySourceType.View, DisplayName = "Validate custom header presence in header parameters for view.")]
[DataRow("UpdateBookTitle", "update_book_title", EntitySourceType.StoredProcedure, DisplayName = "Validate custom header presence in header parameters for stored procedure.")]
public async Task ValidateHeaderParametersForEntity(string entityName, string objectName, EntitySourceType entitySourceType)
{
EntitySource entitySource = new(Object: objectName, Type: entitySourceType, Parameters: null, KeyFields: null);
OpenApiDocument openApiDocument = await GenerateOpenApiDocumentForGivenEntityAsync(entityName, entitySource);
foreach (OpenApiPathItem pathItem in openApiDocument.Paths.Values)
{
foreach ((OperationType operationType, OpenApiOperation operation) in pathItem.Operations)
{
// Assert presence of Authorization header and the expected parameter properties for the header parameter.
Assert.IsTrue(operation.Parameters.Any(
param => param.In is ParameterLocation.Header &&
AuthorizationResolver.AUTHORIZATION_HEADER.Equals(param.Name) &&
JsonDataType.String.ToString().ToLower().Equals(param.Schema.Type) &&
param.Required is false));
// Assert presence of X-MS-API-ROLE header and the expected parameter properties for the header parameter.
Assert.IsTrue(operation.Parameters.Any(
param => param.In is ParameterLocation.Header &&
AuthorizationResolver.CLIENT_ROLE_HEADER.Equals(param.Name) &&
JsonDataType.String.ToString().ToLower().Equals(param.Schema.Type) &&
param.Required is false));
}
}
}
}