Files
tyapi-server/internal/application/certification/already_authorized_test.go
2026-07-27 00:03:07 +08:00

75 lines
2.1 KiB
Go

package certification
import (
"errors"
"fmt"
"testing"
"tyapi-server/internal/shared/fadada"
)
func TestIsEnterpriseAlreadyAuthorizedErr(t *testing.T) {
cases := []struct {
name string
err error
want bool
}{
{
name: "structured 210002",
err: fadada.NewAPIError("获取法大大企业认证链接失败", fadada.CodeAuthScopeDuplicate, "授权范围重复获取", "abc"),
want: true,
},
{
name: "structured 210013",
err: fadada.NewAPIError("获取法大大企业认证链接失败", fadada.CodeAuthScopeDuplicateCorp, "授权范围重复获取", "def"),
want: true,
},
{
name: "legacy string 210002",
err: errors.New("获取法大大企业认证链接失败: code=210002 msg=该企业已授权,无需重复操作 requestId=abc"),
want: true,
},
{
name: "legacy string 210013",
err: errors.New("获取法大大企业认证链接失败: code=210013 msg=授权范围重复获取 requestId=xyz"),
want: true,
},
{
name: "wrapped structured 210002",
err: fmt.Errorf("生成企业认证链接失败: %w", fadada.NewAPIError("获取法大大企业认证链接失败", fadada.CodeAuthScopeDuplicate, "授权范围重复获取", "rid")),
want: true,
},
{
name: "msg only should not match",
err: errors.New("当前应用已存在授权"),
want: false,
},
{
name: "other error 100011",
err: fadada.NewAPIError("获取法大大企业认证链接失败", fadada.CodeParamIllegal, "请求参数非法", "rid"),
want: false,
},
{
name: "nil",
err: nil,
want: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := isEnterpriseAlreadyAuthorizedErr(tc.err); got != tc.want {
t.Fatalf("got %v want %v", got, tc.want)
}
})
}
}
func TestIsEnterpriseAlreadyRealnamedErr(t *testing.T) {
if !isEnterpriseAlreadyRealnamedErr(errors.New("企业用户已实名")) {
t.Fatal("expected true for 已实名")
}
if isEnterpriseAlreadyRealnamedErr(errors.New("该企业已授权,无需重复操作")) {
t.Fatal("已授权 should not match 已实名 helper")
}
}