Files
tyapi-server/internal/application/certification/already_authorized_test.go

75 lines
2.1 KiB
Go
Raw Normal View History

2026-07-26 23:14:52 +08:00
package certification
import (
"errors"
2026-07-27 00:03:07 +08:00
"fmt"
2026-07-26 23:14:52 +08:00
"testing"
2026-07-27 00:03:07 +08:00
"tyapi-server/internal/shared/fadada"
2026-07-26 23:14:52 +08:00
)
func TestIsEnterpriseAlreadyAuthorizedErr(t *testing.T) {
cases := []struct {
name string
err error
want bool
}{
{
2026-07-27 00:03:07 +08:00
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",
2026-07-26 23:14:52 +08:00
err: errors.New("获取法大大企业认证链接失败: code=210002 msg=该企业已授权,无需重复操作 requestId=abc"),
want: true,
},
{
2026-07-27 00:03:07 +08:00
name: "legacy string 210013",
err: errors.New("获取法大大企业认证链接失败: code=210013 msg=授权范围重复获取 requestId=xyz"),
2026-07-26 23:14:52 +08:00
want: true,
},
{
2026-07-27 00:03:07 +08:00
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"),
2026-07-26 23:14:52 +08:00
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")
}
}