diff options
author | Vitaly Takmazov | 2018-06-27 14:04:59 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-06-27 14:04:59 +0300 |
commit | 0f0df74a6a0b069eabbf788d754389ee4c396f84 (patch) | |
tree | c25da351aec88eb5602ea35eb330376ae6d5a7d3 | |
parent | dd605a1731cdcdf02317bedf7d946605a21e0bd8 (diff) |
move hash to /me endpoint
-rw-r--r-- | juick-server/src/main/java/com/juick/server/api/Users.java | 20 | ||||
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/ServerTests.java | 15 |
2 files changed, 29 insertions, 6 deletions
diff --git a/juick-server/src/main/java/com/juick/server/api/Users.java b/juick-server/src/main/java/com/juick/server/api/Users.java index 2783e9bc..4e447d04 100644 --- a/juick-server/src/main/java/com/juick/server/api/Users.java +++ b/juick-server/src/main/java/com/juick/server/api/Users.java @@ -61,14 +61,22 @@ public class Users { if (!users.isEmpty()) return users; if (!UserUtils.getCurrentUser().isAnonymous()) { - com.juick.User visitor = UserUtils.getCurrentUser(); - visitor.setAuthHash(getAuthToken()); - return Collections.singletonList(visitor); + return Collections.singletonList(UserUtils.getCurrentUser()); } throw new HttpNotFoundException(); } + @GetMapping("/me") + public SecureUser getMe() { + User visitor = UserUtils.getCurrentUser(); + SecureUser me = new SecureUser(); + me.setUid(visitor.getUid()); + me.setName(visitor.getName()); + me.setAuthHash(getAuthToken()); + return me; + } + @RequestMapping(value = "/users/read", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public List<User> doGetUserRead( @RequestParam String uname) { @@ -124,4 +132,10 @@ public class Users { } throw new HttpNotFoundException(); } + + class SecureUser extends User { + public String getHash() { + return getAuthHash(); + } + } } diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index 2c524eec..c43ed9bd 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -45,6 +45,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.*; import org.springframework.jdbc.core.JdbcTemplate; @@ -111,6 +112,8 @@ public class ServerTests { @Inject private MockMvc mockMvc; @Inject + private TestRestTemplate restTemplate; + @Inject private MessagesService messagesService; @Inject private UserService userService; @@ -1019,9 +1022,8 @@ public class ServerTests { map.add("body", "yo"); map.add("hash", userService.getHashByUID(ugnich.getUid())); - RestTemplate rest = new RestTemplate(); - ResponseEntity<CommandResult> result = rest.postForEntity( - "http://localhost:8080/post", + ResponseEntity<CommandResult> result = restTemplate.postForEntity( + "/post", request, CommandResult.class); assertThat(result.getStatusCode(), is(HttpStatus.OK)); } @@ -1038,4 +1040,11 @@ public class ServerTests { assertThat(attachment.getHeight(), is(1)); assertThat(attachment.getWidth(), is(1)); } + @Test + public void meContainsHash() throws Exception { + String hash = userService.getHashByUID(ugnich.getUid()); + mockMvc.perform(get("/me") + .with(httpBasic(ugnichName, ugnichPassword))) + .andExpect(jsonPath("$.hash", is(hash))); + } } |