From 07b18d95274cbdf80affa11f11f0a6fde5a19750 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sat, 25 Jan 2014 12:31:14 -0300 Subject: [PATCH] Improve delete message. Add unit test. --- .../java/com/wisemapping/dao/UserManager.java | 2 +- .../com/wisemapping/dao/UserManagerImpl.java | 3 +- .../wisemapping/rest/AccountController.java | 8 +- .../com/wisemapping/rest/AdminController.java | 3 +- .../java/com/wisemapping/security/Utils.java | 5 +- .../wisemapping/service/MindmapService.java | 2 - .../service/MindmapServiceImpl.java | 22 --- .../com/wisemapping/service/UserService.java | 2 +- .../wisemapping/service/UserServiceImpl.java | 6 +- .../src/main/resources/messages_en.properties | 4 +- .../test/rest/RestAccountITCase.java | 158 ++++++++++++++++++ 11 files changed, 173 insertions(+), 42 deletions(-) create mode 100644 wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java b/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java index 6e169473..5efdbc51 100644 --- a/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java +++ b/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java @@ -45,6 +45,6 @@ public interface UserManager { public User createUser(User user, Collaborator col); - public void deleteUser(User user); + public void removeUser(@NotNull User user); } diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java b/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java index 5bac8dc3..f093cc45 100644 --- a/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java @@ -110,9 +110,8 @@ public class UserManagerImpl } @Override - public void deleteUser(@NotNull final User user) { + public void removeUser(@NotNull final User user) { getHibernateTemplate().delete(user); - getHibernateTemplate().flush(); } public void auditLogin(@NotNull AccessAuditory accessAuditory) { diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java b/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java index 52413781..1262c45b 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/AccountController.java @@ -79,7 +79,7 @@ public class AccountController extends BaseController { throw new IllegalArgumentException("Firstname can not be null"); } - final User user = Utils.getUser(); + final User user = Utils.getUser(true); user.setFirstname(firstname); userService.updateUser(user); } @@ -91,7 +91,7 @@ public class AccountController extends BaseController { throw new IllegalArgumentException("lastname can not be null"); } - final User user = Utils.getUser(); + final User user = Utils.getUser(true); user.setLastname(lastname); userService.updateUser(user); } @@ -109,7 +109,7 @@ public class AccountController extends BaseController { userService.updateUser(user); } - @RequestMapping(method = RequestMethod.DELETE, value = "account", consumes = {"text/plain"}) + @RequestMapping(method = RequestMethod.DELETE, value = "account") @ResponseStatus(value = HttpStatus.NO_CONTENT) public void deleleteUser() throws WiseMappingException @@ -120,7 +120,7 @@ public class AccountController extends BaseController { final Mindmap mindmap = collaboration.getMindMap(); mindmapService.removeMindmap(mindmap,user); } - userService.deleteUser(user); + userService.removeUser(user); } diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java index 36f4620a..212d5a00 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java @@ -18,7 +18,6 @@ package com.wisemapping.rest; -import com.mangofactory.swagger.annotations.ApiModel; import com.wisemapping.exceptions.WiseMappingException; import com.wisemapping.model.AuthenticationType; import com.wisemapping.model.Collaboration; @@ -146,7 +145,7 @@ public class AdminController extends BaseController { mindmapService.removeMindmap(mindmap,user); } - userService.deleteUser(user); + userService.removeUser(user); } @ApiOperation("Note: Administration permissions required.") diff --git a/wise-webapp/src/main/java/com/wisemapping/security/Utils.java b/wise-webapp/src/main/java/com/wisemapping/security/Utils.java index d9327800..1c7f1225 100644 --- a/wise-webapp/src/main/java/com/wisemapping/security/Utils.java +++ b/wise-webapp/src/main/java/com/wisemapping/security/Utils.java @@ -21,12 +21,9 @@ package com.wisemapping.security; import com.wisemapping.model.User; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.springframework.security.authentication.AbstractAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; -import javax.servlet.http.HttpServletRequest; - final public class Utils { private Utils() { } @@ -37,7 +34,7 @@ final public class Utils { return getUser(false); } - @Nullable + @NotNull public static User getUser(boolean forceCheck) { User result = null; final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); diff --git a/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java b/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java index a08b132d..dfa2da45 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/MindmapService.java @@ -46,8 +46,6 @@ public interface MindmapService { void removeCollaboration(@NotNull Mindmap mindmap, @NotNull Collaboration collaboration) throws CollaborationException; - void addTags(@NotNull Mindmap mindmap, String tags); - void removeMindmap(@NotNull final Mindmap mindmap, @NotNull final User user) throws WiseMappingException; List search(MindMapCriteria criteria); diff --git a/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java index 2974474d..c7f7f4bd 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/MindmapServiceImpl.java @@ -231,28 +231,6 @@ public class MindmapServiceImpl return collaborator; } - @Override - public void addTags(@NotNull Mindmap mindmap, String tags) { - mindmap.setTags(tags); - mindmapManager.updateMindmap(mindmap, false); - if (tags != null && tags.length() > 0) { - final String tag[] = tags.split(TAG_SEPARATOR); - final User user = mindmap.getCreator(); - // Add new Tags to User - boolean updateUser = false; - for (String userTag : tag) { - if (!user.getTags().contains(userTag)) { - user.getTags().add(userTag); - updateUser = true; - } - } - if (updateUser) { - //update user - userService.updateUser(user); - } - } - } - @Override public List findMindmapHistory(int mindmapId) { diff --git a/wise-webapp/src/main/java/com/wisemapping/service/UserService.java b/wise-webapp/src/main/java/com/wisemapping/service/UserService.java index 9426d246..95451445 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/UserService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/UserService.java @@ -38,7 +38,7 @@ public interface UserService { public void resetPassword(@NotNull String email) throws InvalidUserEmailException, InvalidAuthSchemaException; - public void deleteUser(@NotNull User user); + public void removeUser(@NotNull User user); public void auditLogin(@NotNull User user); diff --git a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java index 6364d3a7..f9a314cb 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java @@ -102,8 +102,10 @@ public class UserServiceImpl } @Override - public void deleteUser(@NotNull User user) { - userManager.deleteUser(user); + public void removeUser(@NotNull User user) { + // Force object reload before removing.... + final User userBy = userManager.getUserBy(user.getEmail()); + userManager.removeUser(userBy); } @Override diff --git a/wise-webapp/src/main/resources/messages_en.properties b/wise-webapp/src/main/resources/messages_en.properties index 3f5935f0..2a47748f 100644 --- a/wise-webapp/src/main/resources/messages_en.properties +++ b/wise-webapp/src/main/resources/messages_en.properties @@ -15,7 +15,7 @@ LOGOUT=Logout PASSWORD=Password NEW_PASSWORD=New password CONFIRM_NEW_PASSWORD=Confirm new password -DELETE__ACCOUNT=Delete account +DELETE__ACCOUNT=Delete My Account MY_WISEMAPS=My Wisemaps RETYPE_PASSWORD=Retype Password REGISTER=Register @@ -32,7 +32,7 @@ YOUR_ROLE=Your Role FORGOT_PASSWORD=Forgot Password ? CHANGE_PASSWORD=Change Password CHANGE_LANGUAGE=Change Language -WARNING_DELETE_USER=Warning! This action cannot be undone. +WARNING_DELETE_USER=If you do not think you will use WiseMapping again and would like your account deleted, we can take care of this for you. Keep in mind that you will not be able retrieve any mindmap you have added.

If you would still like your account deleted, click "Delete My Account". FAQ=Frequent Asked Questions SHORT_FAQ=FAQ LOGIN=Login diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java new file mode 100644 index 00000000..53b3646f --- /dev/null +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountITCase.java @@ -0,0 +1,158 @@ +/* +* Copyright [2012] [wisemapping] +* +* Licensed under WiseMapping Public License, Version 1.0 (the "License"). +* It is basically the Apache License, Version 2.0 (the "License") plus the +* "powered by wisemapping" text requirement on every single page; +* you may not use this file except in compliance with the License. +* You may obtain a copy of the license at +* +* http://www.wisemapping.org/license +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package com.wisemapping.test.rest; + + +import com.wisemapping.rest.model.RestUser; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.security.crypto.codec.Base64; +import org.springframework.web.client.RestTemplate; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import static org.testng.Assert.assertEquals; + + +@Test +public class RestAccountITCase { + + @NonNls + private static final String HOST_PORT = "http://localhost:8080"; + private static final String BASE_REST_URL = HOST_PORT + "/service"; + private static final String ADMIN_CREDENTIALS = "admin@wisemapping.org" + ":" + "admin"; + + + @Test(dataProvider = "ContentType-Provider-Function") + public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ... + final HttpHeaders requestHeaders = createHeaders(mediaType); + final RestTemplate adminTemplate = createTemplate(ADMIN_CREDENTIALS); + + final RestUser dummyUser = createDummyUser(); + createUser(requestHeaders, adminTemplate, dummyUser); + + // Delete user ... + final RestTemplate dummyTemplate = createTemplate(dummyUser.getEmail() + ":fooPassword"); + dummyTemplate.delete(BASE_REST_URL + "/account"); + + // Is the user there ? + // Check that the user has been created ... +// try { +// findUser(requestHeaders, adminTemplate, location); +// fail("User could not be deleted !"); +// } catch (Exception e) { +// } + } + + public String createNewUser(final @NotNull MediaType mediaType) { + + // Configure media types ... + final HttpHeaders requestHeaders = createHeaders(mediaType); + final RestTemplate templateRest = createTemplate(ADMIN_CREDENTIALS); + + // Fill user data ... + final RestUser restUser = createDummyUser(); + + // Create a new user ... + final URI location = createUser(requestHeaders, templateRest, restUser); + + // Check that the user has been created ... + ResponseEntity result = findUser(requestHeaders, templateRest, location); + assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same."); + + // Find by email and check ... + result = findUserByEmail(requestHeaders, templateRest, restUser.getEmail()); + assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same."); + + return restUser.getEmail(); + } + + + private ResponseEntity findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) { + HttpEntity findUserEntity = new HttpEntity(requestHeaders); + final String url = HOST_PORT + location; + return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class); + } + + private ResponseEntity findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) { + HttpEntity findUserEntity = new HttpEntity(requestHeaders); + + // Add extension only to avoid the fact that the last part is extracted ... + final String url = BASE_REST_URL + "/admin/users/email/{email}.json"; + return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class, email); + } + + private URI createUser(HttpHeaders requestHeaders, RestTemplate templateRest, RestUser restUser) { + HttpEntity createUserEntity = new HttpEntity(restUser, requestHeaders); + return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); + } + + private HttpHeaders createHeaders(@NotNull MediaType mediaType) { + List acceptableMediaTypes = new ArrayList(); + acceptableMediaTypes.add(mediaType); + + final HttpHeaders result = new HttpHeaders(); + result.setAccept(acceptableMediaTypes); + result.setContentType(mediaType); + return result; + } + + private RestTemplate createTemplate(@NotNull final String authorisation) { + SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() { + @Override + protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { + super.prepareConnection(connection, httpMethod); + + byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes()); + connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation)); + } + + }; + return new RestTemplate(s); + } + + private RestUser createDummyUser() { + final RestUser restUser = new RestUser(); + final String username = "foo-to-delete" + System.nanoTime(); + final String email = username + "@example.org"; + restUser.setEmail(email); + restUser.setFirstname("foo first name"); + restUser.setLastname("foo last name"); + restUser.setPassword("fooPassword"); + return restUser; + } + + + @DataProvider(name = "ContentType-Provider-Function") + public Object[][] contentTypes() { + return new Object[][]{{MediaType.APPLICATION_XML}, {MediaType.APPLICATION_JSON}}; + } +}