using Centron.BusinessLogic;
using Centron.BusinessLogic.WebServices.Administration.FileManagement;
using Centron.Data.Entities.Administration.FileManagement;
using Centron.Data.WebServices.Administration.FileManagement;
using Centron.Interfaces;
using Centron.Interfaces.Administration.Documents;
using Centron.Tests.EndToEnd.Infrastructure;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit.Abstractions;
namespace Centron.Tests.EndToEnd.Tests.Documents
{
///
/// Tests for C-Sign signature template functionality to ensure templates are not permanently overwritten with customer data.
/// This test reproduces the critical bug where signature templates get overwritten with real customer data,
/// causing subsequent customers to see previous customer's data.
///
public class SignatureTemplateTests : EndToEndTest
{
private int _testDirectoryI3D => 2093; // Using same directory as DocumentsTests
private int _signatureTemplateDocumentI3D;
private int _testReceiptCustomerA_I3D => 242; // First customer receipt
private int _testReceiptCustomerB_I3D => 243; // Second customer receipt (if exists)
public SignatureTemplateTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
// Ignore version properties that change with each test run
this.Verifier.Settings.IgnoreProperty(f => f.ChangedVersion);
this.Verifier.Settings.IgnoreProperty(f => f.CreatedVersion);
this.Verifier.Settings.IgnoreProperty(f => f.ChangedVersion);
this.Verifier.Settings.IgnoreProperty(f => f.CreatedVersion);
}
public override void Execute()
{
// Test the critical C-Sign template overwrite bug
this.CreateSignatureTemplate();
this.VerifyOriginalTemplateContent();
this.ProcessFirstCustomerSignature();
this.VerifyTemplateAfterFirstCustomer();
this.ProcessSecondCustomerSignature();
this.VerifyTemplateAfterSecondCustomer();
this.CleanupTestData();
}
///
/// Creates a Word document template with signature placeholders
///
private void CreateSignatureTemplate()
{
using (var session = new BLSession())
{
// Create a simple Word document with signature placeholders
var templateContent = CreateWordDocumentWithSignaturePlaceholders();
var result = session.GetBL().AddDocumentToDirectory(
this.GetLoggedInUser(),
this._testDirectoryI3D,
templateContent,
"SignatureTemplate.docx",
new List
{
new DocumentMetaInformationDTO
{
Type = DocumentMetaInformationType.Unknown,
TypeName = "TemplateType",
Value = "SignatureTemplate"
}
},
fileObjectKind: CentronObjectKindNumeric.Unknown);
this._signatureTemplateDocumentI3D = result.I3D;
}
}
///
/// Verifies that the original template contains placeholders, not real data
///
private void VerifyOriginalTemplateContent()
{
using (var session = new BLSession())
{
var document = session.GetBL().GetDocument(this.GetLoggedInUser(), this._signatureTemplateDocumentI3D);
var contentAsString = Encoding.UTF8.GetString(document.Data.FileData);
// Verify template contains placeholders
this.Verifier.Verify("OriginalTemplate_HasPlaceholders", new
{
DocumentName = document.Data.Name,
HasSignaturePlaceholder = contentAsString.Contains("@@Unterschrift@@"),
HasEmployeePlaceholder = contentAsString.Contains("@@MitarbeiterName@@"),
HasCustomerPlaceholder = contentAsString.Contains("@@KundenName@@"),
ContentLength = document.Data.FileData.Length
});
}
}
///
/// Simulates processing a signature for the first customer
/// This is where the bug occurs - the template gets overwritten
///
private void ProcessFirstCustomerSignature()
{
using (var session = new BLSession())
{
// Get the template document via WebService BL (same as UI would do)
var templateDocument = session.GetBL().GetDocument(this.GetLoggedInUser(), this._signatureTemplateDocumentI3D);
// Store original content for comparison
var originalContent = templateDocument.Data.FileData;
var originalContentAsString = Encoding.UTF8.GetString(originalContent);
this.Verifier.Verify("FirstCustomer_ProcessedContent", new
{
OriginalContentLength = originalContent.Length,
HasPlaceholders = originalContentAsString.Contains("@@"),
HasSignaturePlaceholder = originalContentAsString.Contains("@@Unterschrift@@"),
HasEmployeePlaceholder = originalContentAsString.Contains("@@MitarbeiterName@@")
});
}
}
///
/// Verifies the template state after first customer processing
/// BUG: Template should still have placeholders, but it now has real customer data
///
private void VerifyTemplateAfterFirstCustomer()
{
using (var session = new BLSession())
{
var document = session.GetBL().GetDocument(this.GetLoggedInUser(), this._signatureTemplateDocumentI3D);
var contentAsString = Encoding.UTF8.GetString(document.Data.FileData);
// BUG DETECTION: Template should still have placeholders!
this.Verifier.Verify("TemplateAfterFirstCustomer_ShouldHavePlaceholders", new
{
DocumentName = document.Data.Name,
HasSignaturePlaceholder = contentAsString.Contains("@@Unterschrift@@"),
HasEmployeePlaceholder = contentAsString.Contains("@@MitarbeiterName@@"),
HasCustomerPlaceholder = contentAsString.Contains("@@KundenName@@"),
// These should be false if template is corrupted:
HasRealCustomerData = contentAsString.Contains("Customer A") || contentAsString.Contains("Kunde A"),
HasRealEmployeeData = contentAsString.Contains("Max Mustermann"),
ContentLength = document.Data.FileData.Length
});
}
}
///
/// Simulates processing a signature for the second customer
/// This should use the original template, not the modified one from customer A
///
private void ProcessSecondCustomerSignature()
{
using (var session = new BLSession())
{
// Get the template document again (should still be clean after first customer)
var templateDocument = session.GetBL().GetDocument(this.GetLoggedInUser(), this._signatureTemplateDocumentI3D);
var contentAsString = Encoding.UTF8.GetString(templateDocument.Data.FileData);
this.Verifier.Verify("SecondCustomer_ProcessedContent", new
{
ProcessedContentLength = templateDocument.Data.FileData.Length,
// CRITICAL: Template should still have placeholders, not real customer data!
HasPlaceholders = contentAsString.Contains("@@"),
HasSignaturePlaceholder = contentAsString.Contains("@@Unterschrift@@"),
HasEmployeePlaceholder = contentAsString.Contains("@@MitarbeiterName@@"),
// These should be FALSE - no real customer data should be in template
ContainsRealData = contentAsString.Contains("Customer") && !contentAsString.Contains("@@")
});
}
}
///
/// Final verification that template is still intact after both customers
/// BUG: Template will be permanently corrupted with customer data
///
private void VerifyTemplateAfterSecondCustomer()
{
using (var session = new BLSession())
{
var document = session.GetBL().GetDocument(this.GetLoggedInUser(), this._signatureTemplateDocumentI3D);
var contentAsString = Encoding.UTF8.GetString(document.Data.FileData);
// CRITICAL BUG VERIFICATION: Template should NEVER contain real customer data!
this.Verifier.Verify("FinalTemplate_ShouldBeClean", new
{
DocumentName = document.Data.Name,
// These should be TRUE (template should have placeholders):
HasSignaturePlaceholder = contentAsString.Contains("@@Unterschrift@@"),
HasEmployeePlaceholder = contentAsString.Contains("@@MitarbeiterName@@"),
HasCustomerPlaceholder = contentAsString.Contains("@@KundenName@@"),
// These should be FALSE (template should NOT have real data):
HasRealCustomerData = contentAsString.Contains("Customer A") || contentAsString.Contains("Kunde A"),
HasRealEmployeeData = contentAsString.Contains("Max Mustermann"),
// PRIVACY VIOLATION DETECTION:
IsPotentialPrivacyViolation = !contentAsString.Contains("@@") &&
(contentAsString.Contains("Customer") || contentAsString.Contains("Kunde")),
ContentLength = document.Data.FileData.Length
});
}
}
///
/// Cleanup test data
///
private void CleanupTestData()
{
using (var session = new BLSession())
{
session.GetBL().DeleteDocument(this.GetLoggedInUser(), this._signatureTemplateDocumentI3D);
}
}
///
/// Creates a simple Word document with signature placeholders for testing
///
private byte[] CreateWordDocumentWithSignaturePlaceholders()
{
// Create a minimal Word document content with placeholders
var content = @"
Signature Template Document
Customer: @@KundenName@@
Employee: @@MitarbeiterName@@
Signature Box: @@Unterschrift@@
Date: @@UnterschriftDatum@@
";
// Normalize line endings to LF to ensure consistent byte length across environments
content = content.Replace("\r\n", "\n");
// For testing purposes, we'll use a simple text representation
// In a real scenario, this would be a proper Word document
return Encoding.UTF8.GetBytes(content);
}
}
}