diff --git a/src/Altinn.App.Api/Controllers/PdfController.cs b/src/Altinn.App.Api/Controllers/PdfController.cs index 174a58353..d762c1b90 100644 --- a/src/Altinn.App.Api/Controllers/PdfController.cs +++ b/src/Altinn.App.Api/Controllers/PdfController.cs @@ -73,11 +73,6 @@ public async Task GetPdfPreview( [FromRoute] Guid instanceGuid ) { - if (_env.IsProduction()) - { - return NotFound(); - } - var instance = await _instanceClient.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); string? taskId = instance.Process?.CurrentTask?.ElementId; if (instance == null || taskId == null) @@ -85,7 +80,7 @@ [FromRoute] Guid instanceGuid return NotFound("Did not find instance or task"); } - Stream pdfContent = await _pdfService.GeneratePdf(instance, taskId, CancellationToken.None); + Stream pdfContent = await _pdfService.GeneratePdf(instance, taskId, true, CancellationToken.None); return new FileStreamResult(pdfContent, "application/pdf"); } diff --git a/src/Altinn.App.Core/Internal/Pdf/IPdfService.cs b/src/Altinn.App.Core/Internal/Pdf/IPdfService.cs index c4d275d54..f2b96c144 100644 --- a/src/Altinn.App.Core/Internal/Pdf/IPdfService.cs +++ b/src/Altinn.App.Core/Internal/Pdf/IPdfService.cs @@ -23,4 +23,17 @@ public interface IPdfService /// The task id for witch the pdf is generated /// Cancellation Token for when a request should be stopped before it's completed. Task GeneratePdf(Instance instance, string taskId, CancellationToken ct); + + // + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// Indicates whether the PDF is a preview version. + Task GeneratePdf(Instance instance, string taskId, bool isPreview, CancellationToken ct); } diff --git a/src/Altinn.App.Core/Internal/Pdf/PdfService.cs b/src/Altinn.App.Core/Internal/Pdf/PdfService.cs index 5848cca7b..47e708466 100644 --- a/src/Altinn.App.Core/Internal/Pdf/PdfService.cs +++ b/src/Altinn.App.Core/Internal/Pdf/PdfService.cs @@ -84,14 +84,14 @@ public async Task GenerateAndStorePdf(Instance instance, string taskId, Cancella TextResource? textResource = await GetTextResource(instance, language); - var pdfContent = await GeneratePdfContent(instance, taskId, language, textResource, ct); + var pdfContent = await GeneratePdfContent(instance, language, false, textResource, ct); string fileName = GetFileName(instance, textResource); await _dataClient.InsertBinaryData(instance.Id, PdfElementType, PdfContentType, fileName, pdfContent, taskId); } /// - public async Task GeneratePdf(Instance instance, string taskId, CancellationToken ct) + public async Task GeneratePdf(Instance instance, string taskId, bool isPreview, CancellationToken ct) { using var activity = _telemetry?.StartGeneratePdfActivity(instance, taskId); @@ -103,13 +103,19 @@ public async Task GeneratePdf(Instance instance, string taskId, Cancella TextResource? textResource = await GetTextResource(instance, language); - return await GeneratePdfContent(instance, taskId, language, textResource, ct); + return await GeneratePdfContent(instance, language, isPreview, textResource, ct); + } + + /// + public async Task GeneratePdf(Instance instance, string taskId, CancellationToken ct) + { + return await GeneratePdf(instance, taskId, false, ct); } private async Task GeneratePdfContent( Instance instance, - string taskId, string language, + bool isPreview, TextResource? textResource, CancellationToken ct ) @@ -122,7 +128,17 @@ CancellationToken ct Uri uri = BuildUri(baseUrl, pagePath, language); bool displayFooter = _pdfGeneratorSettings.DisplayFooter; - string? footerContent = displayFooter ? GetFooterContent(instance, textResource) : null; + + string? footerContent = null; + + if (isPreview == true) + { + footerContent = GetPreviewFooter(textResource); + } + else if (displayFooter) + { + footerContent = GetFooterContent(instance, textResource); + } Stream pdfContent = await _pdfGeneratorClient.GeneratePdf(uri, footerContent, ct); @@ -249,12 +265,39 @@ private static string GetFileName(Instance instance, TextResource? textResource) return null; } + private static string GetPdfPreviewText(TextResource? textResource) + { + if (textResource is not null) + { + TextResourceElement? titleText = textResource.Resources.Find(textResourceElement => + textResourceElement.Id.Equals("pdfPreviewText", StringComparison.Ordinal) + ); + + if (titleText is not null) + { + return titleText.Value; + } + } + + return "Dokumentet er en forhåndsvisning"; + } + private static string GetValidFileName(string fileName) { fileName = Uri.EscapeDataString(fileName.AsFileName(false)); return fileName; } + private static string GetPreviewFooter(TextResource? textResource) + { + string previewText = GetPdfPreviewText(textResource); + return $@"
+
+ {previewText} +
+
"; + } + private string GetFooterContent(Instance instance, TextResource? textResource) { TimeZoneInfo timeZone = TimeZoneInfo.Utc; diff --git a/src/Altinn.App.Core/Internal/Process/ProcessTasks/PaymentProcessTask.cs b/src/Altinn.App.Core/Internal/Process/ProcessTasks/PaymentProcessTask.cs index 7829742e7..d1f6e5e2e 100644 --- a/src/Altinn.App.Core/Internal/Process/ProcessTasks/PaymentProcessTask.cs +++ b/src/Altinn.App.Core/Internal/Process/ProcessTasks/PaymentProcessTask.cs @@ -61,7 +61,7 @@ public async Task End(string taskId, Instance instance) if (paymentStatus != PaymentStatus.Paid) throw new PaymentException("The payment is not completed."); - Stream pdfStream = await _pdfService.GeneratePdf(instance, taskId, CancellationToken.None); + Stream pdfStream = await _pdfService.GeneratePdf(instance, taskId, false, CancellationToken.None); ValidAltinnPaymentConfiguration validatedPaymentConfiguration = paymentConfiguration.Validate(); diff --git a/test/Altinn.App.Api.Tests/Controllers/PdfControllerTests.cs b/test/Altinn.App.Api.Tests/Controllers/PdfControllerTests.cs index 030d5a09f..983675b50 100644 --- a/test/Altinn.App.Api.Tests/Controllers/PdfControllerTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/PdfControllerTests.cs @@ -66,57 +66,6 @@ public PdfControllerTests() ); } - [Fact] - public async Task Request_In_Prod_Should_Be_Blocked() - { - var env = new Mock(); - env.Setup(a => a.EnvironmentName).Returns("Production"); - - IOptions generalSettingsOptions = Options.Create( - new() { HostName = "org.apps.altinn.no" } - ); - - var httpContextAccessor = new Mock(); - httpContextAccessor.Setup(x => x.HttpContext!.Request!.Query["lang"]).Returns(LanguageConst.Nb); - - var handler = new Mock(); - var httpClient = new HttpClient(handler.Object); - - var pdfGeneratorClient = new PdfGeneratorClient( - httpClient, - _pdfGeneratorSettingsOptions, - _platformSettingsOptions, - _userTokenProvider.Object, - httpContextAccessor.Object - ); - var pdfService = new PdfService( - _appResources.Object, - _dataClient.Object, - httpContextAccessor.Object, - _profile.Object, - pdfGeneratorClient, - _pdfGeneratorSettingsOptions, - generalSettingsOptions, - _logger.Object - ); - var pdfController = new PdfController( - _instanceClient.Object, - _pdfFormatter.Object, - _appResources.Object, - _appModel.Object, - _dataClient.Object, - env.Object, - pdfService - ); - - var result = await pdfController.GetPdfPreview(_org, _app, _partyId, _instanceId); - - result.Should().BeOfType(typeof(NotFoundResult)); - handler - .Protected() - .Verify("SendAsync", Times.Never(), ItExpr.IsAny(), ItExpr.IsAny()); - } - [Fact] public async Task Request_In_Dev_Should_Generate() { diff --git a/test/Altinn.App.Api.Tests/Data/apps/tdd/task-action/config/texts/resource.nb.json b/test/Altinn.App.Api.Tests/Data/apps/tdd/task-action/config/texts/resource.nb.json index 0a628564e..c84479f50 100644 --- a/test/Altinn.App.Api.Tests/Data/apps/tdd/task-action/config/texts/resource.nb.json +++ b/test/Altinn.App.Api.Tests/Data/apps/tdd/task-action/config/texts/resource.nb.json @@ -30,4 +30,4 @@ "value": "Avvis" } ] -} \ No newline at end of file +} diff --git a/test/Altinn.App.Core.Tests/Internal/Process/ProcessTasks/PaymentProcessTaskTests.cs b/test/Altinn.App.Core.Tests/Internal/Process/ProcessTasks/PaymentProcessTaskTests.cs index a162dd95f..3856f755e 100644 --- a/test/Altinn.App.Core.Tests/Internal/Process/ProcessTasks/PaymentProcessTaskTests.cs +++ b/test/Altinn.App.Core.Tests/Internal/Process/ProcessTasks/PaymentProcessTaskTests.cs @@ -77,7 +77,7 @@ public async Task End_PaymentCompleted_ShouldGeneratePdfReceipt() await _paymentProcessTask.End(taskId, instance); // Assert - _pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None)); + _pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, false, CancellationToken.None)); _dataClientMock.Verify(x => x.InsertBinaryData( instance.Id, @@ -110,7 +110,7 @@ public async Task End_PaymentSkipped_ShouldNotGeneratePdfReceipt() await _paymentProcessTask.End(taskId, instance); // Assert - _pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None), Times.Never); + _pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, false, CancellationToken.None), Times.Never); _dataClientMock.Verify( x => x.InsertBinaryData( @@ -142,7 +142,7 @@ public async Task End_PaymentNotCompleted_ShouldThrowException() .ReturnsAsync(PaymentStatus.Created); // Act and assert - _pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None), Times.Never); + _pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, false, CancellationToken.None), Times.Never); _dataClientMock.Verify( x => x.InsertBinaryData( @@ -220,7 +220,9 @@ public async Task End_ValidConfiguration_ShouldNotThrow() using var memoryStream = new MemoryStream(); _pdfServiceMock - .Setup(ps => ps.GeneratePdf(It.IsAny(), It.IsAny(), It.IsAny())) + .Setup(ps => + ps.GeneratePdf(It.IsAny(), It.IsAny(), false, It.IsAny()) + ) .ReturnsAsync(memoryStream); Func act = async () => await _paymentProcessTask.End("taskId", new Instance());