add
This commit is contained in:
parent
3b8ccb6f46
commit
e3d48be549
71
src/test/java/com/lk/paopao/service/AuthService2Test.java
Normal file
71
src/test/java/com/lk/paopao/service/AuthService2Test.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package com.lk.paopao.service;
|
||||||
|
|
||||||
|
import com.lk.paopao.dto.rest.request.RegisterRequest;
|
||||||
|
import com.lk.paopao.entity.User;
|
||||||
|
import com.lk.paopao.exception.ResourceExistedException;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.annotation.Rollback;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import java.util.Map;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@ActiveProfiles("test")
|
||||||
|
class AuthService2Test {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AuthService authServiceUnderTest;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Rollback
|
||||||
|
void testRegister_success() {
|
||||||
|
// Arrange
|
||||||
|
final RegisterRequest reg = new RegisterRequest();
|
||||||
|
reg.setUsername("mike2");
|
||||||
|
reg.setPassword("mike");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final User user = authServiceUnderTest.register(reg);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertThat(user.getUsername()).isEqualTo("mike2");
|
||||||
|
assertThat(user.getId()).isGreaterThan(0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegister_ThrowsResourceExistedException() {
|
||||||
|
// Arrange
|
||||||
|
final RegisterRequest reg = new RegisterRequest();
|
||||||
|
reg.setUsername("mike");
|
||||||
|
reg.setPassword("mike");
|
||||||
|
// Act and Assert
|
||||||
|
assertThatThrownBy(() -> authServiceUnderTest.register(reg)).isInstanceOf(ResourceExistedException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLogin() {
|
||||||
|
// Arrange
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final Map<String, String> result = authServiceUnderTest.login("mike", "mike");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertThat(result.get("token")).isNotEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
119
src/test/java/com/lk/paopao/service/AuthServiceTest.java
Normal file
119
src/test/java/com/lk/paopao/service/AuthServiceTest.java
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
package com.lk.paopao.service;
|
||||||
|
|
||||||
|
import com.lk.paopao.dto.rest.request.RegisterRequest;
|
||||||
|
import com.lk.paopao.entity.User;
|
||||||
|
import com.lk.paopao.exception.ResourceExistedException;
|
||||||
|
import com.lk.paopao.repository.UserRepository;
|
||||||
|
import com.lk.paopao.security.JwtUtils;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class AuthServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserRepository mockUserRepository;
|
||||||
|
@Mock
|
||||||
|
private AuthenticationManager mockAuthenticationManager;
|
||||||
|
@Mock
|
||||||
|
private PasswordEncoder mockEncoder;
|
||||||
|
@Mock
|
||||||
|
private JwtUtils mockJwtUtils;
|
||||||
|
|
||||||
|
private AuthService authServiceUnderTest;
|
||||||
|
|
||||||
|
String TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA2NiwidXNlcm5hbWUiOiJ4eXoiLCJpc3MiOiI4ZGNlMGM3NjcyMWFkODFiYzQxMjRhMDhjMTBkNDk3MSIsImV4cCI6MTcxNzYzODgxMn0.TdAMHh4n7SQVipXK-_zRsk5jhCVpoZI4jnlUIR7iO-I";
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
authServiceUnderTest = new AuthService();
|
||||||
|
ReflectionTestUtils.setField(authServiceUnderTest, "DEFAULT_HEAD_ICON", "avatar");
|
||||||
|
authServiceUnderTest.userRepository = mockUserRepository;
|
||||||
|
authServiceUnderTest.authenticationManager = mockAuthenticationManager;
|
||||||
|
authServiceUnderTest.encoder = mockEncoder;
|
||||||
|
authServiceUnderTest.jwtUtils = mockJwtUtils;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegister_normal() {
|
||||||
|
// Arrange
|
||||||
|
final RegisterRequest reg = new RegisterRequest();
|
||||||
|
reg.setUsername("nickname");
|
||||||
|
reg.setPassword("password");
|
||||||
|
|
||||||
|
when(mockUserRepository.findByUsername("nickname")).thenReturn(Optional.empty());
|
||||||
|
when(mockEncoder.encode("password")).thenReturn("password");
|
||||||
|
|
||||||
|
// Configure UserRepository.save(...).
|
||||||
|
final User user = new User();
|
||||||
|
user.setNickname("nickname");
|
||||||
|
user.setUsername("nickname");
|
||||||
|
user.setPhone("phone");
|
||||||
|
user.setPassword("password");
|
||||||
|
user.setAvatar("avatar");
|
||||||
|
when(mockUserRepository.save(any(User.class))).thenReturn(user);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final User result = authServiceUnderTest.register(reg);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertThat(result.getUsername()).isEqualTo("nickname");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegister_ThrowsResourceExistedException() {
|
||||||
|
// Arrange
|
||||||
|
final RegisterRequest reg = new RegisterRequest();
|
||||||
|
reg.setUsername("nickname");
|
||||||
|
reg.setPassword("password");
|
||||||
|
|
||||||
|
final User user1 = new User();
|
||||||
|
user1.setNickname("nickname");
|
||||||
|
user1.setUsername("nickname");
|
||||||
|
user1.setPhone("phone");
|
||||||
|
user1.setPassword("password");
|
||||||
|
user1.setAvatar("avatar");
|
||||||
|
final Optional<User> user = Optional.of(user1);
|
||||||
|
when(mockUserRepository.findByUsername("nickname")).thenReturn(user);
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
assertThatThrownBy(() -> authServiceUnderTest.register(reg)).isInstanceOf(ResourceExistedException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLogin() {
|
||||||
|
// Arrange
|
||||||
|
final Map<String, String> expectedResult = Map.ofEntries(Map.entry("token", TOKEN));
|
||||||
|
|
||||||
|
Authentication at = new UsernamePasswordAuthenticationToken("user", "pass");
|
||||||
|
|
||||||
|
doReturn(at).when(mockAuthenticationManager).authenticate(any());
|
||||||
|
when(mockJwtUtils.generateJwtToken(at)).thenReturn(TOKEN);
|
||||||
|
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final Map<String, String> result = authServiceUnderTest.login("user", "pass");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertThat(result).isEqualTo(expectedResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
49
src/test/resources/application-test.yml
Normal file
49
src/test/resources/application-test.yml
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
spring:
|
||||||
|
|
||||||
|
jackson:
|
||||||
|
property-naming-strategy: SNAKE_CASE # 驼峰转下划线
|
||||||
|
|
||||||
|
datasource:
|
||||||
|
url: jdbc:h2:file:./test.h2 # 使用文件存储
|
||||||
|
driverClassName: org.h2.Driver
|
||||||
|
username: root
|
||||||
|
password: root
|
||||||
|
sql:
|
||||||
|
init:
|
||||||
|
mode: always
|
||||||
|
username: root
|
||||||
|
password: root
|
||||||
|
data-locations: classpath:test-data.sql
|
||||||
|
jpa:
|
||||||
|
show-sql: true
|
||||||
|
open-in-view: false
|
||||||
|
defer-datasource-initialization: true
|
||||||
|
database-platform: org.hibernate.dialect.H2Dialect
|
||||||
|
hibernate:
|
||||||
|
format_sql: true
|
||||||
|
ddl-auto: create-drop
|
||||||
|
# 可选值:create-drop,create,update,none. create-drop:每次启动项目都会删除表,然后重新创建表,适合开发环境;create:每次启动项目都会创建表,适合开发环境;update:每次启动项目都会更新表,适合开发环境;none:不执行任何操作,适合生产环境。
|
||||||
|
|
||||||
|
# 静态资源目录配置
|
||||||
|
resources:
|
||||||
|
static-locations: classpath:/static/,file:/home/whz/tmp/ # 指定静态资源的位置
|
||||||
|
|
||||||
|
app:
|
||||||
|
version: v1
|
||||||
|
default:
|
||||||
|
head-icon: https://assets.paopao.info/public/avatar/default/joshua.png
|
||||||
|
security:
|
||||||
|
jwt:
|
||||||
|
secret-key: uYuVw0mke38MfLhO19wUQyRgwrmYo89ibpQTXPHi4vg=
|
||||||
|
expiration: 36000000
|
||||||
|
white-list: # 白名单
|
||||||
|
- path: "/v1/auth/**"
|
||||||
|
methods: [ "GET","PUT","POST"]
|
||||||
|
- path: "/upload/**"
|
||||||
|
methods: [ "GET" ]
|
||||||
|
allowed-origins: # 允许跨域的域名
|
||||||
|
- "http://localhost:5173"
|
||||||
|
upload:
|
||||||
|
path: /home/whz/tmp/upload # 上传文件路径
|
||||||
|
sub-path-name-length: 2 # 上传文件路径子目录长度
|
||||||
|
base-url: http://localhost:8080/upload # 上传文件路径前缀
|
4
src/test/resources/test-data.sql
Normal file
4
src/test/resources/test-data.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
SET @current_second = 1716955476194;
|
||||||
|
INSERT INTO p_user
|
||||||
|
( created_on, modified_on, deleted_on, is_del, username, nickname, password, avatar, phone, is_admin,status)VALUES
|
||||||
|
( @current_second, @current_second, 0, 0, 'mike', 'mike', '$2a$10$xXjVMZ501vQG1eSbC43mye36bBqNyn97gtYa3hbioO5ka4J4.SrCG', '', '', 0,1);
|
Loading…
Reference in New Issue
Block a user