Skip to content
Merged
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
10 changes: 6 additions & 4 deletions src/Altinn.App.Core/Helpers/AllowedContributorsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ public static void EnsureDataTypeIsAppOwned(ApplicationMetadata metadata, string
return;
}

DataType? dataType = metadata.DataTypes.Find(x => x.Id == dataTypeId);
DataType dataType =
metadata.DataTypes.Find(x => x.Id == dataTypeId)
?? throw new ApplicationConfigException($"Data type {dataTypeId} not found in applicationmetadata.json");
#pragma warning disable CS0618 // Type or member is obsolete
List<string>? allowedContributors = dataType?.AllowedContributers;
List<string>? allowedContributors = dataType.AllowedContributers;
#pragma warning restore CS0618 // Type or member is obsolete

if (allowedContributors is null || allowedContributors.Count == 0)
{
allowedContributors = dataType?.AllowedContributors;
allowedContributors = dataType.AllowedContributors;
}

if (
Expand All @@ -85,7 +87,7 @@ allowedContributors is null
)
{
throw new ApplicationConfigException(
$"AllowedContributors (or AllowedContributers) must be set to ['app:owned'] on the data type ${dataType?.Id}. This is to prevent editing of the data type through the API."
$"AllowedContributors (or AllowedContributers) must be set to ['app:owned'] on the data type ${dataType.Id}. This is to prevent editing of the data type through the API."
);
}
}
Expand Down
36 changes: 24 additions & 12 deletions test/Altinn.App.Api.Tests/Controllers/SigningControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@
using Altinn.App.Core.Internal.Instances;
using Altinn.App.Core.Internal.Process;
using Altinn.App.Core.Internal.Process.Elements.AltinnExtensionProperties;
using Altinn.App.Core.Models;
using Altinn.Platform.Register.Models;
using Altinn.Platform.Storage.Interface.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using Xunit.Abstractions;
using static Altinn.App.Core.Features.Signing.Models.Signee;
using SigneeContextState = Altinn.App.Core.Features.Signing.Models.SigneeContextState;

namespace Altinn.App.Api.Tests.Controllers;

public class SigningControllerTests
{
private readonly Mock<IInstanceClient> _instanceClientMock = new();
private readonly Mock<IProcessReader> _processReaderMock = new();
private readonly Mock<ILogger<SigningController>> _loggerMock = new();
private readonly Mock<ISigningService> _signingServiceMock = new();
private readonly Mock<IDataClient> _dataClientMock = new();
private readonly Mock<IAppMetadata> _applicationMetadataMock = new();
private readonly Mock<IAppModel> _appModelMock = new();
private readonly Mock<IAppResources> _appResourcesMock = new();
private readonly Mock<IInstanceClient> _instanceClientMock = new(MockBehavior.Strict);
private readonly Mock<IProcessReader> _processReaderMock = new(MockBehavior.Strict);
private readonly Mock<ISigningService> _signingServiceMock = new(MockBehavior.Strict);
private readonly Mock<IDataClient> _dataClientMock = new(MockBehavior.Strict);
private readonly Mock<IAppMetadata> _applicationMetadataMock = new(MockBehavior.Strict);
private readonly Mock<IAppModel> _appModelMock = new(MockBehavior.Strict);
private readonly Mock<IAppResources> _appResourcesMock = new(MockBehavior.Strict);
private readonly ServiceCollection _serviceCollection = new();
private readonly Mock<IHttpContextAccessor> _httpContextAccessorMock = new();
private readonly Mock<IHttpContextAccessor> _httpContextAccessorMock = new(MockBehavior.Strict);

private readonly AltinnTaskExtension _altinnTaskExtension = new()
{
Expand All @@ -52,7 +52,7 @@ public class SigningControllerTests
},
};

public SigningControllerTests()
public SigningControllerTests(ITestOutputHelper output)
{
_serviceCollection.AddTransient<ModelSerializationService>();
_serviceCollection.AddTransient<InstanceDataUnitOfWorkInitializer>();
Expand All @@ -66,7 +66,7 @@ public SigningControllerTests()
_serviceCollection.AddSingleton(_appResourcesMock.Object);
_serviceCollection.AddSingleton(_processReaderMock.Object);
_serviceCollection.AddSingleton(_httpContextAccessorMock.Object);
_serviceCollection.AddSingleton(_loggerMock.Object);
_serviceCollection.AddFakeLoggingWithXunit(output);

_instanceClientMock
.Setup(x => x.GetInstance(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<Guid>()))
Expand All @@ -82,6 +82,18 @@ public SigningControllerTests()
);

_processReaderMock.Setup(s => s.GetAltinnTaskExtension(It.IsAny<string>())).Returns(_altinnTaskExtension);

_applicationMetadataMock
.Setup(a => a.GetApplicationMetadata())
.ReturnsAsync(
new ApplicationMetadata("ttd/app")
{
DataTypes =
[
// this test does not verify the data types
],
}
);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Altinn.App.Core.Internal.Process;
using Altinn.App.Core.Internal.Process.Elements.AltinnExtensionProperties;
using Altinn.App.Core.Internal.Process.ProcessTasks;
using Altinn.App.Core.Models;
using Altinn.Platform.Storage.Interface.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -21,16 +22,16 @@ namespace Altinn.App.Core.Tests.Internal.Process.ProcessTasks;

public class SigningProcessTaskTests
{
private readonly Mock<IProcessReader> _processReaderMock = new();
private readonly Mock<IInstanceClient> _instanceClientMock = new();
private readonly Mock<ISigningService> _signingServiceMock = new();
private readonly Mock<ISigneeContextsManager> _signeeContextsManagerMock = new();
private readonly Mock<IAppMetadata> _appMetadataMock = new();
private readonly Mock<IHostEnvironment> _hostEnvironmentMock = new();
private readonly Mock<IAppModel> _appModelMock = new();
private readonly Mock<IDataClient> _dataClientMock = new();
private readonly Mock<IPdfService> _pdfServiceMock = new();
private readonly Mock<IAppResources> _appResourcesMock = new();
private readonly Mock<IProcessReader> _processReaderMock = new(MockBehavior.Strict);
private readonly Mock<IInstanceClient> _instanceClientMock = new(MockBehavior.Strict);
private readonly Mock<ISigningService> _signingServiceMock = new(MockBehavior.Strict);
private readonly Mock<ISigneeContextsManager> _signeeContextsManagerMock = new(MockBehavior.Strict);
private readonly Mock<IAppMetadata> _appMetadataMock = new(MockBehavior.Strict);
private readonly Mock<IHostEnvironment> _hostEnvironmentMock = new(MockBehavior.Strict);
private readonly Mock<IAppModel> _appModelMock = new(MockBehavior.Strict);
private readonly Mock<IDataClient> _dataClientMock = new(MockBehavior.Strict);
private readonly Mock<IPdfService> _pdfServiceMock = new(MockBehavior.Strict);
private readonly Mock<IAppResources> _appResourcesMock = new(MockBehavior.Strict);
private readonly ServiceCollection _serviceCollection = new();

public SigningProcessTaskTests()
Expand All @@ -49,6 +50,30 @@ public SigningProcessTaskTests()
_serviceCollection.AddSingleton(_dataClientMock.Object);
_serviceCollection.AddSingleton(_pdfServiceMock.Object);
_serviceCollection.AddSingleton(_appResourcesMock.Object);

_appMetadataMock
.Setup(a => a.GetApplicationMetadata())
.ReturnsAsync(
new ApplicationMetadata("ttd/app")
{
DataTypes =
[
new DataType()
{
Id = "SignatureDataType",
TaskId = "Task_1",
AllowedContributors = ["app:owned"],
},
new DataType()
{
Id = "SigneeStatesDataTypeId",
TaskId = "Task_1",
AllowedContributors = ["app:owned"],
},
],
}
);
_hostEnvironmentMock.SetupGet(e => e.EnvironmentName).Returns("Development");
}

[Fact]
Expand All @@ -71,34 +96,27 @@ public async Task Start_ShouldDeleteExistingSigningData()
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync([]);

// Act
await signingProcessTask.Start(taskId, instance);

// Assert
_signeeContextsManagerMock.Verify(
x =>
x.GenerateSigneeContexts(
It.IsAny<IInstanceDataMutator>(),
It.IsAny<AltinnSignatureConfiguration>(),
It.IsAny<CancellationToken>()
),
Times.Once
);
.ReturnsAsync([])
.Verifiable(Times.Once);

_signingServiceMock.Verify(
x =>
_signingServiceMock
.Setup(x =>
x.InitializeSignees(
It.IsAny<IInstanceDataMutator>(),
It.IsAny<List<SigneeContext>>(),
It.IsAny<AltinnSignatureConfiguration>(),
It.IsAny<CancellationToken>()
),
Times.Once
);
)
)
.ReturnsAsync([])
.Verifiable(Times.Once);

// Act
await signingProcessTask.Start(taskId, instance);

_signingServiceMock.VerifyNoOtherCalls();
// Assert
_signeeContextsManagerMock.VerifyAll();
_signingServiceMock.VerifyAll();
}

[Fact]
Expand All @@ -113,18 +131,22 @@ public async Task Abandon_ShouldDeleteExistingSigningData()
var signingProcessTask = sp.GetRequiredService<SigningProcessTask>();

_processReaderMock.Setup(x => x.GetAltinnTaskExtension(It.IsAny<string>())).Returns(altinnTaskExtension);
_signingServiceMock
.Setup(x =>
x.AbortRuntimeDelegatedSigning(
It.IsAny<IInstanceDataMutator>(),
altinnTaskExtension.SignatureConfiguration,
It.IsAny<CancellationToken>()
)
)
.Returns(Task.CompletedTask)
.Verifiable(Times.Once);

// Act
await signingProcessTask.Abandon(taskId, instance);

// Assert
_signingServiceMock.Verify(x =>
x.AbortRuntimeDelegatedSigning(
It.IsAny<IInstanceDataMutator>(),
altinnTaskExtension.SignatureConfiguration,
It.IsAny<CancellationToken>()
)
);
_signingServiceMock.VerifyAll();
}

private static Instance CreateInstance()
Expand Down
Loading