Skip to content

Commit

Permalink
Implement expiration notifications Allow and Deny API (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 authored Oct 25, 2023
2 parents 7fc363a + 0a65525 commit 44649b5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.annotations.ColumnDefault;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
Expand Down Expand Up @@ -49,13 +50,18 @@ public class User extends BaseTimeEntity implements UserDetails {
@Column(columnDefinition = "TINYINT", nullable = false)
private Role role;

@Column(columnDefinition = "TINYINT", nullable = false)
@ColumnDefault("1")
private boolean isAllowNotifications;

@Builder
public User(Long id, String username, String password, String nickname, Role role) {
this.id = id;
this.username = username;
this.password = password;
this.nickname = nickname;
this.role = role;
this.isAllowNotifications = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,27 @@ public UserInfoResponseDto readInfo(String username) {
.username(username)
.nickname(user.getNickname())
.oauth(oAuthService.list(user))
.allowNotifications(isExistDeviceToken(user))
.allowNotifications(user.isAllowNotifications())
.anonymous(user.isAnonymous())
.build();
return userInfoResponseDto;
}

/**
* DeviceToken 조회 메서드 (user)
* 알림 동의 여부 허용 메서드 (user)
*/
public boolean isExistDeviceToken(User user) {
return !deviceTokenRepository.findAllByUser(user).isEmpty();
public void allowNotifications(String username) {
User user = read(username);
user.setAllowNotifications(true);
userRepository.save(user);
}

/**
* 알림 동의 여부 거부 메서드 (user)
*/
public void denyNotifications(String username) {
User user = read(username);
user.setAllowNotifications(false);
userRepository.save(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,38 @@ public ResponseEntity<Message> sendNotification(
.path(request.getRequestURI())
.build());
}

@PostMapping("/expiration/allow")
@Operation(summary = "만료 알림 수신 허용 메서드", description = "만료 알림 수신을 허용하기 위한 메서드입니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "만료 알림 수신 허용 성공"),
@ApiResponse(responseCode = "400(404)", description = "존재하지 않는 회원")
})
public ResponseEntity<Message> allowExpirationNotification(
HttpServletRequest request,
@RequestHeader("Authorization") String accessToken) {
String username = jwtProvider.getUsername(accessToken.substring(7));
userService.allowNotifications(username);
return ResponseEntity.ok(
SuccessMessage.builder()
.path(request.getRequestURI())
.build());
}

@PostMapping("/expiration/deny")
@Operation(summary = "만료 알림 수신 거부 메서드", description = "만료 알림 수신을 거부하기 위한 메서드입니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "만료 알림 수신 거부 성공"),
@ApiResponse(responseCode = "400(404)", description = "존재하지 않는 회원")
})
public ResponseEntity<Message> denyExpirationNotification(
HttpServletRequest request,
@RequestHeader("Authorization") String accessToken) {
String username = jwtProvider.getUsername(accessToken.substring(7));
userService.denyNotifications(username);
return ResponseEntity.ok(
SuccessMessage.builder()
.path(request.getRequestURI())
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,41 @@ void deleteDeviceToken() throws Exception {
.content(objectMapper.writeValueAsString(deviceTokenRequestDto)))
.andExpect(status().isOk());
}

/**
* 만료 알림 수신 허용 테스트
*/
@Test
@WithMockUser(username = "이진우", roles = "USER")
void allowNotifications() throws Exception {
String accessToken = "my.access.token";
String username = "이진우";
User user = User.builder().username(username).build();

when(jwtProvider.resolveToken(any())).thenReturn(accessToken);
when(jwtProvider.getUsername(anyString())).thenReturn(username);
when(userService.read(username)).thenReturn(user);

mockMvc.perform(post("/notifications/expiration/allow").header("Authorization", "Bearer " + accessToken))
.andExpect(status().isOk());
}

/**
* 만료 알림 수신 거부 테스트
*/
@Test
@WithMockUser(username = "이진우", roles = "USER")
void denyNotifications() throws Exception {
String accessToken = "my.access.token";
String username = "이진우";
User user = User.builder().username(username).build();

when(jwtProvider.resolveToken(any())).thenReturn(accessToken);
when(jwtProvider.getUsername(anyString())).thenReturn(username);
when(userService.read(username)).thenReturn(user);

mockMvc.perform(post("/notifications/expiration/deny").header("Authorization", "Bearer " + accessToken))
.andExpect(status().isOk());
}

}

0 comments on commit 44649b5

Please sign in to comment.