diff --git a/UserFront/src/main/java/com/userFront/domain/Appointment.java b/UserFront/src/main/java/com/userFront/domain/Appointment.java index 1d6623e7..18f95e31 100644 --- a/UserFront/src/main/java/com/userFront/domain/Appointment.java +++ b/UserFront/src/main/java/com/userFront/domain/Appointment.java @@ -9,6 +9,8 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import com.fasterxml.jackson.annotation.JsonIgnore; + @Entity public class Appointment { @@ -22,6 +24,7 @@ public class Appointment { @ManyToOne @JoinColumn(name = "user_id") + @JsonIgnore private User user; public Long getId() { diff --git a/UserFront/src/main/java/com/userFront/domain/User.java b/UserFront/src/main/java/com/userFront/domain/User.java index ead86044..ebd809f9 100644 --- a/UserFront/src/main/java/com/userFront/domain/User.java +++ b/UserFront/src/main/java/com/userFront/domain/User.java @@ -51,6 +51,7 @@ public class User implements UserDetails{ private List appointmentList; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JsonIgnore private List recipientList; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @@ -129,6 +130,8 @@ public void setRecipientList(List recipientList) { this.recipientList = recipientList; } + @JsonIgnore + @Override public String getPassword() { return password; } @@ -162,14 +165,10 @@ public String toString() { return "User{" + "userId=" + userId + ", username='" + username + '\'' + - ", password='" + password + '\'' + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + ", phone='" + phone + '\'' + - ", appointmentList=" + appointmentList + - ", recipientList=" + recipientList + - ", userRoles=" + userRoles + '}'; } diff --git a/UserFront/src/main/java/com/userFront/resource/AppointmentResource.java b/UserFront/src/main/java/com/userFront/resource/AppointmentResource.java index bcb37490..872a44f1 100644 --- a/UserFront/src/main/java/com/userFront/resource/AppointmentResource.java +++ b/UserFront/src/main/java/com/userFront/resource/AppointmentResource.java @@ -1,6 +1,7 @@ package com.userFront.resource; import java.util.List; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -8,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.userFront.domain.Appointment; +import com.userFront.resource.dto.AdminAppointmentView; import com.userFront.service.AppointmentService; @RestController @@ -20,10 +21,10 @@ public class AppointmentResource { private AppointmentService appointmentService; @RequestMapping("/all") - public List findAppointmentList() { - List appointmentList = appointmentService.findAll(); - - return appointmentList; + public List findAppointmentList() { + return appointmentService.findAll().stream() + .map(AdminAppointmentView::new) + .collect(Collectors.toList()); } @RequestMapping("/{id}/confirm") diff --git a/UserFront/src/main/java/com/userFront/resource/UserResource.java b/UserFront/src/main/java/com/userFront/resource/UserResource.java index d43c01b9..72e62ba9 100644 --- a/UserFront/src/main/java/com/userFront/resource/UserResource.java +++ b/UserFront/src/main/java/com/userFront/resource/UserResource.java @@ -1,6 +1,7 @@ package com.userFront.resource; import java.util.List; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -12,7 +13,7 @@ import com.userFront.domain.PrimaryTransaction; import com.userFront.domain.SavingsTransaction; -import com.userFront.domain.User; +import com.userFront.resource.dto.AdminUserView; import com.userFront.service.TransactionService; import com.userFront.service.UserService; @@ -28,8 +29,10 @@ public class UserResource { private TransactionService transactionService; @RequestMapping(value = "/user/all", method = RequestMethod.GET) - public List userList() { - return userService.findUserList(); + public List userList() { + return userService.findUserList().stream() + .map(AdminUserView::new) + .collect(Collectors.toList()); } @RequestMapping(value = "/user/primary/transaction", method = RequestMethod.GET) diff --git a/UserFront/src/main/java/com/userFront/resource/dto/AdminAppointmentView.java b/UserFront/src/main/java/com/userFront/resource/dto/AdminAppointmentView.java new file mode 100644 index 00000000..5a1ee986 --- /dev/null +++ b/UserFront/src/main/java/com/userFront/resource/dto/AdminAppointmentView.java @@ -0,0 +1,63 @@ +package com.userFront.resource.dto; + +import java.util.Date; + +import com.userFront.domain.Appointment; +import com.userFront.domain.User; + +public class AdminAppointmentView { + + private final Long id; + private final Date date; + private final String location; + private final String description; + private final boolean confirmed; + private final AppointmentUserView user; + + public AdminAppointmentView(Appointment appointment) { + this.id = appointment.getId(); + this.date = appointment.getDate(); + this.location = appointment.getLocation(); + this.description = appointment.getDescription(); + this.confirmed = appointment.isConfirmed(); + User owner = appointment.getUser(); + this.user = owner == null ? null : new AppointmentUserView(owner.getUsername()); + } + + public Long getId() { + return id; + } + + public Date getDate() { + return date; + } + + public String getLocation() { + return location; + } + + public String getDescription() { + return description; + } + + public boolean isConfirmed() { + return confirmed; + } + + public AppointmentUserView getUser() { + return user; + } + + public static class AppointmentUserView { + + private final String username; + + public AppointmentUserView(String username) { + this.username = username; + } + + public String getUsername() { + return username; + } + } +} diff --git a/UserFront/src/main/java/com/userFront/resource/dto/AdminUserView.java b/UserFront/src/main/java/com/userFront/resource/dto/AdminUserView.java new file mode 100644 index 00000000..b195ad2e --- /dev/null +++ b/UserFront/src/main/java/com/userFront/resource/dto/AdminUserView.java @@ -0,0 +1,83 @@ +package com.userFront.resource.dto; + +import java.math.BigDecimal; + +import com.userFront.domain.PrimaryAccount; +import com.userFront.domain.SavingsAccount; +import com.userFront.domain.User; + +public class AdminUserView { + + private final Long userId; + private final String username; + private final String firstName; + private final String lastName; + private final String email; + private final String phone; + private final boolean enabled; + private final AccountBalanceView primaryAccount; + private final AccountBalanceView savingsAccount; + + public AdminUserView(User user) { + this.userId = user.getUserId(); + this.username = user.getUsername(); + this.firstName = user.getFirstName(); + this.lastName = user.getLastName(); + this.email = user.getEmail(); + this.phone = user.getPhone(); + this.enabled = user.isEnabled(); + PrimaryAccount primary = user.getPrimaryAccount(); + SavingsAccount savings = user.getSavingsAccount(); + this.primaryAccount = primary == null ? null : new AccountBalanceView(primary.getAccountBalance()); + this.savingsAccount = savings == null ? null : new AccountBalanceView(savings.getAccountBalance()); + } + + public Long getUserId() { + return userId; + } + + public String getUsername() { + return username; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public String getEmail() { + return email; + } + + public String getPhone() { + return phone; + } + + public boolean isEnabled() { + return enabled; + } + + public AccountBalanceView getPrimaryAccount() { + return primaryAccount; + } + + public AccountBalanceView getSavingsAccount() { + return savingsAccount; + } + + public static class AccountBalanceView { + + private final BigDecimal accountBalance; + + public AccountBalanceView(BigDecimal accountBalance) { + this.accountBalance = accountBalance; + } + + public BigDecimal getAccountBalance() { + return accountBalance; + } + } +} diff --git a/UserFront/src/test/java/com/userFront/resource/AdminResourceSerializationTest.java b/UserFront/src/test/java/com/userFront/resource/AdminResourceSerializationTest.java new file mode 100644 index 00000000..f14cff4a --- /dev/null +++ b/UserFront/src/test/java/com/userFront/resource/AdminResourceSerializationTest.java @@ -0,0 +1,163 @@ +package com.userFront.resource; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.math.BigDecimal; +import java.util.Collections; +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.userFront.domain.Appointment; +import com.userFront.domain.PrimaryAccount; +import com.userFront.domain.Recipient; +import com.userFront.domain.SavingsAccount; +import com.userFront.domain.User; +import com.userFront.service.AppointmentService; +import com.userFront.service.UserService; + +public class AdminResourceSerializationTest { + + private static final String PASSWORD_HASH = "$2a$12$abcdefghijklmnopqrstuuSecretHashValue"; + private static final String RECIPIENT_ACCOUNT = "RECIPIENT-ACCT-998877"; + + private MockMvc mockMvc; + private User user; + + @Before + public void setUp() { + user = sampleUser(); + + UserService userService = mock(UserService.class); + when(userService.findUserList()).thenReturn(Collections.singletonList(user)); + UserResource userResource = new UserResource(); + ReflectionTestUtils.setField(userResource, "userService", userService); + + Appointment appointment = new Appointment(); + appointment.setId(7L); + appointment.setDate(new Date(0)); + appointment.setLocation("Main branch"); + appointment.setDescription("Mortgage review"); + appointment.setUser(user); + AppointmentService appointmentService = mock(AppointmentService.class); + when(appointmentService.findAll()).thenReturn(Collections.singletonList(appointment)); + AppointmentResource appointmentResource = new AppointmentResource(); + ReflectionTestUtils.setField(appointmentResource, "appointmentService", appointmentService); + + mockMvc = MockMvcBuilders.standaloneSetup(userResource, appointmentResource).build(); + } + + @Test + public void userListReturnsOnlyAdminPortalFields() throws Exception { + mockMvc.perform(get("/api/user/all")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].username").value("alice")) + .andExpect(jsonPath("$[0].firstName").value("Alice")) + .andExpect(jsonPath("$[0].email").value("alice@example.com")) + .andExpect(jsonPath("$[0].enabled").value(true)) + .andExpect(jsonPath("$[0].primaryAccount.accountBalance").value(1250.5)) + .andExpect(jsonPath("$[0].savingsAccount.accountBalance").value(300)) + .andExpect(jsonPath("$[0].password").doesNotExist()) + .andExpect(jsonPath("$[0].recipientList").doesNotExist()) + .andExpect(jsonPath("$[0].authorities").doesNotExist()) + .andExpect(jsonPath("$[0].primaryAccount.accountNumber").doesNotExist()) + .andExpect(jsonPath("$[0].savingsAccount.accountNumber").doesNotExist()) + .andExpect(jsonPath("$[0].primaryAccount.id").doesNotExist()); + + String body = mockMvc.perform(get("/api/user/all")).andReturn().getResponse().getContentAsString(); + assertThat(body, not(containsString(PASSWORD_HASH))); + assertThat(body, not(containsString(RECIPIENT_ACCOUNT))); + } + + @Test + public void appointmentListEmbedsOnlyUsername() throws Exception { + mockMvc.perform(get("/api/appointment/all")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id").value(7)) + .andExpect(jsonPath("$[0].description").value("Mortgage review")) + .andExpect(jsonPath("$[0].confirmed").value(false)) + .andExpect(jsonPath("$[0].user.username").value("alice")) + .andExpect(jsonPath("$[0].user.password").doesNotExist()) + .andExpect(jsonPath("$[0].user.recipientList").doesNotExist()) + .andExpect(jsonPath("$[0].user.primaryAccount").doesNotExist()) + .andExpect(jsonPath("$[0].user.email").doesNotExist()); + + String body = mockMvc.perform(get("/api/appointment/all")).andReturn().getResponse().getContentAsString(); + assertThat(body, not(containsString(PASSWORD_HASH))); + assertThat(body, not(containsString(RECIPIENT_ACCOUNT))); + } + + @Test + public void userEntitySerializationOmitsPasswordAndRecipients() throws Exception { + String json = new ObjectMapper().writeValueAsString(user); + + assertThat(json, not(containsString("\"password\""))); + assertThat(json, not(containsString(PASSWORD_HASH))); + assertThat(json, not(containsString("recipientList"))); + assertThat(json, not(containsString(RECIPIENT_ACCOUNT))); + } + + @Test + public void appointmentEntitySerializationOmitsUser() throws Exception { + Appointment appointment = new Appointment(); + appointment.setUser(user); + + String json = new ObjectMapper().writeValueAsString(appointment); + + assertThat(json, not(containsString("\"user\""))); + assertThat(json, not(containsString(PASSWORD_HASH))); + } + + @Test + public void toStringOmitsPasswordAndRecipients() { + String text = user.toString(); + + assertFalse(text.contains(PASSWORD_HASH)); + assertFalse(text.contains("password")); + assertFalse(text.contains(RECIPIENT_ACCOUNT)); + } + + private static User sampleUser() { + PrimaryAccount primary = new PrimaryAccount(); + primary.setId(11L); + primary.setAccountNumber(11223101); + primary.setAccountBalance(new BigDecimal("1250.50")); + + SavingsAccount savings = new SavingsAccount(); + savings.setId(12L); + savings.setAccountNumber(11223102); + savings.setAccountBalance(new BigDecimal("300")); + + User user = new User(); + user.setUserId(1L); + user.setUsername("alice"); + user.setPassword(PASSWORD_HASH); + user.setFirstName("Alice"); + user.setLastName("Smith"); + user.setEmail("alice@example.com"); + user.setPhone("555-0100"); + user.setPrimaryAccount(primary); + user.setSavingsAccount(savings); + + Recipient recipient = new Recipient(); + recipient.setName("Bob Payee"); + recipient.setEmail("bob@example.com"); + recipient.setAccountNumber(RECIPIENT_ACCOUNT); + recipient.setUser(user); + user.setRecipientList(Collections.singletonList(recipient)); + return user; + } +}