Search in sources :

Example 56 with Profile

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

the class UrlAccessRestrictionCheckingProcessorTest method testAllowedAccess.

@Test
public void testAllowedAccess() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", URL);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    Profile profile = new Profile();
    profile.setRoles(SetUtils.asSet(ADMIN_ROLE));
    SecurityUtils.setAuthentication(request, new DefaultAuthentication(new ObjectId().toString(), profile));
    processor.processRequest(context, chain);
    verify(chain).processRequest(context);
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) ObjectId(org.bson.types.ObjectId) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Example 57 with Profile

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

the class ProfileController method getAttachment.

@RequestMapping(value = URL_PROFILE_GET_ATTACHMENT, method = RequestMethod.GET)
@ApiOperation(value = "Gets the requested attachment of the given profile", notes = "If Attachment or profile does not exist will " + "throw error, content-type,content-legnth headers" + " are set")
public void getAttachment(@ApiParam("The profile's ID") @PathVariable(PATH_VAR_ID) String profileId, @ApiParam("Attachment Id to get") @PathVariable(PATH_VAR_ATTACHMENT) String attachmentId, HttpServletResponse response) throws ProfileException, IOException {
    Profile profile = profileService.getProfile(profileId);
    if (profile != null) {
        InputStream input = null;
        try {
            input = profileService.getProfileAttachment(attachmentId, profile.getId().toString());
            if (input != null) {
                ProfileAttachment attachment = profileService.getProfileAttachmentInformation(profile.getId().toString(), attachmentId);
                response.setContentType(attachment.getContentType());
                response.setContentLength((int) attachment.getFileSizeBytes());
                IOUtils.copy(input, response.getOutputStream());
            }
        } catch (ProfileException ex) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            response.setContentLength(0);
        } finally {
            if (input != null) {
                input.close();
            }
        }
    } else {
        throw new NoSuchProfileException.ById(profileId);
    }
}
Also used : ProfileAttachment(org.craftercms.profile.api.services.ProfileAttachment) InputStream(java.io.InputStream) NoSuchProfileException(org.craftercms.profile.exceptions.NoSuchProfileException) ProfileException(org.craftercms.profile.api.exceptions.ProfileException) Profile(org.craftercms.profile.api.Profile) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 58 with Profile

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

the class ProfileServiceIT method testGetProfileByUsername.

@Test
public void testGetProfileByUsername() throws Exception {
    Profile profile = profileService.getProfileByUsername(DEFAULT_TENANT, ADMIN_USERNAME);
    assertAdminProfile(profile);
    // Try with unknown username
    profile = profileService.getProfileByUsername(DEFAULT_TENANT, "unknown");
    assertNull(profile);
}
Also used : Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 59 with Profile

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

the class ProfileServiceIT method testResetAndChangePassword.

@Test
public void testResetAndChangePassword() throws Exception {
    GreenMail mailServer = new GreenMail(ServerSetupTest.SMTP);
    mailServer.start();
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, true, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
    try {
        profile = profileService.resetPassword(profile.getId().toString(), RESET_PASSWORD_URL);
        assertNotNull(profile);
        // 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 resetTokenId = emailMatcher.group(1);
        Profile profileAfterPswdReset = profileService.changePassword(resetTokenId, AVASQUEZ_PASSWORD2);
        assertNotNull(profileAfterPswdReset);
        assertEquals(profile.getId(), profileAfterPswdReset.getId());
        assertNull(profileAfterPswdReset.getPassword());
    } finally {
        profileService.deleteProfile(profile.getId().toString());
        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)

Example 60 with Profile

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

the class ProfileServiceIT method testGetProfile.

@Test
public void testGetProfile() throws Exception {
    ObjectId profileId = profileService.getProfileByUsername(DEFAULT_TENANT, ADMIN_USERNAME).getId();
    Profile profile = profileService.getProfile(profileId.toString());
    assertAdminProfile(profile);
    // Try with unknown profile ID
    profileId = ObjectId.get();
    profile = profileService.getProfile(profileId.toString());
    assertNull(profile);
}
Also used : ObjectId(org.bson.types.ObjectId) 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