<commit_before>package org.sagebionetworks.bridge.services;

import static org.junit.Assert.assertTrue;

import java.io.FileInputStream;

import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.bridge.TestConstants;
import org.sagebionetworks.bridge.dynamodb.DynamoStudy;
import org.sagebionetworks.bridge.models.accounts.SharingScope;
import org.sagebionetworks.bridge.models.accounts.StudyParticipant;
import org.sagebionetworks.bridge.models.studies.Study;
import org.sagebionetworks.bridge.models.subpopulations.ConsentSignature;

public class ConsentPdfTest {
    private static final long TIMESTAMP = DateTime.parse("2017-10-04").getMillis();
    private static final String LEGACY_DOCUMENT = "<html><head></head><body>Passed through as is." +
            "|@@name@@|@@signing.date@@|@@email@@|@@sharing@@|" +
            "<img src=\"cid:consentSignature\" /></body></html>";
    private static final String NEW_DOCUMENT_FRAGMENT = "<p>This is a consent agreement body</p>";
    // This is an actual 2x2 image
    private static final String DUMMY_IMAGE_DATA =
            "Qk1GAAAAAAAAADYAAAAoAAAAAgAAAAIAAAABABgAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAA////AAAAAAAAAAD///8AAA==";
    private static final StudyParticipant EMAIL_PARTICIPANT = new StudyParticipant.Builder()
            .withEmail("<EMAIL>").withEmailVerified(true).build();;
    
    private String consentBodyTemplate;
    private Study study;
    
    @Before
    public void before() throws Exception {
        DateTimeUtils.setCurrentMillisFixed(TIMESTAMP);
        consentBodyTemplate = IOUtils.toString(new FileInputStream("conf/study-defaults/consent-page.xhtml"));
        
        study = new DynamoStudy();
        study.setName("Study Name");
        study.setSponsorName("Sp<NAME>");
        study.setSupportEmail("<EMAIL>");
        study.setConsentNotificationEmail("<EMAIL>");
        study.setConsentNotificationEmailVerified(true);
    }
    
    @After
    public void after() {
        DateTimeUtils.setCurrentMillisSystem();
    }
    
    @Test
    public void createsBytes() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);
        
        assertTrue(consentPdf.getBytes().length > 0);
    }

    @Test
    public void docWithNullUserTimeZone() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);
        
        String output = consentPdf.getFormattedConsentDocument();
        String dateStr = ConsentPdf.FORMATTER.print(DateTime.now(DateTimeZone.UTC));
        assertTrue("Signing date formatted with default zone", output.contains(dateStr));
    }
    
    @Test
    public void legacyDocWithoutSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);

        String output = consentPdf.getFormattedConsentDocument();
        validateLegacyDocBody(output);
    }

    @Test
    public void newDocWithoutSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING,
                NEW_DOCUMENT_FRAGMENT, consentBodyTemplate);

        String output = consentPdf.getFormattedConsentDocument(); 
        validateNewDocBody(output);
    }

    @Test
    public void legacyDocWithSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);

        String output = consentPdf.getFormattedConsentDocument();
        validateLegacyDocBody(output);
    }

    @Test
    public void newDocWithSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING,
                NEW_DOCUMENT_FRAGMENT, consentBodyTemplate);
        
        String output = consentPdf.getFormattedConsentDocument();
        validateNewDocBody(output);
    }
    @Test
    public void legacyDocWithInvalidSig() throws Exception {
        ConsentSignature sig = makeInvalidSignature();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING,
                NEW_DOCUMENT_FRAGMENT, consentBodyTemplate);
        
        String output = consentPdf.getFormattedConsentDocument();
        validateNewDocBody(output);
    }
<commit_msg>Adding test for legacy document where there's no email address.<commit_after>package org.sagebionetworks.bridge.services;

import static org.junit.Assert.assertTrue;

import java.io.FileInputStream;

import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.bridge.TestConstants;
import org.sagebionetworks.bridge.dynamodb.DynamoStudy;
import org.sagebionetworks.bridge.models.accounts.SharingScope;
import org.sagebionetworks.bridge.models.accounts.StudyParticipant;
import org.sagebionetworks.bridge.models.studies.Study;
import org.sagebionetworks.bridge.models.subpopulations.ConsentSignature;

public class ConsentPdfTest {
    private static final long TIMESTAMP = DateTime.parse("2017-10-04").getMillis();
    private static final String LEGACY_DOCUMENT = "<html><head></head><body>Passed through as is." +
            "|@@name@@|@@signing.date@@|@@email@@|@@sharing@@|" +
            "<img src=\"cid:consentSignature\" /></body></html>";
    private static final String NEW_DOCUMENT_FRAGMENT = "<p>This is a consent agreement body</p>";
    // This is an actual 2x2 image
    private static final String DUMMY_IMAGE_DATA =
            "Qk1GAAAAAAAAADYAAAAoAAAAAgAAAAIAAAABABgAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAA////AAAAAAAAAAD///8AAA==";
    private static final StudyParticipant EMAIL_PARTICIPANT = new StudyParticipant.Builder()
            .withEmail("<EMAIL>").withEmailVerified(true).build();;
    
    private String consentBodyTemplate;
    private Study study;
    
    @Before
    public void before() throws Exception {
        DateTimeUtils.setCurrentMillisFixed(TIMESTAMP);
        consentBodyTemplate = IOUtils.toString(new FileInputStream("conf/study-defaults/consent-page.xhtml"));
        
        study = new DynamoStudy();
        study.setName("Study Name");
        study.setSponsorName("Sponsor Name");
        study.setSupportEmail("<EMAIL>");
        study.setConsentNotificationEmail("<EMAIL>");
        study.setConsentNotificationEmailVerified(true);
    }
    
    @After
    public void after() {
        DateTimeUtils.setCurrentMillisSystem();
    }
    
    @Test
    public void createsBytes() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);
        
        assertTrue(consentPdf.getBytes().length > 0);
    }
    
    @Test
    public void docWithNullUserTimeZone() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);
        
        String output = consentPdf.getFormattedConsentDocument();
        String dateStr = ConsentPdf.FORMATTER.print(DateTime.now(DateTimeZone.UTC));
        assertTrue("Signing date formatted with default zone", output.contains(dateStr));
    }
    
    @Test
    public void legacyDocWithoutSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);

        String output = consentPdf.getFormattedConsentDocument();
        validateLegacyDocBody(output);
    }

    @Test
    public void newDocWithoutSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithoutImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING,
                NEW_DOCUMENT_FRAGMENT, consentBodyTemplate);

        String output = consentPdf.getFormattedConsentDocument(); 
        validateNewDocBody(output);
    }

    @Test
    public void legacyDocWithSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);

        String output = consentPdf.getFormattedConsentDocument();
        validateLegacyDocBody(output);
    }

    @Test
    public void newDocWithSigImage() throws Exception {
        ConsentSignature sig = makeSignatureWithImage();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING,
                NEW_DOCUMENT_FRAGMENT, consentBodyTemplate);
        
        String output = consentPdf.getFormattedConsentDocument();
        validateNewDocBody(output);
    }
    @Test
    public void legacyDocWithInvalidSig() throws Exception {
        ConsentSignature sig = makeInvalidSignature();
        
        ConsentPdf consentPdf = new ConsentPdf(study, EMAIL_PARTICIPANT, sig, SharingScope.NO_SHARING,
                NEW_DOCUMENT_FRAGMENT, consentBodyTemplate);
        
        String output = consentPdf.getFormattedConsentDocument();
        validateNewDocBody(output);
    }
    
    @Test
    public void legacyDocWithNoEmail() throws Exception {
        ConsentSignature sig = makeSignatureWithImage();
        
        StudyParticipant noEmailParticipant = new StudyParticipant.Builder().copyOf(EMAIL_PARTICIPANT)
                .withEmail(null).build();
        
        ConsentPdf consentPdf = new ConsentPdf(study, noEmailParticipant, sig, SharingScope.NO_SHARING, LEGACY_DOCUMENT,
                consentBodyTemplate);

        String output = consentPdf.getFormattedConsentDocument();
        assertTrue(!output.contains("@@email@@"));
    }

{'fraction_non_alphanumeric': 0.08867308041386822, 'fraction_numerical': 0.002359774913777455, 'mean_word_length': 3.3587816455696204, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6344322', 'n_tokens_mistral': 3234, 'n_tokens_neox': 3148, 'n_words': 591}