Skip to content

Commit

Permalink
feat: 관리자용 소셜미디어 조회 API 추가
Browse files Browse the repository at this point in the history
- findByOwnerIdAndOwnerType: 목록 조회
- findById: 단건 조회
  • Loading branch information
seokjin8678 committed Apr 8, 2024
1 parent 2abd12a commit ce59ce1
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.festago.admin.presentation.v1;

import com.festago.admin.application.AdminSocialMediaV1QueryService;
import com.festago.admin.dto.socialmedia.AdminSocialMediaV1Response;
import com.festago.admin.dto.socialmedia.SocialMediaCreateV1Request;
import com.festago.admin.dto.socialmedia.SocialMediaUpdateV1Request;
import com.festago.socialmedia.application.SocialMediaCommandService;
import com.festago.socialmedia.domain.OwnerType;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -22,6 +28,24 @@
public class AdminSocialMediaV1Controller {

private final SocialMediaCommandService socialMediaCommandService;
private final AdminSocialMediaV1QueryService adminSocialMediaV1QueryService;

@GetMapping
public ResponseEntity<List<AdminSocialMediaV1Response>> findByOwnerIdAndOwnerType(
@RequestParam Long ownerId,
@RequestParam OwnerType ownerType
) {
return ResponseEntity.ok()
.body(adminSocialMediaV1QueryService.findByOwnerIdAndOwnerType(ownerId, ownerType));
}

@GetMapping("/{socialMediaId}")
public ResponseEntity<AdminSocialMediaV1Response> findById(
@PathVariable Long socialMediaId
) {
return ResponseEntity.ok()
.body(adminSocialMediaV1QueryService.findById(socialMediaId));
}

@PostMapping
public ResponseEntity<Void> createSocialMedia(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.festago.admin.presentation.v1;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.festago.admin.application.AdminSocialMediaV1QueryService;
import com.festago.admin.dto.socialmedia.AdminSocialMediaV1Response;
import com.festago.admin.dto.socialmedia.SocialMediaCreateV1Request;
import com.festago.admin.dto.socialmedia.SocialMediaUpdateV1Request;
import com.festago.auth.domain.Role;
Expand All @@ -18,6 +22,7 @@
import com.festago.support.CustomWebMvcTest;
import com.festago.support.WithMockAuth;
import jakarta.servlet.http.Cookie;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand All @@ -43,6 +48,108 @@ class AdminSocialMediaV1ControllerTest {
@Autowired
SocialMediaCommandService socialMediaCommandService;

@Autowired
AdminSocialMediaV1QueryService adminSocialMediaV1QueryService;

@Nested
class 소셜미디어_단건_조회 {

final String uri = "/admin/api/v1/socialmedias/{socialMediaId}";
Long socialMediaId = 1L;

@Nested
@DisplayName("GET " + uri)
class 올바른_주소로 {

@Test
@WithMockAuth(role = Role.ADMIN)
void 요청을_보내면_200_응답이_반환된다() throws Exception {
// given
given(adminSocialMediaV1QueryService.findById(anyLong()))
.willReturn(new AdminSocialMediaV1Response(
1L,
SocialMediaType.INSTAGRAM,
"테코대학교 총학생회 인스타그램",
"https://image.com/logo.png",
"htps://instagram.com/tecodaehak"
));

// when & then
mockMvc.perform(get(uri, socialMediaId)
.cookie(TOKEN_COOKIE)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void 토큰_없이_보내면_401_응답이_반환된다() throws Exception {
// when & then
mockMvc.perform(get(uri, socialMediaId))
.andExpect(status().isUnauthorized());
}

@Test
@WithMockAuth(role = Role.MEMBER)
void 토큰의_권한이_Admin이_아니면_404_응답이_반환된다() throws Exception {
// when & then
mockMvc.perform(get(uri, socialMediaId)
.cookie(TOKEN_COOKIE))
.andExpect(status().isNotFound());
}
}
}

@Nested
class 소셜미디어_목록_조회 {

final String uri = "/admin/api/v1/socialmedias";

@Nested
@DisplayName("GET " + uri)
class 올바른_주소로 {

@Test
@WithMockAuth(role = Role.ADMIN)
void 요청을_보내면_200_응답이_반환된다() throws Exception {
// given
given(adminSocialMediaV1QueryService.findByOwnerIdAndOwnerType(anyLong(), any(OwnerType.class)))
.willReturn(List.of(
new AdminSocialMediaV1Response(
1L,
SocialMediaType.INSTAGRAM,
"테코대학교 총학생회 인스타그램",
"https://image.com/logo.png",
"htps://instagram.com/tecodaehak"
)
));

// when & then
mockMvc.perform(get(uri)
.param("ownerId", "1")
.param("ownerType", "SCHOOL")
.cookie(TOKEN_COOKIE)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void 토큰_없이_보내면_401_응답이_반환된다() throws Exception {
// when & then
mockMvc.perform(get(uri))
.andExpect(status().isUnauthorized());
}

@Test
@WithMockAuth(role = Role.MEMBER)
void 토큰의_권한이_Admin이_아니면_404_응답이_반환된다() throws Exception {
// when & then
mockMvc.perform(get(uri)
.cookie(TOKEN_COOKIE))
.andExpect(status().isNotFound());
}
}
}

@Nested
class 소셜미디어_생성 {

Expand Down

0 comments on commit ce59ce1

Please sign in to comment.