New Backoffice - Exposing hardcoded Open API contract (#12953)

* Only expose swagger ui when in development

* Only expose swagger ui when in development + Test confirming the contract is respected + contract

* Rollback test code

* Fix up formatting

* Add reference to managementapi in integration tests.

* Revert "Add reference to managementapi in integration tests."

This reverts commit 67e2bf827758767e9031d40d146d6f27fd6c031e.

* Fix integration tests

* Add reference to management api in integration tests

* Fix up schema

* Fixed tests

* Fix test.. do not execute that composer twice

* Revert "Updated dependencies and fixed new NRT issues"

This reverts commit b2b2903a6e.

* Added debug info to test, to debug on azure pipeline

* Try and fix OpenApi test

* Only run test in release

* Try fixing OpenApi.json

Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch>
This commit is contained in:
Bjarke Berg
2022-09-20 09:50:34 +02:00
committed by GitHub
parent 8d5fb41ab7
commit 48954bb36a
8 changed files with 691 additions and 32 deletions

View File

@@ -0,0 +1,57 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Tests.Integration.TestServerTest;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.Integration.NewBackoffice;
// We only run this test in release because the schema looks different depending if it's built against release or debug.
// XML summaries is included in the description of a response model in release, but not debug mode.
#if DEBUG
[Ignore("This test runs only in release")]
#endif
[TestFixture]
public class OpenAPIContractTest : UmbracoTestServerTestBase
{
private GlobalSettings GlobalSettings => GetRequiredService<IOptions<GlobalSettings>>().Value;
private IHostingEnvironment HostingEnvironment => GetRequiredService<IHostingEnvironment>();
[Test]
public async Task Validate_OpenApi_Contract_is_implemented()
{
string[] keysToIgnore = { "servers" };
var officePath = GlobalSettings.GetBackOfficePath(HostingEnvironment);
var urlToContract = $"{officePath}/api/openapi.json";
var swaggerPath = $"{officePath}/swagger/All/swagger.json";
var apiContract = JObject.Parse(await Client.GetStringAsync(urlToContract));
var generatedJsonString = await Client.GetStringAsync(swaggerPath);
var mergedContract = JObject.Parse(generatedJsonString);
var originalGeneratedContract = JObject.Parse(generatedJsonString);
mergedContract.Merge(apiContract, new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Merge
});
foreach (var key in keysToIgnore)
{
originalGeneratedContract.Remove(key);
mergedContract.Remove(key);
}
Assert.AreEqual(originalGeneratedContract, mergedContract, $"Generated API do not respect the contract:{Environment.NewLine}Expected:{Environment.NewLine}{originalGeneratedContract.ToString(Formatting.Indented)}{Environment.NewLine}{Environment.NewLine}Actual:{Environment.NewLine}{mergedContract.ToString(Formatting.Indented)}");
}
}