Refactor employee management

This commit is contained in:
Frudrax Cheng
2026-05-28 10:05:59 +08:00
parent f394d3a8bd
commit d3ee215f61
17 changed files with 5391 additions and 174 deletions
+26 -7
View File
@@ -53,7 +53,7 @@ func TestAuthService_ValidateUser_Success(t *testing.T) {
Password: string(password),
Name: "测试用户",
Email: "test@example.com",
Role: "user",
Role: "technician",
}
database.DB.Create(&user)
@@ -75,7 +75,7 @@ func TestAuthService_ValidateUser_WrongPassword(t *testing.T) {
Password: string(password),
Name: "测试用户2",
Email: "test2@example.com",
Role: "user",
Role: "technician",
}
database.DB.Create(&user)
@@ -87,6 +87,25 @@ func TestAuthService_ValidateUser_WrongPassword(t *testing.T) {
database.DB.Unscoped().Delete(&user)
}
func TestAuthService_ValidateUser_EmployeeCannotLogin(t *testing.T) {
password, _ := bcrypt.GenerateFromPassword([]byte("password123"), bcrypt.DefaultCost)
user := models.User{
Username: "employee_no_login",
Password: string(password),
Name: "普通员工",
Role: "employee",
}
database.DB.Create(&user)
authService := AuthService{}
_, err := authService.ValidateUser("employee_no_login", "password123")
assert.Error(t, err)
assert.Contains(t, err.Error(), "用户名或密码不正确")
database.DB.Unscoped().Delete(&user)
}
func TestAuthService_ValidateUser_UserNotFound(t *testing.T) {
authService := AuthService{}
_, err := authService.ValidateUser("nonexistent", "password")
@@ -98,7 +117,7 @@ func TestAuthService_GenerateToken_Success(t *testing.T) {
user := &models.User{
ID: 1,
Username: "testuser",
Role: "user",
Role: "technician",
}
authService := AuthService{}
@@ -117,7 +136,7 @@ func TestAuthService_GetProfile_Success(t *testing.T) {
Password: string(password),
Name: "测试用户3",
Email: "test3@example.com",
Role: "user",
Role: "technician",
}
database.DB.Create(&user)
@@ -140,7 +159,7 @@ func TestAuthService_ChangePassword_Success(t *testing.T) {
Password: string(password),
Name: "测试用户4",
Email: "test4@example.com",
Role: "user",
Role: "technician",
}
database.DB.Create(&user)
@@ -165,7 +184,7 @@ func TestAuthService_ChangePassword_WrongCurrentPassword(t *testing.T) {
Password: string(password),
Name: "测试用户5",
Email: "test5@example.com",
Role: "user",
Role: "technician",
}
database.DB.Create(&user)
@@ -185,7 +204,7 @@ func TestAuthService_UpdateProfile_Success(t *testing.T) {
Password: string(password),
Name: "测试用户6",
Email: "test6@example.com",
Role: "user",
Role: "technician",
}
database.DB.Create(&user)