-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63f6470
commit 8d392ae
Showing
6 changed files
with
219 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,11 @@ | |
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.annotation.Rollback; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
@@ -32,6 +34,8 @@ | |
@AutoConfigureMockMvc | ||
@ExtendWith(MockitoExtension.class) | ||
@Testcontainers | ||
@Transactional | ||
@Rollback | ||
class AuthControllerIntegrationTest { | ||
@Container | ||
@ServiceConnection | ||
|
@@ -134,6 +138,22 @@ void loginFailedWhenInvalidEmailGivenTest(String email) throws Exception { | |
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Email address entered incorrectly!")); | ||
} | ||
|
||
/** | ||
* Test case to verify successful user registration. | ||
* | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@Test | ||
void registerSuccessfulTest() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Pass1234"); | ||
this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "register") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("User registered successfully!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.jwtToken").exists()); | ||
} | ||
|
||
/** | ||
* Parameterized test to verify registration failure with invalid passwords. | ||
* | ||
|
149 changes: 149 additions & 0 deletions
149
src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.linkurlshorter.urlshortener.link; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.linkurlshorter.urlshortener.auth.dto.AuthRequest; | ||
import org.json.JSONObject; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.annotation.Rollback; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.util.UUID; | ||
|
||
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; | ||
|
||
/** | ||
* Integration tests for the LinkController class. | ||
* | ||
* @author Ivan Shalaiev | ||
* @version 1.0 | ||
*/ | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ExtendWith(MockitoExtension.class) | ||
@Testcontainers | ||
@Transactional | ||
@Rollback | ||
class LinkControllerIntegrationTest { | ||
@Container | ||
@ServiceConnection | ||
static PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres:16.0-alpine"); | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
private final String baseUrl = "/api/V1/link/"; | ||
private String token; | ||
private AuthRequest authRequest; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@BeforeEach | ||
void setUp() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Pass1234"); | ||
ResultActions result = this.mockMvc.perform(MockMvcRequestBuilders.post("/api/V1/auth/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))); | ||
MvcResult mvcResult = result.andDo(print()).andReturn(); | ||
String contentAsString = mvcResult.getResponse().getContentAsString(); | ||
JSONObject jsonObject = new JSONObject(contentAsString); | ||
this.token = "Bearer " + jsonObject.getString("jwtToken"); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"https://www.youtube.com", | ||
"https://open.spotify.com/", | ||
"https://www.google.com", | ||
"https://www.facebook.com"}) | ||
void createShortLinkWorksCorrectly(String url) throws Exception { | ||
CreateLinkRequest createLinkRequest = new CreateLinkRequest(url); | ||
mockMvc.perform(post(baseUrl + "create") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token) | ||
.content(objectMapper.writeValueAsString(createLinkRequest))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.error").value("ok")) | ||
.andExpect(jsonPath("$.shortLink").isNotEmpty()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"https://www.", | ||
"https://open.spotifycom", | ||
"https://www.google.com@", | ||
"https://www.facebook.com%"}) | ||
void createShortLinkFailsWhenUrlIsInvalid(String url) throws Exception { | ||
CreateLinkRequest createLinkRequest = new CreateLinkRequest(url); | ||
mockMvc.perform(post(baseUrl + "create") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token) | ||
.content(objectMapper.writeValueAsString(createLinkRequest))) | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(jsonPath("$.statusCode").value(400)) | ||
.andExpect(jsonPath("$.message").value("Not valid format url!")) | ||
.andExpect(jsonPath("$.path").value("/api/V1/link/create")); | ||
} | ||
|
||
@Test | ||
void deleteLinkWorksCorrectly() throws Exception { | ||
UUID id = UUID.fromString("3053e49b-6da3-4389-9d06-23b2d57b6f25"); | ||
mockMvc.perform(post(baseUrl + "delete" + "?id=" + id) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.error").value("ok")); | ||
} | ||
@Test | ||
void deleteLinkFailsWhenIdIsInvalid() throws Exception { | ||
UUID id = UUID.randomUUID(); | ||
mockMvc.perform(post(baseUrl + "delete" + "?id=" + id) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token)) | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(jsonPath("$.statusCode").value(404)) | ||
.andExpect(jsonPath("$.message").value("No link by provided id found")) | ||
.andExpect(jsonPath("$.path").value("/api/V1/link/delete")); | ||
} | ||
@Test | ||
void deleteLinkFailsWhenIdIsNull() throws Exception { | ||
mockMvc.perform(post(baseUrl + "delete" + "?id=" + null) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token)) | ||
.andExpect(status().is4xxClientError()); | ||
} | ||
@Test | ||
void deleteLinkFailsWhenUserHasNoRightsForThisLink() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Pass1234"); | ||
ResultActions result = this.mockMvc.perform(MockMvcRequestBuilders.post("/api/V1/auth/register") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))); | ||
MvcResult mvcResult = result.andDo(print()).andReturn(); | ||
String contentAsString = mvcResult.getResponse().getContentAsString(); | ||
JSONObject jsonObject = new JSONObject(contentAsString); | ||
this.token = "Bearer " + jsonObject.getString("jwtToken"); | ||
UUID id = UUID.fromString("3053e49b-6da3-4389-9d06-23b2d57b6f25"); | ||
mockMvc.perform(post(baseUrl + "delete" + "?id=" + id) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token)) | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(jsonPath("$.statusCode").value(403)) | ||
.andExpect(jsonPath("$.message").value("Operation forbidden!")) | ||
.andExpect(jsonPath("$.path").value("/api/V1/link/delete")); | ||
} | ||
} |
Oops, something went wrong.