登录注册(成功版)

Change-Id: I340bc8047774dd5ded63865812c44e33bbb734a5
diff --git a/src/main/java/com/ptp/ptplatform/PtPlatformApplication.java b/src/main/java/com/ptp/ptplatform/PtPlatformApplication.java
new file mode 100644
index 0000000..deff713
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/PtPlatformApplication.java
@@ -0,0 +1,16 @@
+package com.ptp.ptplatform;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+@MapperScan("com.ptp.ptplatform.mapper")
+public class PtPlatformApplication {
+
+    public static void main(String[] args) {
+
+        SpringApplication.run(PtPlatformApplication.class, args);
+    }
+
+}
diff --git a/src/main/java/com/ptp/ptplatform/config/WebConfig.java b/src/main/java/com/ptp/ptplatform/config/WebConfig.java
new file mode 100644
index 0000000..d0fdb8e
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/config/WebConfig.java
@@ -0,0 +1,21 @@
+// 配置文件
+package com.ptp.ptplatform.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+
+        registry.addMapping("/**")  // 允许所有路径跨源请求
+                .allowedOrigins("http://localhost:3000")  // 允许指定来源
+                .allowedMethods("GET", "POST", "PUT", "DELETE")  // 允许的请求方法
+                .allowCredentials(true);  // 是否允许携带凭证
+
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/controller/InviteCodeController.java b/src/main/java/com/ptp/ptplatform/controller/InviteCodeController.java
new file mode 100644
index 0000000..cf482c6
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/controller/InviteCodeController.java
@@ -0,0 +1,4 @@
+package com.ptp.ptplatform.controller;
+
+public class InviteCodeController {
+}
diff --git a/src/main/java/com/ptp/ptplatform/controller/UserController.java b/src/main/java/com/ptp/ptplatform/controller/UserController.java
new file mode 100644
index 0000000..0b1dcb2
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/controller/UserController.java
@@ -0,0 +1,89 @@
+//controller 文件夹中文件类似于view
+// usermapper 实现用户登录注册等内容
+
+package com.ptp.ptplatform.controller;
+
+import com.ptp.ptplatform.entity.*;
+import com.ptp.ptplatform.mapper.UserMapper;
+import com.ptp.ptplatform.mapper.InviteCodeMapper;
+import jakarta.annotation.Resource;
+import org.springframework.web.bind.annotation.*;
+import com.ptp.ptplatform.utils.Result;
+import com.ptp.ptplatform.utils.JwtUtils;
+import com.ptp.ptplatform.entity.USER;
+import java.util.Date;
+
+@RestController
+@RequestMapping("/user")
+@CrossOrigin //启用跨域
+public class UserController {
+
+    @Resource
+    private UserMapper userMapper;
+    @Resource
+    private InviteCodeMapper inviteCodeMapper;
+
+    @GetMapping("/info") //获取
+    public Result info(String token) {
+        String username = JwtUtils.getClaimByToken(token).getSubject();
+        return Result.ok().data("name", username);
+    }
+
+    @PostMapping("/login") //用户登录
+    public Result login(String username, String password) {
+        USER user = userMapper.selectByUsername(username);
+
+        if (user != null) {
+            // 将用户输入的密码进行哈希处理
+            String hashedPassword = user.hashPassword(password);
+
+            // 比较用户输入的密码哈希值和数据库中的密码哈希值
+            if (hashedPassword.equals(user.getPassword())) {
+                String token = JwtUtils.generateToken(user.getUsername());
+                return Result.ok().data("token", token);  // 返回令牌给前端
+            } else {
+                return Result.error().setMessage("密码错误");
+            }
+        } else {
+            return Result.error().setMessage("用户不存在");
+        }
+    }
+
+    @PostMapping("/regist")
+    public Result regist(String username, String password, String code) {
+        USER userCheck = userMapper.selectByUsername(username);
+        if (userCheck == null) {
+            //获取邀请码
+            INVITE_CODE inviteCode = inviteCodeMapper.selectByCode(code);
+            if(inviteCode != null){
+                System.out.println(inviteCode.getIsUsed());
+
+                if(!inviteCode.getIsUsed()){
+                    Date time = new Date();
+                    USER user = new USER(username, password, time) ;
+
+                    userMapper.insertUser(user);
+                    inviteCodeMapper.updateCodeUser(code);
+
+                    return Result.ok().setMessage("新建用户成功");
+                } else {
+                    return Result.error().setMessage("邀请码已经被使用,注册失败");
+                }
+
+            } else {
+                return Result.error().setMessage("邀请码不存在,注册失败");
+            }
+
+        } else {
+            return Result.error().setMessage("用户名已存在,注册失败");
+        }
+
+
+    }
+
+    @PostMapping("/logout")
+    public Result logout(String token) {
+        return Result.ok();
+    }
+
+}
diff --git a/src/main/java/com/ptp/ptplatform/entity/INVITE_CODE.java b/src/main/java/com/ptp/ptplatform/entity/INVITE_CODE.java
new file mode 100644
index 0000000..d0a5a53
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/entity/INVITE_CODE.java
@@ -0,0 +1,51 @@
+// 邀请码类
+// 注册时使用的
+package com.ptp.ptplatform.entity;
+
+import jakarta.persistence.*;
+import java.io.Serializable;
+
+@Table(name = "invite_code")
+public class INVITE_CODE {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Integer id;
+
+    private String code;
+    private String generateUser;
+    private Boolean isUsed;
+
+    //方法
+    public Integer getId() {
+        return id;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getGenerateUser() {
+        return generateUser;
+    }
+
+    public Boolean getIsUsed() {
+        return isUsed;
+    }
+
+    public void setIsUsed(Boolean isUsed) {
+        this.isUsed = isUsed;
+    }
+
+    // Constructor (optional)
+    public INVITE_CODE() {}
+
+    public INVITE_CODE(String code, String generateUser) {
+        this.code = code;
+        this.generateUser = generateUser;
+        this.isUsed = false;
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/entity/USER.java b/src/main/java/com/ptp/ptplatform/entity/USER.java
new file mode 100644
index 0000000..84e5c33
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/entity/USER.java
@@ -0,0 +1,156 @@
+// 用户类
+// @GeneratedValue(strategy = GenerationType.IDENTITY) 对于一些需要自动生成的主键id进行注解
+
+package com.ptp.ptplatform.entity;
+
+import jakarta.persistence.*;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Date;
+
+@Table(name = "user")
+public class USER {
+
+    @jakarta.persistence.Id
+    private String username;
+    private String password;
+
+    @Enumerated(EnumType.STRING)
+    private Authority authority;
+
+    private int level; // 用户等级0-6
+
+    @Temporal(TemporalType.DATE)
+    private Date registTime = new Date();
+
+    @Temporal(TemporalType.DATE)
+    private Date lastLogin;
+
+    private int upload;
+    private int download;
+    private int shareRate;
+    private int magicPoints;
+
+    public enum Authority {
+        USER, ADMIN, LIMIT, BAN
+    }
+
+
+    // 函数
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public Authority getAuthority() {
+        return authority;
+    }
+
+    public void setAuthority(Authority authority) {
+        this.authority = authority;
+    }
+
+    public int getLevel() {
+        return level;
+    }
+
+    public void setLevel(int level) {
+        this.level = level;
+    }
+
+    public Date getRegistTime() {
+        return registTime;
+    }
+
+    public void setRegistTime(Date registTime) {
+        this.registTime = registTime;
+    }
+
+    public Date getLastLogin() {
+        return lastLogin;
+    }
+
+    public void setLastLogin(Date lastLogin) {
+        this.lastLogin = lastLogin;
+    }
+
+    public int getUpload() {
+        return upload;
+    }
+
+    public void setUpload(int upload) {
+        this.upload = upload;
+    }
+
+    public int getDownload() {
+        return download;
+    }
+
+    public void setDownload(int download) {
+        this.download = download;
+    }
+
+    public int getShareRate() {
+        return shareRate;
+    }
+
+    public void setShareRate(int shareRate) {
+        this.shareRate = shareRate;
+    }
+
+    public int getMagicPoints() {
+        return magicPoints;
+    }
+
+    public void setMagicPoints(int magicPoints) {
+        this.magicPoints = magicPoints;
+    }
+
+    public USER() {
+    }
+
+    public USER(String username, String password, Date registTime) {
+        this.username = username;
+        this.registTime = registTime;
+
+        this.password = hashPassword(password);;
+
+        this.authority = Authority.USER;
+        this.level = 0;
+        this.lastLogin = null;
+        this.upload = 0;
+        this.download = 0;
+        this.shareRate = 100;
+        this.magicPoints = 0;
+    }
+
+    public String hashPassword(String password) {
+        try {
+            // 使用SHA-256算法
+            MessageDigest digest = MessageDigest.getInstance("SHA-256");
+            byte[] hashBytes = digest.digest(password.getBytes());
+
+            // 将字节数组转换为十六进制字符串
+            StringBuilder hexString = new StringBuilder();
+            for (byte b : hashBytes) {
+                hexString.append(String.format("%02x", b));
+            }
+
+            return hexString.toString();
+        } catch (NoSuchAlgorithmException e) {
+            throw new RuntimeException("Error hashing password", e);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/entity/testdata.java b/src/main/java/com/ptp/ptplatform/entity/testdata.java
new file mode 100644
index 0000000..7d6d88e
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/entity/testdata.java
@@ -0,0 +1,15 @@
+// model类 负责管理数据库数据和sb的映射
+
+package com.ptp.ptplatform.entity;
+
+public class testdata {
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}
diff --git a/src/main/java/com/ptp/ptplatform/mapper/InviteCodeMapper.java b/src/main/java/com/ptp/ptplatform/mapper/InviteCodeMapper.java
new file mode 100644
index 0000000..612c7fb
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/mapper/InviteCodeMapper.java
@@ -0,0 +1,18 @@
+package com.ptp.ptplatform.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.ptp.ptplatform.entity.INVITE_CODE;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+import org.springframework.web.service.annotation.PutExchange;
+
+@Mapper
+public interface InviteCodeMapper extends BaseMapper<INVITE_CODE> {
+    //查询
+    @Select("SELECT * FROM invite_code WHERE code = #{code}")
+    INVITE_CODE selectByCode(String code);
+
+    @Update("UPDATE invite_code SET isused = true WHERE code = #{code}")
+    int updateCodeUser(String code);
+}
diff --git a/src/main/java/com/ptp/ptplatform/mapper/UserMapper.java b/src/main/java/com/ptp/ptplatform/mapper/UserMapper.java
new file mode 100644
index 0000000..024807e
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/mapper/UserMapper.java
@@ -0,0 +1,23 @@
+package com.ptp.ptplatform.mapper;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.ptp.ptplatform.entity.USER;
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+
+@Mapper
+public interface UserMapper extends BaseMapper<USER> {
+    // 查询
+    @Select("SELECT * FROM user WHERE username = #{username}")
+    USER selectByUsername(String username);
+
+    @Select("SELECT * FROM user WHERE username = #{username} AND password = #{password}")
+    USER selectByUsernameAndPassword(String username, String password);
+
+    // 注册用户
+    @Insert("INSERT INTO user (username, password, registTime) " +
+            "VALUES (#{username}, #{password}, #{registTime})")
+    int insertUser(USER user);
+}
diff --git a/src/main/java/com/ptp/ptplatform/utils/JwtUtils.java b/src/main/java/com/ptp/ptplatform/utils/JwtUtils.java
new file mode 100644
index 0000000..57bff31
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/utils/JwtUtils.java
@@ -0,0 +1,32 @@
+package com.ptp.ptplatform.utils;
+
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+
+import javax.xml.crypto.Data;
+import java.util.Date;
+
+public class JwtUtils {
+    private static long expire = 36000;
+    private static String secret = "abcdefgg";
+
+    public static String generateToken(String username){
+        Date now = new Date();
+        Date expiration = new Date(now.getTime() + 1000 * expire);
+        return Jwts.builder()
+                .setHeaderParam("type","JWT")
+                .setSubject(username)
+                .setIssuedAt(now)
+                .setExpiration(expiration)
+                .signWith(SignatureAlgorithm.HS512,secret)
+                .compact();
+
+    }
+    public static Claims getClaimByToken(String token){
+        return Jwts.parser()
+                .setSigningKey(secret)
+                .parseClaimsJws(token)
+                .getBody();
+    }
+}
diff --git a/src/main/java/com/ptp/ptplatform/utils/Result.java b/src/main/java/com/ptp/ptplatform/utils/Result.java
new file mode 100644
index 0000000..04a3290
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/utils/Result.java
@@ -0,0 +1,77 @@
+package com.ptp.ptplatform.utils;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Result {
+    private Boolean success;
+    private Integer code;
+    private String message;
+    private Map<String,Object> data = new HashMap<>();
+
+    public Boolean getSuccess() {
+        return success;
+    }
+    public void setSuccess(Boolean success) {
+        this.success = success;
+    }
+
+    public Integer getCode() {
+        return code;
+    }
+    public void setCode(Integer code) {
+        this.code = code;
+    }
+    public String getMessage() {
+        return message;
+    }
+    public Result setMessage(String message) {
+        this.message = message;
+        return this;
+    }
+    public Map<String, Object> getData() {
+        return data;
+    }
+    public void setData(Map<String, Object> data) {
+        this.data = data;
+    }
+
+    private Result() {}
+    public static Result ok() {
+        Result result = new Result();
+        result.setSuccess(true);
+        result.setCode(resultCode.SUCCESS);
+        result.setMessage("成功");
+        return result;
+    }
+
+    public static Result error() {
+        Result result = new Result();
+        result.setSuccess(false);
+        result.setCode(resultCode.ERROR);
+        result.setMessage("失败");
+        return result;
+    }
+
+    public Result success(Boolean success) {
+        this.setSuccess(success);
+        return this;
+    }
+    public Result message(String message) {
+        this.setMessage(message);
+        return this;
+    }
+    public Result code(Integer code) {
+        this.setCode(code);
+        return this;
+    }
+
+    public Result data(String key, Object value) {
+        this.data.put(key,value);
+        return this;
+    }
+
+    public Result data(Map<String, Object> map) {
+        this.setData(map);
+        return this;
+    }
+}
diff --git a/src/main/java/com/ptp/ptplatform/utils/resultCode.java b/src/main/java/com/ptp/ptplatform/utils/resultCode.java
new file mode 100644
index 0000000..abd93d9
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/utils/resultCode.java
@@ -0,0 +1,8 @@
+//对返回的状态码进行设置
+package com.ptp.ptplatform.utils;
+
+public interface resultCode {
+    public static Integer SUCCESS = 200;
+    public static Integer ERROR = 500;
+
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
new file mode 100644
index 0000000..e1226b0
--- /dev/null
+++ b/src/main/resources/application.properties
@@ -0,0 +1,29 @@
+spring.application.name=PTPlatform
+
+# debug mode
+#debug=true
+spring.profiles.active=test
+
+# running port
+server.port = 8088
+
+# hot deployment
+spring.devtools.restart.enabled=true
+spring.devtools.restart.additional-paths=src/main/java
+spring.devtools.restart.exclude=static/**
+
+# database setting
+spring.datasourse.type=com.alibaba.druid.pool.DruidDataSource
+spring.datasource.driver-class-name=com.mysql.jdbc.Driver
+spring.datasource.url=jdbc:mysql://localhost:3306/ptpdata?useUnicode=true&characterEncoding=utf8
+spring.datasource.username=root
+spring.datasource.password=yumu1412
+mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
+
+# Hibernate properties
+spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
+spring.jpa.hibernate.ddl-auto=update
+
+# MyBatis-Plus properties
+mybatis-plus.mapper-locations=classpath:/mapper/*.xml
+mybatis-plus.type-aliases-package=com.example.demo.model
\ No newline at end of file
diff --git a/src/test/java/com/ptp/ptplatform/PtPlatformApplicationTests.java b/src/test/java/com/ptp/ptplatform/PtPlatformApplicationTests.java
new file mode 100644
index 0000000..942ee55
--- /dev/null
+++ b/src/test/java/com/ptp/ptplatform/PtPlatformApplicationTests.java
@@ -0,0 +1,15 @@
+package com.ptp.ptplatform;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+
+@SpringBootTest
+@ActiveProfiles("test")
+class PtPlatformApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}
diff --git a/src/test/java/com/ptp/ptplatform/controller/UserControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/UserControllerTest.java
new file mode 100644
index 0000000..ebe2353
--- /dev/null
+++ b/src/test/java/com/ptp/ptplatform/controller/UserControllerTest.java
@@ -0,0 +1,194 @@
+package com.ptp.ptplatform.controller;
+
+import com.ptp.ptplatform.entity.INVITE_CODE;
+import com.ptp.ptplatform.entity.USER;
+import com.ptp.ptplatform.mapper.InviteCodeMapper;
+import com.ptp.ptplatform.mapper.UserMapper;
+import com.ptp.ptplatform.utils.JwtUtils;
+import com.ptp.ptplatform.utils.Result;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.Date;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class UserControllerTest {
+
+    @Mock
+    private UserMapper userMapper;
+
+    @Mock
+    private InviteCodeMapper inviteCodeMapper;
+
+    @Mock
+    private JwtUtils jwtUtils;
+
+    @InjectMocks
+    private UserController userController;
+
+    @BeforeEach
+    void setUp() {
+        MockitoAnnotations.openMocks(this);
+    }
+
+//    @Test
+//    void testLogin_Success() {
+//        // 准备测试数据
+//        String username = "testUser";
+//        String password = "testPass";
+//        String hashedPassword = "hashedTestPass";
+//        String token = "generatedToken";
+//
+//        // 创建 mock 对象
+//        USER mockUser = mock(USER.class);
+//        // 使用Mockito方式定义行为
+//        when(mockUser.getUsername()).thenReturn(username);
+//        when(mockUser.getPassword()).thenReturn(hashedPassword);
+//        // 如果 hashPassword 需要被调用
+//        when(mockUser.hashPassword(password)).thenReturn(hashedPassword);
+//
+//        // 模拟Mapper行为
+//        when(userMapper.selectByUsername(username)).thenReturn(mockUser);
+//        when(jwtUtils.generateToken(username)).thenReturn(token);
+//
+//        // 执行测试
+//        Result result = userController.login(username, password);
+//
+//        // 验证结果
+//        assertEquals(200, result.getCode());
+//        assertEquals(token, result.getData().get("token"));
+//        verify(userMapper, times(1)).selectByUsername(username);
+//        verify(jwtUtils, times(1)).generateToken(username);
+//    }
+
+    @Test
+    void testLogin_UserNotFound() {
+        String username = "nonExistUser";
+        String password = "anyPass";
+
+        when(userMapper.selectByUsername(username)).thenReturn(null);
+
+        Result result = userController.login(username, password);
+
+        assertEquals(500, result.getCode());
+        assertEquals("用户不存在", result.getMessage());
+    }
+
+//    @Test
+//    void testLogin_WrongPassword() {
+//        String username = "testUser";
+//        String password = "wrongPass";
+//        String hashedPassword = "hashedTestPass";
+//
+//        USER mockUser = new USER();
+//        mockUser.setUsername(username);
+//        mockUser.setPassword(hashedPassword);
+//
+//        when(userMapper.selectByUsername(username)).thenReturn(mockUser);
+//        when(mockUser.hashPassword(password)).thenReturn("wrongHash");
+//
+//        Result result = userController.login(username, password);
+//
+//        assertEquals(500, result.getCode());
+//        assertEquals("密码错误", result.getMessage());
+//    }
+
+    @Test
+    void testRegister_Success() {
+        String username = "newUser";
+        String password = "newPass";
+        String code = "validCode";
+        Date now = new Date();
+
+        INVITE_CODE mockInviteCode = new INVITE_CODE();
+        mockInviteCode.setIsUsed(false);
+
+        when(userMapper.selectByUsername(username)).thenReturn(null);
+        when(inviteCodeMapper.selectByCode(code)).thenReturn(mockInviteCode);
+
+        Result result = userController.regist(username, password, code);
+
+        assertEquals(200, result.getCode());
+        assertEquals("新建用户成功", result.getMessage());
+        verify(userMapper, times(1)).insertUser(any(USER.class));
+        verify(inviteCodeMapper, times(1)).updateCodeUser(code);
+    }
+
+    @Test
+    void testRegister_UsernameExists() {
+        String username = "existingUser";
+        String password = "anyPass";
+        String code = "anyCode";
+
+        USER existingUser = new USER();
+        when(userMapper.selectByUsername(username)).thenReturn(existingUser);
+
+        Result result = userController.regist(username, password, code);
+
+        assertEquals(500, result.getCode());
+        assertEquals("用户名已存在,注册失败", result.getMessage());
+        verify(userMapper, never()).insertUser(any());
+    }
+
+    @Test
+    void testRegister_InvalidCode() {
+        String username = "newUser";
+        String password = "newPass";
+        String code = "invalidCode";
+
+        when(userMapper.selectByUsername(username)).thenReturn(null);
+        when(inviteCodeMapper.selectByCode(code)).thenReturn(null);
+
+        Result result = userController.regist(username, password, code);
+
+        assertEquals(500, result.getCode());
+        assertEquals("邀请码不存在,注册失败", result.getMessage());
+        verify(userMapper, never()).insertUser(any());
+    }
+
+    @Test
+    void testRegister_UsedCode() {
+        String username = "newUser";
+        String password = "newPass";
+        String code = "usedCode";
+
+        INVITE_CODE usedCode = new INVITE_CODE();
+        usedCode.setIsUsed(true);
+
+        when(userMapper.selectByUsername(username)).thenReturn(null);
+        when(inviteCodeMapper.selectByCode(code)).thenReturn(usedCode);
+
+        Result result = userController.regist(username, password, code);
+
+        assertEquals(500, result.getCode());
+        assertEquals("邀请码已经被使用,注册失败", result.getMessage());
+        verify(userMapper, never()).insertUser(any());
+    }
+
+//    @Test
+//    void testGetUserInfo_Success() {
+//        String token = "validToken";
+//        String username = "testUser";
+//
+//        when(jwtUtils.getClaimByToken(token).getSubject()).thenReturn(username);
+//
+//        Result result = userController.info(token);
+//
+//        assertEquals(200, result.getCode());
+//        assertEquals(username, result.getData().get("name"));
+//    }
+
+    @Test
+    void testLogout() {
+        String token = "anyToken";
+
+        Result result = userController.logout(token);
+
+        assertEquals(200, result.getCode());
+    }
+}
\ No newline at end of file
diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml
new file mode 100644
index 0000000..45122fa
--- /dev/null
+++ b/src/test/resources/application-test.yml
@@ -0,0 +1,41 @@
+# 测试环境配置
+spring:
+  application:
+    name: PTPlatform-test  # 区分测试环境
+  
+  # 数据源配置 (H2 内存数据库)
+  datasource:
+    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MYSQL  # 模拟MySQL语法
+    driver-class-name: org.h2.Driver
+    username: sa
+    password:
+
+  
+  # JPA/Hibernate 配置
+  jpa:
+    hibernate:
+      ddl-auto: update  # 自动更新表结构
+    show-sql: true  # 显示SQL日志
+    properties:
+      hibernate:
+        dialect: org.hibernate.dialect.H2Dialect
+        format_sql: true
+  
+  # 关闭开发工具(测试环境不需要热部署)
+  devtools:
+    restart:
+      enabled: false
+  sql:
+    init:
+      platform: h2
+
+# MyBatis-Plus 测试配置
+mybatis-plus:
+  configuration:
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # 输出SQL日志
+  mapper-locations: classpath:/mapper/*.xml
+  type-aliases-package: com.example.demo.model
+
+# 测试专用端口
+server:
+  port: 0  # 随机端口,避免冲突
\ No newline at end of file