Skip to content

Commit

Permalink
For #551 - delete notification template associated with user-specific…
Browse files Browse the repository at this point in the history
… notification settings when user is being deleted, added test
  • Loading branch information
vitalidze committed Apr 23, 2016
1 parent b749de6 commit b1dacbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ public User removeUser(User user) throws AccessDeniedException {
throw new AccessDeniedException();
}
entityManager.createQuery("DELETE FROM UIStateEntry s WHERE s.user=:user").setParameter("user", user).executeUpdate();
for (NotificationSettings settings : entityManager
.createQuery("SELECT S FROM " + NotificationSettings.class.getName() + " S WHERE S.user = :user", NotificationSettings.class)
.setParameter("user", user)
.getResultList()) {
entityManager.createQuery("DELETE FROM NotificationTemplate T WHERE T.settings = :settings")
.setParameter("settings", settings)
.executeUpdate();
}
entityManager.createQuery("DELETE FROM NotificationSettings s WHERE s.user=:user").setParameter("user", user).executeUpdate();
entityManager.createQuery("UPDATE Device d SET d.owner=null WHERE d.owner=:user").setParameter("user", user).executeUpdate();
for (Device device : user.getDevices()) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/traccar/web/server/model/DataServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -152,6 +153,28 @@ public void testDeleteUserWithNotificationSettings() throws TraccarException {
assertEquals(originalUserId.longValue(), dataService.getUsers().get(0).getId());
}

@Test
public void testDeleteUserWithNotificationSettingsAndTemplate() throws TraccarException {
Long originalUserId = injector.getProvider(User.class).get().getId();

User user = new User("test", "test");
user.setManager(true);
user = dataService.addUser(user);

NotificationService notificationService = injector.getInstance(NotificationService.class);
currentUserId = user.getId();
NotificationSettings settings = new NotificationSettings();
settings.setTransferTemplates(new HashMap<DeviceEventType, NotificationTemplate>());
settings.getTransferTemplates().put(DeviceEventType.OFFLINE, new NotificationTemplate());
notificationService.saveSettings(settings);

currentUserId = originalUserId;
dataService.removeUser(user);

assertEquals(1, dataService.getUsers().size());
assertEquals(originalUserId.longValue(), dataService.getUsers().get(0).getId());
}

@Test
public void testResetPasswordByAdmin() throws TraccarException {
User user = new User("test", "test");
Expand Down

0 comments on commit b1dacbd

Please sign in to comment.