Search in sources :

Example 6 with UserSession

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

the class CacheProviderTest method testSetUserSessionNullSessionToken.

@Test
public void testSetUserSessionNullSessionToken() throws Exception {
    StudyParticipant participant = new StudyParticipant.Builder().withEmail("userEmail").withId(USER_ID).withHealthCode("healthCode").build();
    UserSession session = new UserSession(participant);
    try {
        cacheProvider.setUserSession(session);
    } catch (NullPointerException e) {
        assertTrue(true, "NPE expected.");
    } catch (Throwable e) {
        fail(e.getMessage());
    }
    verify(transaction, never()).setex(eq(TOKEN_TO_USER_ID.toString()), anyInt(), anyString());
    verify(transaction, never()).setex(eq(USER_ID_TO_SESSION.toString()), anyInt(), eq(DECRYPTED_SESSION_TOKEN));
    verify(transaction, never()).exec();
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Example 7 with UserSession

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

the class CacheProviderTest method testRemoveSession.

@Test
public void testRemoveSession() {
    StudyParticipant participant = new StudyParticipant.Builder().withId(USER_ID).build();
    UserSession session = new UserSession(participant);
    session.setSessionToken(DECRYPTED_SESSION_TOKEN);
    cacheProvider.removeSession(session);
    cacheProvider.getUserSession(DECRYPTED_SESSION_TOKEN);
    verify(transaction).del(TOKEN_TO_USER_ID.toString());
    verify(transaction).del(USER_ID_TO_SESSION.toString());
    verify(transaction).exec();
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Example 8 with UserSession

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

the class CacheProviderTest method getUserSessionSuccessful.

@Test
public void getUserSessionSuccessful() throws Exception {
    UserSession session = new UserSession(new StudyParticipant.Builder().build());
    session.setSessionToken(DECRYPTED_SESSION_TOKEN);
    when(jedisOps.get(USER_ID_TO_SESSION.toString())).thenReturn(BridgeObjectMapper.get().writeValueAsString(session));
    UserSession retrieved = cacheProvider.getUserSession(DECRYPTED_SESSION_TOKEN);
    assertEquals(retrieved.getSessionToken(), session.getSessionToken());
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) Test(org.testng.annotations.Test)

Example 9 with UserSession

use of org.sagebionetworks.bridge.models.accounts.UserSession 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 10 with UserSession

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

the class CacheProviderTest method getUserSessionUserHasNoSession.

@Test
public void getUserSessionUserHasNoSession() {
    // When token --> userId mapping is mocked, but there's no session, return null
    UserSession retrieved = cacheProvider.getUserSession(DECRYPTED_SESSION_TOKEN);
    assertNull(retrieved);
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) Test(org.testng.annotations.Test)

Aggregations

UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)483 PostMapping (org.springframework.web.bind.annotation.PostMapping)149 GetMapping (org.springframework.web.bind.annotation.GetMapping)114 App (org.sagebionetworks.bridge.models.apps.App)104 Test (org.testng.annotations.Test)102 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)89 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)65 StatusMessage (org.sagebionetworks.bridge.models.StatusMessage)60 Account (org.sagebionetworks.bridge.models.accounts.Account)56 BeforeMethod (org.testng.annotations.BeforeMethod)47 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)45 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)43 UnauthorizedException (org.sagebionetworks.bridge.exceptions.UnauthorizedException)38 CriteriaContext (org.sagebionetworks.bridge.models.CriteriaContext)31 DateTime (org.joda.time.DateTime)27 AccountId (org.sagebionetworks.bridge.models.accounts.AccountId)27 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 ResourceList (org.sagebionetworks.bridge.models.ResourceList)21 SubpopulationGuid (org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid)21 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)18