Integration test using Content Delivery API

,

Hi there!

I’m trying to create an integration test in combination with the Umbraco Delivery API. First off; it’s really cool to see native support for integration tests within Umbraco! I’ve followed the documentation (Integration Testing | Umbraco CMS) and got it all working.

For my own implementation I would like to create an integration test for our own extention of the Umbraco Delivery API. Unfortunately I cannot seem to get the basic setup to work. This is what I have so far:

[TestFixture]
public class DeliveryApiTests : UmbracoTestServerTestBase
{
    [Test]
    public async Task DeliveryApiTest()
    {
        var xml = PackageMigrationResource.GetEmbeddedPackageDataManifest(GetType());
        var packagingService = GetRequiredService<IPackagingService>();
        packagingService.InstallCompiledPackageData(xml);

        var contentService = GetRequiredService<IContentService>();
        var rootContent = contentService.GetRootContent();

        var settings = GetRequiredService<IOptionsMonitor<DeliveryApiSettings>>().CurrentValue;

        // Arrange
        var url = "/umbraco/delivery/api/v2/content/item/";

        // Act
        Client.DefaultRequestHeaders.Add("Api-Key", "00414A76-6AFF-49C1-9912-226B9D623EFE");
        var response = await Client.GetAsync(url);

        // Assert
        Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
    }
}

With the following settings in appsettings.Tests.Local.json:

"Umbraco": {
  "CMS": {
    "DeliveryApi": {
      "Enabled": true,
      "PublicAccess": true,
      "ApiKey": "00414A76-6AFF-49C1-9912-226B9D623EFE"
    }
  }
}

For now I have created a package and using the packaging service to spin up an Umbraco instance with some content. I’m double checking to see if it does indeed exist (during debugging). I have altered my appsettings and am double checking to see if the Content Delivery API is enabled. Then I call the API with the Api Key, but the response is always 404 Not Found.

Can anyone point me in the right direction? Am I missing something (obvious) or am I doing something wrong?

Any help is greatly appreciated!

There is no ‘item’ endpoint. You need to supply an id or a path for that. If you want all items, the endpoint is called items (plural).

But an item endpoint without an id or path does not exist.

Hi Luuk,

Thanks for your reply, I appreciatie it!

As per your suggestion I tried the other endpoints, and the ‘content’ endpoint is at least giving me a 200, so that’s some progress! However, there is no content in the response. This is probably also why the ‘item’ endpoint gives a 404. Normally this endpoint resolves the path ‘/’ when left empty, which is usually the root node. But since there is nothing in the index the 404 is thrown.

I’ve tried several things to make this work, but to no avail;

        var contentService = GetRequiredService<IContentService>();
        //rootContent exists and Published is true
        var rootContent = contentService.GetRootContent();

        //Trying to
        contentService.Save(rootContent?.FirstOrDefault());
        contentService.Publish(rootContent?.FirstOrDefault(), new string[] { "nl-NL", "en-US" });

        var rebuilder = GetRequiredService<IIndexRebuilder>();
        rebuilder.RebuildIndexes(false);

        //Doubt that this should work in an integration test, but trying it nontheless
        var contentFactory = GetRequiredService<IUmbracoContextFactory>();
        var serviceProvider = GetRequiredService<IServiceProvider>();
        using var _ = contentFactory.EnsureUmbracoContext();
        using var serviceScope = serviceProvider.CreateScope();
        var queryer = serviceScope.ServiceProvider.GetRequiredService<IPublishedContentQuery>();

        //Result is null
        var pubContent = queryer.Content(rootContent?.FirstOrDefault().Id);

Also tried the ‘item’ endpoint with the key of the rootContent item, which also gives a 404.

Any other pointers? :slight_smile:

The content delivery API works with cached content and your IContentService works directly on the database. So if your content is there in the IContentService, but the content delivery API can’t find it, it would look into rebuilding indexes and cache first.

I tried doing this by (re)publishing the root content node and explicitly rebuilding all the indexes;

        contentService.Save(rootContent?.FirstOrDefault());
        contentService.Publish(rootContent?.FirstOrDefault(), new string[] { "nl-NL", "en-US" });

and;

        var rebuilder = GetRequiredService<IIndexRebuilder>();
        rebuilder.RebuildIndexes(false);

Any other steps I could try to make sure the content is within the cache?