-
Notifications
You must be signed in to change notification settings - Fork 18
[OpenAPI] Add endpoint for create new admin resource details #307 #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
justinyoo
merged 5 commits into
aliencube:main
from
o-ii:feature/307-endpoint-admin-resources
Sep 16, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
59d099e
[OpenAPI] Add endpoint for create new admin resource details #307
o-ii 9288e8f
Merge branch 'aliencube:main' into feature/307-endpoint-admin-resources
o-ii 5fd1c8c
Added tests for AdminResourceDetails and updated admin tag description
o-ii 2e7c174
Merge branch 'aliencube:main' into feature/307-endpoint-admin-resources
o-ii d95cc42
Refactor AdminCreateResourcesOpenApiTests to use InlineData for test …
o-ii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/AzureOpenAIProxy.ApiApp/Endpoints/AdminResourceEndpoints.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using AzureOpenAIProxy.ApiApp.Models; | ||
using AzureOpenAIProxy.ApiApp.Services; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace AzureOpenAIProxy.ApiApp.Endpoints; | ||
|
||
/// <summary> | ||
/// This represents the endpoint entity for resource details by admin | ||
/// </summary> | ||
public static class AdminResourceEndpoints | ||
{ | ||
/// <summary> | ||
/// Adds the admin resource endpoint | ||
/// </summary> | ||
/// <param name="app"><see cref="WebApplication"/> instance.</param> | ||
/// <returns>Returns <see cref="RouteHandlerBuilder"/> instance.</returns> | ||
public static RouteHandlerBuilder AddNewAdminResource(this WebApplication app) | ||
{ | ||
var builder = app.MapPost(AdminEndpointUrls.AdminResources, async ( | ||
[FromBody] AdminResourceDetails payload, | ||
IAdminEventService service, | ||
ILoggerFactory loggerFactory) => | ||
{ | ||
var logger = loggerFactory.CreateLogger(nameof(AdminResourceEndpoints)); | ||
logger.LogInformation("Received a new resource request"); | ||
|
||
if (payload is null) | ||
{ | ||
logger.LogError("No payload found"); | ||
|
||
return Results.BadRequest("Payload is null"); | ||
} | ||
|
||
return await Task.FromResult(Results.Ok()); | ||
}) | ||
.Accepts<AdminResourceDetails>(contentType: "application/json") | ||
.Produces<AdminResourceDetails>(statusCode: StatusCodes.Status200OK, contentType: "application/json") | ||
.Produces(statusCode: StatusCodes.Status400BadRequest) | ||
.Produces(statusCode: StatusCodes.Status401Unauthorized) | ||
.Produces<string>(statusCode: StatusCodes.Status500InternalServerError, contentType: "text/plain") | ||
.WithTags("admin") | ||
.WithName("CreateAdminResource") | ||
.WithOpenApi(operation => | ||
{ | ||
operation.Summary = "Create admin resource"; | ||
operation.Description = "Create admin resource"; | ||
|
||
return operation; | ||
}); | ||
|
||
return builder; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,6 @@ | |
app.AddGetAdminEvent(); | ||
app.AddUpdateAdminEvent(); | ||
|
||
app.AddNewAdminResource(); | ||
|
||
await app.RunAsync(); |
302 changes: 302 additions & 0 deletions
302
test/AzureOpenAIProxy.AppHost.Tests/ApiApp/Endpoints/AdminCreateResourcesOpenApiTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,302 @@ | ||
using System.Text.Json; | ||
|
||
using AzureOpenAIProxy.AppHost.Tests.Fixtures; | ||
|
||
using FluentAssertions; | ||
|
||
using IdentityModel.Client; | ||
|
||
namespace AzureOpenAIProxy.AppHost.Tests.ApiApp.Endpoints; | ||
|
||
public class AdminCreateResourcesOpenApiTests(AspireAppHostFixture host) : IClassFixture<AspireAppHostFixture> | ||
{ | ||
[Fact] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Path() | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("paths") | ||
.GetProperty("/admin/resources"); | ||
result.ValueKind.Should().Be(JsonValueKind.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Verb() | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("paths") | ||
.GetProperty("/admin/resources") | ||
.GetProperty("post"); | ||
result.ValueKind.Should().Be(JsonValueKind.Object); | ||
} | ||
|
||
[Theory] | ||
[InlineData("admin")] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Tags(string tag) | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("paths") | ||
.GetProperty("/admin/resources") | ||
.GetProperty("post") | ||
.GetProperty("tags"); | ||
result.ValueKind.Should().Be(JsonValueKind.Array); | ||
result.EnumerateArray().Select(p => p.GetString()).Should().Contain(tag); | ||
} | ||
|
||
[Theory] | ||
[InlineData("summary")] | ||
[InlineData("description")] | ||
[InlineData("operationId")] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Value(string attribute) | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("paths") | ||
.GetProperty("/admin/resources") | ||
.GetProperty("post") | ||
.GetProperty(attribute); | ||
result.ValueKind.Should().Be(JsonValueKind.String); | ||
} | ||
|
||
[Theory] | ||
[InlineData("requestBody")] | ||
[InlineData("responses")] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Object(string attribute) | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("paths") | ||
.GetProperty("/admin/resources") | ||
.GetProperty("post") | ||
.GetProperty(attribute); | ||
result.ValueKind.Should().Be(JsonValueKind.Object); | ||
} | ||
|
||
[Theory] | ||
[InlineData("200")] | ||
[InlineData("400")] | ||
[InlineData("401")] | ||
[InlineData("500")] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Response(string attribute) | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("paths") | ||
.GetProperty("/admin/resources") | ||
.GetProperty("post") | ||
.GetProperty("responses") | ||
.GetProperty(attribute); | ||
result.ValueKind.Should().Be(JsonValueKind.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Schemas() | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("components") | ||
.GetProperty("schemas"); | ||
result.ValueKind.Should().Be(JsonValueKind.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Model() | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("components") | ||
.GetProperty("schemas") | ||
.GetProperty("AdminResourceDetails"); | ||
result.ValueKind.Should().Be(JsonValueKind.Object); | ||
} | ||
|
||
[Theory] | ||
[InlineData("resourceId", true)] | ||
[InlineData("friendlyName", true)] | ||
[InlineData("deploymentName", true)] | ||
[InlineData("resourceType", true)] | ||
[InlineData("endpoint", true)] | ||
[InlineData("apiKey", true)] | ||
[InlineData("region", true)] | ||
[InlineData("isActive", true)] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Required(string attribute, bool isRequired) | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("components") | ||
.GetProperty("schemas") | ||
.GetProperty("AdminResourceDetails") | ||
.TryGetStringArray("required") | ||
.ToList(); | ||
result.Contains(attribute).Should().Be(isRequired); | ||
} | ||
|
||
[Theory] | ||
[InlineData("resourceId")] | ||
[InlineData("friendlyName")] | ||
[InlineData("deploymentName")] | ||
[InlineData("resourceType")] | ||
[InlineData("endpoint")] | ||
[InlineData("apiKey")] | ||
[InlineData("region")] | ||
[InlineData("isActive")] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Property(string attribute) | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("components") | ||
.GetProperty("schemas") | ||
.GetProperty("AdminResourceDetails") | ||
.GetProperty("properties") | ||
.GetProperty(attribute); | ||
result.ValueKind.Should().Be(JsonValueKind.Object); | ||
} | ||
|
||
[Theory] | ||
[InlineData("resourceId", "string")] | ||
[InlineData("friendlyName", "string")] | ||
[InlineData("deploymentName", "string")] | ||
[InlineData("resourceType", "string")] | ||
[InlineData("endpoint", "string")] | ||
[InlineData("apiKey", "string")] | ||
[InlineData("region", "string")] | ||
[InlineData("isActive", "boolean")] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Type(string attribute, string type) | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("components") | ||
.GetProperty("schemas") | ||
.GetProperty("AdminResourceDetails") | ||
.GetProperty("properties") | ||
.GetProperty(attribute); | ||
|
||
if (!result.TryGetProperty("type", out var typeProperty)) | ||
{ | ||
var refPath = result.TryGetString("$ref").TrimStart('#', '/').Split('/'); | ||
var refSchema = openapi.RootElement; | ||
|
||
foreach (var part in refPath) | ||
{ | ||
refSchema = refSchema.GetProperty(part); | ||
} | ||
|
||
typeProperty = refSchema.GetProperty("type"); | ||
} | ||
|
||
typeProperty.GetString().Should().Be(type); | ||
} | ||
|
||
[Fact] | ||
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Validate_ResourceType_As_Enum() | ||
{ | ||
// Arrange | ||
using var httpClient = host.App!.CreateHttpClient("apiapp"); | ||
await host.ResourceNotificationService.WaitForResourceAsync("apiapp", KnownResourceStates.Running).WaitAsync(TimeSpan.FromSeconds(30)); | ||
|
||
// Act | ||
var json = await httpClient.GetStringAsync("/swagger/v1.0.0/swagger.json"); | ||
var openapi = JsonSerializer.Deserialize<JsonDocument>(json); | ||
|
||
// Assert | ||
var result = openapi!.RootElement.GetProperty("components") | ||
.GetProperty("schemas") | ||
.GetProperty("AdminResourceDetails") | ||
.GetProperty("properties") | ||
.GetProperty("resourceType"); | ||
|
||
var refPath = result.TryGetString("$ref").TrimStart('#', '/').Split('/'); | ||
var refSchema = openapi.RootElement; | ||
|
||
foreach (var part in refPath) | ||
{ | ||
refSchema = refSchema.GetProperty(part); | ||
} | ||
|
||
var enumValues = refSchema.GetProperty("enum") | ||
.EnumerateArray() | ||
.Select(p => p.GetString()) | ||
.ToList(); | ||
|
||
enumValues.Should().BeEquivalentTo(["none", "chat", "image"]); | ||
} | ||
} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.