Search in sources :

Example 1 with ConsentStatus

use of org.sagebionetworks.bridge.models.accounts.ConsentStatus in project BridgeServer2 by Sage-Bionetworks.

the class CacheProviderTest method assertSession.

private void assertSession(String json) {
    JedisOps jedisOps = mock(JedisOps.class);
    when(jedisOps.get(TOKEN_TO_USER_ID.toString())).thenReturn(USER_ID);
    when(jedisOps.get(USER_ID_TO_SESSION.toString())).thenReturn(json);
    cacheProvider.setJedisOps(jedisOps);
    UserSession session = cacheProvider.getUserSession(DECRYPTED_SESSION_TOKEN);
    assertTrue(session.isAuthenticated());
    assertEquals(session.getEnvironment(), Environment.LOCAL);
    assertEquals(session.getSessionToken(), DECRYPTED_SESSION_TOKEN);
    assertEquals(session.getInternalSessionToken(), "4f0937a5-6ebf-451b-84bc-fbf649b9e93c");
    assertEquals(session.getId(), "6gq4jGXLmAxVbLLmVifKN4");
    assertEquals(session.getAppId(), TEST_APP_ID);
    StudyParticipant participant = session.getParticipant();
    assertEquals(participant.getFirstName(), "Bridge");
    assertEquals(participant.getLastName(), "IT");
    assertEquals(participant.getEmail(), "bridgeit@sagebase.org");
    assertEquals(participant.getSharingScope(), SharingScope.NO_SHARING);
    assertEquals(participant.getCreatedOn(), DateTime.parse("2016-04-21T16:48:22.386Z"));
    assertEquals(participant.getRoles(), Sets.newHashSet(Roles.ADMIN));
    assertEquals(participant.getLanguages(), ImmutableList.of("en", "fr"));
    assertEquals(participant.getExternalId(), "ABC");
    assertEquals(ENCRYPTOR.decrypt(ENCRYPTED_SESSION_TOKEN), participant.getHealthCode());
    SubpopulationGuid apiGuid = SubpopulationGuid.create(TEST_APP_ID);
    Map<SubpopulationGuid, ConsentStatus> consentStatuses = session.getConsentStatuses();
    ConsentStatus status = consentStatuses.get(apiGuid);
    assertEquals(status.getName(), "Default Consent Group");
    assertEquals(status.getSubpopulationGuid(), apiGuid.getGuid());
    assertTrue(status.getSignedMostRecentConsent());
    assertTrue(status.isRequired());
    assertFalse(status.isConsented());
}
Also used : JedisOps(org.sagebionetworks.bridge.redis.JedisOps) ConsentStatus(org.sagebionetworks.bridge.models.accounts.ConsentStatus) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant)

Example 2 with ConsentStatus

use of org.sagebionetworks.bridge.models.accounts.ConsentStatus in project BridgeServer2 by Sage-Bionetworks.

the class ConsentService method getConsentStatuses.

/**
 * Get all the consent status objects for this user. From these, we determine if the user
 * has consented to the right consents to have access to the app, and whether or not those
 * consents are up-to-date.
 */
public Map<SubpopulationGuid, ConsentStatus> getConsentStatuses(CriteriaContext context, Account account) {
    checkNotNull(context);
    ImmutableMap.Builder<SubpopulationGuid, ConsentStatus> builder = new ImmutableMap.Builder<>();
    for (Subpopulation subpop : subpopService.getSubpopulationsForUser(context)) {
        ConsentSignature signature = account.getActiveConsentSignature(subpop.getGuid());
        boolean hasConsented = (signature != null);
        boolean hasSignedActiveConsent = (hasConsented && signature.getConsentCreatedOn() == subpop.getPublishedConsentCreatedOn());
        ConsentStatus status = new ConsentStatus.Builder().withName(subpop.getName()).withGuid(subpop.getGuid()).withRequired(subpop.isRequired()).withConsented(hasConsented).withSignedMostRecentConsent(hasSignedActiveConsent).withSignedOn(hasConsented ? signature.getSignedOn() : null).build();
        builder.put(subpop.getGuid(), status);
    }
    return builder.build();
}
Also used : ConsentSignature(org.sagebionetworks.bridge.models.subpopulations.ConsentSignature) ConsentStatus(org.sagebionetworks.bridge.models.accounts.ConsentStatus) Subpopulation(org.sagebionetworks.bridge.models.subpopulations.Subpopulation) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with ConsentStatus

use of org.sagebionetworks.bridge.models.accounts.ConsentStatus in project BridgeServer2 by Sage-Bionetworks.

the class SubpopulationGuidDeserializerTest method consentStatusAsKeySerializes.

@Test
public void consentStatusAsKeySerializes() throws Exception {
    // Don't use BridgeObjectMapper because this is one of the rare objects that is serialized/
    // deserialized in Redis, and that uses an unconfigured ObjectMapper instance
    ObjectMapper mapper = new ObjectMapper();
    Map<SubpopulationGuid, ConsentStatus> map = Maps.newHashMap();
    map.put(SubpopulationGuid.create(TestConstants.REQUIRED_SIGNED_CURRENT.getSubpopulationGuid()), TestConstants.REQUIRED_SIGNED_CURRENT);
    map.put(SubpopulationGuid.create(TestConstants.REQUIRED_SIGNED_OBSOLETE.getSubpopulationGuid()), TestConstants.REQUIRED_SIGNED_OBSOLETE);
    map.put(SubpopulationGuid.create(TestConstants.REQUIRED_UNSIGNED.getSubpopulationGuid()), TestConstants.REQUIRED_UNSIGNED);
    String json = mapper.writeValueAsString(map);
    JsonNode node = mapper.readTree(json);
    assertNotNull(node.get("foo1"));
    assertNotNull(node.get("foo2"));
    assertNotNull(node.get("foo5"));
    JsonNode consentNode = node.get("foo5");
    ConsentStatus consent5 = mapper.readValue(consentNode.toString(), ConsentStatus.class);
    assertEquals(consent5, TestConstants.REQUIRED_UNSIGNED);
    // Test deserialization in a class that has the annotation to restore the SubpopulationGuid key
    MapTest mapTest = new MapTest();
    mapTest.setConsentStatuses(map);
    MapTest deserializedMapTest = mapper.readValue(mapper.writeValueAsString(mapTest), MapTest.class);
    assertEquals(deserializedMapTest.getConsentStatuses(), map);
}
Also used : ConsentStatus(org.sagebionetworks.bridge.models.accounts.ConsentStatus) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 4 with ConsentStatus

use of org.sagebionetworks.bridge.models.accounts.ConsentStatus in project BridgeServer2 by Sage-Bionetworks.

the class ParticipantServiceTest method getParticipant.

@Test
public void getParticipant() {
    when(participantService.getAccount()).thenReturn(account);
    when(participantService.generateGUID()).thenReturn(ID);
    when(accountService.getAccount(ACCOUNT_ID)).thenReturn(Optional.of(account));
    // A lot of mocks have to be set up first, this call aggregates almost everything we know about the user
    DateTime createdOn = DateTime.now();
    account.setHealthCode(HEALTH_CODE);
    account.setAppId(APP.getIdentifier());
    account.setId(ID);
    account.setEmail(EMAIL);
    account.setPhone(PHONE);
    account.setCreatedOn(createdOn);
    account.setFirstName(FIRST_NAME);
    account.setLastName(LAST_NAME);
    account.setEmailVerified(Boolean.TRUE);
    account.setPhoneVerified(Boolean.FALSE);
    account.setStatus(AccountStatus.DISABLED);
    account.getAttributes().put("attr2", "anAttribute2");
    List<ConsentSignature> sigs1 = Lists.newArrayList(new ConsentSignature.Builder().withName("Name 1").withBirthdate("1980-01-01").build());
    account.setConsentSignatureHistory(SUBPOP_GUID_1, sigs1);
    account.setClientData(TestUtils.getClientData());
    account.setSharingScope(SharingScope.ALL_QUALIFIED_RESEARCHERS);
    account.setNotifyByEmail(Boolean.TRUE);
    account.setDataGroups(TestUtils.newLinkedHashSet("group1", "group2"));
    account.setLanguages(USER_LANGUAGES);
    account.setTimeZone(USER_TIME_ZONE);
    account.setSynapseUserId(SYNAPSE_USER_ID);
    Enrollment en1 = Enrollment.create(TEST_APP_ID, "studyA", ID, "externalIdA");
    en1.setEnrolledOn(ENROLLMENT);
    Enrollment en2 = Enrollment.create(TEST_APP_ID, "studyB", ID, "externalIdB");
    en2.setEnrolledOn(ENROLLMENT.plusDays(1));
    Enrollment en3 = Enrollment.create(TEST_APP_ID, "studyC", ID);
    en3.setEnrolledOn(ENROLLMENT.plusDays(2));
    // no third external ID, this one is just not in the external IDs map
    account.setEnrollments(ImmutableSet.of(en1, en2, en3));
    account.setOrgMembership(TEST_ORG_ID);
    account.setNote(TEST_NOTE);
    account.setClientTimeZone(TEST_CLIENT_TIME_ZONE);
    List<Subpopulation> subpopulations = Lists.newArrayList();
    // Two subpopulations for mocking.
    Subpopulation subpop1 = Subpopulation.create();
    subpop1.setGuidString("guid1");
    subpop1.setPublishedConsentCreatedOn(CONSENT_PUBLICATION_DATE);
    subpopulations.add(subpop1);
    Subpopulation subpop2 = Subpopulation.create();
    subpop2.setGuidString("guid2");
    subpop2.setPublishedConsentCreatedOn(CONSENT_PUBLICATION_DATE);
    subpopulations.add(subpop2);
    when(subpopService.getSubpopulations(TEST_APP_ID, false)).thenReturn(subpopulations);
    when(subpopService.getSubpopulation(TEST_APP_ID, SUBPOP_GUID_1)).thenReturn(subpop1);
    // Mock CacheProvider to return request info.
    when(requestInfoService.getRequestInfo(ID)).thenReturn(REQUEST_INFO);
    // Mock ConsentService to return consent statuses for criteria.
    ConsentStatus consentStatus1 = new ConsentStatus.Builder().withName("consent1").withGuid(SUBPOP_GUID_1).withRequired(true).withConsented(true).withSignedMostRecentConsent(true).build();
    when(consentService.getConsentStatuses(any(), any())).thenReturn(ImmutableMap.of(SUBPOP_GUID_1, consentStatus1));
    // Get the fully initialized participant object (including histories)
    StudyParticipant participant = participantService.getParticipant(APP, ID, true);
    assertTrue(participant.isConsented());
    assertEquals(participant.getFirstName(), FIRST_NAME);
    assertEquals(participant.getLastName(), LAST_NAME);
    assertTrue(participant.isNotifyByEmail());
    assertEquals(participant.getDataGroups(), ImmutableSet.of("group1", "group2"));
    assertTrue(collectExternalIds(account).contains("externalIdA"));
    assertTrue(collectExternalIds(account).contains("externalIdB"));
    assertEquals(participant.getSharingScope(), SharingScope.ALL_QUALIFIED_RESEARCHERS);
    assertEquals(participant.getHealthCode(), HEALTH_CODE);
    assertEquals(participant.getEmail(), EMAIL);
    assertEquals(participant.getPhone().getNationalFormat(), PHONE.getNationalFormat());
    assertEquals(participant.getEmailVerified(), Boolean.TRUE);
    assertEquals(participant.getPhoneVerified(), Boolean.FALSE);
    assertEquals(participant.getId(), ID);
    assertEquals(participant.getStatus(), AccountStatus.DISABLED);
    assertEquals(participant.getCreatedOn(), createdOn);
    assertEquals(participant.getTimeZone(), USER_TIME_ZONE);
    assertEquals(participant.getLanguages(), USER_LANGUAGES);
    assertEquals(participant.getClientData(), TestUtils.getClientData());
    assertEquals(participant.getSynapseUserId(), SYNAPSE_USER_ID);
    assertEquals(participant.getExternalIds().size(), 2);
    assertEquals(participant.getExternalIds().get("studyA"), "externalIdA");
    assertEquals(participant.getExternalIds().get("studyB"), "externalIdB");
    assertEquals(participant.getOrgMembership(), TEST_ORG_ID);
    assertEquals(participant.getNote(), TEST_NOTE);
    assertEquals(participant.getClientTimeZone(), TEST_CLIENT_TIME_ZONE);
    EnrollmentInfo detailA = participant.getEnrollments().get("studyA");
    assertEquals(detailA.getExternalId(), "externalIdA");
    assertEquals(detailA.getEnrolledOn(), ENROLLMENT);
    EnrollmentInfo detailB = participant.getEnrollments().get("studyB");
    assertEquals(detailB.getExternalId(), "externalIdB");
    assertEquals(detailB.getEnrolledOn(), ENROLLMENT.plusDays(1));
    EnrollmentInfo detailC = participant.getEnrollments().get("studyC");
    assertNull(detailC.getExternalId());
    assertEquals(detailC.getEnrolledOn(), ENROLLMENT.plusDays(2));
    assertNull(participant.getAttributes().get("attr1"));
    assertEquals(participant.getAttributes().get("attr2"), "anAttribute2");
    List<UserConsentHistory> retrievedHistory1 = participant.getConsentHistories().get(subpop1.getGuidString());
    assertEquals(retrievedHistory1.size(), 1);
    List<UserConsentHistory> retrievedHistory2 = participant.getConsentHistories().get(subpop2.getGuidString());
    assertTrue(retrievedHistory2.isEmpty());
    // Verify context passed to consent service.
    ArgumentCaptor<CriteriaContext> criteriaContextCaptor = ArgumentCaptor.forClass(CriteriaContext.class);
    verify(consentService).getConsentStatuses(criteriaContextCaptor.capture(), same(account));
    CriteriaContext criteriaContext = criteriaContextCaptor.getValue();
    assertEquals(criteriaContext.getAppId(), TEST_APP_ID);
    assertEquals(criteriaContext.getUserId(), ID);
    assertEquals(criteriaContext.getHealthCode(), HEALTH_CODE);
    assertEquals(criteriaContext.getClientInfo(), CLIENT_INFO);
    assertEquals(criteriaContext.getLanguages(), TestConstants.LANGUAGES);
    assertEquals(criteriaContext.getUserDataGroups(), TestConstants.USER_DATA_GROUPS);
    assertEquals(criteriaContext.getUserStudyIds(), ImmutableSet.of("studyA", "studyB", "studyC"));
}
Also used : UserConsentHistory(org.sagebionetworks.bridge.models.accounts.UserConsentHistory) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) DateTime(org.joda.time.DateTime) EnrollmentInfo(org.sagebionetworks.bridge.models.studies.EnrollmentInfo) ConsentSignature(org.sagebionetworks.bridge.models.subpopulations.ConsentSignature) Subpopulation(org.sagebionetworks.bridge.models.subpopulations.Subpopulation) ConsentStatus(org.sagebionetworks.bridge.models.accounts.ConsentStatus) Enrollment(org.sagebionetworks.bridge.models.studies.Enrollment) CriteriaContext(org.sagebionetworks.bridge.models.CriteriaContext) Test(org.testng.annotations.Test)

Example 5 with ConsentStatus

use of org.sagebionetworks.bridge.models.accounts.ConsentStatus in project BridgeServer2 by Sage-Bionetworks.

the class ParticipantServiceTest method getParticipantIsConsentedFalse.

@Test
public void getParticipantIsConsentedFalse() {
    // Set up mocks.
    mockHealthCodeAndAccountRetrieval();
    doReturn(APP.getIdentifier()).when(subpopulation).getGuidString();
    doReturn(SUBPOP_GUID).when(subpopulation).getGuid();
    doReturn(Lists.newArrayList(subpopulation)).when(subpopService).getSubpopulations(TEST_APP_ID, false);
    when(requestInfoService.getRequestInfo(ID)).thenReturn(REQUEST_INFO);
    ConsentStatus consentStatus1 = new ConsentStatus.Builder().withName("consent1").withGuid(SUBPOP_GUID).withRequired(true).withConsented(false).withSignedMostRecentConsent(false).build();
    when(consentService.getConsentStatuses(any(), any())).thenReturn(ImmutableMap.of(SUBPOP_GUID_1, consentStatus1));
    // Execute and validate
    StudyParticipant participant = participantService.getParticipant(APP, ID, true);
    assertFalse(participant.isConsented());
}
Also used : ConsentStatus(org.sagebionetworks.bridge.models.accounts.ConsentStatus) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Aggregations

ConsentStatus (org.sagebionetworks.bridge.models.accounts.ConsentStatus)18 SubpopulationGuid (org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid)13 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)9 Test (org.testng.annotations.Test)9 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)7 CriteriaContext (org.sagebionetworks.bridge.models.CriteriaContext)6 App (org.sagebionetworks.bridge.models.apps.App)4 ConsentSignature (org.sagebionetworks.bridge.models.subpopulations.ConsentSignature)4 Subpopulation (org.sagebionetworks.bridge.models.subpopulations.Subpopulation)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 SignIn (org.sagebionetworks.bridge.models.accounts.SignIn)3 Enrollment (org.sagebionetworks.bridge.models.studies.Enrollment)3 DateTime (org.joda.time.DateTime)2 RequestContext (org.sagebionetworks.bridge.RequestContext)2 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DynamoApp (org.sagebionetworks.bridge.dynamodb.DynamoApp)1 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)1