Skip to content

Commit

Permalink
add tests for dto, permission controller, fix logic
Browse files Browse the repository at this point in the history
Took 37 seconds
  • Loading branch information
gribanoveu committed Oct 5, 2023
1 parent 21817f7 commit cc1a818
Show file tree
Hide file tree
Showing 13 changed files with 934 additions and 42 deletions.
52 changes: 19 additions & 33 deletions src/test/java/com/github/gribanoveu/auth/base/BaseMockMvcTest.java
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
Expand All @@ -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");
}
}
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();
}
}
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));
}
}
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));
}
}
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));
}
}
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));
}

}
Loading

0 comments on commit cc1a818

Please sign in to comment.