Search in sources :

Example 66 with Profile

use of org.craftercms.profile.api.Profile 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 67 with Profile

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

the class ProfileServiceIT method testUpdateAttributes.

@Test
@DirtiesContext
public void testUpdateAttributes() throws Exception {
    // Update a bunch attributes
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, false, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
    Map<String, Object> attributes = new HashMap<>();
    try {
        Map<String, Object> subscriptions = new HashMap<>();
        subscriptions.put("frequency", JDOE_SUBSCRIPTIONS_FREQUENCY);
        subscriptions.put("autoWatch", JDOE_SUBSCRIPTIONS_AUTO_WATCH);
        subscriptions.put("targets", JDOE_SUBSCRIPTIONS_TARGETS);
        attributes.put("subscriptions", subscriptions);
        profile = profileService.updateAttributes(profile.getId().toString(), attributes);
        attributes = profile.getAttributes();
        assertNotNull(attributes);
        assertEquals(1, attributes.size());
        subscriptions = (Map<String, Object>) attributes.get("subscriptions");
        assertNotNull(subscriptions);
        assertEquals(3, subscriptions.size());
        assertEquals(JDOE_SUBSCRIPTIONS_FREQUENCY, subscriptions.get("frequency"));
        assertEquals(JDOE_SUBSCRIPTIONS_AUTO_WATCH, subscriptions.get("autoWatch"));
        assertEquals(JDOE_SUBSCRIPTIONS_TARGETS, subscriptions.get("targets"));
        accessTokenIdResolver.setAccessTokenId(RANDOM_APP_ACCESS_TOKEN_ID);
        // Unallowed updates should be rejected
        try {
            profileService.updateAttributes(profile.getId().toString(), attributes);
            fail("Exception " + ProfileRestServiceException.class.getName() + " expected");
        } catch (ProfileRestServiceException e) {
            assertEquals(HttpStatus.FORBIDDEN, e.getStatus());
            assertEquals(ErrorCode.ACTION_DENIED, e.getErrorCode());
        }
    } finally {
        profileService.deleteProfile(profile.getId().toString());
    }
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 68 with Profile

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

the class ProfileServiceIT method testDeleteProfile.

@Test
public void testDeleteProfile() throws Exception {
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, false, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
    assertNotNull(profile);
    profileService.deleteProfile(profile.getId().toString());
    profile = profileService.getProfile(profile.getId().toString());
    assertNull(profile);
}
Also used : Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 69 with Profile

use of org.craftercms.profile.api.Profile 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)

Example 70 with Profile

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

the class ProfileServiceIT method testCreateAndVerifyProfile.

@Test
public void testCreateAndVerifyProfile() throws Exception {
    GreenMail mailServer = new GreenMail(ServerSetupTest.SMTP);
    mailServer.start();
    tenantService.verifyNewProfiles(DEFAULT_TENANT, true);
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, true, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
    try {
        assertNotNull(profile);
        assertNotNull(profile.getId());
        assertEquals(AVASQUEZ_USERNAME, profile.getUsername());
        assertNull(profile.getPassword());
        assertEquals(AVASQUEZ_EMAIL1, profile.getEmail());
        assertFalse(profile.isVerified());
        assertFalse(profile.isEnabled());
        assertNotNull(profile.getCreatedOn());
        assertNotNull(profile.getLastModified());
        assertEquals(DEFAULT_TENANT, profile.getTenant());
        assertEquals(AVASQUEZ_ROLES1, profile.getRoles());
        assertNotNull(profile.getAttributes());
        assertEquals(0, profile.getAttributes().size());
        // Wait a few seconds so that the email can be sent
        Thread.sleep(3000);
        String email = GreenMailUtil.getBody(mailServer.getReceivedMessages()[0]);
        assertNotNull(email);
        Pattern emailPattern = Pattern.compile(VERIFICATION_EMAIL_REGEX, Pattern.DOTALL);
        Matcher emailMatcher = emailPattern.matcher(email);
        assertTrue(emailMatcher.matches());
        String verificationTokenId = emailMatcher.group(1);
        Profile verifiedProfile = profileService.verifyProfile(verificationTokenId);
        assertNotNull(verifiedProfile);
        assertEquals(profile.getId(), verifiedProfile.getId());
        assertTrue(verifiedProfile.isEnabled());
        assertTrue(verifiedProfile.isVerified());
    } finally {
        profileService.deleteProfile(profile.getId().toString());
        tenantService.verifyNewProfiles(DEFAULT_TENANT, false);
        mailServer.stop();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) GreenMail(com.icegreen.greenmail.util.GreenMail) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

Profile (org.craftercms.profile.api.Profile)111 Test (org.junit.Test)54 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)19 MongoDataException (org.craftercms.commons.mongo.MongoDataException)15 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)15 LinkedHashMap (java.util.LinkedHashMap)13 VerificationToken (org.craftercms.profile.api.VerificationToken)13 DefaultAuthentication (org.craftercms.security.authentication.impl.DefaultAuthentication)12 Date (java.util.Date)11 Map (java.util.Map)11 ObjectId (org.bson.types.ObjectId)10 RequestContext (org.craftercms.commons.http.RequestContext)9 Authentication (org.craftercms.security.authentication.Authentication)9 ArgumentMatcher (org.mockito.ArgumentMatcher)9 Mockito.anyString (org.mockito.Mockito.anyString)9 RequestSecurityProcessorChain (org.craftercms.security.processors.RequestSecurityProcessorChain)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 Tenant (org.craftercms.profile.api.Tenant)6 HashMap (java.util.HashMap)4