-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for dto, permission controller, fix logic
Took 37 seconds
- Loading branch information
1 parent
21817f7
commit cc1a818
Showing
13 changed files
with
934 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
package com.github.gribanoveu.auth.base; | ||
|
||
import com.github.gribanoveu.auth.controllers.dtos.request.LoginDto; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.TestInstance; | ||
import com.github.gribanoveu.auth.entities.enums.TokenType; | ||
import com.github.gribanoveu.auth.entities.services.contract.TokenService; | ||
import com.github.gribanoveu.auth.security.CustomUserDetails; | ||
import com.github.gribanoveu.auth.security.CustomUserDetailsService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
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.MockMvcBuilder; | ||
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.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* @author Evgeny Gribanov | ||
* @version 11.09.2023 | ||
|
@@ -29,33 +24,24 @@ | |
public abstract class BaseMockMvcTest { | ||
@Autowired protected MockMvc mockMvc; | ||
@Autowired protected TestJsonUtils testJsonUtils; | ||
@Autowired private TokenService tokenService; | ||
@Autowired private CustomUserDetailsService usrDetailsService; | ||
|
||
@Container | ||
@ServiceConnection | ||
static PostgreSQLContainer<?> postgres = | ||
new PostgreSQLContainer<>("postgres:15.4-alpine"); | ||
|
||
protected String adminToken; | ||
protected String userToken; | ||
|
||
// fixme why @WithUserDetails is not working | ||
// fixme make create test users only for tests | ||
// fixme mock rsa keys and run in ci/cd | ||
protected String issueJwtTokenAsAdmin() throws Exception { | ||
var result = mockMvc.perform(post("/v1/token/issue") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(testJsonUtils.convertDtoToJson( | ||
new LoginDto("[email protected]", "Qwerty123")))) | ||
.andExpect(status().isOk()).andReturn(); | ||
@BeforeEach | ||
public void setUp() { // fixme why @WithUserDetails is not working | ||
var admin = (CustomUserDetails) usrDetailsService.loadUserByUsername("[email protected]"); | ||
adminToken = "Bearer " + tokenService.generateToken(admin, TokenType.ACCESS); | ||
|
||
return "Bearer " + testJsonUtils.getJsonValueFromMvcResult(result, "accessToken"); | ||
var user = (CustomUserDetails) usrDetailsService.loadUserByUsername("[email protected]"); | ||
userToken = "Bearer " + tokenService.generateToken(user, TokenType.ACCESS); | ||
} | ||
|
||
protected String issueTokenAsUser() throws Exception { | ||
var result = mockMvc.perform(post("/v1/token/issue") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(testJsonUtils.convertDtoToJson( | ||
new LoginDto("[email protected]", "Qwerty123")))) | ||
.andExpect(status().isOk()).andReturn(); | ||
@Container | ||
@ServiceConnection | ||
static PostgreSQLContainer<?> postgres = | ||
new PostgreSQLContainer<>("postgres:15.4-alpine"); | ||
|
||
return "Bearer " + testJsonUtils.getJsonValueFromMvcResult(result, "accessToken"); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/com/github/gribanoveu/auth/base/BaseValidatorTest.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,27 @@ | ||
package com.github.gribanoveu.auth.base; | ||
|
||
import jakarta.validation.Validation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.ValidatorFactory; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
/** | ||
* @author Evgeny Gribanov | ||
* @version 05.10.2023 | ||
*/ | ||
public abstract class BaseValidatorTest { | ||
protected ValidatorFactory factory; | ||
protected Validator validator; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
factory = Validation.buildDefaultValidatorFactory(); | ||
validator = factory.getValidator(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
factory.close(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/com/github/gribanoveu/auth/controllers/dtos/request/ChangeEmailDtoTest.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,49 @@ | ||
package com.github.gribanoveu.auth.controllers.dtos.request; | ||
|
||
import com.github.gribanoveu.auth.base.BaseValidatorTest; | ||
import com.github.gribanoveu.auth.constants.ValidationMessages; | ||
import jakarta.validation.ConstraintViolation; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Evgeny Gribanov | ||
* @version 05.10.2023 | ||
*/ | ||
class ChangeEmailDtoTest extends BaseValidatorTest { | ||
|
||
@Test | ||
void testValidChangeEmailDto() { | ||
ChangeEmailDto changeEmailDto = new ChangeEmailDto("[email protected]"); | ||
Set<ConstraintViolation<ChangeEmailDto>> violations = validator.validate(changeEmailDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage).hasSize(0); | ||
} | ||
|
||
@Test | ||
void testInvalidChangeEmailDtoWithIncorrectEmailFormat() { | ||
ChangeEmailDto changeEmailDto = new ChangeEmailDto("newemailexample.com"); | ||
Set<ConstraintViolation<ChangeEmailDto>> violations = validator.validate(changeEmailDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.PATTERN_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidChangeEmailDtoWithLongEmail() { | ||
ChangeEmailDto changeEmailDto = new ChangeEmailDto(StringUtils.repeat("a", 254) + "@example.com"); | ||
Set<ConstraintViolation<ChangeEmailDto>> violations = validator.validate(changeEmailDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.SIZE_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidChangeEmailDtoWithNullEmail() { | ||
ChangeEmailDto changeEmailDto = new ChangeEmailDto(null); | ||
Set<ConstraintViolation<ChangeEmailDto>> violations = validator.validate(changeEmailDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/test/java/com/github/gribanoveu/auth/controllers/dtos/request/ChangePasswordDtoTest.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,76 @@ | ||
package com.github.gribanoveu.auth.controllers.dtos.request; | ||
|
||
import com.github.gribanoveu.auth.base.BaseValidatorTest; | ||
import com.github.gribanoveu.auth.constants.ValidationMessages; | ||
import jakarta.validation.ConstraintViolation; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Evgeny Gribanov | ||
* @version 05.10.2023 | ||
*/ | ||
class ChangePasswordDtoTest extends BaseValidatorTest { | ||
|
||
@Test | ||
void testValidChangePasswordDto() { | ||
ChangePasswordDto changePasswordDto = new ChangePasswordDto("oldPassword", "newPassword", "newPassword"); | ||
Set<ConstraintViolation<ChangePasswordDto>> violations = validator.validate(changePasswordDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage).hasSize(0); | ||
} | ||
|
||
@Test | ||
void testInvalidChangePasswordDtoWithEmptyOldPassword() { | ||
ChangePasswordDto changePasswordDto = new ChangePasswordDto("", "newPassword", "newPassword"); | ||
Set<ConstraintViolation<ChangePasswordDto>> violations = validator.validate(changePasswordDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidChangePasswordDtoWithEmptyPassword() { | ||
ChangePasswordDto changePasswordDto = new ChangePasswordDto("oldPassword", "", "newPassword"); | ||
Set<ConstraintViolation<ChangePasswordDto>> violations = validator.validate(changePasswordDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidChangePasswordDtoWithEmptyConfirmPassword() { | ||
ChangePasswordDto changePasswordDto = new ChangePasswordDto("oldPassword", "newPassword", ""); | ||
Set<ConstraintViolation<ChangePasswordDto>> violations = validator.validate(changePasswordDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidChangePasswordDtoWithLongOldPassword() { | ||
String longOldPassword = StringUtils.repeat("a", 81); | ||
ChangePasswordDto changePasswordDto = new ChangePasswordDto(longOldPassword, "newPassword", "newPassword"); | ||
Set<ConstraintViolation<ChangePasswordDto>> violations = validator.validate(changePasswordDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.SIZE_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidChangePasswordDtoWithLongPassword() { | ||
String longPassword = StringUtils.repeat("a", 81); | ||
ChangePasswordDto changePasswordDto = new ChangePasswordDto("oldPassword", longPassword, "newPassword"); | ||
Set<ConstraintViolation<ChangePasswordDto>> violations = validator.validate(changePasswordDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.SIZE_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidChangePasswordDtoWithLongConfirmPassword() { | ||
String longConfirmPassword = StringUtils.repeat("a", 81); | ||
ChangePasswordDto changePasswordDto = new ChangePasswordDto("oldPassword", "newPassword", longConfirmPassword); | ||
Set<ConstraintViolation<ChangePasswordDto>> violations = validator.validate(changePasswordDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.SIZE_EXCEPTION_MESSAGE)); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/com/github/gribanoveu/auth/controllers/dtos/request/GenerateOtpDtoTest.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,50 @@ | ||
package com.github.gribanoveu.auth.controllers.dtos.request; | ||
|
||
import com.github.gribanoveu.auth.base.BaseValidatorTest; | ||
import com.github.gribanoveu.auth.constants.ValidationMessages; | ||
import jakarta.validation.ConstraintViolation; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Evgeny Gribanov | ||
* @version 05.10.2023 | ||
*/ | ||
class GenerateOtpDtoTest extends BaseValidatorTest { | ||
|
||
@Test | ||
void testValidGenerateOtpDto() { | ||
GenerateOtpDto generateOtpDto = new GenerateOtpDto("[email protected]"); | ||
Set<ConstraintViolation<GenerateOtpDto>> violations = validator.validate(generateOtpDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage).hasSize(0); | ||
} | ||
|
||
@Test | ||
void testInvalidGenerateOtpDtoWithIncorrectEmailFormat() { | ||
GenerateOtpDto generateOtpDto = new GenerateOtpDto("testexample.com"); | ||
Set<ConstraintViolation<GenerateOtpDto>> violations = validator.validate(generateOtpDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.PATTERN_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidGenerateOtpDtoWithLongEmail() { | ||
String longEmail = StringUtils.repeat("a", 31) + "@example.com"; | ||
GenerateOtpDto generateOtpDto = new GenerateOtpDto(longEmail); | ||
Set<ConstraintViolation<GenerateOtpDto>> violations = validator.validate(generateOtpDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.SIZE_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidGenerateOtpDtoWithNullEmail() { | ||
GenerateOtpDto generateOtpDto = new GenerateOtpDto(null); | ||
Set<ConstraintViolation<GenerateOtpDto>> violations = validator.validate(generateOtpDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/test/java/com/github/gribanoveu/auth/controllers/dtos/request/LoginDtoTest.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,83 @@ | ||
package com.github.gribanoveu.auth.controllers.dtos.request; | ||
|
||
import com.github.gribanoveu.auth.base.BaseValidatorTest; | ||
import com.github.gribanoveu.auth.constants.ValidationMessages; | ||
import jakarta.validation.ConstraintViolation; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Evgeny Gribanov | ||
* @version 04.10.2023 | ||
*/ | ||
class LoginDtoTest extends BaseValidatorTest { | ||
|
||
@Test | ||
void testValidLoginDto() { | ||
LoginDto loginDto = new LoginDto("[email protected]", "password"); | ||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage).hasSize(0); | ||
} | ||
|
||
@Test | ||
void testInvalidLoginDtoWithIncorrectEmailFormat() { | ||
LoginDto loginDto = new LoginDto("testexample.com", "password"); | ||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.PATTERN_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidLoginDtoWithEmptyEmail() { | ||
LoginDto loginDto = new LoginDto("", "password"); | ||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.containsAll(List.of(ValidationMessages.PATTERN_EXCEPTION_MESSAGE, ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidLoginDtoWithEmptyPassword() { | ||
LoginDto loginDto = new LoginDto("[email protected]", ""); | ||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidLoginDtoWithLongEmail() { | ||
LoginDto loginDto = new LoginDto(StringUtils.repeat("a", 254) + "@example.com", "password"); | ||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.SIZE_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidLoginDtoWithLongPassword() { | ||
LoginDto loginDto = new LoginDto("[email protected]", StringUtils.repeat("a", 254)); | ||
|
||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.SIZE_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidLoginDtoWithNullEmail() { | ||
LoginDto loginDto = new LoginDto(null, "password"); | ||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
@Test | ||
void testInvalidLoginDtoWithNullPassword() { | ||
LoginDto loginDto = new LoginDto("[email protected]", null); | ||
Set<ConstraintViolation<LoginDto>> violations = validator.validate(loginDto); | ||
Assertions.assertThat(violations).extracting(ConstraintViolation::getMessage) | ||
.isEqualTo(List.of(ValidationMessages.NOT_BLANK_EXCEPTION_MESSAGE)); | ||
} | ||
|
||
} |
Oops, something went wrong.