Skip to content

Commit 7ae2183

Browse files
committed
boot4_4_fix_userCache
1 parent 97c957a commit 7ae2183

File tree

8 files changed

+61
-11
lines changed

8 files changed

+61
-11
lines changed

src/main/java/ru/javaops/bootjava/user/web/AbstractUserController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ public User get(int id) {
3232
return repository.getExisted(id);
3333
}
3434

35-
public void delete(int id) {
35+
public void delete(int id, String invalidateEmail) {
3636
log.info("delete {}", id);
3737
repository.deleteExisted(id);
38+
userCache.removeUserFromCache(invalidateEmail);
39+
}
40+
41+
public void update(User user, String invalidateEmail) {
42+
repository.prepareAndSave(user);
43+
userCache.removeUserFromCache(invalidateEmail);
3844
}
3945
}

src/main/java/ru/javaops/bootjava/user/web/AdminUserController.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ public User get(@PathVariable int id) {
2929
return super.get(id);
3030
}
3131

32-
@Override
3332
@DeleteMapping("/{id}")
3433
@ResponseStatus(HttpStatus.NO_CONTENT)
3534
public void delete(@PathVariable int id) {
3635
User user = repository.getExisted(id);
37-
super.delete(id);
38-
userCache.removeUserFromCache(user.getEmail());
36+
super.delete(id, user.getEmail());
3937
}
4038

4139
@GetMapping
@@ -60,8 +58,8 @@ public ResponseEntity<User> createWithLocation(@Valid @RequestBody User user) {
6058
public void update(@Valid @RequestBody User user, @PathVariable int id) {
6159
log.info("update {} with id={}", user, id);
6260
assureIdConsistent(user, id);
63-
repository.prepareAndSave(user);
64-
userCache.removeUserFromCache(user.getEmail());
61+
User dbUser = repository.getExisted(id);
62+
super.update(user, dbUser.getEmail());
6563
}
6664

6765
@GetMapping("/by-email")

src/main/java/ru/javaops/bootjava/user/web/ProfileController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public User get(@AuthenticationPrincipal AuthUser authUser) {
3535
@DeleteMapping
3636
@ResponseStatus(HttpStatus.NO_CONTENT)
3737
public void delete(@AuthenticationPrincipal AuthUser authUser) {
38-
super.delete(authUser.id());
39-
userCache.removeUserFromCache(authUser.getUsername());
38+
super.delete(authUser.id(), authUser.getUsername());
4039
}
4140

4241
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@@ -57,7 +56,6 @@ public void update(@RequestBody @Valid UserTo userTo, @AuthenticationPrincipal A
5756
log.info("update {} with id={}", userTo, authUser.id());
5857
assureIdConsistent(userTo, authUser.id());
5958
User user = authUser.getUser();
60-
repository.prepareAndSave(UsersUtil.updateFromTo(user, userTo));
61-
userCache.removeUserFromCache(authUser.getUsername());
59+
super.update(UsersUtil.updateFromTo(user, userTo), authUser.getUsername());
6260
}
6361
}

src/test/java/ru/javaops/bootjava/AbstractControllerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.boot.test.context.SpringBootTest;
55
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
6+
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
67
import org.springframework.test.context.ActiveProfiles;
78
import org.springframework.test.web.servlet.MockMvc;
89
import org.springframework.test.web.servlet.ResultActions;
910
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
11+
import org.springframework.test.web.servlet.request.RequestPostProcessor;
1012
import org.springframework.transaction.annotation.Transactional;
1113
import ru.javaops.bootjava.app.config.WebConfig;
14+
import ru.javaops.bootjava.user.model.User;
1215

1316
//https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications
1417
@SpringBootTest
@@ -25,4 +28,8 @@ protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Ex
2528
builder.header(WebConfig.VERSION_HEADER, WebConfig.CURRENT_VERSION);
2629
return mockMvc.perform(builder);
2730
}
31+
32+
protected static RequestPostProcessor userHttpBasic(User user) {
33+
return SecurityMockMvcRequestPostProcessors.httpBasic(user.getEmail(), user.getPassword());
34+
}
2835
}

src/test/java/ru/javaops/bootjava/user/UserTestData.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class UserTestData {
1919
public static final String USER_MAIL = "user@yandex.ru";
2020
public static final String ADMIN_MAIL = "admin@gmail.com";
2121
public static final String GUEST_MAIL = "guest@gmail.com";
22+
public static final String NEW_MAIL = "new@gmail.com";
2223

2324
public static final User user = new User(USER_ID, "User", USER_MAIL, "password", Role.USER);
2425
public static final User admin = new User(ADMIN_ID, "Admin", ADMIN_MAIL, "admin", Role.ADMIN, Role.USER);

src/test/java/ru/javaops/bootjava/user/web/AdminUserControllerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ void update() throws Exception {
110110
USER_MATCHER.assertMatch(repository.getExisted(USER_ID), getUpdated());
111111
}
112112

113+
@Test
114+
void updateEmail() throws Exception {
115+
perform(MockMvcRequestBuilders.get(ProfileController.REST_URL)
116+
.with(userHttpBasic(user)))
117+
.andExpect(status().isOk());
118+
119+
User updated = getUpdated();
120+
updated.setEmail(NEW_MAIL);
121+
perform(MockMvcRequestBuilders.put(REST_URL_SLASH + USER_ID)
122+
.with(userHttpBasic(admin))
123+
.contentType(MediaType.APPLICATION_JSON)
124+
.content(jsonWithPassword(updated, "newPass")))
125+
.andDo(print())
126+
.andExpect(status().isNoContent());
127+
USER_MATCHER.assertMatch(repository.getExisted(USER_ID), updated);
128+
129+
perform(MockMvcRequestBuilders.get(ProfileController.REST_URL)
130+
.with(userHttpBasic(user)))
131+
.andExpect(status().isUnauthorized());
132+
}
133+
113134
@Test
114135
@WithUserDetails(value = ADMIN_MAIL)
115136
void createWithLocation() throws Exception {

src/test/java/ru/javaops/bootjava/user/web/ProfileControllerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ void registerInvalid() throws Exception {
8787
.andExpect(status().isUnprocessableContent());
8888
}
8989

90+
@Test
91+
void updateEmail() throws Exception {
92+
perform(MockMvcRequestBuilders.get(REST_URL)
93+
.with(userHttpBasic(user)))
94+
.andExpect(status().isOk());
95+
96+
UserTo updatedTo = new UserTo(null, "newName", NEW_MAIL, "newPassword");
97+
perform(MockMvcRequestBuilders.put(REST_URL).contentType(MediaType.APPLICATION_JSON)
98+
.with(userHttpBasic(user))
99+
.content(JsonUtil.writeValue(updatedTo)))
100+
.andDo(print())
101+
.andExpect(status().isNoContent());
102+
USER_MATCHER.assertMatch(repository.getExisted(USER_ID), UsersUtil.updateFromTo(new User(user), updatedTo));
103+
104+
perform(MockMvcRequestBuilders.get(REST_URL)
105+
.with(userHttpBasic(user)))
106+
.andExpect(status().isUnauthorized());
107+
}
108+
90109
@Test
91110
@WithUserDetails(value = USER_MAIL)
92111
void updateInvalid() throws Exception {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
spring.cache.type: none
1+
#spring.cache.type: none

0 commit comments

Comments
 (0)