Skip to content

Commit

Permalink
Revert and fix update endpoint specs to meet current task
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungHuLee committed Aug 22, 2024
1 parent 2a5e391 commit 9ffee77
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEventEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,35 @@ public static RouteHandlerBuilder AddAdminEvents(this WebApplication app)

return builder;
}

/// <summary>
/// Adds the update event details by admin endpoint
/// </summary>
/// <param name="app"><see cref="WebApplication"/> instance.</param>
/// <returns>Returns <see cref="RouteHandlerBuilder"/> instance.</returns>
public static RouteHandlerBuilder AddUpdateAdminEvents(this WebApplication app)
{
// Todo: Issue #19 https://github.com/aliencube/azure-openai-sdk-proxy/issues/19
// Need authorization by admin
var builder = app.MapPut(AdminEndpointUrls.AdminEventDetails, (
[FromRoute] string eventId) =>
{
// Todo: Issue #203 https://github.com/aliencube/azure-openai-sdk-proxy/issues/203
return Results.Ok();
})
.Produces(statusCode: StatusCodes.Status200OK)
.Produces(statusCode: StatusCodes.Status401Unauthorized)
.Produces<string>(statusCode: StatusCodes.Status500InternalServerError, contentType: "text/plain")
.WithTags("admin")
.WithName("UpdateAdminEventDetails")
.WithOpenApi(operation =>
{
operation.Summary = "Updates event details by the given event ID";
operation.Description = "This endpoint updates event details by event id, api for admin users";
return operation;
});

return builder;
}
}
1 change: 1 addition & 0 deletions src/AzureOpenAIProxy.ApiApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@

// Admin Endpoints
app.AddAdminEvents();
app.AddUpdateAdminEvents();

await app.RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System.Text.Json;

using FluentAssertions;

using AzureOpenAIProxy.AppHost.Tests.Fixtures;

namespace AzureOpenAIProxy.AppHost.Tests.ApiApp.Endpoints;

public class AdminUpdateEventDetailsOpenApiTests(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");

// 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")
.TryGetProperty("/admin/events/{eventId}", out var property) ? property : default;
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");

// 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/events/{eventId}")
.TryGetProperty("put", out var property) ? property : default;
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");

// 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/events/{eventId}")
.GetProperty("put")
.TryGetProperty("tags", out var property) ? property : default;
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");

// 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/events/{eventId}")
.GetProperty("put")
.TryGetProperty(attribute, out var property) ? property : default;
result.ValueKind.Should().Be(JsonValueKind.String);
}

[Theory]
[InlineData("parameters")]
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Array(string attribute)
{
// Arrange
using var httpClient = host.App!.CreateHttpClient("apiapp");

// 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/events/{eventId}")
.GetProperty("put")
.TryGetProperty(attribute, out var property) ? property : default;
result.ValueKind.Should().Be(JsonValueKind.Array);
}

[Theory]
[InlineData("eventId")]
public async Task Given_Resource_When_Invoked_Endpoint_Then_It_Should_Return_Path_Parameter(string name)
{
// Arrange
using var httpClient = host.App!.CreateHttpClient("apiapp");

// 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/events/{eventId}")
.GetProperty("put")
.GetProperty("parameters")
.EnumerateArray()
.Where(p => p.GetProperty("in").GetString() == "path")
.Select(p => p.GetProperty("name").ToString());
result.Should().Contain(name);
}

[Theory]
[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");

// 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/events/{eventId}")
.GetProperty("put")
.TryGetProperty(attribute, out var property) ? property : default;
result.ValueKind.Should().Be(JsonValueKind.Object);
}

[Theory]
[InlineData("200")]
[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");

// 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/events/{eventId}")
.GetProperty("put")
.GetProperty("responses")
.TryGetProperty(attribute, out var property) ? property : default;
result.ValueKind.Should().Be(JsonValueKind.Object);
}
}

0 comments on commit 9ffee77

Please sign in to comment.