Search in sources :

Example 6 with VerificationToken

use of org.craftercms.profile.api.VerificationToken in project profile by craftercms.

the class ProfileServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(tenantPermissionEvaluator.isAllowed(anyString(), anyString())).thenReturn(true);
    when(attributePermissionEvaluator.isAllowed(any(AttributeDefinition.class), anyString())).thenReturn(true);
    when(attributePermissionEvaluator.isAllowed(eq(new AttributeDefinition(ATTRIB_NAME_PRIVATE)), anyString())).thenReturn(false);
    when(tenantService.getTenant(TENANT1_NAME)).thenReturn(getTenant1());
    when(tenantService.getTenant(TENANT2_NAME)).thenReturn(getTenant2());
    when(authenticationService.getTicket(TICKET_ID)).thenReturn(getTicket());
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            Profile profile = (Profile) invocation.getArguments()[0];
            profile.setId(new ObjectId());
            return null;
        }
    }).when(profileRepository).insert(any(Profile.class));
    when(profileRepository.findOneByQuery(String.format(ProfileServiceImpl.QUERY_FINAL_FORMAT, TENANT1_NAME, QUERY), new String[0])).thenReturn(getTenant1Profile());
    when(profileRepository.findById(PROFILE1_ID.toString(), new String[0])).thenReturn(getTenant1Profile());
    when(profileRepository.findById(PROFILE1_ID.toString(), NO_ATTRIBUTE)).thenReturn(getTenant1ProfileNoAttributes());
    when(profileRepository.findById(PROFILE1_ID.toString(), ATTRIB_NAME_FIRST_NAME)).thenReturn(getTenant1ProfileNoLastName());
    when(profileRepository.findById(PROFILE2_ID.toString(), new String[0])).thenReturn(getTenant2Profile());
    when(profileRepository.findByQuery(String.format(ProfileServiceImpl.QUERY_FINAL_FORMAT, TENANT1_NAME, QUERY), SORT_BY, SortOrder.ASC, START, COUNT, new String[0])).thenReturn(getAllTenant1Profiles());
    when(profileRepository.findByTenantAndUsername(TENANT1_NAME, USERNAME1, new String[0])).thenReturn(getTenant1Profile());
    when(profileRepository.findByIds(TENANT1_PROFILE_IDS, SORT_BY, SortOrder.ASC)).thenReturn(getAllTenant1Profiles());
    when(profileRepository.findRange(TENANT1_NAME, SORT_BY, SortOrder.ASC, START, COUNT)).thenReturn(getAllTenant1Profiles());
    when(profileRepository.findByTenantAndRole(TENANT1_NAME, ROLE1, SORT_BY, SortOrder.ASC)).thenReturn(getAllTenant1Profiles());
    when(profileRepository.findByTenantAndAttributeValue(TENANT1_NAME, ATTRIB_NAME_FIRST_NAME, FIRST_NAME, SORT_BY, SortOrder.ASC)).thenReturn(getAllTenant1Profiles());
    when(profileRepository.countByTenant(TENANT1_NAME)).thenReturn(10L);
    when(profileRepository.count(String.format(ProfileServiceImpl.QUERY_FINAL_FORMAT, TENANT1_NAME, QUERY))).thenReturn(1L);
    when(verificationService.createToken(any(Profile.class))).then(new Answer<VerificationToken>() {

        @Override
        public VerificationToken answer(final InvocationOnMock invocation) throws Throwable {
            Profile profile = (Profile) invocation.getArguments()[0];
            VerificationToken token = new VerificationToken();
            token.setId(VERIFICATION_TOKEN_ID1);
            token.setTenant(profile.getTenant());
            token.setProfileId(profile.getId().toString());
            token.setTimestamp(new Date());
            return token;
        }
    });
    VerificationToken token1 = new VerificationToken();
    token1.setId(VERIFICATION_TOKEN_ID1);
    token1.setTenant(TENANT1_NAME);
    token1.setProfileId(PROFILE1_ID.toString());
    token1.setTimestamp(new Date());
    VerificationToken token2 = new VerificationToken();
    token2.setId(VERIFICATION_TOKEN_ID2);
    token2.setTenant(TENANT2_NAME);
    token2.setProfileId(PROFILE2_ID.toString());
    token2.setTimestamp(new Date());
    when(verificationService.getToken(VERIFICATION_TOKEN_ID1)).thenReturn(token1);
    when(verificationService.getToken(VERIFICATION_TOKEN_ID2)).thenReturn(token2);
    profileService = new ProfileServiceImpl();
    profileService.setTenantPermissionEvaluator(tenantPermissionEvaluator);
    profileService.setAttributePermissionEvaluator(attributePermissionEvaluator);
    profileService.setProfileRepository(profileRepository);
    profileService.setTenantService(tenantService);
    profileService.setVerificationService(verificationService);
    profileService.setNewProfileEmailFromAddress(VERIFICATION_FROM_ADDRESS);
    profileService.setNewProfileEmailSubject(VERIFICATION_SUBJECT);
    profileService.setNewProfileEmailTemplateName(VERIFICATION_TEMPLATE_NAME);
    profileService.setResetPwdEmailFromAddress(RESET_PASSWORD_FROM_ADDRESS);
    profileService.setResetPwdEmailSubject(RESET_PASSWORD_SUBJECT);
    profileService.setResetPwdEmailTemplateName(RESET_PASSWORD_TEMPLATE_NAME);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) VerificationToken(org.craftercms.profile.api.VerificationToken) ObjectId(org.bson.types.ObjectId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date) Before(org.junit.Before)

Example 7 with VerificationToken

use of org.craftercms.profile.api.VerificationToken in project profile by craftercms.

the class VerificationServiceImplTest method testGetToken.

@Test
public void testGetToken() throws Exception {
    VerificationToken token = verificationService.getToken(TOKEN_ID);
    assertNotNull(token);
    assertEquals(TOKEN_ID, token.getId());
    assertEquals(PROFILE_ID.toString(), token.getProfileId());
    assertNotNull(token.getTimestamp());
    verify(tokenRepository).findByStringId(TOKEN_ID);
}
Also used : VerificationToken(org.craftercms.profile.api.VerificationToken) Test(org.junit.Test)

Example 8 with VerificationToken

use of org.craftercms.profile.api.VerificationToken in project profile by craftercms.

the class ProfileServiceIT method testDeleteVerificationToken.

@Test
public void testDeleteVerificationToken() throws Exception {
    Profile profile = profileService.getProfileByUsername(DEFAULT_TENANT, ADMIN_USERNAME);
    String profileId = profile.getId().toString();
    VerificationToken token = profileService.createVerificationToken(profileId);
    assertNotNull(token);
    profileService.deleteVerificationToken(token.getId());
    token = profileService.getVerificationToken(token.getId());
    assertNull(token);
}
Also used : VerificationToken(org.craftercms.profile.api.VerificationToken) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 9 with VerificationToken

use of org.craftercms.profile.api.VerificationToken in project profile by craftercms.

the class ProfileServiceIT method testCreateVerificationToken.

@Test
public void testCreateVerificationToken() throws Exception {
    Profile profile = profileService.getProfileByUsername(DEFAULT_TENANT, ADMIN_USERNAME);
    String profileId = profile.getId().toString();
    VerificationToken token = profileService.createVerificationToken(profileId);
    assertNotNull(token);
    try {
        assertNotNull(token.getId());
        assertEquals(profile.getTenant(), token.getTenant());
        assertEquals(profileId, token.getProfileId());
        assertNotNull(token.getTimestamp());
    } finally {
        profileService.deleteVerificationToken(token.getId());
    }
}
Also used : VerificationToken(org.craftercms.profile.api.VerificationToken) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 10 with VerificationToken

use of org.craftercms.profile.api.VerificationToken in project profile by craftercms.

the class ProfileServiceIT method testGetVerificationToken.

@Test
public void testGetVerificationToken() throws Exception {
    Profile profile = profileService.getProfileByUsername(DEFAULT_TENANT, ADMIN_USERNAME);
    String profileId = profile.getId().toString();
    VerificationToken originalToken = profileService.createVerificationToken(profileId);
    assertNotNull(originalToken);
    try {
        VerificationToken token = profileService.getVerificationToken(originalToken.getId());
        assertNotNull(token);
        assertEquals(originalToken.getId(), token.getId());
        assertEquals(originalToken.getTenant(), token.getTenant());
        assertEquals(originalToken.getProfileId(), token.getProfileId());
        assertEquals(originalToken.getTimestamp(), token.getTimestamp());
    } finally {
        profileService.deleteVerificationToken(originalToken.getId());
    }
}
Also used : VerificationToken(org.craftercms.profile.api.VerificationToken) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

VerificationToken (org.craftercms.profile.api.VerificationToken)18 Profile (org.craftercms.profile.api.Profile)12 Test (org.junit.Test)10 Date (java.util.Date)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 MongoDataException (org.craftercms.commons.mongo.MongoDataException)2 AttributeDefinition (org.craftercms.profile.api.AttributeDefinition)2 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)2 NoSuchVerificationTokenException (org.craftercms.profile.exceptions.NoSuchVerificationTokenException)2 Before (org.junit.Before)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 ObjectId (org.bson.types.ObjectId)1 DuplicateKeyException (org.craftercms.commons.mongo.DuplicateKeyException)1 Tenant (org.craftercms.profile.api.Tenant)1 InvalidEmailAddressException (org.craftercms.profile.exceptions.InvalidEmailAddressException)1 ProfileExistsException (org.craftercms.profile.exceptions.ProfileExistsException)1