Skip to content
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

delete #321

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEndpointUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ public static class AdminEndpointUrls
/// - POST method for new event creation
/// </remarks>
public const string AdminEvents = "/admin/events";

/// <summary>
/// Declares the admin resource details endpoint.
/// </summary>
/// <remarks>
/// - POST method for new resource creation
/// </remarks>
public const string AdminResources = "/admin/resources";
}
54 changes: 54 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Endpoints/AdminResourceEndpoints.cs
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;
}
}
2 changes: 1 addition & 1 deletion src/AzureOpenAIProxy.ApiApp/Filters/OpenApiTagFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
[
new OpenApiTag { Name = "weather", Description = "Weather forecast operations" },
new OpenApiTag { Name = "openai", Description = "Azure OpenAI operations" },
new OpenApiTag { Name = "admin", Description = "Admin for organizing events" },
new OpenApiTag { Name = "admin", Description = "Admin operations for managing events and resources" },
new OpenApiTag { Name = "events", Description = "User events" }
];
}
Expand Down
2 changes: 2 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@
app.AddGetAdminEvent();
app.AddUpdateAdminEvent();

app.AddNewAdminResource();

await app.RunAsync();
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"]);
}
}
Loading
Loading