test: add service layer unit tests for webhook/metadata/error/config
- webhook_service_test.go: isPrivateIP, isSafeURL, computeHMAC - request_metadata_test.go: context functions - classified_error_test.go: error types - config_defaults_test.go: password reset/SMS defaults - email_config_test.go: email code defaults - auth_runtime_test.go: isUserNotFoundError Service coverage: 11.2% -> 14.7%
This commit is contained in:
75
internal/service/auth_runtime_test.go
Normal file
75
internal/service/auth_runtime_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// =============================================================================
|
||||
// Auth Runtime Helper Functions Tests
|
||||
// =============================================================================
|
||||
|
||||
func TestIsUserNotFoundError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "nil error",
|
||||
err: nil,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "gorm record not found",
|
||||
err: gorm.ErrRecordNotFound,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped gorm record not found",
|
||||
err: errors.Join(gorm.ErrRecordNotFound, errors.New("additional context")),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "other error",
|
||||
err: errors.New("some other error"),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "generic error",
|
||||
err: errors.New("something went wrong"),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "error containing user not found",
|
||||
err: errors.New("user not found"),
|
||||
expected: true, // contains "user not found" in lowercase
|
||||
},
|
||||
{
|
||||
name: "error containing record not found",
|
||||
err: errors.New("record not found"),
|
||||
expected: true, // contains "record not found"
|
||||
},
|
||||
{
|
||||
name: "error containing not found",
|
||||
err: errors.New("entity not found"),
|
||||
expected: true, // contains "not found"
|
||||
},
|
||||
{
|
||||
name: "error containing 用户不存在",
|
||||
err: errors.New("用户不存在"),
|
||||
expected: true, // contains Chinese "用户不存在"
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := isUserNotFoundError(tt.err)
|
||||
if result != tt.expected {
|
||||
t.Errorf("isUserNotFoundError(%v) = %v, want %v", tt.err, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user