-
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.
Merge pull request #52 from nastiausenko/auth-integration-test
Added AuthControllerIntegrationTest.class
- Loading branch information
Showing
3 changed files
with
235 additions
and
8 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
189 changes: 189 additions & 0 deletions
189
src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.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,189 @@ | ||
package com.linkurlshorter.urlshortener.auth; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.linkurlshorter.urlshortener.auth.dto.AuthRequest; | ||
import com.linkurlshorter.urlshortener.user.ChangeUserEmailRequest; | ||
import com.linkurlshorter.urlshortener.user.ChangeUserPasswordRequest; | ||
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.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* Integration tests for the AuthController class. | ||
* | ||
* @author Ivan Shalaiev | ||
* @version 1.0 | ||
*/ | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ExtendWith(MockitoExtension.class) | ||
@Testcontainers | ||
class AuthControllerIntegrationTest { | ||
@Container | ||
@ServiceConnection | ||
static PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres:16.0-alpine"); | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
private final String baseUrl = "/api/V1/auth/"; | ||
private AuthRequest authRequest; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
/** | ||
* Test case to verify successful user login. | ||
* | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@Test | ||
void loginSuccessfulTest() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Pass1234"); | ||
this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("User logged in successfully!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.jwtToken").exists()); | ||
} | ||
|
||
/** | ||
* Test case to verify login failure when user does not exist. | ||
* | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@Test | ||
void loginFailedWhenUserDoesNotExistTest() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Pass1234"); | ||
this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(MockMvcResultMatchers.status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(401)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Authentication failed!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage").value("No user by provided email found")); | ||
} | ||
|
||
/** | ||
* Test case to verify login failure when password does not match. | ||
* | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@Test | ||
void loginFailedWhenPasswordDoesNotMatchTest() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Pass12345"); | ||
this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(MockMvcResultMatchers.status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(401)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Bad Credentials!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage").value("Bad credentials")); | ||
} | ||
|
||
/** | ||
* Parameterized test to verify login failure with invalid passwords. | ||
* | ||
* @param password the password to test | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@ParameterizedTest | ||
@ValueSource(strings = {"", "Password", "Pass123", "pass1234", "Pass 1234", "PASS1234"}) | ||
void loginFailedWhenInvalidPasswordGivenTest(String password) throws Exception { | ||
authRequest = new AuthRequest("[email protected]", password); | ||
mockMvc.perform(post(baseUrl + "login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Validation failed!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage").value("Password " + | ||
"must be at least 8 characters long and contain at least one digit, one uppercase letter, " + | ||
"and one lowercase letter. No spaces are allowed.")); | ||
} | ||
|
||
/** | ||
* Parameterized test to verify login failure with invalid emails. | ||
* | ||
* @param email the email address to test | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@ParameterizedTest | ||
@ValueSource(strings = {"", "user [email protected]", | ||
" [email protected]", | ||
"user-test%@example.com", | ||
"user-test#@example.com", | ||
"user-test.example.com"}) | ||
// TODO: add more email to test "user-test@example" | ||
void loginFailedWhenInvalidEmailGivenTest(String email) throws Exception { | ||
authRequest = new AuthRequest(email, "Pass1234"); | ||
mockMvc.perform(post(baseUrl + "login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Validation failed!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage") | ||
.value("Email address entered incorrectly!")); | ||
} | ||
|
||
/** | ||
* Parameterized test to verify registration failure with invalid passwords. | ||
* | ||
* @param password the password to test | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@ParameterizedTest | ||
@ValueSource(strings = {"", "Password", "Pass123", "pass1234", "Pass 1234", "PASS1234"}) | ||
void registerFailedWhenInvalidPasswordGivenTest(String password) throws Exception { | ||
authRequest = new AuthRequest("[email protected]", password); | ||
mockMvc.perform(post(baseUrl + "register") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Validation failed!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage").value("Password " + | ||
"must be at least 8 characters long and contain at least one digit, one uppercase letter, " + | ||
"and one lowercase letter. No spaces are allowed.")); | ||
} | ||
|
||
/** | ||
* Parameterized test to verify registration failure with invalid emails. | ||
* | ||
* @param email the email address to test | ||
* @throws Exception if any error occurs during the test | ||
*/ | ||
@ParameterizedTest | ||
@ValueSource(strings = {"", "user [email protected]", | ||
" [email protected]", | ||
"user-test%@example.com", | ||
"user-test#@example.com", | ||
"user-test.example.com"}) | ||
// TODO: add more email to test "user-test@example" | ||
void registerFailedWhenInvalidEmailGivenTest(String email) throws Exception { | ||
authRequest = new AuthRequest(email, "Pass1234"); | ||
mockMvc.perform(post(baseUrl + "register") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(authRequest))) | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Validation failed!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage") | ||
.value("Email address entered incorrectly!")); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -30,6 +30,12 @@ | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* Integration tests for {@link UserController} class. | ||
* | ||
* @author Ivan Shalaiev | ||
* @version 1.0 | ||
*/ | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ExtendWith(MockitoExtension.class) | ||
|
@@ -59,6 +65,11 @@ void setUp() throws Exception { | |
this.token = "Bearer " + jsonObject.getString("jwtToken"); | ||
} | ||
|
||
/** | ||
* Test case for changing user password. | ||
* | ||
* @throws Exception if an error occurs during the test execution | ||
*/ | ||
@Test | ||
void changePasswordTest() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Password1"); | ||
|
@@ -79,17 +90,33 @@ void changePasswordTest() throws Exception { | |
.andExpect(jsonPath("$.error").value("ok")); | ||
} | ||
|
||
/** | ||
* Test case for changing user password when an invalid password is given. | ||
* | ||
* @param password the invalid password to test | ||
* @throws Exception if an error occurs during the test execution | ||
*/ | ||
@ParameterizedTest | ||
@ValueSource(strings = {"", "Password", "Pass123", "pass1234", "Pass 1234"}) | ||
@ValueSource(strings = {"", "Password", "Pass123", "pass1234", "Pass 1234", "PASS1234"}) | ||
void changePasswordFailedWhenInvalidPasswordGivenTest(String password) throws Exception { | ||
ChangeUserPasswordRequest request = new ChangeUserPasswordRequest(password); | ||
mockMvc.perform(post(baseUrl + "change-password") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token) | ||
.content(objectMapper.writeValueAsString(request))) | ||
.andExpect(status().is4xxClientError()); | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Validation failed!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage").value("Password " + | ||
"must be at least 8 characters long and contain at least one digit, one uppercase letter, " + | ||
"and one lowercase letter. No spaces are allowed.")); | ||
} | ||
|
||
/** | ||
* Test case for changing user email. | ||
* | ||
* @throws Exception if an error occurs during the test execution | ||
*/ | ||
@Test | ||
void changeEmailTest() throws Exception { | ||
authRequest = new AuthRequest("[email protected]", "Password1"); | ||
|
@@ -110,19 +137,29 @@ void changeEmailTest() throws Exception { | |
.andExpect(jsonPath("$.error").value("ok")); | ||
} | ||
|
||
/** | ||
* Test case for changing user email when an invalid email is given. | ||
* | ||
* @param email the invalid email to test | ||
* @throws Exception if an error occurs during the test execution | ||
*/ | ||
@ParameterizedTest | ||
@ValueSource(strings = {"", "change [email protected]", | ||
" change-email[email protected]", | ||
"change-email-test%@email.com", | ||
"change-email-test#@email.com", | ||
"change-email-test.email.com"}) | ||
" user[email protected]", | ||
"user-test%@email.com", | ||
"user-test#@email.com", | ||
"user-test.email.com"}) | ||
// TODO: add more email to test "change-email-test@email" | ||
void changeEmailFailedWhenInvalidEmailGivenTest(String email) throws Exception { | ||
ChangeUserEmailRequest request = new ChangeUserEmailRequest(email); | ||
mockMvc.perform(post(baseUrl + "change-email") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", token) | ||
.content(objectMapper.writeValueAsString(request))) | ||
.andExpect(status().is4xxClientError()); | ||
.andExpect(status().is4xxClientError()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Validation failed!")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.exceptionMessage") | ||
.value("Email address entered incorrectly!")); | ||
} | ||
} |