From e2c2cf416db9637ebd41c489cf5a533824d17d4c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 1 Nov 2017 12:55:21 +0300 Subject: notifications: fix tokens deletion --- .../java/com/juick/api/tests/MessagesTests.java | 23 ++++++++++++++++------ .../src/main/java/com/juick/ExternalToken.java | 6 ++++++ .../service/NotificationsTokenService.java | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java index 63fac6a1..13d399ca 100644 --- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java +++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java @@ -81,8 +81,8 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests { @Inject private ImagesService imagesService; - private static User ugnich, freefd; - static String ugnichName, ugnichPassword, freefdName, freefdPassword; + private static User ugnich, freefd, juick; + static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword; static Message msg; private static boolean isSetUp = false; @@ -99,11 +99,15 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests { ugnichPassword = "MyPassw0rd!"; freefdName = "freefd"; freefdPassword = "MyPassw0rd!"; - + juickName = "juick"; + juickPassword = "demo"; int ugnichId = userService.createUser(ugnichName, ugnichPassword); ugnich = userService.getUserByUID(ugnichId).orElseThrow(IllegalStateException::new); int freefdId = userService.createUser(freefdName, freefdPassword); freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); + int juickId = userService.createUser(juickName, juickPassword); + juick = userService.getUserByUID(juickId).orElseThrow(IllegalStateException::new); + String msgText = "Привет, я - Угнич"; int mid = messagesService.createMessage(ugnich.getUid(), msgText, "png", null); @@ -245,9 +249,6 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests { } @Test public void registerForNotificationsTests() throws Exception { - String juickName = "juick"; - String juickPassword = "demo"; - int juickId = userService.createUser(juickName, juickPassword); String token = "123456"; ExternalToken registration = new ExternalToken(null, "apns", token, null); mockMvc.perform(put("/notifications").with(httpBasic(ugnichName, ugnichPassword)) @@ -270,4 +271,14 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests { assertThat(uriComponents.getPath().substring(1), is("123456")); assertThat(uriComponents.getFragment(), is("23")); } + @Test + public void notificationsTokensTest() throws Exception { + List tokens = Collections.singletonList(new ExternalToken(null, "gcm", "123456", null)); + mockMvc.perform(delete("/notifications").with(httpBasic(ugnichName, ugnichPassword)) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .content(jsonMapper.writeValueAsBytes(tokens))).andExpect(status().isForbidden()); + mockMvc.perform(delete("/notifications").with(httpBasic(juickName, juickPassword)) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .content(jsonMapper.writeValueAsBytes(tokens))).andExpect(status().isOk()); + } } diff --git a/juick-core/src/main/java/com/juick/ExternalToken.java b/juick-core/src/main/java/com/juick/ExternalToken.java index 933ecf82..f6094478 100644 --- a/juick-core/src/main/java/com/juick/ExternalToken.java +++ b/juick-core/src/main/java/com/juick/ExternalToken.java @@ -38,6 +38,12 @@ public class ExternalToken { this.type = type; this.token = token; this.secret = secret; + if (this.type == null) { + throw new IllegalStateException("Token must have type"); + } + if (this.token == null) { + throw new IllegalStateException("Token must have value"); + } } public String getType() { diff --git a/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java b/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java index fce52987..6e6c8632 100644 --- a/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java +++ b/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java @@ -29,7 +29,7 @@ public class NotificationsTokenService extends BaseRestService implements TokenS public void deleteTokens(String type, List devices) { if (devices.size() > 0) { List list = devices.stream() - .map(d -> new ExternalToken(null, null, type, d)).collect(Collectors.toList()); + .map(d -> new ExternalToken(null, type, d, null)).collect(Collectors.toList()); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); getRest().exchange("http://api.juick.com/notifications", -- cgit v1.2.3