mirror of
https://github.com/bitwarden/server
synced 2025-12-10 05:13:48 +00:00
[PM-25182] Improve swagger OperationIDs: Part 1 (#6229)
* Improve swagger OperationIDs: Part 1 * Fix tests and fmt * Improve docs and add more tests * Fmt * Improve Swagger OperationIDs for Auth * Fix review feedback * Use generic getcustomattributes * Format * replace swaggerexclude by split+obsolete * Format * Some remaining excludes
This commit is contained in:
67
test/SharedWeb.Test/ActionNameOperationFilterTest.cs
Normal file
67
test/SharedWeb.Test/ActionNameOperationFilterTest.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Bit.SharedWeb.Swagger;
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||
using Microsoft.OpenApi.Any;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace SharedWeb.Test;
|
||||
|
||||
public class ActionNameOperationFilterTest
|
||||
{
|
||||
[Fact]
|
||||
public void WithValidActionNameAddsActionNameExtensions()
|
||||
{
|
||||
// Arrange
|
||||
var operation = new OpenApiOperation();
|
||||
var actionDescriptor = new ActionDescriptor();
|
||||
actionDescriptor.RouteValues["action"] = "GetUsers";
|
||||
|
||||
var apiDescription = new ApiDescription
|
||||
{
|
||||
ActionDescriptor = actionDescriptor
|
||||
};
|
||||
|
||||
var context = new OperationFilterContext(apiDescription, null, null, null);
|
||||
var filter = new ActionNameOperationFilter();
|
||||
|
||||
// Act
|
||||
filter.Apply(operation, context);
|
||||
|
||||
// Assert
|
||||
Assert.True(operation.Extensions.ContainsKey("x-action-name"));
|
||||
Assert.True(operation.Extensions.ContainsKey("x-action-name-snake-case"));
|
||||
|
||||
var actionNameExt = operation.Extensions["x-action-name"] as OpenApiString;
|
||||
var actionNameSnakeCaseExt = operation.Extensions["x-action-name-snake-case"] as OpenApiString;
|
||||
|
||||
Assert.NotNull(actionNameExt);
|
||||
Assert.NotNull(actionNameSnakeCaseExt);
|
||||
Assert.Equal("GetUsers", actionNameExt.Value);
|
||||
Assert.Equal("get_users", actionNameSnakeCaseExt.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithMissingActionRouteValueDoesNotAddExtensions()
|
||||
{
|
||||
// Arrange
|
||||
var operation = new OpenApiOperation();
|
||||
var actionDescriptor = new ActionDescriptor();
|
||||
// Not setting the "action" route value at all
|
||||
|
||||
var apiDescription = new ApiDescription
|
||||
{
|
||||
ActionDescriptor = actionDescriptor
|
||||
};
|
||||
|
||||
var context = new OperationFilterContext(apiDescription, null, null, null);
|
||||
var filter = new ActionNameOperationFilter();
|
||||
|
||||
// Act
|
||||
filter.Apply(operation, context);
|
||||
|
||||
// Assert
|
||||
Assert.False(operation.Extensions.ContainsKey("x-action-name"));
|
||||
Assert.False(operation.Extensions.ContainsKey("x-action-name-snake-case"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user