|
| 1 | +package com.joaov1ct0r.restful_api_users_java.modules.users.controllers.posts; |
| 2 | + |
| 3 | +import com.github.javafaker.Faker; |
| 4 | +import com.joaov1ct0r.restful_api_users_java.modules.auth.dtos.SignInDTO; |
| 5 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.FindAllPostsDTO; |
| 6 | +import com.joaov1ct0r.restful_api_users_java.modules.users.entities.UserEntity; |
| 7 | +import com.joaov1ct0r.restful_api_users_java.modules.users.utils.TestUtils; |
| 8 | +import jakarta.servlet.http.Cookie; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; |
| 16 | +import org.springframework.test.context.ActiveProfiles; |
| 17 | +import org.springframework.test.context.junit4.SpringRunner; |
| 18 | +import org.springframework.test.web.servlet.MockMvc; |
| 19 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 20 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 21 | +import org.springframework.web.context.WebApplicationContext; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import java.time.LocalDateTime; |
| 24 | +import java.util.UUID; |
| 25 | + |
| 26 | +@RunWith(SpringRunner.class) |
| 27 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 28 | +@ActiveProfiles("test") |
| 29 | +public class FindAllPostsControllerTest { |
| 30 | + private MockMvc mvc; |
| 31 | + |
| 32 | + private Faker faker; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private WebApplicationContext context; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setup() { |
| 39 | + this.mvc = MockMvcBuilders.webAppContextSetup(context) |
| 40 | + .apply(SecurityMockMvcConfigurers.springSecurity()) |
| 41 | + .build(); |
| 42 | + |
| 43 | + this.faker = new Faker(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void shouldBeAbleToFindAllPosts() throws Exception { |
| 48 | + UUID userId = UUID.randomUUID(); |
| 49 | + var createUserDTO = new UserEntity( |
| 50 | + userId, |
| 51 | + faker.name().username(), |
| 52 | + faker.internet().emailAddress(), |
| 53 | + faker.name().firstName(), |
| 54 | + faker.internet().password(), |
| 55 | + "any_photo_url", |
| 56 | + LocalDateTime.now(), |
| 57 | + LocalDateTime.now(), |
| 58 | + null |
| 59 | + ); |
| 60 | + |
| 61 | + var createUserJson = TestUtils.stringToMMF(createUserDTO); |
| 62 | + mvc.perform( |
| 63 | + MockMvcRequestBuilders.multipart("/signup/") |
| 64 | + .file(createUserJson) |
| 65 | + .contentType(MediaType.MULTIPART_FORM_DATA) |
| 66 | + .with(request -> { |
| 67 | + request.setMethod("POST"); |
| 68 | + return request; |
| 69 | + }) |
| 70 | + ).andReturn().getResponse(); |
| 71 | + |
| 72 | + var signInDTO = new SignInDTO( |
| 73 | + createUserDTO.getUsername(), |
| 74 | + createUserDTO.getPassword() |
| 75 | + ); |
| 76 | + Cookie cookieAuthorization = mvc.perform( |
| 77 | + MockMvcRequestBuilders.post("/signin/") |
| 78 | + .contentType(MediaType.APPLICATION_JSON) |
| 79 | + .content(TestUtils.objectToJson(signInDTO)) |
| 80 | + ).andReturn().getResponse().getCookie("authorization"); |
| 81 | + |
| 82 | + assert cookieAuthorization != null; |
| 83 | + |
| 84 | + FindAllPostsDTO findAllPostsDTO = new FindAllPostsDTO( |
| 85 | + 20, |
| 86 | + 1, |
| 87 | + "any_content" |
| 88 | + ); |
| 89 | + |
| 90 | + var findAllPostsResponse = mvc.perform( |
| 91 | + MockMvcRequestBuilders.get("/post/") |
| 92 | + .cookie(cookieAuthorization) |
| 93 | + .contentType(MediaType.APPLICATION_JSON) |
| 94 | + .content(TestUtils.objectToJson(findAllPostsDTO)) |
| 95 | + ).andReturn().getResponse(); |
| 96 | + |
| 97 | + int findAllPostsResponseStatusCode = findAllPostsResponse.getStatus(); |
| 98 | + |
| 99 | + assertThat(findAllPostsResponseStatusCode).isEqualTo(200); |
| 100 | + } |
| 101 | +} |
0 commit comments