Skip to content

Commit

Permalink
[TEST] 이미지 업로드 컨트롤러 테스트 및 rest docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Dec 12, 2023
1 parent 38dc731 commit 5d49934
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions backend/src/docs/image.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:doctype: book
:icons: font
:source-highlighter: highlightjs
:toc: left
:toclevels: 4

== Image
=== 프로필 이미지 업로드
operation::post upload image[snippets='http-request,http-response']
1 change: 1 addition & 0 deletions backend/src/docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ include::place.adoc[]
include::member.adoc[]
include::friend.adoc[]
include::group.adoc[]
include::image.adoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.twtw.backend.domain.image.controller;

import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest;
import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.twtw.backend.domain.image.dto.ImageResponse;
import com.twtw.backend.domain.image.service.ImageService;
import com.twtw.backend.support.docs.RestDocsTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.ResultActions;

@DisplayName("ImageController의")
@WebMvcTest(ImageController.class)
class ImageControllerTest extends RestDocsTest {

@MockBean private ImageService imageService;

@Test
@DisplayName("이미지 업로드 API가 수행되는가")
void uploadImage() throws Exception {
// given
final ImageResponse expected = new ImageResponse("https://storage.googleapis.com/bucket-name/some-file-id");
given(imageService.uploadImage(any())).willReturn(expected);

// when
final ResultActions perform =
mockMvc.perform(
post("/images")
.contentType(MediaType.MULTIPART_FORM_DATA)
.content(toRequestBody("image를 request시 넣어주세요"))
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));

// then
perform.andExpect(status().isOk())
.andExpect(jsonPath("$.imageUrl").isString());

// docs
perform.andDo(print())
.andDo(document("post upload image", getDocumentRequest(), getDocumentResponse()));
}
}

0 comments on commit 5d49934

Please sign in to comment.